Refactor props handling and inventory logic; add ghost prop preview

This commit is contained in:
2026-03-28 23:07:42 +00:00
parent 9b0d29555b
commit 1fbd12e6f6
10 changed files with 138 additions and 49 deletions
+30 -14
View File
@@ -8,11 +8,26 @@ class Prop
# A prop is one that is placed at the center of a tile (radar, torch & chest)
attr_reader :x, :y
def self.setup_sprites(path, frames)
@frames_count = frames
@spritesheet = Gosu::Image.load_tiles(path, 20, 20, retro: true)
end
def self.spritesheet
@spritesheet
end
def self.frames_count
@frames_count
end
def self.ghost
@spritesheet&.first
end
def initialize(x, y)
@x = x
@y = y
@frames_count = 0
@spritesheet = nil
@health = 100
end
@@ -34,20 +49,21 @@ class Prop
end
def update(dt)
return unless Gosu.button_down?(Gosu::MS_LEFT)
return unless Gosu.button_down?(Gosu::MS_LEFT) || Gosu.button_down?(Gosu::KB_X)
player_x, player_y = $bus.get(:player_position) || [0, 0]
tile_x, tile_y = [(player_x - 90) / 120, (player_y - 90) / 120].map(&:round)
return unless tile_x == @x && tile_y == @y
mouse_x, mouse_y = $bus.get(:mouse_pos)
cam_x, cam_y = $bus.get(:camera_pos)
world_mouse_x = mouse_x + cam_x
world_mouse_y = mouse_y + cam_y
return unless intersects?([world_mouse_x, world_mouse_y, 1, 1], self.rect)
if Gosu.button_down?(Gosu::MS_LEFT)
mouse_x, mouse_y = $bus.get(:mouse_pos)
cam_x, cam_y = $bus.get(:camera_pos)
world_mouse_x = mouse_x + cam_x
world_mouse_y = mouse_y + cam_y
return unless intersects?([world_mouse_x, world_mouse_y, 1, 1], self.rect)
end
@health -= DESTRUCTION_SPEED * dt
if @health <= 0
@@ -60,16 +76,16 @@ class Prop
end
def draw
return unless @spritesheet
return unless self.class.spritesheet
cam_x, cam_y = $bus.get(:camera_pos) || [0, 0]
screen_x = @x * 120 + 90 - cam_x
screen_y = @y * 120 + 90 - cam_y
elapsed = Gosu.milliseconds / 1000.0
frame_index = ((elapsed * @frames_count).floor) % @frames_count
frame_index = ((elapsed * self.class.frames_count).floor) % self.class.frames_count
@spritesheet[frame_index].draw(screen_x - 20, screen_y - 20, screen_y, 2, 2)
self.class.spritesheet[frame_index].draw(screen_x - 20, screen_y - 20, screen_y, 2, 2)
if @health < 100
percent = 1.0 - @health / 100.0
@@ -81,7 +97,7 @@ class Prop
bar_x = screen_x - w / 2
bar_y = screen_y + h + 5
Gosu.draw_rect(bar_x, bar_y, bar_width, bar_height, Gosu::Color::RED, screen_y + 1)
Gosu.draw_rect(bar_x, bar_y, bar_width, bar_height, Gosu::Color::RED, screen_y - 19)
end
return unless DEBUG