Class: Nimo::Screen

Inherits:
Object show all
Extended by:
Forwardable
Includes:
EventListener, InputListener
Defined in:
lib/nimo/screen.rb

Overview

Represent a game’s screen, holding representations of domain objects for updates and draws. TODO: Add info on how to add ‘quad’, ‘image’ (whatever) methods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from InputListener

#any_key, #button_down, #process_inputs, #when_key

Methods included from EventListener

#listen_to, #notify

Constructor Details

#initialize(id, game_window, resources) ⇒ Screen

Returns a new instance of Screen.



17
18
19
20
21
22
23
# File 'lib/nimo/screen.rb', line 17

def initialize(id, game_window, resources)
  @id, @game_window, @resources = id, game_window, resources
  
  @representations = []
  @events = {}
			@timers = []
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/nimo/screen.rb', line 12

def id
  @id
end

Class Method Details

.register_representation(representation_name, representation_class) ⇒ Object

Register a representation to be called as a method. Example:

Screen.register_representation(:blah, Blah)
a_screen.blah :for => obj, :with => {:attr => 42} do
  # Initialising representation
end


32
33
34
35
36
37
# File 'lib/nimo/screen.rb', line 32

def self.register_representation(representation_name, representation_class)
  define_method representation_name do |options, &blk|
     representation = add(representation_class, options)
     representation.instance_eval &blk if blk
  end
end

Instance Method Details

#add(representation_class, options) ⇒ Object

Add a representation to the screen. The options params is used to specify a GameObject and params. It returns the constructed representation. Examples of usage:

screen.add(SomeRepresentation, :for => object, :with => { :attr => "something"}) # will construct SomeRepresentation with the supplied object and :with params
screen.add(SomeRepresentation, :with => { :attr => "something"}) # will construct SomeRepresentation with a vanilla Nimo::GameObject


45
46
47
48
49
50
51
52
53
54
# File 'lib/nimo/screen.rb', line 45

def add(representation_class, options)
  params = options.has_key?(:with) ? options[:with] : {}
  game_object = options.has_key?(:for) ? options[:for] : Nimo::GameObject.new(params)
  
  representation = representation_class.new(@game_window, game_object)
  representation.load(@resources, params)
  
  @representations << representation
  representation
end

#drawObject

Draws all representations.



74
75
76
# File 'lib/nimo/screen.rb', line 74

def draw
  @representations.each { |representation| representation.draw }
end

#timer_for(seconds, &action) ⇒ Object

Defines an action to be executed after some seconds.



58
59
60
# File 'lib/nimo/screen.rb', line 58

def timer_for(seconds, &action)
			@timers << Timer.new(Time.now.to_f, seconds, action)
end

#updateObject

Updates all representations.



66
67
68
69
70
# File 'lib/nimo/screen.rb', line 66

def update
			process_timers
			process_inputs(@game_window)
  @representations.each { |representation| representation.update }
end