916 lines
31 KiB
GDScript
916 lines
31 KiB
GDScript
extends CharacterBody2D
|
|
|
|
|
|
signal died
|
|
|
|
|
|
# The five scene variants share this controller. Their movement, jump and
|
|
# attack settings live in enemies/variants; no separate AI hierarchy is needed.
|
|
|
|
const PLAYER_HURTBOX_MASK: int = 1 << 3
|
|
const DROP_MANAGER := preload("res://pickups/drop_manager.gd")
|
|
const COMBAT_FX = preload("res://effects/combat_fx.gd")
|
|
|
|
|
|
enum AttackState {
|
|
IDLE,
|
|
WINDUP,
|
|
ACTIVE,
|
|
RECOVERY,
|
|
}
|
|
|
|
|
|
# ============================================================================
|
|
# MOVEMENT
|
|
# ============================================================================
|
|
|
|
@export_category("Movement")
|
|
|
|
@export var move_speed: float = 78.0
|
|
@export var max_fall_speed: float = 800.0
|
|
|
|
## Seeing an enemy on screen wakes it, but proximity starts the pursuit.
|
|
## Once engaged it keeps chasing up to 1.5 times this distance, within its leash.
|
|
@export_range(80.0, 1200.0, 10.0) var aggro_range: float = 340.0
|
|
|
|
## Only agile variants jump. Others still stop at walls and platform edges.
|
|
@export var can_jump: bool = false
|
|
@export_range(-900.0, -100.0, 10.0) var jump_velocity: float = -560.0
|
|
@export_range(0.2, 3.0, 0.05) var jump_cooldown: float = 0.9
|
|
@export var hop_while_chasing: bool = false
|
|
|
|
## A landing must exist within this drop; never jump into an unbounded pit.
|
|
@export_range(0.0, 400.0, 10.0) var max_jump_drop: float = 200.0
|
|
|
|
|
|
# ============================================================================
|
|
# COMBAT
|
|
# ============================================================================
|
|
|
|
@export_category("Combat")
|
|
|
|
@export_range(1, 100, 1) var max_health: int = 20
|
|
@export_range(0, 100, 1) var attack_damage: int = 1
|
|
|
|
## Outgoing push on the player, independent of damage and enemy knockback.
|
|
@export_range(0.0, 3.0, 0.05) var attack_knockback_multiplier: float = 1.0
|
|
|
|
## Minimum time between the beginning of two attacks.
|
|
@export var attack_interval: float = 2.0
|
|
|
|
## Horizontal distance at which the Walker stops chasing and prepares a hit.
|
|
## This is intentionally wider than the final hitbox so entering range does not
|
|
## mean taking unavoidable damage.
|
|
@export var attack_trigger_distance: float = 140.0
|
|
|
|
## Keeps the Walker from starting a ground attack against a player far above or
|
|
## below it.
|
|
@export var attack_vertical_tolerance: float = 80.0
|
|
|
|
## Readable pause before the dangerous part of the attack starts. During this
|
|
## time the direction is locked, so moving behind the enemy is a valid dodge.
|
|
@export var attack_windup_duration: float = 0.35
|
|
|
|
## Duration of the actual hit. The hitbox sweeps forward during this window.
|
|
@export var attack_duration: float = 0.22
|
|
|
|
## Small recovery after the hit before the enemy can hitbox again.
|
|
@export var attack_recovery_duration: float = 0.30
|
|
|
|
## Where the hitbox starts, relative to the enemy while facing right.
|
|
@export var attack_start_offset: Vector2 = Vector2(34.0, 0.0)
|
|
|
|
## Furthest horizontal position reached by the moving hitbox.
|
|
@export var attack_reach: float = 120.0
|
|
|
|
## A successful player hit briefly interrupts the Walker. This makes attacking
|
|
## during its windup useful instead of forcing an unavoidable damage trade.
|
|
@export var hit_stun_duration: float = 0.18
|
|
|
|
## Strength of the knockback applied per point of damage. The final push is
|
|
## divided by the enemy's max health, so tougher enemies move less.
|
|
@export var knockback_strength: float = 400.0
|
|
|
|
## How long the knockback impulse lasts before friction brings the enemy to a halt.
|
|
@export var knockback_duration: float = 0.4
|
|
|
|
## Damage threshold to trigger a longer stun (e.g. a fully charged special attack).
|
|
@export_range(0, 100, 1) var stun_damage_threshold: int = 5
|
|
|
|
## Stun duration when the received damage exceeds the threshold.
|
|
@export var strong_hit_stun_duration: float = 2.0
|
|
|
|
|
|
# ============================================================================
|
|
# RESPAWN
|
|
# ============================================================================
|
|
|
|
@export_category("Drops")
|
|
|
|
## Default weighted drops for normal encounters.
|
|
@export var drop_table: PickupDropTable
|
|
|
|
## Optional fixed drop, used by the Level 01 test route. When assigned, this
|
|
## object drops with 100% probability INSTEAD of the random table.
|
|
@export var guaranteed_drop: PickupData
|
|
|
|
## In the Android lab, Level01 watches deaths and spawns the fixed reward.
|
|
## Other levels keep using this enemy's own random/guaranteed drop logic.
|
|
var drops_handled_by_level: bool = false
|
|
|
|
|
|
@export_category("Respawn")
|
|
|
|
## A value of 0 disables respawn (one enemy = one test item).
|
|
@export var respawn_delay: float = 5.0
|
|
|
|
## Visible launch, collapse and fade before the body disappears.
|
|
@export_range(0.3, 2.5, 0.05) var death_animation_duration: float = 0.9
|
|
|
|
## Horizontal leash from spawn; 0 = unrestricted. Losing the target or
|
|
## reaching the leash makes the enemy return home rather than follow forever.
|
|
@export var chase_radius: float = 0.0
|
|
|
|
## Only used to describe enemy variants in test HUDs.
|
|
@export var enemy_display_name: String = "Walker"
|
|
|
|
|
|
# ============================================================================
|
|
# DEBUG
|
|
# ============================================================================
|
|
|
|
@export_category("Debug")
|
|
|
|
## Enabled while the enemy has no final sprite/animation. The grey rectangle is
|
|
## the temporary body. Red overlays are the hurtbox and active attack hitbox.
|
|
## The orange area during windup previews the complete attack sweep.
|
|
@export var show_debug_hitboxes: bool = false
|
|
|
|
## Shows the enemy health bar above its head and floating damage numbers when
|
|
## it is hit. Purely debug: can be disabled once final art and UI exist.
|
|
@export var show_debug_enemy_info: bool = true
|
|
|
|
|
|
# ============================================================================
|
|
# NODES
|
|
# ============================================================================
|
|
|
|
@onready var body_collision: CollisionShape2D = $CollisionShape2D
|
|
@onready var hurtbox_collision: CollisionShape2D = $Hurtbox/CollisionShape2D
|
|
@onready var attack_hitbox: Area2D = $AttackHitbox
|
|
@onready var attack_collision: CollisionShape2D = $AttackHitbox/CollisionShape2D
|
|
@onready var floor_probe: RayCast2D = $FloorProbe
|
|
@onready var visibility_notifier: VisibleOnScreenNotifier2D = $VisibleOnScreenNotifier2D
|
|
@onready var visual: Sprite2D = $Visual
|
|
|
|
|
|
# ============================================================================
|
|
# RUNTIME STATE
|
|
# ============================================================================
|
|
|
|
var current_health: int
|
|
var facing_direction: float = -1.0
|
|
|
|
var attack_state: int = AttackState.IDLE
|
|
var attack_phase_timer: float = 0.0
|
|
var attack_cooldown_timer: float = 0.0
|
|
var attack_direction: float = -1.0
|
|
var hit_stun_timer: float = 0.0
|
|
|
|
var is_awake: bool = false
|
|
var is_dead: bool = false
|
|
|
|
var spawn_position: Vector2
|
|
var target: Node2D
|
|
var _is_pursuing: bool = false
|
|
var _jump_cooldown_timer: float = 0.0
|
|
var _jump_in_progress: bool = false
|
|
var _jump_target_x: float = 0.0
|
|
|
|
## Prevents one sweep from damaging the same target once per physics frame.
|
|
var attack_hit_targets: Dictionary = {}
|
|
|
|
## Timer for the knockback impulse applied when the enemy is hit.
|
|
var knockback_timer: float = 0.0
|
|
|
|
## Floating damage numbers shown above the enemy when it takes damage.
|
|
## Each entry: { text: String, local_pos: Vector2, timer: float }
|
|
var _damage_texts: Array = []
|
|
var _hit_flash_timer: float = 0.0
|
|
var _death_elapsed: float = 0.0
|
|
var _last_hit_direction: float = 1.0
|
|
var _visual_position: Vector2
|
|
var _visual_scale: Vector2
|
|
var _visual_rotation: float
|
|
var _visual_color: Color
|
|
|
|
|
|
func _ready() -> void:
|
|
_visual_position = visual.position
|
|
_visual_scale = visual.scale
|
|
_visual_rotation = visual.rotation
|
|
_visual_color = visual.self_modulate
|
|
spawn_position = global_position
|
|
current_health = max_health
|
|
target = get_tree().get_first_node_in_group(&"player") as Node2D
|
|
|
|
attack_hitbox.collision_layer = 0
|
|
attack_hitbox.collision_mask = PLAYER_HURTBOX_MASK
|
|
attack_hitbox.monitoring = true
|
|
|
|
if drop_table == null:
|
|
var default_table := load("res://enemies/walker/walker_drop.tres")
|
|
if default_table != null:
|
|
drop_table = default_table
|
|
|
|
visibility_notifier.screen_entered.connect(_wake_up)
|
|
|
|
_update_facing()
|
|
_reset_attack_hitbox_position()
|
|
queue_redraw()
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if is_dead:
|
|
_update_death_animation(delta)
|
|
return
|
|
|
|
_hit_flash_timer = maxf(0.0, _hit_flash_timer - delta)
|
|
visual.self_modulate = Color(1.8, 1.8, 1.8) if _hit_flash_timer > 0.0 else _visual_color
|
|
_update_timers(delta)
|
|
_update_damage_texts(delta)
|
|
_apply_gravity(delta)
|
|
|
|
# The enemy deliberately does nothing until the camera has shown it once.
|
|
# After waking up it may pursue or return home even outside the screen.
|
|
if not is_awake:
|
|
velocity.x = 0.0
|
|
move_and_slide()
|
|
queue_redraw()
|
|
return
|
|
|
|
_acquire_target_if_needed()
|
|
if target == null:
|
|
velocity.x = 0.0
|
|
move_and_slide()
|
|
queue_redraw()
|
|
return
|
|
|
|
# Being hit pauses both movement and attack. While the knockback impulse
|
|
# lasts the enemy is pushed back, then friction brings it to a halt. The
|
|
# cooldown keeps running, so interrupting an attack does not make the enemy
|
|
# immediately start another one.
|
|
if hit_stun_timer > 0.0:
|
|
if knockback_timer > 0.0:
|
|
knockback_timer = max(knockback_timer - delta, 0.0)
|
|
var fade: float = knockback_timer / knockback_duration if knockback_duration > 0.0 else 0.0
|
|
velocity.x = move_toward(velocity.x, 0.0, knockback_strength * delta * (1.0 - fade + 0.1))
|
|
else:
|
|
velocity.x = move_toward(velocity.x, 0.0, knockback_strength * delta)
|
|
|
|
move_and_slide()
|
|
queue_redraw()
|
|
return
|
|
|
|
if attack_state == AttackState.IDLE:
|
|
_update_idle_behaviour(delta)
|
|
else:
|
|
_update_attack(delta)
|
|
|
|
move_and_slide()
|
|
_update_jump_visual()
|
|
queue_redraw()
|
|
|
|
|
|
# ============================================================================
|
|
# ACTIVATION AND TARGETING
|
|
# ============================================================================
|
|
|
|
func _wake_up() -> void:
|
|
is_awake = true
|
|
|
|
|
|
func _acquire_target_if_needed() -> void:
|
|
if target != null and is_instance_valid(target):
|
|
return
|
|
|
|
target = get_tree().get_first_node_in_group(&"player") as Node2D
|
|
|
|
|
|
func _face_target() -> void:
|
|
var direction_to_target: float = signf(target.global_position.x - global_position.x)
|
|
if direction_to_target == 0.0 or direction_to_target == facing_direction:
|
|
return
|
|
|
|
facing_direction = direction_to_target
|
|
_update_facing()
|
|
|
|
|
|
func _update_facing() -> void:
|
|
# The ledge probe follows the current movement direction. Attack direction is
|
|
# stored separately once a windup starts so the enemy cannot rotate mid-hit.
|
|
floor_probe.position.x = abs(floor_probe.position.x) * facing_direction
|
|
|
|
if attack_state == AttackState.IDLE:
|
|
_reset_attack_hitbox_position()
|
|
# The five variants share one collision box and AI, but use different sprites.
|
|
visual.flip_h = facing_direction < 0.0
|
|
|
|
|
|
# ============================================================================
|
|
# MOVEMENT
|
|
# ============================================================================
|
|
|
|
func _apply_gravity(delta: float) -> void:
|
|
if is_on_floor():
|
|
return
|
|
|
|
velocity += get_gravity() * delta
|
|
velocity.y = min(velocity.y, max_fall_speed)
|
|
|
|
|
|
func _update_idle_behaviour(delta: float) -> void:
|
|
var offset_to_target: Vector2 = target.global_position - global_position
|
|
var detection_distance: float = aggro_range * (1.5 if _is_pursuing else 1.0)
|
|
var target_inside_leash: bool = (
|
|
chase_radius <= 0.0
|
|
or absf(target.global_position.x - spawn_position.x) <= chase_radius
|
|
)
|
|
_is_pursuing = (
|
|
not bool(target.get("is_dead"))
|
|
and target_inside_leash
|
|
and offset_to_target.length_squared() <= detection_distance * detection_distance
|
|
)
|
|
|
|
if _is_pursuing:
|
|
_face_target()
|
|
if attack_cooldown_timer <= 0.0 and _is_target_in_attack_trigger():
|
|
_start_attack()
|
|
return
|
|
_move_towards(target.global_position, delta)
|
|
else:
|
|
# Return naturally, without teleporting or healing. Non-jumpers still
|
|
# refuse ledges; this is local steering, not a global pathfinding system.
|
|
_move_towards(spawn_position, delta)
|
|
|
|
|
|
func _move_towards(destination: Vector2, delta: float) -> void:
|
|
# Commit to the chosen landing instead of homing onto a moving player in
|
|
# mid-air. A hit clears this goal so it never cancels received knockback.
|
|
if _jump_in_progress and not is_on_floor():
|
|
velocity.x = clampf((_jump_target_x - global_position.x) / delta, -move_speed, move_speed)
|
|
return
|
|
|
|
var offset: Vector2 = destination - global_position
|
|
if absf(offset.x) > 8.0:
|
|
facing_direction = signf(offset.x)
|
|
_update_facing()
|
|
|
|
if not is_on_floor():
|
|
velocity.x = move_toward(velocity.x, signf(offset.x) * move_speed, move_speed * 3.0 * delta)
|
|
return
|
|
|
|
var stopping_distance: float = 38.0 if _is_pursuing else 8.0
|
|
if absf(offset.x) <= stopping_distance and absf(offset.y) <= 36.0:
|
|
velocity.x = 0.0
|
|
return
|
|
|
|
# Refresh after turning: the previous physics frame's probe points the
|
|
# other way. Look ahead early enough to jump BEFORE touching a step.
|
|
floor_probe.force_raycast_update()
|
|
var edge_ahead: bool = not floor_probe.is_colliding()
|
|
var look_ahead: Vector2 = Vector2(facing_direction * maxf(32.0, move_speed * 0.25), 0.0)
|
|
var wall_ahead: bool = test_move(global_transform, look_ahead)
|
|
if _try_jump(destination, wall_ahead, edge_ahead):
|
|
return
|
|
|
|
if absf(offset.x) <= stopping_distance or edge_ahead:
|
|
velocity.x = 0.0
|
|
return
|
|
# Use a fresh forward test, not is_on_wall() from the previous direction.
|
|
if test_move(global_transform, Vector2(facing_direction * 3.0, 0.0)):
|
|
velocity.x = 0.0
|
|
return
|
|
velocity.x = facing_direction * minf(move_speed, absf(offset.x) / delta)
|
|
|
|
|
|
func _try_jump(destination: Vector2, wall_ahead: bool, edge_ahead: bool) -> bool:
|
|
if not can_jump or not is_on_floor() or _jump_cooldown_timer > 0.0:
|
|
return false
|
|
var offset: Vector2 = destination - global_position
|
|
var climb_needed: bool = offset.y < -36.0
|
|
var wants_hop: bool = (
|
|
hop_while_chasing and _is_pursuing
|
|
and absf(offset.x) > attack_trigger_distance + 40.0
|
|
and absf(offset.y) < 48.0
|
|
)
|
|
if not wall_ahead and not edge_ahead and not climb_needed and not wants_hop:
|
|
return false
|
|
|
|
# Failed attempts are throttled too: no terrain scans every physics frame.
|
|
_jump_cooldown_timer = 0.25
|
|
var gravity_y: float = get_gravity().y
|
|
if gravity_y <= 0.0 or jump_velocity >= 0.0:
|
|
return false
|
|
var max_rise: float = jump_velocity * jump_velocity / (2.0 * gravity_y) - 10.0
|
|
if max_rise <= 0.0 or test_move(global_transform, Vector2(0.0, -max_rise)):
|
|
return false
|
|
|
|
var landing: Vector2 = _find_jump_landing(destination, max_rise, gravity_y, climb_needed)
|
|
if not landing.is_finite():
|
|
return false
|
|
|
|
_jump_target_x = landing.x
|
|
_jump_in_progress = true
|
|
_jump_cooldown_timer = jump_cooldown
|
|
velocity = Vector2(facing_direction * move_speed, jump_velocity)
|
|
COMBAT_FX.spawn(get_parent(), global_position + Vector2(0.0, 26.0), &"dust")
|
|
return true
|
|
|
|
|
|
func _find_jump_landing(destination: Vector2, max_rise: float, gravity_y: float, climbing: bool) -> Vector2:
|
|
var body_shape: RectangleShape2D = body_collision.shape as RectangleShape2D
|
|
var half_size: Vector2 = body_shape.size * 0.5
|
|
var feet_y: float = global_position.y + half_size.y
|
|
var best_landing: Vector2 = Vector2.INF
|
|
var best_score: float = INF
|
|
var space: PhysicsDirectSpaceState2D = get_world_2d().direct_space_state
|
|
# A handful of local candidates covers steps, nearby platforms and small
|
|
# gaps. Both feet must fit, and flight time must permit the horizontal move.
|
|
for distance: float in [48.0, 80.0, 120.0, 160.0, 200.0, 240.0]:
|
|
var x: float = global_position.x + facing_direction * distance
|
|
if chase_radius > 0.0 and absf(x - spawn_position.x) > chase_radius:
|
|
continue
|
|
var query := PhysicsRayQueryParameters2D.create(
|
|
Vector2(x, feet_y - max_rise), Vector2(x, feet_y + max_jump_drop + 2.0),
|
|
collision_mask, [get_rid()]
|
|
)
|
|
var hit: Dictionary = space.intersect_ray(query)
|
|
if hit.is_empty() or (hit["normal"] as Vector2).y > -0.7:
|
|
continue
|
|
var surface: Vector2 = hit["position"]
|
|
if climbing and surface.y >= feet_y - 16.0:
|
|
continue
|
|
var flight_squared: float = jump_velocity * jump_velocity + 2.0 * gravity_y * (surface.y - feet_y)
|
|
if flight_squared <= 0.0:
|
|
continue
|
|
var flight_time: float = (-jump_velocity + sqrt(flight_squared)) / gravity_y
|
|
if distance > move_speed * flight_time * 0.9:
|
|
continue
|
|
|
|
var stable_landing: bool = true
|
|
for side: float in [-1.0, 1.0]:
|
|
var foot_x: float = x + side * (half_size.x + 3.0)
|
|
query.from = Vector2(foot_x, surface.y - 4.0)
|
|
query.to = Vector2(foot_x, surface.y + 4.0)
|
|
var foot_hit: Dictionary = space.intersect_ray(query)
|
|
if foot_hit.is_empty() or (foot_hit["normal"] as Vector2).y > -0.7:
|
|
stable_landing = false
|
|
break
|
|
if not stable_landing:
|
|
continue
|
|
|
|
var landing: Vector2 = Vector2(x, surface.y - half_size.y - 2.0)
|
|
var landing_transform: Transform2D = global_transform
|
|
landing_transform.origin = landing
|
|
if test_move(landing_transform, Vector2.UP * 2.0, null, 0.08, true):
|
|
continue
|
|
var score: float = landing.distance_squared_to(destination)
|
|
if score < best_score:
|
|
best_score = score
|
|
best_landing = landing
|
|
return best_landing
|
|
|
|
|
|
func _update_jump_visual() -> void:
|
|
if not _jump_in_progress:
|
|
visual.scale = _visual_scale
|
|
return
|
|
if is_on_floor():
|
|
_jump_in_progress = false
|
|
visual.scale = _visual_scale
|
|
COMBAT_FX.spawn(get_parent(), global_position + Vector2(0.0, 26.0), &"dust")
|
|
else:
|
|
# A small airborne pose without changing collision sizes or sprite art.
|
|
visual.scale = _visual_scale * Vector2(0.9, 1.08)
|
|
|
|
|
|
# ============================================================================
|
|
# ATTACK
|
|
# ============================================================================
|
|
|
|
func _is_target_in_attack_trigger() -> bool:
|
|
var offset_to_target: Vector2 = target.global_position - global_position
|
|
if (
|
|
not is_on_floor()
|
|
or absf(offset_to_target.x) > attack_trigger_distance
|
|
or absf(offset_to_target.y) > attack_vertical_tolerance
|
|
):
|
|
return false
|
|
# A player behind a step should trigger navigation, not attacks through it.
|
|
var query := PhysicsRayQueryParameters2D.create(
|
|
global_position, target.global_position, collision_mask, [get_rid()]
|
|
)
|
|
return get_world_2d().direct_space_state.intersect_ray(query).is_empty()
|
|
|
|
|
|
func _start_attack() -> void:
|
|
# Lock the direction here. During windup/active frames the player can dodge
|
|
# behind the enemy without the hitbox snapping around to follow them.
|
|
_face_target()
|
|
attack_direction = facing_direction
|
|
attack_state = AttackState.WINDUP
|
|
attack_phase_timer = attack_windup_duration
|
|
attack_cooldown_timer = attack_interval
|
|
attack_hit_targets.clear()
|
|
velocity.x = 0.0
|
|
_reset_attack_hitbox_position()
|
|
|
|
|
|
func _update_attack(delta: float) -> void:
|
|
velocity.x = 0.0
|
|
|
|
match attack_state:
|
|
AttackState.WINDUP:
|
|
_update_attack_windup(delta)
|
|
AttackState.ACTIVE:
|
|
_update_active_attack(delta)
|
|
AttackState.RECOVERY:
|
|
_update_attack_recovery(delta)
|
|
|
|
|
|
func _update_attack_windup(delta: float) -> void:
|
|
attack_phase_timer = max(attack_phase_timer - delta, 0.0)
|
|
if attack_phase_timer > 0.0:
|
|
return
|
|
|
|
attack_state = AttackState.ACTIVE
|
|
attack_phase_timer = attack_duration
|
|
_set_attack_hitbox_progress(0.0)
|
|
|
|
|
|
func _update_active_attack(delta: float) -> void:
|
|
attack_phase_timer = max(attack_phase_timer - delta, 0.0)
|
|
|
|
# Sweep the actual physics hitbox from close to the enemy towards its maximum
|
|
# reach. This gives the player time to jump, back away or move behind it.
|
|
var progress: float = 1.0
|
|
if attack_duration > 0.0:
|
|
progress = 1.0 - (attack_phase_timer / attack_duration)
|
|
_set_attack_hitbox_progress(clampf(progress, 0.0, 1.0))
|
|
_update_attack_damage()
|
|
|
|
if attack_phase_timer > 0.0:
|
|
return
|
|
|
|
attack_state = AttackState.RECOVERY
|
|
attack_phase_timer = attack_recovery_duration
|
|
|
|
|
|
func _update_attack_recovery(delta: float) -> void:
|
|
attack_phase_timer = max(attack_phase_timer - delta, 0.0)
|
|
if attack_phase_timer > 0.0:
|
|
return
|
|
|
|
attack_state = AttackState.IDLE
|
|
attack_hit_targets.clear()
|
|
_reset_attack_hitbox_position()
|
|
|
|
|
|
func _set_attack_hitbox_progress(progress: float) -> void:
|
|
var start_x: float = abs(attack_start_offset.x)
|
|
var current_x: float = lerpf(start_x, attack_reach, progress)
|
|
attack_hitbox.position = Vector2(
|
|
current_x * attack_direction,
|
|
attack_start_offset.y
|
|
)
|
|
|
|
|
|
func _reset_attack_hitbox_position() -> void:
|
|
var direction: float = attack_direction
|
|
if attack_state == AttackState.IDLE:
|
|
direction = facing_direction
|
|
|
|
attack_hitbox.position = Vector2(
|
|
abs(attack_start_offset.x) * direction,
|
|
attack_start_offset.y
|
|
)
|
|
|
|
|
|
func _update_attack_damage() -> void:
|
|
for area in attack_hitbox.get_overlapping_areas():
|
|
var damage_target: Node = area.get_parent()
|
|
if damage_target == null or not damage_target.has_method("take_damage"):
|
|
continue
|
|
|
|
var target_id: int = damage_target.get_instance_id()
|
|
if attack_hit_targets.has(target_id):
|
|
continue
|
|
|
|
# A swept hitbox must not reach through solid walls. Use the enemy's
|
|
# body as the source: a hitbox past the player could push them backwards.
|
|
var query := PhysicsRayQueryParameters2D.create(
|
|
global_position, (damage_target as Node2D).global_position,
|
|
collision_mask, [get_rid()]
|
|
)
|
|
if not get_world_2d().direct_space_state.intersect_ray(query).is_empty():
|
|
continue
|
|
attack_hit_targets[target_id] = true
|
|
damage_target.take_damage(attack_damage, global_position, attack_knockback_multiplier)
|
|
|
|
|
|
func _interrupt_attack() -> void:
|
|
if attack_state == AttackState.IDLE:
|
|
return
|
|
|
|
attack_state = AttackState.IDLE
|
|
attack_phase_timer = 0.0
|
|
attack_hit_targets.clear()
|
|
_reset_attack_hitbox_position()
|
|
|
|
|
|
func _update_timers(delta: float) -> void:
|
|
attack_cooldown_timer = max(attack_cooldown_timer - delta, 0.0)
|
|
hit_stun_timer = max(hit_stun_timer - delta, 0.0)
|
|
_jump_cooldown_timer = maxf(_jump_cooldown_timer - delta, 0.0)
|
|
|
|
|
|
# Updates floating damage numbers: they drift upward and fade out over time.
|
|
func _update_damage_texts(delta: float) -> void:
|
|
for i in range(_damage_texts.size() - 1, -1, -1):
|
|
var entry: Dictionary = _damage_texts[i]
|
|
entry.timer = max(entry.timer - delta, 0.0)
|
|
entry.local_pos.y -= 40.0 * delta
|
|
if entry.timer <= 0.0:
|
|
_damage_texts.remove_at(i)
|
|
|
|
# ============================================================================
|
|
# DAMAGE AND RESPAWN
|
|
# ============================================================================
|
|
|
|
func take_damage(amount: int, source_position: Vector2) -> void:
|
|
if amount <= 0 or is_dead:
|
|
return
|
|
|
|
current_health = max(current_health - amount, 0)
|
|
_hit_flash_timer = 0.09
|
|
is_awake = true
|
|
_is_pursuing = true
|
|
_jump_in_progress = false
|
|
visual.scale = _visual_scale
|
|
_last_hit_direction = signf(global_position.x - source_position.x)
|
|
if _last_hit_direction == 0.0:
|
|
_last_hit_direction = -facing_direction
|
|
COMBAT_FX.spawn(get_parent(), global_position, &"hit", _last_hit_direction)
|
|
var camera: Camera2D = get_viewport().get_camera_2d()
|
|
if camera != null and camera.has_method("impact"):
|
|
camera.call("impact", 1.0, 0.12)
|
|
|
|
# Floating damage number (debug only).
|
|
if show_debug_enemy_info:
|
|
_damage_texts.append({
|
|
"text": str(amount),
|
|
"local_pos": Vector2(0, -25),
|
|
"timer": 1.0
|
|
})
|
|
|
|
if current_health == 0:
|
|
_die()
|
|
return
|
|
|
|
# Light enemies are interruptible. This makes a well-timed player attack a
|
|
# defensive option and avoids mandatory damage trades at close range.
|
|
_interrupt_attack()
|
|
|
|
# Knockback: stronger hits push harder, tougher enemies (more max health)
|
|
# resist the push. Direction is away from the damage source.
|
|
var knockback_dir: float = signf(global_position.x - source_position.x)
|
|
if knockback_dir == 0.0:
|
|
knockback_dir = 1.0
|
|
|
|
var knockback_force: float = float(amount) * knockback_strength / float(max_health)
|
|
velocity.x = knockback_dir * knockback_force
|
|
velocity.y = -180.0
|
|
|
|
knockback_timer = knockback_duration
|
|
|
|
# Stun: normal hits use the brief hit stun; very strong hits (e.g. a fully
|
|
# charged special attack) trigger a longer stun.
|
|
if amount >= stun_damage_threshold:
|
|
hit_stun_timer = max(hit_stun_timer, strong_hit_stun_duration)
|
|
else:
|
|
hit_stun_timer = max(hit_stun_timer, hit_stun_duration)
|
|
|
|
var reaction_direction: float = signf(source_position.x - global_position.x)
|
|
if reaction_direction != 0.0:
|
|
facing_direction = reaction_direction
|
|
_update_facing()
|
|
|
|
|
|
func _die() -> void:
|
|
if is_dead:
|
|
return
|
|
_interrupt_attack()
|
|
is_dead = true
|
|
_death_elapsed = 0.0
|
|
velocity = Vector2(_last_hit_direction * 150.0, -210.0)
|
|
COMBAT_FX.spawn(get_parent(), global_position, &"death", _last_hit_direction)
|
|
# Logical death happens now, exactly once. Animation must not delay rewards.
|
|
died.emit()
|
|
|
|
# Test encounters always drop their assigned item, including the two items
|
|
# absent from the normal Walker table. Other levels keep their random loot.
|
|
var drops: Array[PickupData] = []
|
|
if not drops_handled_by_level:
|
|
if guaranteed_drop != null:
|
|
drops.append(guaranteed_drop)
|
|
elif drop_table != null:
|
|
drops = DROP_MANAGER.roll_drop(drop_table)
|
|
if not drops.is_empty():
|
|
var parent: Node = get_tree().current_scene
|
|
if parent == null:
|
|
parent = get_tree().root
|
|
DROP_MANAGER.spawn_drops(drops, parent, global_position)
|
|
|
|
# Disable combat immediately; keep the body colliding with the WORLD while it
|
|
# falls. Disabling the body now would let the death pose fall through streets.
|
|
hurtbox_collision.set_deferred("disabled", true)
|
|
attack_collision.set_deferred("disabled", true)
|
|
queue_redraw()
|
|
|
|
|
|
func _update_death_animation(delta: float) -> void:
|
|
_death_elapsed += delta
|
|
_update_damage_texts(delta)
|
|
var duration: float = maxf(death_animation_duration, 0.3)
|
|
if _death_elapsed < duration:
|
|
_apply_gravity(delta)
|
|
velocity.x = move_toward(velocity.x, 0.0, 180.0 * delta)
|
|
move_and_slide()
|
|
var collapse: float = smoothstep(0.06, duration * 0.55, _death_elapsed)
|
|
visual.rotation = _visual_rotation + _last_hit_direction * PI * 0.5 * collapse
|
|
visual.position = _visual_position + Vector2(0, 10.0 * collapse)
|
|
visual.scale = _visual_scale * Vector2(1.0, lerpf(1.0, 0.85, collapse))
|
|
var fade: float = 1.0 - smoothstep(duration * 0.65, duration, _death_elapsed)
|
|
var color: Color = Color(1.8, 1.8, 1.8) if _death_elapsed < 0.09 else _visual_color
|
|
color.a = _visual_color.a * fade
|
|
visual.self_modulate = color
|
|
queue_redraw()
|
|
return
|
|
|
|
visible = false
|
|
velocity = Vector2.ZERO
|
|
body_collision.set_deferred("disabled", true)
|
|
if respawn_delay <= 0.0:
|
|
# Keep the dead node for the lab's KO polling and Level02's total count.
|
|
set_physics_process(false)
|
|
elif _death_elapsed >= maxf(respawn_delay, duration):
|
|
_respawn()
|
|
|
|
|
|
func _respawn() -> void:
|
|
set_physics_process(true)
|
|
_death_elapsed = 0.0
|
|
_hit_flash_timer = 0.0
|
|
_is_pursuing = false
|
|
_jump_in_progress = false
|
|
_jump_cooldown_timer = 0.0
|
|
_jump_target_x = spawn_position.x
|
|
visual.position = _visual_position
|
|
visual.scale = _visual_scale
|
|
visual.rotation = _visual_rotation
|
|
visual.self_modulate = _visual_color
|
|
_damage_texts.clear()
|
|
global_position = spawn_position
|
|
velocity = Vector2.ZERO
|
|
current_health = max_health
|
|
|
|
attack_state = AttackState.IDLE
|
|
attack_phase_timer = 0.0
|
|
attack_cooldown_timer = 0.0
|
|
hit_stun_timer = 0.0
|
|
knockback_timer = 0.0
|
|
attack_hit_targets.clear()
|
|
attack_direction = facing_direction
|
|
_reset_attack_hitbox_position()
|
|
|
|
body_collision.set_deferred("disabled", false)
|
|
hurtbox_collision.set_deferred("disabled", false)
|
|
attack_collision.set_deferred("disabled", false)
|
|
|
|
visible = true
|
|
is_dead = false
|
|
|
|
# If the spawn point is currently inside the camera, wake immediately.
|
|
# Otherwise the notifier will wake the enemy when the camera reaches it.
|
|
is_awake = visibility_notifier.is_on_screen()
|
|
queue_redraw()
|
|
|
|
|
|
# ============================================================================
|
|
# TEMPORARY VISUAL + DEBUG HITBOXES
|
|
# ============================================================================
|
|
|
|
func _draw() -> void:
|
|
if is_dead:
|
|
if show_debug_enemy_info:
|
|
_draw_damage_texts()
|
|
return
|
|
# Retain a fallback body if an imported sprite is missing.
|
|
if visual.texture == null:
|
|
_draw_rectangle_collision(
|
|
body_collision,
|
|
Color(0.18, 0.18, 0.20, 0.90),
|
|
Color(0.85, 0.85, 0.90, 1.0)
|
|
)
|
|
|
|
# Enemy health bar and floating damage numbers (debug overlay).
|
|
if show_debug_enemy_info:
|
|
_draw_enemy_health_bar()
|
|
_draw_damage_texts()
|
|
|
|
# Orange windup is important gameplay feedback, not a debug hitbox.
|
|
# Keep it visible on Android even when red collision overlays are disabled.
|
|
if attack_state == AttackState.WINDUP:
|
|
_draw_attack_telegraph()
|
|
|
|
if not show_debug_hitboxes:
|
|
return
|
|
|
|
# Hurtbox: where punches / ground pound are allowed to damage the enemy.
|
|
_draw_rectangle_collision(
|
|
hurtbox_collision,
|
|
Color(1.0, 0.05, 0.05, 0.12),
|
|
Color(1.0, 0.05, 0.05, 0.55)
|
|
)
|
|
|
|
# Active attack: draw the real moving physics hitbox in strong red.
|
|
if attack_state == AttackState.ACTIVE:
|
|
_draw_rectangle_collision(
|
|
attack_collision,
|
|
Color(1.0, 0.05, 0.05, 0.28),
|
|
Color(1.0, 0.05, 0.05, 0.95),
|
|
attack_hitbox.position
|
|
)
|
|
|
|
|
|
func _draw_attack_telegraph() -> void:
|
|
var rectangle: RectangleShape2D = attack_collision.shape as RectangleShape2D
|
|
if rectangle == null:
|
|
return
|
|
|
|
var start_x: float = abs(attack_start_offset.x) * attack_direction
|
|
var end_x: float = attack_reach * attack_direction
|
|
var center_x: float = (start_x + end_x) * 0.5
|
|
var sweep_width: float = abs(end_x - start_x) + rectangle.size.x
|
|
var center: Vector2 = Vector2(center_x, attack_start_offset.y)
|
|
var sweep_size: Vector2 = Vector2(sweep_width, rectangle.size.y)
|
|
var rect: Rect2 = Rect2(center - sweep_size / 2.0, sweep_size)
|
|
|
|
draw_rect(rect, Color(1.0, 0.55, 0.05, 0.12), true)
|
|
draw_rect(rect, Color(1.0, 0.55, 0.05, 0.85), false, 2.0)
|
|
|
|
|
|
func _draw_rectangle_collision(
|
|
collision: CollisionShape2D,
|
|
fill_color: Color,
|
|
outline_color: Color,
|
|
extra_offset: Vector2 = Vector2.ZERO
|
|
) -> void:
|
|
var rectangle: RectangleShape2D = collision.shape as RectangleShape2D
|
|
if rectangle == null:
|
|
return
|
|
|
|
var center: Vector2 = collision.position + extra_offset
|
|
var rect: Rect2 = Rect2(center - rectangle.size / 2.0, rectangle.size)
|
|
draw_rect(rect, fill_color, true)
|
|
draw_rect(rect, outline_color, false, 2.0)
|
|
|
|
# Draws a small horizontal health bar just above the enemy's head.
|
|
func _draw_enemy_health_bar() -> void:
|
|
var bar_width: float = 40.0
|
|
var bar_height: float = 5.0
|
|
var bar_pos: Vector2 = Vector2(-bar_width * 0.5, -48.0)
|
|
|
|
# Background.
|
|
draw_rect(Rect2(bar_pos, Vector2(bar_width, bar_height)), Color(0.2, 0.2, 0.2, 0.8), true)
|
|
# Fill.
|
|
var fill_ratio: float = float(current_health) / float(max_health) if max_health > 0 else 0.0
|
|
draw_rect(
|
|
Rect2(bar_pos, Vector2(bar_width * fill_ratio, bar_height)),
|
|
Color(0.85, 0.2, 0.2, 0.95),
|
|
true
|
|
)
|
|
# Border.
|
|
draw_rect(Rect2(bar_pos, Vector2(bar_width, bar_height)), Color(1, 1, 1, 0.6), false, 1.0)
|
|
|
|
# Draws floating damage numbers that drift upward and fade out.
|
|
func _draw_damage_texts() -> void:
|
|
var font: Font = ThemeDB.get_default_theme().get_font("font", "Label")
|
|
if font == null:
|
|
return
|
|
|
|
for entry in _damage_texts:
|
|
var alpha: float = clamp(entry.timer, 0.0, 1.0)
|
|
draw_string(font, entry.local_pos, entry.text, HORIZONTAL_ALIGNMENT_LEFT, -1, 16, Color(1, 1, 1, alpha))
|