Class: Nimo::Screen
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
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
-
.register_representation(representation_name, representation_class) ⇒ Object
Register a representation to be called as a method.
Instance Method Summary collapse
-
#add(representation_class, options) ⇒ Object
Add a representation to the screen.
-
#draw ⇒ Object
Draws all representations.
-
#initialize(id, game_window, resources) ⇒ Screen
constructor
A new instance of Screen.
-
#timer_for(seconds, &action) ⇒ Object
Defines an
action
to be executed after someseconds
. -
#update ⇒ Object
Updates all representations.
Methods included from InputListener
#any_key, #button_down, #process_inputs, #when_key
Methods included from EventListener
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
#id ⇒ Object (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 |, &blk| representation = add(representation_class, ) 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, ) params = .has_key?(:with) ? [:with] : {} game_object = .has_key?(:for) ? [:for] : Nimo::GameObject.new(params) representation = representation_class.new(@game_window, game_object) representation.load(@resources, params) @representations << representation representation end |
#draw ⇒ Object
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 |
#update ⇒ Object
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 |