Class: BehaviorDefinition

Inherits:
Object show all
Defined in:
lib/gamebox/core/behavior_definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#helpers_blockObject

Returns the value of attribute helpers_block.



2
3
4
# File 'lib/gamebox/core/behavior_definition.rb', line 2

def helpers_block
  @helpers_block
end

#react_to_blockObject

Returns the value of attribute react_to_block.



2
3
4
# File 'lib/gamebox/core/behavior_definition.rb', line 2

def react_to_block
  @react_to_block
end

#remove_blockObject

Returns the value of attribute remove_block.



2
3
4
# File 'lib/gamebox/core/behavior_definition.rb', line 2

def remove_block
  @remove_block
end

#required_behaviorsObject

Returns the value of attribute required_behaviors.



2
3
4
# File 'lib/gamebox/core/behavior_definition.rb', line 2

def required_behaviors
  @required_behaviors
end

#required_injectionsObject

Returns the value of attribute required_injections.



2
3
4
# File 'lib/gamebox/core/behavior_definition.rb', line 2

def required_injections
  @required_injections
end

#setup_blockObject

Returns the value of attribute setup_block.



2
3
4
# File 'lib/gamebox/core/behavior_definition.rb', line 2

def setup_block
  @setup_block
end

#sourceObject

Returns the value of attribute source.



2
3
4
# File 'lib/gamebox/core/behavior_definition.rb', line 2

def source
  @source
end

Instance Method Details

#helpers(&helpers_block) ⇒ Object

Define methods and include modules for use by your behavior.

helpers do
  include MyHelper
  def jump
    ...
  end
end


71
72
73
# File 'lib/gamebox/core/behavior_definition.rb', line 71

def helpers(&helpers_block)
  @helpers_block = helpers_block
end

#react_to(&react_to_block) ⇒ Object

Override the default react_to method of the behavior. You should probably not be doing this and should be calling #reacts_with in #setup.

react_to do |message_type, *opts, &blk|
  case message_type
  when :blow_up
    ..
end


59
60
61
# File 'lib/gamebox/core/behavior_definition.rb', line 59

def react_to(&react_to_block)
  @react_to_block = react_to_block
end

#remove(&remove_block) ⇒ Object

Remove callback that is called when the behavior is removed. The actor will still be available.

remove do
  input_manager.unsubscribe_all self
end


46
47
48
# File 'lib/gamebox/core/behavior_definition.rb', line 46

def remove(&remove_block)
  @remove_block = remove_block
end

#requires(*injections_needed) ⇒ Object

Sets the dependencies for this Behavior.

These will be pulled from the Actor’s object context at view construction time.

requires :input_manager, :timer_manager, :director


11
12
13
# File 'lib/gamebox/core/behavior_definition.rb', line 11

def requires(*injections_needed)
  @required_injections = injections_needed
end

#requires_behaviors(*behaviors_needed) ⇒ Object

Sets the behavior dependencies for this Behavior.

Any behaviors listed here that are not on the actor will be constructed and added without any opts.

requires_behaviors :positioned


21
22
23
# File 'lib/gamebox/core/behavior_definition.rb', line 21

def requires_behaviors(*behaviors_needed)
  @required_behaviors = behaviors_needed
end

#setup(&setup_block) ⇒ Object

Setup callback that is called when the behavior is added. The actor will be set before your setup block is executed, and the opts method will be available.

setup do
  actor.has_attributes speed: opts[:speed]
  input_manager.when :down, KbSpace do
    jump
  end
end


35
36
37
# File 'lib/gamebox/core/behavior_definition.rb', line 35

def setup(&setup_block)
  @setup_block = setup_block
end