Class: Nimo::ObjectRepresentation

Inherits:
Object
  • Object
show all
Includes:
EventListener, InputListener
Defined in:
lib/nimo/representations/object_representation.rb

Overview

Nimo::GameObject’s view. It holds actions to be executed on every update or when a key is pressed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from InputListener

#any_key, #button_down, #process_inputs, #when_key

Methods included from EventListener

#notify

Constructor Details

#initialize(game_window, game_object) ⇒ ObjectRepresentation

Returns a new instance of ObjectRepresentation.



11
12
13
14
# File 'lib/nimo/representations/object_representation.rb', line 11

def initialize(game_window, game_object)
  @game_window, @game_object = game_window, game_object
  @always_actions = []
end

Instance Attribute Details

#game_objectObject (readonly)

Returns the value of attribute game_object.



8
9
10
# File 'lib/nimo/representations/object_representation.rb', line 8

def game_object
  @game_object
end

#game_windowObject

Returns the value of attribute game_window.



9
10
11
# File 'lib/nimo/representations/object_representation.rb', line 9

def game_window
  @game_window
end

Instance Method Details

#act_uponObject



53
54
55
# File 'lib/nimo/representations/object_representation.rb', line 53

def act_upon
  @game_object
end

#always(&action) ⇒ Object

Register and action that always execute on a game update.



22
23
24
25
# File 'lib/nimo/representations/object_representation.rb', line 22

def always(&action)
  @always_actions << action
  self
end

#drawObject

Should be overriden by childs.



49
50
51
# File 'lib/nimo/representations/object_representation.rb', line 49

def draw
  raise "Draw should be overriden by representation"
end

#listen_to(event_type, &action) ⇒ Object

Register an action that will execute when the game object sends a notification. Overrides EventListener to add default registration to game_object.



30
31
32
33
# File 'lib/nimo/representations/object_representation.rb', line 30

def listen_to(event_type, &action)
  super
  game_object.register_listener(event_type, self)
end

#load(resources, params) ⇒ Object

Hook to load data (i.e. images and fonts) from the game window.



17
18
19
# File 'lib/nimo/representations/object_representation.rb', line 17

def load(resources, params)
  raise "Load should be overriden by representation"
end

#updateObject



42
43
44
45
46
# File 'lib/nimo/representations/object_representation.rb', line 42

def update
  @always_actions.each { |action| @game_object.instance_eval(&action) }
  process_inputs(@game_window)
			@observer.call(self, game_object) unless @observer.nil?
end

#with_observer(&observer) ⇒ Object

Register an observer to be invoked every update, after all actions runned. This could be useful when a more complex behavior is required from the representation, and there is a need to inspect the game object to change some state.



38
39
40
# File 'lib/nimo/representations/object_representation.rb', line 38

def with_observer(&observer)
	@observer = observer
end