Class: Gubby::GameEngine
Instance Attribute Summary collapse
-
#entities ⇒ Object
readonly
Returns the value of attribute entities.
-
#systems ⇒ Object
Returns the value of attribute systems.
Instance Method Summary collapse
- #[](*selectors) ⇒ Object
- #add_entity(e) ⇒ Object
-
#initialize(*systems) ⇒ GameEngine
constructor
A new instance of GameEngine.
- #refresh_entity(e) ⇒ Object
- #remove_entity(e) ⇒ Object
- #run(command, *args) ⇒ Object
Constructor Details
#initialize(*systems) ⇒ GameEngine
Returns a new instance of GameEngine.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/gubby/game_engine.rb', line 8 def initialize(*systems) @systems = [] @entities = [] systems.each do |s| @systems.push(s) end end |
Instance Attribute Details
#entities ⇒ Object (readonly)
Returns the value of attribute entities.
6 7 8 |
# File 'lib/gubby/game_engine.rb', line 6 def entities @entities end |
#systems ⇒ Object
Returns the value of attribute systems.
5 6 7 |
# File 'lib/gubby/game_engine.rb', line 5 def systems @systems end |
Instance Method Details
#[](*selectors) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/gubby/game_engine.rb', line 52 def [](*selectors) found = [] selectors.each do |s| found.concat(@entities.select{|e| e.name == s}) if s.is_a?(String) found.concat(@entities.select{|e| e.is_a?(s)}) if s.is_a?(Module) end return found end |
#add_entity(e) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/gubby/game_engine.rb', line 28 def add_entity(e) @entities.push(e) unless @entities.include?(e) e.engine = self @systems.each do |s| s.refresh_entity(e) end end |
#refresh_entity(e) ⇒ Object
36 37 38 39 40 |
# File 'lib/gubby/game_engine.rb', line 36 def refresh_entity(e) @systems.each do |s| s.refresh_entity(e) end end |
#remove_entity(e) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/gubby/game_engine.rb', line 42 def remove_entity(e) @entities.delete(e) @systems.each do |s| if s.entities.include?(e) then s.entities.delete(e) end end e.engine = nil end |
#run(command, *args) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/gubby/game_engine.rb', line 20 def run(command, *args) @systems.select{ |s| s.respond_to?(command) }.each do |s| s.send(command.to_s, *args) end end |