Permalink
Browse files

behavior DSL working in basic actor spec

  • Loading branch information...
1 parent 63c3593 commit aaeee91f2de0d6a4afc0cfeeba63d40be9a0aa35 @shawn42 committed Apr 9, 2012
View
@@ -1,5 +1,7 @@
# Behavior is any type of behavior an actor can exibit.
class Behavior
+ construct_with :actor
+
attr_accessor :opts
def configure(opts={})
@@ -25,19 +27,44 @@ def required_behaviors
self.class.required_behaviors
end
- def self.required_behaviors
- @required_behaviors ||= []
- end
+ class << self
+
+ def required_behaviors
+ @required_behaviors ||= []
+ end
- def self.requires_behaviors(*args)
- @required_behaviors ||= []
- for a in args
- @required_behaviors << a
+ def requires_behaviors(*args)
+ @required_behaviors ||= []
+ for a in args
+ @required_behaviors << a
+ end
+ @behaviors
+ end
+
+ def requires_behavior(*args)
+ requires_behaviors(*args)
+ end
+
+ def define(actor_type)
+ @definitions ||= {}
+ definition = BehaviorDefinition.new
+ yield definition if block_given?
+ @definitions[actor_type] = definition
+ end
+
+ def definitions
+ @definitions ||= {}
end
- @behaviors
end
- def self.requires_behavior(*args)
- requires_behaviors(*args)
+ class BehaviorDefinition
+ attr_accessor :setup_block, :required_injections
+ def requires(*injections_needed)
+ @required_injections = injections_needed
+ end
+
+ def setup(&setup_block)
+ @setup_block = setup_block
+ end
end
end
@@ -9,16 +9,34 @@ class BehaviorFactory
# add_behavior(:shootable) or add_behavior(:shootable => {:range=>3})
# this will create a new instance of Shootable and pass
# :range=>3 to it
- def add_behavior(actor, behavior, opts = {})
+ def add_behavior(actor, behavior_name, opts = {})
raise "nil actor" if actor.nil?
- raise "nil behavior definition" if behavior.nil?
+ raise "nil behavior definition" if behavior_name.nil?
+
+ behavior_definition = Behavior.definitions[behavior_name]
+
+ raise "unknown behavior #{behavior_name}" unless behavior_definition
+ context = actor.this_object_context
+ context[:behavior].tap do |behavior|
+ reqs = behavior_definition.required_injections
+ if reqs
+ reqs.each do |req|
+ object = context[req]
+ # object.send "#{req}=", req
+ behavior.define_singleton_method req do
+ components[req]
+ end
+ components = behavior.send :components
+ components[req] = object
+ end
+ end
- actor.this_object_context[behavior].tap do |behavior|
deps = behavior.required_behaviors
deps.each do |beh|
add_behavior actor, beh unless @actor.is? beh
end
behavior.configure(opts)
+ behavior.instance_eval &behavior_definition.setup_block if behavior_definition.setup_block
end
end
@@ -1,10 +1,9 @@
-class GraphicalActorView < ActorView(....)
+class GraphicalActorView < ActorView
# # TODO ANNOYING to have to do this!!
- # construct_with *ActorView.object_definition.component_names
- # public *ActorView.object_definition.component_names
- construct_with :foo_thing
+ construct_with *ActorView.object_definition.component_names
+ public *ActorView.object_definition.component_names
def draw(target, x_off, y_off, z)
img = actor.do_or_do_not(:image)
@@ -19,7 +19,6 @@
actor.has_attribute :bullets, opts[:bullets]
director.when :update do |time|
actor.bullets -= time
- update(time)
end
end
end
@@ -33,23 +32,23 @@
end
end
- ActorView.define :mc_bane_view do |view|
- view.requires :resource_manager # needs these injected
- view.configure do
- @image = resource_manager.load_actor_image(actor)
- end
+ # ActorView.define :mc_bane_view do |view|
+ # view.requires :resource_manager # needs these injected
+ # view.configure do
+ # @image = resource_manager.load_actor_image(actor)
+ # end
- view.draw do |target, x_off, y_off, z|
- @image.draw(4,7)
- end
- end
+ # view.draw do |target, x_off, y_off, z|
+ # @image.draw(4,7)
+ # end
+ # end
# no code is allowed in the actor!
# all done through behaviors
Actor.define :mc_bane do |actor|
actor.has_behavior shooty: { bullets: 50 }
actor.has_behavior :death_on_d
- actor.has_behavior :graphical
+ # actor.has_behavior :graphical
# FEATURE REQUEST
# actor.has_view do |view|

0 comments on commit aaeee91

Please sign in to comment.