Class: OrangeZest::Component
- Inherits:
-
Object
- Object
- OrangeZest::Component
- Defined in:
- lib/orange_zest/component.rb
Overview
An object which can exist in the game world, and responds to Gosu’s ‘#update` and `#draw` methods.
Instance Attribute Summary collapse
-
#group ⇒ Group?
readonly
The group which this component is registered with, if any.
Class Method Summary collapse
-
.anon(update: nil, draw: nil) ⇒ Component
A helper method to easily instantiate a new ‘Component` without creating a named subclass.
Instance Method Summary collapse
-
#draw ⇒ Object
Called during Gosu’s draw phase.
-
#register(group = nil) ⇒ Component
Adds this component to a group.
-
#unregister ⇒ Object
Removes this component from its group.
-
#update ⇒ Object
Called during Gosu’s update phase.
Instance Attribute Details
#group ⇒ Group? (readonly)
The group which this component is registered with, if any.
7 8 9 |
# File 'lib/orange_zest/component.rb', line 7 def group @group end |
Class Method Details
.anon(update: nil, draw: nil) ⇒ Component
A helper method to easily instantiate a new ‘Component` without creating a named subclass. Useful for components which will be instantiated exactly once and never need to be referred to again.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/orange_zest/component.rb', line 39 def self.anon(update: nil, draw: nil) _update = update || ->{} _draw = draw || ->{} Class.new(Component) do @@update = _update @@draw = _draw def update @@update.() end def draw @@draw.() end end.new end |
Instance Method Details
#draw ⇒ Object
Called during Gosu’s draw phase. A stub to be overridden.
13 |
# File 'lib/orange_zest/component.rb', line 13 def draw; end |
#register(group = nil) ⇒ Component
Adds this component to a group. If no group is given, uses the ‘Main` group. Returns `self` so you can chain this with an assignment.
19 20 21 22 23 24 |
# File 'lib/orange_zest/component.rb', line 19 def register(group=nil) group ||= Group::Main group.add(self) @group = group self end |
#unregister ⇒ Object
Removes this component from its group.
27 28 29 30 |
# File 'lib/orange_zest/component.rb', line 27 def unregister raise 'tried to unregister component which is not registered' unless group group.remove(self) end |
#update ⇒ Object
Called during Gosu’s update phase. A stub to be overridden.
10 |
# File 'lib/orange_zest/component.rb', line 10 def update; end |