Class: OrangeZest::Component

Inherits:
Object
  • Object
show all
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.

Direct Known Subclasses

Entity, Group

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#groupGroup? (readonly)

The group which this component is registered with, if any.

Returns:



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.

Parameters:

  • update (#call, nil) (defaults to: nil)
  • draw (#call, nil) (defaults to: nil)

Returns:



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

#drawObject

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.

Parameters:

  • group (Group, nil) (defaults to: nil)

Returns:



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

#unregisterObject

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

#updateObject

Called during Gosu’s update phase. A stub to be overridden.



10
# File 'lib/orange_zest/component.rb', line 10

def update; end