Class: Behavior

Inherits:
Object show all
Defined in:
lib/gamebox/core/behavior.rb,
lib/gamebox/core/deprecated.rb

Overview

Behavior is any type of behavior an actor can exibit.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optsObject

Returns the value of attribute opts.



5
6
7
# File 'lib/gamebox/core/behavior.rb', line 5

def opts
  @opts
end

Class Method Details

.define(behavior_type, &blk) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/gamebox/core/behavior.rb', line 55

def define(behavior_type, &blk)
  @definitions ||= {}
  definition = BehaviorDefinition.new
  definition.source = caller.detect{|c|!c.match /core/}
  definition.instance_eval &blk if block_given?
  @definitions[behavior_type] = definition
end

.definitionsObject



63
64
65
# File 'lib/gamebox/core/behavior.rb', line 63

def definitions
  @definitions ||= {}
end

.inherited(klass) ⇒ Object



7
8
9
# File 'lib/gamebox/core/deprecated.rb', line 7

def self.inherited(klass)
  log "Cannot extend #{self} anymore #{klass}"
end

Instance Method Details

#add_behavior(behavior_name, opts = {}) ⇒ Object

Builds and adds a behavior based on the name and options passed in.

actor.input.when :shields_up do
  add_behavior :invincible, duration: actor.shield_charge
end


39
40
41
# File 'lib/gamebox/core/behavior.rb', line 39

def add_behavior(behavior_name, opts = {})
  behavior_factory.add_behavior actor, behavior_name, opts
end

#react_to(message_type, *opts, &blk) ⇒ Object

Dispatches reactions to helper methods based on name. See BehaviorDefinition to see how to override this.



27
28
29
30
31
# File 'lib/gamebox/core/behavior.rb', line 27

def react_to(message_type, *opts, &blk)
  if @message_handlers && @message_handlers.include?(message_type)
    send message_type, *opts, &blk
  end
end

#reacts_with(*messages_with_methods) ⇒ Object

Specifies messages that your behavior is interested in and that you have defined methods for in your helpers block.

reacts_with :remove

helpers do
  def remove
    input_manager.unsubscribe_all self
  end
end


17
18
19
20
# File 'lib/gamebox/core/behavior.rb', line 17

def reacts_with(*messages_with_methods)
  @message_handlers ||= Set.new
  @message_handlers.merge(messages_with_methods)
end

#remove_behavior(behavior_name) ⇒ Object

Removes the behavior by name. Any added attributes will remain on the actor.

actor.input.when :shields_down do
  remove_behavior :invincible
end


49
50
51
# File 'lib/gamebox/core/behavior.rb', line 49

def remove_behavior(behavior_name)
  actor.remove_behavior(behavior_name)
end