Implement traps system: add Trap, Landmine, and EventHorizon classes with collision detection and interaction logic; integrate trap handling in the game scene and enemy AI.

This commit is contained in:
2026-04-02 22:46:24 +01:00
parent db7c2aa7a2
commit 7c35419026
20 changed files with 294 additions and 13 deletions
+2 -2
View File
@@ -15,7 +15,7 @@ class EnemyAI
@time_since_attack = ATTACK_COOLDOWN
end
def update(player_x, player_y, dt)
def update(player_x, player_y, dt, force = false)
distance = Math.sqrt((player_x - @enemy.x)**2 + (player_y - @enemy.y)**2)
return unless distance < AGGRO_RADIUS
@@ -76,7 +76,7 @@ class EnemyAI
end
end
if @waypoints.empty? || @last_player_tile != player_tile
if force || @waypoints.empty? || @last_player_tile != player_tile
@last_player_tile = player_tile
@tile_path = $bus.get(:maze_solve, enemy_tile[0], enemy_tile[1], player_tile[0], player_tile[1]) || []
if $bus.get(:prop_at, enemy_tile[0], enemy_tile[1]).nil?
+33 -1
View File
@@ -26,6 +26,8 @@ class Enemy
@w = 25
@h = 30
@health = health
@stun = 0
@last_lock = :none
@lorentz = $bus.get(:lorentz_field?) || false
$bus.on(:lorentz_field!) do |lorentz|
@@ -37,6 +39,20 @@ class Enemy
$bus.on(:collides?) do |rect|
next collides?(rect) ? :enemy : nil
end
$bus.on(:blast) do |x, y, radius, damage, _|
dist = Math.hypot(@x - x, @y - y)
if dist <= radius + 20
take_damage(damage * 2)
end
end
$bus.on(:stun) do |x, y|
dist = Math.hypot(@x - x, @y - y)
if dist <= 100
@stun = 60 * 1 # stun for 1 second
end
end
end
def take_damage(amount)
@@ -56,8 +72,24 @@ class Enemy
end
def update(dt)
if @stun > 0
@stun -= 1
return
end
player_pos = $bus.get(:player_position)
@ai.update(*player_pos, dt) if player_pos
closest_event_horizon = $bus.get(:closest_event_horizon, @x, @y, 200)
if closest_event_horizon
@ai.update(*closest_event_horizon, dt, @last_lock != :event_horizon)
@last_lock = :event_horizon
else
@ai.update(*player_pos, dt, @last_lock != :player) if player_pos
@last_lock = :player
end
if $bus.get_all(:collides?, rect).include?(:trap)
$bus.emit(:trap_stepped_on, rect, :enemy)
end
end
def draw