Mejoras en los enemigos
This commit is contained in:
1 parent
8a5290c5ef
commit
56f9b475b6
12 files changed
+631
-51
No files matched your search
+205
-28
@@ -4,9 +4,8 @@ extends CharacterBody2D
|
||||
signal died
|
||||
|
||||
|
||||
# First real enemy on purpose: no generic Enemy base class yet.
|
||||
# Once a second/third enemy exists, we can extract only the behaviour they
|
||||
# actually share instead of guessing an abstraction in advance.
|
||||
# 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")
|
||||
@@ -30,6 +29,19 @@ enum AttackState {
|
||||
@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
|
||||
@@ -40,6 +52,9 @@ enum AttackState {
|
||||
@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
|
||||
|
||||
@@ -112,8 +127,8 @@ var drops_handled_by_level: bool = false
|
||||
## Visible launch, collapse and fade before the body disappears.
|
||||
@export_range(0.3, 2.5, 0.05) var death_animation_duration: float = 0.9
|
||||
|
||||
## Limits how far the enemy may chase from its spawn point; 0 = unrestricted.
|
||||
## Small limits keep independent testing encounters from piling up.
|
||||
## 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.
|
||||
@@ -167,6 +182,10 @@ 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 = {}
|
||||
@@ -223,8 +242,7 @@ func _physics_process(delta: float) -> void:
|
||||
_apply_gravity(delta)
|
||||
|
||||
# The enemy deliberately does nothing until the camera has shown it once.
|
||||
# After waking up it stays active, even if the player later runs away from
|
||||
# the screen area where the enemy was first seen.
|
||||
# After waking up it may pursue or return home even outside the screen.
|
||||
if not is_awake:
|
||||
velocity.x = 0.0
|
||||
move_and_slide()
|
||||
@@ -255,11 +273,12 @@ func _physics_process(delta: float) -> void:
|
||||
return
|
||||
|
||||
if attack_state == AttackState.IDLE:
|
||||
_update_idle_behaviour()
|
||||
_update_idle_behaviour(delta)
|
||||
else:
|
||||
_update_attack(delta)
|
||||
|
||||
move_and_slide()
|
||||
_update_jump_visual()
|
||||
queue_redraw()
|
||||
|
||||
|
||||
@@ -310,35 +329,170 @@ func _apply_gravity(delta: float) -> void:
|
||||
velocity.y = min(velocity.y, max_fall_speed)
|
||||
|
||||
|
||||
func _update_idle_behaviour() -> void:
|
||||
_face_target()
|
||||
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 attack_cooldown_timer <= 0.0 and _is_target_in_attack_trigger():
|
||||
_start_attack()
|
||||
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
|
||||
|
||||
_chase_target()
|
||||
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
|
||||
|
||||
func _chase_target() -> void:
|
||||
# Limit chasing to the current testing station when chase_radius is set.
|
||||
# Going back towards the spawn point is always allowed.
|
||||
var distance_from_spawn: float = global_position.x - spawn_position.x
|
||||
if chase_radius > 0.0 and absf(distance_from_spawn) >= chase_radius and signf(distance_from_spawn) == facing_direction:
|
||||
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
|
||||
|
||||
# Do not blindly walk off a platform while chasing. Proper pathfinding is
|
||||
# intentionally postponed until levels actually require it.
|
||||
if is_on_floor() and not floor_probe.is_colliding():
|
||||
velocity.x = 0.0
|
||||
# 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 is_on_wall():
|
||||
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)
|
||||
|
||||
velocity.x = facing_direction * move_speed
|
||||
|
||||
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)
|
||||
|
||||
|
||||
# ============================================================================
|
||||
@@ -347,10 +501,17 @@ func _chase_target() -> void:
|
||||
|
||||
func _is_target_in_attack_trigger() -> bool:
|
||||
var offset_to_target: Vector2 = target.global_position - global_position
|
||||
return (
|
||||
abs(offset_to_target.x) <= attack_trigger_distance
|
||||
and abs(offset_to_target.y) <= attack_vertical_tolerance
|
||||
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:
|
||||
@@ -446,8 +607,16 @@ func _update_attack_damage() -> void:
|
||||
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, attack_hitbox.global_position)
|
||||
damage_target.take_damage(attack_damage, global_position, attack_knockback_multiplier)
|
||||
|
||||
|
||||
func _interrupt_attack() -> void:
|
||||
@@ -463,6 +632,7 @@ func _interrupt_attack() -> void:
|
||||
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.
|
||||
@@ -485,6 +655,9 @@ func take_damage(amount: int, source_position: Vector2) -> void:
|
||||
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
|
||||
@@ -599,6 +772,10 @@ 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
|
||||
|
||||
Reference in new issue
Block a user