Class: Nimo::GameObject
Overview
Base game domain object containing position, dimension and state information. Any object that represents a game entity should extend this class.
The position (:x and :y) and dimension (:width and :height) are used for visual representations and any useful game behavior.
Instance Attribute Summary collapse
-
#height ⇒ Object
Returns the value of attribute height.
-
#width ⇒ Object
Returns the value of attribute width.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #at(x, y) ⇒ Object
- #center ⇒ Object
- #collide?(obj) ⇒ Boolean
-
#configure_with(config_options) ⇒ Object
config_options is a hash that can take the following keys: :x, :y, :width, :height.
- #dimension(width, height) ⇒ Object
-
#initialize(config_options = {}) ⇒ GameObject
constructor
A new instance of GameObject.
- #intersection(obj) ⇒ Object
- #notify(event_type) ⇒ Object
- #register_listener(event_type, listener) ⇒ Object
Constructor Details
#initialize(config_options = {}) ⇒ GameObject
Returns a new instance of GameObject.
13 14 15 16 |
# File 'lib/nimo/game_object.rb', line 13 def initialize( = {}) configure_with({:x => 0, :y => 0, :width => 0, :height => 0}.merge()) @listeners = {} end |
Instance Attribute Details
#height ⇒ Object
Returns the value of attribute height.
11 12 13 |
# File 'lib/nimo/game_object.rb', line 11 def height @height end |
#width ⇒ Object
Returns the value of attribute width.
11 12 13 |
# File 'lib/nimo/game_object.rb', line 11 def width @width end |
#x ⇒ Object
Returns the value of attribute x.
11 12 13 |
# File 'lib/nimo/game_object.rb', line 11 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
11 12 13 |
# File 'lib/nimo/game_object.rb', line 11 def y @y end |
Instance Method Details
#at(x, y) ⇒ Object
18 19 20 21 |
# File 'lib/nimo/game_object.rb', line 18 def at(x, y) @x = x @y = y end |
#center ⇒ Object
43 44 45 |
# File 'lib/nimo/game_object.rb', line 43 def center Object.from_hash(:x => @x + (@width/2), :y => @y + (@height/2)) end |
#collide?(obj) ⇒ Boolean
34 35 36 37 |
# File 'lib/nimo/game_object.rb', line 34 def collide?(obj) !(obj.x > (@x + @width) || @x > (obj.x + obj.width) || obj.y > (@y + @height) || @y > (obj.y + obj.height)) end |
#configure_with(config_options) ⇒ Object
config_options is a hash that can take the following keys: :x, :y, :width, :height. The key restriction is not being enforced.
30 31 32 |
# File 'lib/nimo/game_object.rb', line 30 def configure_with() .each { |attribute, value| instance_variable_set("@#{attribute}", value) } end |
#dimension(width, height) ⇒ Object
23 24 25 26 |
# File 'lib/nimo/game_object.rb', line 23 def dimension(width, height) @width = width @height = height end |
#intersection(obj) ⇒ Object
39 40 41 |
# File 'lib/nimo/game_object.rb', line 39 def intersection(obj) collide?(obj) ? Intersection.between(self, obj) : nil end |
#notify(event_type) ⇒ Object
52 53 54 |
# File 'lib/nimo/game_object.rb', line 52 def notify(event_type) @listeners[event_type].each { |listener| listener.notify(event_type) } if @listeners.has_key? event_type end |
#register_listener(event_type, listener) ⇒ Object
47 48 49 50 |
# File 'lib/nimo/game_object.rb', line 47 def register_listener(event_type, listener) @listeners[event_type] ||= [] @listeners[event_type] << listener end |