mirror of
https://github.com/cyberarm/i-mic-rts.git
synced 2025-12-16 00:02:33 +00:00
Added building component, refactored components to have setup, draw, update and tick methods, added fencing for building construction area parimeter, misc other changes.
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
class IMICRTS
|
||||
class BuildQueue < Component
|
||||
Item = Struct.new(:entity, :progress)
|
||||
def initialize(parent:)
|
||||
@parent = parent
|
||||
|
||||
def setup
|
||||
@queue = []
|
||||
end
|
||||
|
||||
|
||||
95
lib/components/building.rb
Normal file
95
lib/components/building.rb
Normal file
@@ -0,0 +1,95 @@
|
||||
class IMICRTS
|
||||
class Building < Component
|
||||
def setup
|
||||
data.construction_progress ||= 0
|
||||
data.construction_goal ||= 100
|
||||
|
||||
@text = CyberarmEngine::Text.new("", y: @parent.position.y, z: Float::INFINITY, size: 12)
|
||||
data.state = :construct # deconstruct, building, idle
|
||||
end
|
||||
|
||||
def draw
|
||||
case data.state
|
||||
when :construct
|
||||
draw_construction unless @text.text.empty?
|
||||
@text.text = "Building: #{(construction_progress * 100.0).round}%"
|
||||
@text.x = @parent.position.x - @text.width / 2
|
||||
@text.draw
|
||||
when :deconstruct
|
||||
when :building
|
||||
when :idle
|
||||
else
|
||||
raise "Unknown state!"
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
case data.state
|
||||
when :construct
|
||||
@parent.color.alpha = 255 * construction_progress
|
||||
data.state = :idle if construction_complete?
|
||||
end
|
||||
end
|
||||
|
||||
def construction_complete?
|
||||
data.construction_progress >= data.construction_goal
|
||||
end
|
||||
|
||||
# WARNING: returns a floating point number, not network safe!
|
||||
def construction_progress
|
||||
data.construction_progress.to_f / data.construction_goal
|
||||
end
|
||||
|
||||
def construction_work(work)
|
||||
raise TypeError, "Got a non integer value!" unless work.is_a?(Integer)
|
||||
|
||||
data.construction_progress += work
|
||||
data.construction_progress = data.construction_goal if construction_complete?
|
||||
end
|
||||
|
||||
def draw_construction
|
||||
@fencing ||= Gosu::Image.new(IMICRTS::ASSETS_PATH + "/fencing/fencing.png")
|
||||
@fencing_edge ||= Gosu::Image.new(IMICRTS::ASSETS_PATH + "/fencing/fencing_edge.png")
|
||||
|
||||
tiles = []
|
||||
each_tile(@parent.position / @parent.director.map.tile_size) do |tile, data, x, y|
|
||||
tiles << tile
|
||||
end
|
||||
|
||||
tiles.each do |tile|
|
||||
# X
|
||||
if tiles.find { |t| t.grid_position.x < tile.grid_position.x && t.grid_position.y == tile.grid_position.y} == nil
|
||||
@fencing_edge.draw(tile.position.x - 30, tile.position.y, Float::INFINITY)
|
||||
end
|
||||
if tiles.find { |t| t.grid_position.x > tile.grid_position.x && t.grid_position.y == tile.grid_position.y} == nil
|
||||
@fencing_edge.draw(tile.position.x, tile.position.y, Float::INFINITY)
|
||||
end
|
||||
# Y
|
||||
if tiles.find { |t| t.grid_position.x == tile.grid_position.x && t.grid_position.y < tile.grid_position.y} == nil
|
||||
@fencing.draw(tile.position.x, tile.position.y - 24, Float::INFINITY)
|
||||
end
|
||||
if tiles.find { |t| t.grid_position.x == tile.grid_position.x && t.grid_position.y > tile.grid_position.y} == nil
|
||||
@fencing.draw(tile.position.x, tile.position.y, Float::INFINITY)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def each_tile(vector, &block)
|
||||
if tile = @parent.director.map.tile_at(vector.x, vector.y)
|
||||
ent = Entity.get(@parent.name)
|
||||
origin = (tile.grid_position - 2)
|
||||
|
||||
ent.tiles.each_with_index do |array, y|
|
||||
array.each_with_index do |space_required, x|
|
||||
next unless space_required.is_a?(TrueClass)
|
||||
|
||||
other_tile = @parent.director.map.tile_at(origin.x + x, origin.y + y)
|
||||
if other_tile
|
||||
block.call(other_tile, space_required, origin.x + x, origin.y + y)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,8 +1,13 @@
|
||||
class IMICRTS
|
||||
class Movement < Component
|
||||
attr_accessor :pathfinder
|
||||
def initialize(parent:)
|
||||
@parent = parent
|
||||
|
||||
def update
|
||||
if pathfinder && pathfinder.path_current_node
|
||||
rotate_towards(pathfinder.path_current_node.tile.position + @parent.director.map.tile_size / 2)
|
||||
end
|
||||
|
||||
follow_path
|
||||
end
|
||||
|
||||
def rotate_towards(vector)
|
||||
|
||||
@@ -3,9 +3,7 @@ class IMICRTS
|
||||
Action = Struct.new(:label, :image, :description, :block)
|
||||
|
||||
attr_reader :actions
|
||||
def initialize(parent:)
|
||||
@parent = parent
|
||||
|
||||
def setup
|
||||
@actions = []
|
||||
end
|
||||
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
class IMICRTS
|
||||
class Turret < Component
|
||||
attr_accessor :angle, :center
|
||||
def initialize(parent:)
|
||||
@parent = parent
|
||||
|
||||
def setup
|
||||
@angle = 0
|
||||
@center = CyberarmEngine::Vector.new(0.5, 0.5)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user