Class: Slickr::Reactor

Inherits:
Object
  • Object
show all
Defined in:
lib/slickr/reactor.rb

Overview

Reactors take info about the game world and tell relevant entities to do something. Emphasis is on the word “tell”.

Concerns

Reactors are always concerned with entities who implement one or more specific behaviors. For instance, an # InputReactor would care about entities who implement the Controllable behavior.

Using Ractors

Reactors are middleware of your game’s Engine. They are called during every update tick of your game. They are passed the game’s container and the delta of time that has passed since the last tick.

if you need to do some special setup in your Reactors, do it in the initializer like any normal ruby object.

Reacting

Reactors simply orcestrate. They take that delta and tell the entities they care about to do something. Following the above example, here’s a simple Reactor that moves entities around the world.

Examples:


class InputReactor < Slickr::Reactors
  concerned_with :controllable
end

class Engine < BasicGame
  use InputReactor
end

class Engine < BasicGame
  use InputReactor.new(:ignore => :up)
end

class InputReactor < Slickr::Reactor
  concerned_with :controllable

  def call(container, delta)
    entities.each do |entity|
      entity.up(delta)    if input.is_key_down?(Input::KEY_UP)
      entity.down(delta)  if input.is_key_down?(Input::KEY_DOWN)
      entity.right(delta) if input.is_key_down?(Input::KEY_RIGHT)
      entity.left(delta)  if input.is_key_down?(Input::KEY_LEFT)
    end
  end
end

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.concernsObject

Returns the value of attribute concerns.



64
65
66
# File 'lib/slickr/reactor.rb', line 64

def concerns
  @concerns
end

Class Method Details

.concerned_with(*behaviors) ⇒ Object



67
68
69
70
# File 'lib/slickr/reactor.rb', line 67

def self.concerned_with(*behaviors)
  @concerns ||= []
  @concerns += behaviors
end

Instance Method Details

#call(*args) ⇒ Object



72
73
74
# File 'lib/slickr/reactor.rb', line 72

def call(*args)
  raise NotImplementedError
end

#entitiesObject



76
77
78
# File 'lib/slickr/reactor.rb', line 76

def entities
  EntityManager.entities_with(*concerns)
end