Implement enemy AI and handler; add placeable radar object; enhance maze functionality
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
class Placeable
|
||||
SIZE = [25, 15]
|
||||
|
||||
# The base class for all placeables in the game. Placeables are things that can be interacted with, but do not move (unlike characters).
|
||||
# They cannot be walked through, but can be interacted with (e.g. a chest can be opened, a torch can be lit, a radar can be used).
|
||||
# They are placed at the center of a tile, and their position is represented by the tile coordinate
|
||||
# An object is one that is placed at the center of a tile (radar, torch & chest)
|
||||
attr_reader :x, :y
|
||||
|
||||
def initialize(x, y)
|
||||
@x = x
|
||||
@y = y
|
||||
@frames_count = 0
|
||||
@spritesheet = nil
|
||||
end
|
||||
|
||||
def collides?(rect)
|
||||
return true if intersects?(self.rect, rect)
|
||||
false
|
||||
end
|
||||
|
||||
def rect
|
||||
[@x * 120 + 90 - SIZE[0] / 2, @y * 120 + 90 - SIZE[1], SIZE[0], SIZE[1]]
|
||||
end
|
||||
|
||||
def update
|
||||
# For now, placeables don't have any behavior
|
||||
end
|
||||
|
||||
def draw
|
||||
return unless @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
|
||||
|
||||
@spritesheet[frame_index].draw(screen_x - 20, screen_y - 20, screen_y, 2, 2)
|
||||
|
||||
return unless DEBUG
|
||||
|
||||
# collision box centered on same point
|
||||
Gosu.draw_rect(screen_x - SIZE[0] / 2, screen_y - SIZE[1], SIZE[0], SIZE[1], Gosu::Color.new(0x88ff0000), Float::INFINITY)
|
||||
end
|
||||
end
|
||||
|
||||
class Radar < Placeable
|
||||
def initialize(x, y)
|
||||
super(x, y)
|
||||
@frames_count = 4
|
||||
@spritesheet = Gosu::Image.load_tiles("assets/images/radar.png", 20, 20, retro: true)
|
||||
end
|
||||
end
|
||||
|
||||
class ObjectHandler
|
||||
attr_reader :objects
|
||||
|
||||
def initialize
|
||||
start_room_coords = $bus.get(:start_room_coords)
|
||||
@objects = [Radar.new(start_room_coords[0] / 2 - 1, start_room_coords[1] / 2 - 1)]
|
||||
|
||||
$bus.on_retrievable(:object_at) do |x, y|
|
||||
next @objects.find { |obj| obj.x == x && obj.y == y }
|
||||
end
|
||||
end
|
||||
|
||||
def add(object)
|
||||
@objects << object
|
||||
end
|
||||
|
||||
def collides?(rect)
|
||||
@objects.any? { |obj| obj.collides?(rect) }
|
||||
end
|
||||
|
||||
def update
|
||||
@objects.each(&:update)
|
||||
end
|
||||
|
||||
def draw
|
||||
@objects.each(&:draw)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user