Class: Actor

Inherits:
Object show all
Extended by:
Publisher
Includes:
Gamebox::Extensions::Object::Yoda, ObservableAttributes
Defined in:
lib/gamebox/core/actor.rb,
lib/gamebox/core/deprecated.rb

Overview

Actor represent a game object. Actors can have behaviors added and removed from them. Such as :physical or :animated. They are created and hooked up to their optional View class in Stage#create_actor.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gamebox::Extensions::Object::Yoda

#do_or_do_not, #you_must

Methods included from ObservableAttributes

#attributes, #has_attribute, #has_attribute?, #has_attributes, included, #update_attributes

Constructor Details

#initializeActor

Returns a new instance of Actor.



13
14
15
16
# File 'lib/gamebox/core/actor.rb', line 13

def initialize
  has_attribute :alive, true
  @behaviors = {}
end

Instance Attribute Details

#actor_typeObject

Returns the value of attribute actor_type.



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

def actor_type
  @actor_type
end

Class Method Details

.define(actor_type, opts = {}, &blk) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/gamebox/core/actor.rb', line 92

def define(actor_type, opts={}, &blk)
  @definitions ||= {}
  raise "Actor [#{actor_type}] already defined at #{@definitions[actor_type].source}" if @definitions[actor_type]

  definition = ActorDefinition.new
  # TODO evaluate the perf of doing this
  definition.source = caller.detect{|c|!c.match /core/}
  definition.instance_eval &blk if block_given?

  view_blk = definition.view_blk
  if view_blk
    ActorView.define "#{actor_type}_view".to_sym, &view_blk
  end

  behavior_blk = definition.behavior_blk
  if behavior_blk
    Behavior.define actor_type, &behavior_blk
    definition.has_behavior actor_type
  end

  @definitions[actor_type] = definition
end

.definitionsObject



115
116
117
# File 'lib/gamebox/core/actor.rb', line 115

def definitions
  @definitions ||= {}
end

.inherited(klass) ⇒ Object



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

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

Instance Method Details

#add_behavior(name, behavior) ⇒ Object

Adds the behavior object to the Actor. You should never use this method in your game.



25
26
27
# File 'lib/gamebox/core/actor.rb', line 25

def add_behavior(name, behavior)
  @behaviors[name] = behavior
end

#configure(opts = {}) ⇒ Object

:nodoc:



18
19
20
21
# File 'lib/gamebox/core/actor.rb', line 18

def configure(opts={}) # :nodoc:
  self.actor_type = opts.delete(:actor_type)
  has_attributes opts
end

#controllerObject



73
74
75
76
# File 'lib/gamebox/core/actor.rb', line 73

def controller
  # TODO conject should have a lazily loaded dependency mechanism
  @input_mapper ||= this_object_context[:input_mapper]
end

#emit(event, *args) ⇒ Object



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

def emit(event, *args)
  fire event, *args
end

#has_behavior?(name) ⇒ Boolean

Returns true if the Actor has the named behavior.

This is mostly used internally by Gamebox. You should favor not knowing behaviors if possible and instead look at the Actor’s attributes via #do_or_do_not

Returns:

  • (Boolean)


43
44
45
# File 'lib/gamebox/core/actor.rb', line 43

def has_behavior?(name)
  @behaviors[name]
end

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

Propogates the reaction to all behaviors of the Actor. Any behavior can react to these messages.



51
52
53
54
55
56
57
# File 'lib/gamebox/core/actor.rb', line 51

def react_to(message, *opts, &blk)
  # TODO cache the values array?
  @behaviors.values.each do |behavior|
    behavior.react_to(message, *opts, &blk)
  end
  nil
end

#removeObject

Sets the Actor to no longer being alive.

Sends a :remove reaction to all the Actor’s behaviors. Emits a :remove_me event.



67
68
69
70
71
# File 'lib/gamebox/core/actor.rb', line 67

def remove
  self.alive = false
  react_to :remove
  emit :remove_me
end

#remove_behavior(name) ⇒ Object

Removes the behavior object from the Actor. Sends :remove reaction to the removed behavior. You should never use this method in your game.



32
33
34
35
36
# File 'lib/gamebox/core/actor.rb', line 32

def remove_behavior(name)
  @behaviors.delete(name).tap do |behavior|
    behavior.react_to :remove if behavior
  end
end

#to_sObject Also known as: pretty_inspect



78
79
80
81
82
83
84
85
# File 'lib/gamebox/core/actor.rb', line 78

def to_s
  """
  #{actor_type}:#{self.object_id}
  Behaviors:
  #{@behaviors.keys.sort}
  Attributes:
  #{attributes.map{|name, val| "#{name}: #{val}"}.join("\n")} """
end