peachyontop

Enemy.gd

Oct 4th, 2025
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.83 KB | None | 0 0
  1. # Enemy.gd (Godot 4.5, patrol + chase, zero warnings)
  2. extends CharacterBody2D
  3.  
  4. # --------------------------------------------------
  5. # Exported stats
  6. # --------------------------------------------------
  7. @export var enemy_name: String = "Red Snail"
  8. @export var enemy_level: int = 5
  9. @export var max_health: int = 50
  10. @export var health: int = 50:
  11. set(value):
  12. health = clamp(value, 0, max_health)
  13. _update_health_bar()
  14. if health <= 0:
  15. _die()
  16.  
  17. @export var exp_reward: int = 25
  18. @export var move_speed: float = 50.0
  19. @export var patrol_distance: float = 200.0
  20.  
  21. @export var detect_distance: float = 250.0
  22. @export var chase_speed: float = 120.0
  23.  
  24. @export var touch_damage: int = 5
  25. @export var damage_cooldown: float = 1.0
  26.  
  27. # --------------------------------------------------
  28. # On-ready & internal
  29. # --------------------------------------------------
  30. @onready var GRAVITY: float = ProjectSettings.get_setting("physics/2d/default_gravity")
  31.  
  32. var start_position: Vector2
  33. var move_direction: int = 1
  34. var patrol_timer: float = 0.0
  35. var last_damage_time: float = -99.0
  36. var can_chase: bool = true
  37. var chase_cooldown: float = 0.0
  38.  
  39. # UI
  40. var name_label: Label
  41. var health_bar: ProgressBar
  42. # --- CHANGE 1: Explicitly type the preloaded scene.
  43. @onready var DAMAGE_NUMBER_SCENE: PackedScene = preload("res://scenes/ui/DamageNumber.tscn")
  44.  
  45. # --------------------------------------------------
  46. # Life-cycle
  47. # --------------------------------------------------
  48. func _ready():
  49. add_to_group("enemies")
  50.  
  51. # Wait one frame to ensure position is set
  52. await get_tree().process_frame
  53.  
  54. start_position = global_position
  55. _create_ui()
  56.  
  57. # Set up collision layers
  58. # Layer 1: Players, Layer 2: Enemies, Layer 4: Environment (platforms)
  59. collision_layer = 2 # This enemy is on layer 2
  60. collision_mask = 5 # Collide with layers 1 (players) and 4 (environment/platforms)
  61.  
  62. # Reset state
  63. move_direction = 1
  64. patrol_timer = 0.0
  65. can_chase = true
  66. chase_cooldown = 0.0
  67.  
  68. if not multiplayer.is_server():
  69. set_physics_process(false)
  70.  
  71. # --------------------------------------------------
  72. # Physics (server only)
  73. # --------------------------------------------------
  74. func _physics_process(delta):
  75. if not multiplayer.is_server():
  76. return
  77.  
  78. # Gravity
  79. if not is_on_floor():
  80. velocity.y += GRAVITY * delta
  81. else:
  82. velocity.y = 0
  83.  
  84. # Update chase cooldown
  85. if chase_cooldown > 0:
  86. chase_cooldown -= delta
  87. if chase_cooldown <= 0:
  88. can_chase = true
  89.  
  90. # Simple patrol timer
  91. patrol_timer += delta
  92. if patrol_timer >= 4.0: # Change direction every 4 seconds
  93. move_direction *= -1
  94. patrol_timer = 0.0
  95.  
  96. # Check if too far from start
  97. var dist_from_start = abs(global_position.x - start_position.x)
  98. if dist_from_start > patrol_distance:
  99. move_direction = sign(start_position.x - global_position.x)
  100. patrol_timer = 0.0
  101.  
  102. # Look for nearby players (only if can chase)
  103. var nearest_player = _find_nearest_player()
  104. if nearest_player and can_chase:
  105. var dist_to_player = abs(nearest_player.global_position.x - global_position.x)
  106. if dist_to_player > 120.0: # Large dead zone
  107. # Chase player
  108. velocity.x = sign(nearest_player.global_position.x - global_position.x) * chase_speed
  109. else:
  110. # Too close, stop
  111. velocity.x = 0.0
  112. else:
  113. # Normal patrol
  114. velocity.x = move_direction * move_speed
  115.  
  116. move_and_slide()
  117. _deal_touch_damage()
  118.  
  119. # --------------------------------------------------
  120. # Helper - Find nearest player on same platform
  121. # --------------------------------------------------
  122. func _find_nearest_player():
  123. var nearest = null
  124. var closest_dist = detect_distance
  125.  
  126. for player in get_tree().get_nodes_in_group("players"):
  127. if not is_instance_valid(player):
  128. continue
  129.  
  130. var horizontal_dist = abs(player.global_position.x - global_position.x)
  131. var vertical_dist = abs(player.global_position.y - global_position.y)
  132.  
  133. # Only consider players on same platform (within 60 pixels vertically)
  134. if vertical_dist > 60.0:
  135. continue
  136.  
  137. # Find closest
  138. if horizontal_dist < closest_dist:
  139. closest_dist = horizontal_dist
  140. nearest = player
  141.  
  142. return nearest
  143.  
  144. # --------------------------------------------------
  145. # Touch damage
  146. # --------------------------------------------------
  147. func _deal_touch_damage():
  148. var now := Time.get_ticks_msec() / 1000.0
  149. if now - last_damage_time < damage_cooldown:
  150. return
  151.  
  152. for i in get_slide_collision_count():
  153. # --- CHANGE 2: Wrap the assignment in parentheses to satisfy the analyzer.
  154. var victim: Node = (get_slide_collision(i).get_collider() as Node)
  155. if victim and victim.is_in_group("players"):
  156. victim.take_damage(touch_damage)
  157. last_damage_time = now
  158. return
  159.  
  160. # --------------------------------------------------
  161. # Damage + death (unchanged)
  162. # --------------------------------------------------
  163. @rpc("authority", "call_local", "reliable")
  164. func take_damage(amount: int, _attacker_id: int):
  165. if not multiplayer.is_server():
  166. return
  167. health -= amount
  168. rpc("_show_damage_number", amount)
  169.  
  170. @rpc("authority", "call_local", "reliable")
  171. func _show_damage_number(damage: int):
  172. # --- CHANGE 3: Wrap the assignment in parentheses to satisfy the analyzer.
  173. var dn: Node2D = (DAMAGE_NUMBER_SCENE.instantiate() as Node2D)
  174. if dn:
  175. dn.damage = damage
  176. dn.global_position = global_position + Vector2(randf_range(-20, 20), -40)
  177. get_tree().root.add_child(dn)
  178.  
  179. func _die():
  180. if not multiplayer.is_server():
  181. return
  182.  
  183. # Award experience to nearby players
  184. for p in get_tree().get_nodes_in_group("players"):
  185. if p is CharacterBody2D and "gain_exp" in p:
  186. if global_position.distance_to(p.global_position) <= 300:
  187. p.gain_exp(exp_reward)
  188.  
  189. # Notify Main scene to respawn this enemy
  190. var main_scene = get_tree().current_scene
  191. if main_scene and main_scene.has_method("schedule_enemy_respawn"):
  192. main_scene.schedule_enemy_respawn(start_position, enemy_name, enemy_level)
  193.  
  194. # Call RPC to remove enemy on all clients
  195. rpc("_die_on_client")
  196. queue_free()
  197.  
  198. @rpc("authority", "call_local", "reliable")
  199. func _die_on_client():
  200. # This is called on all clients to remove the enemy visually
  201. queue_free()
  202.  
  203. # --------------------------------------------------
  204. # UI creation (unchanged)
  205. # --------------------------------------------------
  206. func _create_ui():
  207. var ui_container := Control.new()
  208. ui_container.set_anchors_preset(Control.PRESET_FULL_RECT)
  209. ui_container.mouse_filter = Control.MOUSE_FILTER_IGNORE
  210. ui_container.z_index = 10 # Render above platforms
  211. add_child(ui_container)
  212.  
  213. name_label = Label.new()
  214. name_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
  215. name_label.position = Vector2(-80, 10) # Closer to enemy
  216. name_label.size = Vector2(160, 20)
  217. name_label.add_theme_font_size_override("font_size", 11)
  218. name_label.add_theme_color_override("font_color", Color(1, 0.8, 0.3))
  219. name_label.add_theme_color_override("font_outline_color", Color.BLACK)
  220. name_label.add_theme_constant_override("outline_size", 2)
  221. name_label.text = "Lv." + str(enemy_level) + " " + enemy_name
  222. ui_container.add_child(name_label)
  223.  
  224. health_bar = ProgressBar.new()
  225. health_bar.position = Vector2(-30, -45)
  226. health_bar.size = Vector2(60, 3)
  227. health_bar.max_value = max_health
  228. health_bar.value = health
  229. health_bar.show_percentage = false
  230. var fill := StyleBoxFlat.new()
  231. fill.bg_color = Color(0.2, 0.8, 0.2)
  232. fill.content_margin_left = 0
  233. fill.content_margin_right = 0
  234. fill.content_margin_top = 0
  235. fill.content_margin_bottom = 0
  236. health_bar.add_theme_stylebox_override("fill", fill)
  237. var bg := StyleBoxFlat.new()
  238. bg.bg_color = Color(0.2, 0.05, 0.05)
  239. bg.content_margin_left = 0
  240. bg.content_margin_right = 0
  241. bg.content_margin_top = 0
  242. bg.content_margin_bottom = 0
  243. health_bar.add_theme_stylebox_override("background", bg)
  244. ui_container.add_child(health_bar)
  245.  
  246. func _update_health_bar():
  247. if health_bar:
  248. health_bar.value = health
  249.  
Advertisement
Add Comment
Please, Sign In to add comment