Extracted movement and turret into 'components' that entity get add to its self when defined

This commit is contained in:
2019-11-20 09:08:54 -06:00
parent 82db9dd14d
commit 2d70736753
8 changed files with 145 additions and 50 deletions

View File

@@ -0,0 +1,31 @@
class IMICRTS
class Movement < Component
attr_accessor :pathfinder
def initialize(parent:)
@parent = parent
end
def rotate_towards(vector)
_angle = Gosu.angle(@parent.position.x, @parent.position.y, vector.x, vector.y)
a = (360.0 + (_angle - @parent.angle)) % 360.0
# FIXME: Fails if vector is directly behind entity
if @parent.angle.between?(_angle - 3, _angle + 3)
@parent.angle = _angle
elsif a < 180
@parent.angle -= 1.0
else
@parent.angle += 1.0
end
@parent.angle %= 360.0
end
def follow_path
if @pathfinder && node = @pathfinder.path_current_node
@pathfinder.path_next_node if @pathfinder.at_current_path_node?(@parent)
@parent.position -= (@parent.position.xy - node.tile.position.xy).normalized * @parent.speed
end
end
end
end

37
lib/components/turret.rb Normal file
View File

@@ -0,0 +1,37 @@
class IMICRTS
class Turret < Component
attr_accessor :angle, :center
def initialize(parent:)
@parent = parent
@angle = 0
@center = CyberarmEngine::Vector.new(0.5, 0.5)
end
def body_image=(image)
@body_image = Gosu::Image.new("#{IMICRTS::ASSETS_PATH}/#{image}", retro: true)
end
def shell_image=(image)
@shell_image = Gosu::Image.new("#{IMICRTS::ASSETS_PATH}/#{image}", retro: true)
end
def overlay_image=(image)
@overlay_image = Gosu::Image.new("#{IMICRTS::ASSETS_PATH}/#{image}", retro: true)
end
def render
@render = Gosu.render(32, 32, retro: true) do
@body_image.draw(0, 0, 0) if @body_image
@shell_image.draw_rot(0, 0, 0, 0, 0, 0, 1, 1, @parent.player.color) if @shell_image
@overlay_image.draw(0, 0, 0) if @overlay_image
end
end
def draw
render unless @render
@angle += 0.1
@render.draw_rot(@parent.position.x + @center.x, @parent.position.y + @center.y, @parent.position.z, @angle)
end
end
end