Permalink
Browse files

basic actor spec works again

  • Loading branch information...
1 parent bf63ed9 commit 7baa8fbb8d17c5aea85e646a0761836d2e72de99 @shawn42 committed Mar 28, 2012
View
@@ -23,7 +23,7 @@ Gem::Specification.new do |s|
s.add_dependency "conject", ">= 0.0.3"
s.add_dependency "tween"
s.add_dependency "require_all"
- s.add_dependency "kvo"
+ s.add_dependency "kvo", ">= 0.0.2"
s.add_development_dependency "pry", '~>0.9.7'
s.add_development_dependency "pry-remote"
View
@@ -3,43 +3,67 @@
# They are created and hooked up to their optional View class in Stage#create_actor.
class Actor
include Kvo
+ can_fire_anything
+ # TODO show/hide methods? go in a behavior? base behavior ActorBehavior?
kvo_attr_accessor :alive
+ attr_accessor :actor_type
+
+ def initialize
+ @behaviors = []
+ end
+
+ def configure(opts={}) # :nodoc:
+ @opts = opts
+ @actor_type = @opts[:actor_type]
+ self.alive = true
+ end
+
+ def add_behavior(name, behavior)
+ # TODO do we need a name here?
+ @behaviors << behavior
+ end
def react_to(message, *opts)
- behaviors.each do |behavior|
+ @behaviors.each do |behavior|
behavior.react_to(message)
end
end
+ # Tells the actor's Director that he wants to be removed; and unsubscribes
+ # the actor from all input events.
+ def remove_self
+ self.alive = false
+ fire :remove_me
+ end
+
def to_s
- "#{self.class.name}:#{self.object_id} with behaviors\n#{self.behaviors.map(&:class).inspect}"
+ "#{self.class.name}:#{self.object_id} with behaviors\n#{@behaviors.map(&:class).inspect}"
end
+ # TODO should this live somewhere else?
+ # TODO should we support "inheritance" of components?
class << self
- def behaviors
- @behaviors ||= []
+
+ attr_accessor :definitions
+ def define(actor_type, &block)
+ @definitions ||= {}
+ definition = ActorDefinition.new
+ definition.instance_eval &block
+ @definitions[actor_type] = definition
end
- def has_behaviors(*args)
+ end
+
+ class ActorDefinition
+ attr_accessor :behaviors
+ def has_behaviors(*behaviors)
@behaviors ||= []
- for a in args
- if a.is_a? Hash
- for k,v in a
- h = {}
- h[k]=v
- @behaviors << h
- end
- else
- @behaviors << a
- end
+ behaviors.each do |beh|
+ @behaviors << beh
end
- @behaviors
- end
-
- def has_behavior(*args)
- has_behaviors *args
end
+ alias has_behavior has_behaviors
end
end
@@ -62,17 +86,6 @@ def initialize
@behaviors = {}
end
- def configure(opts={}) # :nodoc:
- @opts = DEFAULT_PARAMS.merge opts
- self.x = @opts[:x]
- self.y = @opts[:y]
-
- @actor_type = @opts[:actor_type]
- self.alive = true
-
- setup
- end
-
# Called at the end of actor/behavior initialization. To be defined by the
# child class.
def setup
@@ -13,38 +13,19 @@ def build(actor, stage, opts={})
model = nil
this_object_context.in_subcontext do |actor_context|
begin
- model = actor_context[actor]
+ model = actor_context[:actor]
- # ============= STOLEN FROM ACTOR =====================
- # add our classes behaviors and parents behaviors
- behavior_defs = {}
- ordered_behaviors = []
+ Actor.definitions[actor].behaviors.each do |behavior|
+ beh_opts = {}
+ beh_key = behavior
- actor_klasses = class_ancestors(model)
- actor_klasses.reverse.each do |actor_klass|
- actor_behaviors = actor_klass.behaviors.dup
- actor_behaviors.each do |behavior|
-
- behavior_sym = behavior
- behavior_opts = {}
- if behavior.is_a?(Hash)
- behavior_sym = behavior.keys.first
- behavior_opts = behavior[behavior_sym]
- end
-
- # TODO does an actor really need to know its list of behaviors?... probably..
- # TODO clean up behaviors on is_no_longer
- ordered_behaviors << behavior_sym unless ordered_behaviors.include? behavior_sym
- behavior_defs[behavior_sym] = behavior_opts
+ if behavior.is_a?(Hash)
+ beh_opts = behavior.values.first
+ beh_key = behavior.keys.first
end
- end
- ordered_behaviors.each do |behavior|
- # TODO make this use the actor_context
- beh_opts = behavior_defs[behavior] || {}
- model.behaviors[behavior] = behavior_factory.add_behavior model, behavior, beh_opts
+ model.add_behavior beh_key, behavior_factory.add_behavior(model, beh_key, beh_opts)
end
- # ============= STOLEN FROM ACTOR =====================
model.configure(merged_opts)
@@ -64,10 +45,15 @@ def build(actor, stage, opts={})
log "could not find view class for #{actor} with key #{view_klass}"
end
- model.show unless opts[:hide]
+ # TODO figure this out too
+ # model.show unless opts[:hide]
rescue Exception => e
# binding.pry
- raise "#{actor} not found: #{e.inspect}"
+ raise """
+ #{actor} not found:
+ #{e.inspect}
+ #{e.backtrace[0..6].join("\n")}
+ """
end
end
model
@@ -1,3 +1,4 @@
+__END__
require 'publisher'
@@ -1,3 +1,4 @@
+__END__
class FpsView < LabelView; end
class Fps < Label
@@ -1,3 +1,4 @@
+__END__
class LabelView < ActorView
def draw(target,x_off,y_off,z)
@converted_color ||= target.convert_color(actor.color)
@@ -1,3 +1,4 @@
+__END__
class Logo < Actor
has_behaviors :graphical,
@@ -1,3 +1,5 @@
+__END__
+
class ScoreView < ActorView
def draw(target,x_off,y_off,z)
text = @actor.score.to_s
@@ -15,30 +15,31 @@
let(:gosu) { MockGosuWindow.new }
class Shooty < Behavior
+ construct_with :wrapped_screen
attr_accessor :bullets
def setup
relegates :bullets
@bullets = opts[:bullets]
- end
+ wrapped_screen.screen.when :update do |time|
+ @bullets -= time
+ end
- def update(time)
- @bullets -= time
end
end
+
class DeathOnD < Behavior
+ construct_with :input_manager
def setup
- @actor.input_manager.reg :up, KbD do
+ input_manager.reg :up, KbD do
@actor.remove_self
end
end
end
- class McBane < Actor
- construct_with *Actor.object_definition.component_names
- public *Actor.object_definition.component_names
-
- has_behavior :updatable
- has_behavior shooty: { bullets: 50 }
+ # no code is allowed in the actor!
+ # all done through behaviors
+ Actor.define :mc_bane do
+ has_behavior shooty: { bullets: 50 }
has_behavior :death_on_d
end
@@ -151,7 +152,7 @@ def see_actor_attrs(actor_type, attrs)
end
def update(time)
- game.update time
+ gosu.update time
end
def send_up(button_id)
View
@@ -1,29 +1,18 @@
require File.join(File.dirname(__FILE__),'helper')
describe Actor do
- inject_mocks :stage, :input_manager, :director, :resource_manager, :wrapped_screen,
- :backstage, :sound_manager
before do
subject.configure actor_type: :actor
end
it 'should be alive' do
- subject.alive?.should be_true
+ subject.alive.should be_true
end
it 'should be the correct type' do
subject.actor_type.should == :actor
end
- it 'should be at (0,0)' do
- subject.x.should equal(0)
- subject.y.should equal(0)
- end
-
- it 'should have atts set' do
- subject.behaviors.size.should equal(0)
- end
-
it 'should fire anything' do
Proc.new {
subject.when :foofoo_bar do
@@ -32,32 +21,42 @@
}.should_not raise_error
end
- it 'should inherit parents behaviors' do
- @shawn = create_actor :shawn
- @shawn.is?(:smart).should be_true
- end
+ # it 'should inherit parents behaviors' do
+ # @shawn = create_actor :shawn
+ # @shawn.is?(:smart).should be_true
+ # end
+
+ # it 'should be able to override parents behaviors' do
+ # @james = create_actor :james_kilton
+ # @james.is?(:smart).should be_true
+ # @james.instance_variable_get('@behaviors')[:smart].instance_variable_get('@opts').should == {:really=>true}
+ # end
- it 'should be able to override parents behaviors' do
- @james = create_actor :james_kilton
- @james.is?(:smart).should be_true
- @james.instance_variable_get('@behaviors')[:smart].instance_variable_get('@opts').should == {:really=>true}
+ describe "#add_behavior" do
+ it 'can add a behavior to the actors list of behaviors'
end
- describe '#viewport' do
- it 'should return the stages viewport' do
- @stage.stubs(:viewport).returns(:da_viewport)
- subject.viewport.should == :da_viewport
+ describe ".define" do
+ it 'adds an actor definition' do
+ Actor.define :mc_bane do
+ has_behavior shooty: { bullets: 50 }
+ has_behavior :death_on_d
+ end
+
+ definition = Actor.definitions[:mc_bane]
+ definition.should be
+ definition.behaviors.should == [{shooty: {bullets:50}}, :death_on_d]
end
end
end
-
-class Cool < Behavior; end
-class Smart < Behavior; end
-class Coder < Actor
- has_behavior :smart, :cool
-end
-class Shawn < Coder; end
-class JamesKilton < Coder
- has_behavior :smart => {:really => true}
-end
+#
+# class Cool < Behavior; end
+# class Smart < Behavior; end
+# class Coder < Actor
+# has_behavior :smart, :cool
+# end
+# class Shawn < Coder; end
+# class JamesKilton < Coder
+# has_behavior :smart => {:really => true}
+# end

0 comments on commit 7baa8fb

Please sign in to comment.