mirror of
https://github.com/cyberarm/i-mic-rts.git
synced 2025-12-13 14:52:35 +00:00
Added FriendlyHash for orders, refactored Order#execute arguments
This commit is contained in:
@@ -24,6 +24,7 @@ require_relative "lib/entity"
|
|||||||
# require_relative "lib/entities/"
|
# require_relative "lib/entities/"
|
||||||
|
|
||||||
require_relative "lib/order"
|
require_relative "lib/order"
|
||||||
|
require_relative "lib/friendly_hash"
|
||||||
require_relative "lib/director"
|
require_relative "lib/director"
|
||||||
require_relative "lib/player"
|
require_relative "lib/player"
|
||||||
require_relative "lib/connection"
|
require_relative "lib/connection"
|
||||||
|
|||||||
@@ -32,9 +32,12 @@ class IMICRTS
|
|||||||
@players.find { |player| player.id == id }
|
@players.find { |player| player.id == id }
|
||||||
end
|
end
|
||||||
|
|
||||||
def issue_order(player_id, order_id, *args)
|
def issue_order(order_id, *args)
|
||||||
# pp Order.order_name(order_id)
|
if order = Order.get(order_id)
|
||||||
# pp args
|
order.execute(self, *args)
|
||||||
|
else
|
||||||
|
raise "Undefined order: #{Order.order_name(order_id)}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
23
lib/friendly_hash.rb
Normal file
23
lib/friendly_hash.rb
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
class IMICRTS
|
||||||
|
class FriendlyHash
|
||||||
|
def initialize
|
||||||
|
@hash = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
def [](key)
|
||||||
|
@hash[key.to_sym]
|
||||||
|
end
|
||||||
|
|
||||||
|
def []=(key, value)
|
||||||
|
@hash[key.to_sym] = value
|
||||||
|
end
|
||||||
|
|
||||||
|
def method_missing(method)
|
||||||
|
if value = @hash.dig(method)
|
||||||
|
value
|
||||||
|
else
|
||||||
|
raise "Unknown value for: #{method}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
30
lib/order.rb
30
lib/order.rb
@@ -6,18 +6,42 @@ class IMICRTS
|
|||||||
IMICRTS::Order.constants(false).find { |const| IMICRTS::Order.const_get(const) == order_id }
|
IMICRTS::Order.constants(false).find { |const| IMICRTS::Order.const_get(const) == order_id }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.get(order_id)
|
||||||
|
@@orders.dig(order_id)
|
||||||
|
end
|
||||||
|
|
||||||
def self.define_handler(order_id, arguments: [], &handler)
|
def self.define_handler(order_id, arguments: [], &handler)
|
||||||
raise "Handler from #{order_name(order_id)} already defined!" if @@orders.dig(order_id)
|
raise "Handler from #{order_name(order_id)} already defined!" if @@orders.dig(order_id)
|
||||||
|
|
||||||
@@orders[order_id] = IMICRTS::Order.new(id: order_id, arguments: arguments, &handler)
|
@@orders[order_id] = IMICRTS::Order.new(id: order_id, arguments: arguments, &handler)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
attr_reader :id
|
||||||
def initialize(id:, arguments:, &handler)
|
def initialize(id:, arguments:, &handler)
|
||||||
|
@id = id
|
||||||
|
@arguments = arguments
|
||||||
|
@handler = handler
|
||||||
end
|
end
|
||||||
|
|
||||||
def execute
|
def execute(director, *arguments)
|
||||||
@handler.call(self, Director.instance)
|
@handler.call(arguments(arguments), director)
|
||||||
|
end
|
||||||
|
|
||||||
|
def arguments(args)
|
||||||
|
raise "Did not receive correct number of arguments: got #{args.size} expected #{@arguments.size}." unless @arguments.size == args.size
|
||||||
|
|
||||||
|
hash = FriendlyHash.new
|
||||||
|
@arguments.each_with_index do |key, value|
|
||||||
|
hash[key] = args[value]
|
||||||
|
end
|
||||||
|
|
||||||
|
return hash
|
||||||
|
end
|
||||||
|
|
||||||
|
def serialize
|
||||||
|
end
|
||||||
|
|
||||||
|
def deserialize
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
5
lib/orders/selected_units.rb
Normal file
5
lib/orders/selected_units.rb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
IMICRTS::Order.define_handler(IMICRTS::Order::SELECTED_UNITS, arguments: [:player_id, :ids]) do |order, director|
|
||||||
|
director.player(order.player_id).selected_entities.clear
|
||||||
|
director.player(order.player_id).selected_entities.push(order.ids)
|
||||||
|
end
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@ class IMICRTS
|
|||||||
|
|
||||||
@selected_entities = []
|
@selected_entities = []
|
||||||
|
|
||||||
@mouse_pos = CyberarmEngine::Text.new("X: 0\nY: 0", x: 500, y: 10, z: Float::INFINITY)
|
@debug_info = CyberarmEngine::Text.new("X: 0\nY: 0", x: 500, y: 10, z: Float::INFINITY)
|
||||||
|
|
||||||
@sidebar = stack(height: 1.0) do
|
@sidebar = stack(height: 1.0) do
|
||||||
background [0x55555555, 0x55666666]
|
background [0x55555555, 0x55666666]
|
||||||
@@ -66,7 +66,7 @@ class IMICRTS
|
|||||||
Gosu.draw_rect(@box.min.x, @box.min.y, @box.width, @box.height, Gosu::Color.rgba(50, 50, 50, 150), Float::INFINITY) if @box
|
Gosu.draw_rect(@box.min.x, @box.min.y, @box.width, @box.height, Gosu::Color.rgba(50, 50, 50, 150), Float::INFINITY) if @box
|
||||||
end
|
end
|
||||||
|
|
||||||
@mouse_pos.draw
|
@debug_info.draw
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
@@ -84,7 +84,19 @@ class IMICRTS
|
|||||||
end
|
end
|
||||||
|
|
||||||
mouse = @player.camera.mouse_pick(window.mouse_x, window.mouse_y)
|
mouse = @player.camera.mouse_pick(window.mouse_x, window.mouse_y)
|
||||||
@mouse_pos.text = "Aspect Ratio: #{@player.camera.aspect_ratio}\nZoom: #{@player.camera.zoom}\nX: #{window.mouse_x}\nY: #{window.mouse_y}\n\nX: #{mouse.x}\nY: #{mouse.y}"
|
@debug_info.text = %(
|
||||||
|
Aspect Ratio: #{@player.camera.aspect_ratio}
|
||||||
|
Zoom: #{@player.camera.zoom}
|
||||||
|
Window Mouse X: #{window.mouse_x}
|
||||||
|
Window Mouse Y: #{window.mouse_y}
|
||||||
|
|
||||||
|
World Mouse X: #{mouse.x}
|
||||||
|
World Mouse Y: #{mouse.y}
|
||||||
|
|
||||||
|
Director Tick: #{@director.current_tick}
|
||||||
|
).lines.map { |line| line.strip }.join("\n")
|
||||||
|
|
||||||
|
@debug_info.x = @sidebar.width + 20
|
||||||
end
|
end
|
||||||
|
|
||||||
def button_down(id)
|
def button_down(id)
|
||||||
@@ -111,7 +123,7 @@ class IMICRTS
|
|||||||
@box = nil
|
@box = nil
|
||||||
@selection_start = nil
|
@selection_start = nil
|
||||||
|
|
||||||
@director.issue_order(@player.id, Order::SELECTED_UNITS, @selected_entities)
|
@director.issue_order(Order::SELECTED_UNITS, @player.id, @selected_entities)
|
||||||
end
|
end
|
||||||
|
|
||||||
@player.camera.button_up(id)
|
@player.camera.button_up(id)
|
||||||
|
|||||||
Reference in New Issue
Block a user