Permalink
Browse files

dreaming up DSL for views and behaviors

  • Loading branch information...
shawn42 committed Apr 7, 2012
1 parent de27c57 commit 63c359305526adbd717406b5f1a92c0fc673f5ab
View
@@ -1,5 +1,6 @@
TODO:
- double check actor view mechanism
+ - pull drawing code out of stage?
- update all gamebox actors
- update all behaviors
- solidify configuration and config_manager
View
@@ -1,20 +1,25 @@
class ActorView
- construct_with :stage, :wrapped_screen, :resource_manager
- public :stage, :wrapped_screen, :resource_manager
+ construct_with :stage, :wrapped_screen, :resource_manager, :actor
+ public :stage, :wrapped_screen, :resource_manager, :actor
- attr_accessor :actor, :layer, :parallax
- def configure(actor)
- @actor = actor
-
- @layer = @actor.do_or_do_not(:layer) || 0
- @parallax = @actor.do_or_do_not(:parallax) || 1
+ attr_accessor :layer, :parallax
+ def configure
+ @layer = actor.do_or_do_not(:layer) || 0
+ @parallax = actor.do_or_do_not(:parallax) || 1
actor.when :remove_me do unregister end
actor.when :hide_me do unregister end
actor.when :show_me do register end
setup
end
+
+ def self.inherited(klass)
+ klass.instance_eval do
+ construct_with *ActorView.object_definition.component_names
+ public *ActorView.object_definition.component_names
+ end
+ end
def register
stage.register_drawable self
@@ -1,8 +1,3 @@
-
-
-
-
-
# available collidable_shapes are :circle, :polygon, :aabb
class Collidable < Behavior
View
@@ -23,6 +23,7 @@ def do_or_do_not(name, *args)
return nil unless name
respond_to?(name) ? send(name, *args) : nil
end
+ alias yoda do_or_do_not
# You must unlearn what you have learned. -- Yoda
#
@@ -1,8 +1,13 @@
-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
+
def draw(target, x_off, y_off, z)
- img = actor.image
+ img = actor.do_or_do_not(:image)
return if img.nil?
x = actor.x
@@ -13,32 +13,56 @@
end
let(:gosu) { MockGosuWindow.new }
- class Shooty < Behavior
- construct_with :actor, :director
- def setup
+ Behavior.define :shooty do |beh|
+ beh.requires :director
+ beh.setup do
actor.has_attribute :bullets, opts[:bullets]
director.when :update do |time|
actor.bullets -= time
+ update(time)
end
end
end
- class DeathOnD < Behavior
- construct_with :actor, :input_manager
- def setup
+ Behavior.define :death_on_d do |beh|
+ beh.requires :input_manager
+ beh.setup do
input_manager.reg :up, KbD do
actor.remove
end
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
+
+ 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
+
+ # FEATURE REQUEST
+ # actor.has_view do |view|
+ # view.uses :resource_manager
+ # view.configure do
+ # end
+
+ # view.draw do |target, x_off, y_off, z|
+ # end
+ # end
end
+
it 'creates an actor from within stage with the correct behaviors and updates' do
# going for a capybara style "page" reference for the game
game.stage do |stage| # instance of TestingStage
@@ -47,6 +71,8 @@ def setup
see_actor_attrs :mc_bane, bullets: 50
update 10
+ see_actor_drawn :mc_bane
+
see_actor_attrs :mc_bane, bullets: 40
game.should have_actor(:mc_bane)
@@ -141,6 +167,11 @@ def actor(actor_type)
end
}
+ def see_actor_drawn(actor_type)
+ act = game.actor(actor_type)
+ act.should be
+ end
+
def see_actor_attrs(actor_type, attrs)
act = game.actor(actor_type)
act.should be
@@ -159,3 +190,23 @@ def send_down(button_id)
gosu.button_down button_id
end
end
+
+#
+# class Shooty < Behavior
+# construct_with :actor, :director
+# def setup
+# actor.has_attribute :bullets, opts[:bullets]
+# director.when :update do |time|
+# actor.bullets -= time
+# end
+# end
+# end
+#
+# class DeathOnD < Behavior
+# construct_with :actor, :input_manager
+# def setup
+# input_manager.reg :up, KbD do
+# actor.remove
+# end
+# end
+# end

0 comments on commit 63c3593

Please sign in to comment.