Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Enemy.gd (Godot 4.5, patrol + chase, zero warnings)
- extends CharacterBody2D
- # --------------------------------------------------
- # Exported stats
- # --------------------------------------------------
- @export var enemy_name: String = "Red Snail"
- @export var enemy_level: int = 5
- @export var max_health: int = 50
- @export var health: int = 50:
- set(value):
- health = clamp(value, 0, max_health)
- _update_health_bar()
- if health <= 0:
- _die()
- @export var exp_reward: int = 25
- @export var move_speed: float = 50.0
- @export var patrol_distance: float = 200.0
- @export var detect_distance: float = 250.0
- @export var chase_speed: float = 120.0
- @export var touch_damage: int = 5
- @export var damage_cooldown: float = 1.0
- # --------------------------------------------------
- # On-ready & internal
- # --------------------------------------------------
- @onready var GRAVITY: float = ProjectSettings.get_setting("physics/2d/default_gravity")
- var start_position: Vector2
- var move_direction: int = 1
- var patrol_timer: float = 0.0
- var last_damage_time: float = -99.0
- var can_chase: bool = true
- var chase_cooldown: float = 0.0
- # UI
- var name_label: Label
- var health_bar: ProgressBar
- # --- CHANGE 1: Explicitly type the preloaded scene.
- @onready var DAMAGE_NUMBER_SCENE: PackedScene = preload("res://scenes/ui/DamageNumber.tscn")
- # --------------------------------------------------
- # Life-cycle
- # --------------------------------------------------
- func _ready():
- add_to_group("enemies")
- # Wait one frame to ensure position is set
- await get_tree().process_frame
- start_position = global_position
- _create_ui()
- # Set up collision layers
- # Layer 1: Players, Layer 2: Enemies, Layer 4: Environment (platforms)
- collision_layer = 2 # This enemy is on layer 2
- collision_mask = 5 # Collide with layers 1 (players) and 4 (environment/platforms)
- # Reset state
- move_direction = 1
- patrol_timer = 0.0
- can_chase = true
- chase_cooldown = 0.0
- if not multiplayer.is_server():
- set_physics_process(false)
- # --------------------------------------------------
- # Physics (server only)
- # --------------------------------------------------
- func _physics_process(delta):
- if not multiplayer.is_server():
- return
- # Gravity
- if not is_on_floor():
- velocity.y += GRAVITY * delta
- else:
- velocity.y = 0
- # Update chase cooldown
- if chase_cooldown > 0:
- chase_cooldown -= delta
- if chase_cooldown <= 0:
- can_chase = true
- # Simple patrol timer
- patrol_timer += delta
- if patrol_timer >= 4.0: # Change direction every 4 seconds
- move_direction *= -1
- patrol_timer = 0.0
- # Check if too far from start
- var dist_from_start = abs(global_position.x - start_position.x)
- if dist_from_start > patrol_distance:
- move_direction = sign(start_position.x - global_position.x)
- patrol_timer = 0.0
- # Look for nearby players (only if can chase)
- var nearest_player = _find_nearest_player()
- if nearest_player and can_chase:
- var dist_to_player = abs(nearest_player.global_position.x - global_position.x)
- if dist_to_player > 120.0: # Large dead zone
- # Chase player
- velocity.x = sign(nearest_player.global_position.x - global_position.x) * chase_speed
- else:
- # Too close, stop
- velocity.x = 0.0
- else:
- # Normal patrol
- velocity.x = move_direction * move_speed
- move_and_slide()
- _deal_touch_damage()
- # --------------------------------------------------
- # Helper - Find nearest player on same platform
- # --------------------------------------------------
- func _find_nearest_player():
- var nearest = null
- var closest_dist = detect_distance
- for player in get_tree().get_nodes_in_group("players"):
- if not is_instance_valid(player):
- continue
- var horizontal_dist = abs(player.global_position.x - global_position.x)
- var vertical_dist = abs(player.global_position.y - global_position.y)
- # Only consider players on same platform (within 60 pixels vertically)
- if vertical_dist > 60.0:
- continue
- # Find closest
- if horizontal_dist < closest_dist:
- closest_dist = horizontal_dist
- nearest = player
- return nearest
- # --------------------------------------------------
- # Touch damage
- # --------------------------------------------------
- func _deal_touch_damage():
- var now := Time.get_ticks_msec() / 1000.0
- if now - last_damage_time < damage_cooldown:
- return
- for i in get_slide_collision_count():
- # --- CHANGE 2: Wrap the assignment in parentheses to satisfy the analyzer.
- var victim: Node = (get_slide_collision(i).get_collider() as Node)
- if victim and victim.is_in_group("players"):
- victim.take_damage(touch_damage)
- last_damage_time = now
- return
- # --------------------------------------------------
- # Damage + death (unchanged)
- # --------------------------------------------------
- @rpc("authority", "call_local", "reliable")
- func take_damage(amount: int, _attacker_id: int):
- if not multiplayer.is_server():
- return
- health -= amount
- rpc("_show_damage_number", amount)
- @rpc("authority", "call_local", "reliable")
- func _show_damage_number(damage: int):
- # --- CHANGE 3: Wrap the assignment in parentheses to satisfy the analyzer.
- var dn: Node2D = (DAMAGE_NUMBER_SCENE.instantiate() as Node2D)
- if dn:
- dn.damage = damage
- dn.global_position = global_position + Vector2(randf_range(-20, 20), -40)
- get_tree().root.add_child(dn)
- func _die():
- if not multiplayer.is_server():
- return
- # Award experience to nearby players
- for p in get_tree().get_nodes_in_group("players"):
- if p is CharacterBody2D and "gain_exp" in p:
- if global_position.distance_to(p.global_position) <= 300:
- p.gain_exp(exp_reward)
- # Notify Main scene to respawn this enemy
- var main_scene = get_tree().current_scene
- if main_scene and main_scene.has_method("schedule_enemy_respawn"):
- main_scene.schedule_enemy_respawn(start_position, enemy_name, enemy_level)
- # Call RPC to remove enemy on all clients
- rpc("_die_on_client")
- queue_free()
- @rpc("authority", "call_local", "reliable")
- func _die_on_client():
- # This is called on all clients to remove the enemy visually
- queue_free()
- # --------------------------------------------------
- # UI creation (unchanged)
- # --------------------------------------------------
- func _create_ui():
- var ui_container := Control.new()
- ui_container.set_anchors_preset(Control.PRESET_FULL_RECT)
- ui_container.mouse_filter = Control.MOUSE_FILTER_IGNORE
- ui_container.z_index = 10 # Render above platforms
- add_child(ui_container)
- name_label = Label.new()
- name_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
- name_label.position = Vector2(-80, 10) # Closer to enemy
- name_label.size = Vector2(160, 20)
- name_label.add_theme_font_size_override("font_size", 11)
- name_label.add_theme_color_override("font_color", Color(1, 0.8, 0.3))
- name_label.add_theme_color_override("font_outline_color", Color.BLACK)
- name_label.add_theme_constant_override("outline_size", 2)
- name_label.text = "Lv." + str(enemy_level) + " " + enemy_name
- ui_container.add_child(name_label)
- health_bar = ProgressBar.new()
- health_bar.position = Vector2(-30, -45)
- health_bar.size = Vector2(60, 3)
- health_bar.max_value = max_health
- health_bar.value = health
- health_bar.show_percentage = false
- var fill := StyleBoxFlat.new()
- fill.bg_color = Color(0.2, 0.8, 0.2)
- fill.content_margin_left = 0
- fill.content_margin_right = 0
- fill.content_margin_top = 0
- fill.content_margin_bottom = 0
- health_bar.add_theme_stylebox_override("fill", fill)
- var bg := StyleBoxFlat.new()
- bg.bg_color = Color(0.2, 0.05, 0.05)
- bg.content_margin_left = 0
- bg.content_margin_right = 0
- bg.content_margin_top = 0
- bg.content_margin_bottom = 0
- health_bar.add_theme_stylebox_override("background", bg)
- ui_container.add_child(health_bar)
- func _update_health_bar():
- if health_bar:
- health_bar.value = health
Advertisement
Add Comment
Please, Sign In to add comment