Class: Nimo::GameObject

Inherits:
Object show all
Defined in:
lib/nimo/game_object.rb

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

Instance Method Summary collapse

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(config_options = {})
  configure_with({:x => 0, :y => 0, :width => 0, :height => 0}.merge(config_options))
  @listeners = {}
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



11
12
13
# File 'lib/nimo/game_object.rb', line 11

def height
  @height
end

#widthObject

Returns the value of attribute width.



11
12
13
# File 'lib/nimo/game_object.rb', line 11

def width
  @width
end

#xObject

Returns the value of attribute x.



11
12
13
# File 'lib/nimo/game_object.rb', line 11

def x
  @x
end

#yObject

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

#centerObject



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

Returns:

  • (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(config_options)
  config_options.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