Added last 2 units, added sidebar actions and build queue components, entities can now have their build actions put on the sidebar

This commit is contained in:
2019-11-21 14:04:33 -06:00
parent 902e48d53a
commit cec0c45932
12 changed files with 107 additions and 8 deletions

View File

@@ -7,7 +7,7 @@ class IMICRTS
end
def self.inherited(klass)
name = klass.to_s.split("::").last.downcase.to_sym
name = klass.to_s.split("::").last.gsub(/([^A-Z])([A-Z]+)/,'\1_\2').downcase.to_sym
if get(name)
raise "#{klass.inspect} is already defined!"

View File

@@ -0,0 +1,14 @@
class IMICRTS
class BuildQueue < Component
Item = Struct.new(:entity, :progress)
def initialize(parent:)
@parent = parent
@queue = []
end
def add(type)
@queue << Item.new(Entity.get(type), 0.0)
end
end
end

View File

@@ -0,0 +1,27 @@
class IMICRTS
class SidebarActions < Component
Action = Struct.new(:label, :image, :description, :block)
attr_reader :actions
def initialize(parent:)
@parent = parent
@actions = []
end
def add(action, *args)
case action
when :add_to_build_queue
action = Action.new
ent = IMICRTS::Entity.get(args.first)
action.label = ent.name.to_s
action.description = "Cost: #{ent.cost}\n#{ent.description}"
action.block = proc { @parent.component(:build_queue).add(args.first) }
@actions << action
else
raise "Unhandled sidebar action: #{action.inspect}"
end
end
end
end

View File

@@ -1,4 +1,6 @@
IMICRTS::Entity.define_entity(:barracks, :building, 400, "Builds and soldiers") do |entity|
entity.has(:build_queue)
entity.radius = 44
entity.max_health = 100.0

View File

@@ -1,4 +1,8 @@
IMICRTS::Entity.define_entity(:construction_yard, :building, 2_000, "Provides radar and builds construction workers") do |entity|
entity.has(:build_queue)
entity.has(:sidebar_actions)
entity.component(:sidebar_actions).add(:add_to_build_queue, :construction_worker)
entity.radius = 40
entity.max_health = 100.0

View File

@@ -1,4 +1,8 @@
IMICRTS::Entity.define_entity(:helipad, :building, 1_000, "Builds and rearms helicopters") do |entity|
entity.has(:build_queue)
entity.has(:sidebar_actions)
entity.component(:sidebar_actions).add(:add_to_build_queue, :helicopter)
entity.radius = 26
entity.max_health = 100.0

View File

@@ -1,4 +1,10 @@
IMICRTS::Entity.define_entity(:war_factory, :building, 2_000, "Generates credits") do |entity|
IMICRTS::Entity.define_entity(:war_factory, :building, 2_000, "Builds units") do |entity|
entity.has(:build_queue)
entity.has(:sidebar_actions)
entity.component(:sidebar_actions).add(:add_to_build_queue, :jeep)
entity.component(:sidebar_actions).add(:add_to_build_queue, :tank)
entity.component(:sidebar_actions).add(:add_to_build_queue, :harvester)
entity.radius = 48
entity.max_health = 100.0

View File

@@ -0,0 +1,14 @@
IMICRTS::Entity.define_entity(:helicopter, :unit, 400, "Attacks ground targets") do |entity|
entity.has(:movement)
entity.radius = 14
entity.movement = :air
entity.max_health = 100.0
entity.body_image = "vehicles/helicopter/helicopter.png"
entity.shell_image = "vehicles/helicopter/helicopter_shell.png"
entity.overlay_image = "vehicles/helicopter/helicopter_overlay.png"
entity.on_tick do
end
end

View File

@@ -0,0 +1,17 @@
IMICRTS::Entity.define_entity(:jeep, :unit, 400, "Attacks ground targets") do |entity|
entity.has(:movement)
entity.has(:turret)
entity.radius = 14
entity.movement = :ground
entity.max_health = 100.0
entity.body_image = "vehicles/jeep/jeep.png"
entity.shell_image = "vehicles/jeep/tank_shell.png"
entity.component(:turret).shell_image = "vehicles/jeep/jeep_turret_shell.png"
entity.component(:turret).center.y = 0.3125
entity.on_tick do
end
end

View File

@@ -67,7 +67,7 @@ class IMICRTS
if component
@components[symbol] = component.new(parent: self)
else
raise "Unknown component: #{component.inspect}"
raise "Unknown component: #{symbol.inspect}"
end
end

View File

@@ -69,6 +69,17 @@ class IMICRTS
@director.schedule_order(Order::SELECTED_UNITS, @player.id, @selected_entities)
else
pick_entity
if ent = @selected_entities.first
return unless ent.component(:sidebar_actions)
@game.sidebar_actions.clear do |stack|
ent.component(:sidebar_actions).actions.each do |action|
stack.button action.label, tip: action.description do
action.block.call if action.block
end
end
end
end
end
end
end

View File

@@ -2,7 +2,7 @@ class IMICRTS
class Game < CyberarmEngine::GuiState
Overlay = Struct.new(:image, :position, :angle, :alpha)
attr_reader :sidebar, :overlays
attr_reader :sidebar, :sidebar_actions, :overlays
def setup
window.show_cursor = true
@options[:networking_mode] ||= :host
@@ -21,8 +21,8 @@ class IMICRTS
label "SIDEBAR", text_size: 78, margin_bottom: 20
flow(width: 1.0) do
@buttons = stack(width: 0.9) do
@h = button("Harvester", width: 1.0) do
@sidebar_actions = stack(width: 0.9) do
button("Harvester", width: 1.0) do
@player.entities << Entity.new(
name: :harvester,
director: @director,
@@ -32,7 +32,7 @@ class IMICRTS
angle: rand(360)
)
end
@c = button("Construction Worker", width: 1.0) do
button("Construction Worker", width: 1.0) do
@player.entities << Entity.new(
name: :construction_worker,
director: @director,
@@ -42,7 +42,7 @@ class IMICRTS
angle: rand(360)
)
end
@t = button("Tank", width: 1.0) do
button("Tank", width: 1.0) do
@player.entities << Entity.new(
name: :tank,
director: @director,