Skip to content
Browse files

Renamed old project, starting out with a clean slate. Doing some test…

…ing of an implmentation for the behavior system in module_test.rb, seems to be working quite well, I'll move it over to main.rb or a more appropriate place tomorrow.
  • Loading branch information...
1 parent 23e17a9 commit 5fc8c11424c99670e97b837385c34ea141e1c30a @dkoontz dkoontz committed
Showing with 413 additions and 153 deletions.
  1. +2 −2 .gitignore
  2. +190 −0 module_test.rb
  3. +30 −151 src/main.rb
  4. +191 −0 src/test_game.rb
View
4 .gitignore
@@ -1,4 +1,4 @@
-nbproject/
+nbproject
build.xml
manifest.mf
-build/
+build
View
190 module_test.rb
@@ -0,0 +1,190 @@
+class Behavior
+ attr_accessor :dependant_behaviors
+
+ protected
+ def self.depends_on(behavior)
+ add_dependency(behavior)
+ end
+
+ def self.has_callback(*args)
+
+ end
+
+ private
+ @@method_list = Hash.new{|h,k| h[k] = []}
+ def self.declared_methods(*methods)
+ @@method_list[self] = methods
+ end
+
+ @@behavior_dependencies = Hash.new{|h,k| h[k] = []}
+ def self.add_dependency(behavior)
+ @@behavior_dependencies[self] << behavior
+ end
+ def self.dependencies
+ @@behavior_dependencies[self]
+ end
+ public
+
+ def initialize(target)
+ @target = target
+ @dependant_behaviors = []
+ self.class.dependencies.each do |dependant_behavior|
+ dependant_instance = Object.const_get(dependant_behavior).new(@target)
+ dependant_instance.load
+ @dependant_behaviors << dependant_instance
+ end
+
+ @@method_list[self.class].each do |method|
+ if method.to_s =~ /=/
+ code = <<-ENDL
+ def #{method}(arg)
+ puts "in method #{method}"
+ @behaviors[:#{self.class}].#{method}(arg)
+ end
+ ENDL
+ else
+ code = <<-ENDL
+ def #{method}(*args, &blk)
+ puts "in method #{method}"
+ @behaviors[:#{self.class}].#{method}(*args, &blk)
+ end
+ ENDL
+ end
+ @target.send(:instance_eval, code, __FILE__, __LINE__)
+ end
+ end
+
+ def add; end
+ def remove; end
+ def load; end
+
+ def method_missing(method, *args, &blk)
+ @target.send(method, *args, &blk)
+ end
+end
+
+class Position2D < Behavior
+ attr_accessor :x, :y
+ declared_methods :x, :y, :x=, :y=
+
+ def load
+ @x = 0
+ @y = 0
+ end
+end
+
+class Movable2D < Behavior
+ depends_on :Position2D
+ declared_methods :move
+ has_callback :before_move
+ has_callback :after_move
+
+ def move(x, y)
+ # notify :before_move
+ self.x = x
+ self.y = y
+ # notify :after_move
+ puts "in move!"
+ end
+end
+
+class GameObject
+ @@behaviors = Hash.new{|h,k| h[k] = []}
+ def self.has_behavior(behavior)
+ @@behaviors[self] << behavior
+ end
+
+ private
+ def behaviors
+ @@behaviors[self.class]
+ end
+ public
+
+ def initialize
+ @behaviors = {}
+ behaviors.each do |behavior|
+ klass = Object.const_get(behavior)
+ behavior_instance = klass.new(self)
+ behavior_instance.load
+ @behaviors[klass.name.to_sym] = behavior_instance
+ behavior_instance.dependant_behaviors.each do |dependant_behavior|
+ @behaviors[dependant_behavior.class.name.to_sym] = dependant_behavior
+ end
+ end
+ end
+end
+
+class PacMan < GameObject
+ has_behavior :Movable2D
+# before_move { check_for_wall_collision }
+end
+
+pacman = PacMan.new
+p pacman.methods.sort
+pacman.move(5,4)
+puts pacman.x
+puts pacman.y
+
+
+# class Fast
+# def foo
+# "The result is: #{((10*354)/25)%17}"
+# end
+# end
+#
+# class Slow
+# define_method :foo, lambda {"The result is: #{((10*354)/25)%17}"}
+# end
+#
+# class Between
+# eval <<-ENDL
+# def foo
+# "The result is: \#{((10*354)/25) % 17}"
+# end
+# ENDL
+# end
+#
+# class Lambda
+# @@lambda = lambda {"The result is: #{((10*354)/25)%17}"}
+# def foo
+# @@lambda.call
+# end
+# end
+#
+# class Lambda2
+# @@lambda = lambda {"The result is: #{((10*354)/25)%17}"}
+# eval <<-ENDL
+# def foo
+# @@lambda.call
+# end
+# ENDL
+# end
+#
+# require 'benchmark'
+#
+# Benchmark.bm do |x|
+# f = Fast.new
+# s = Slow.new
+# b = Between.new
+# l = Lambda.new
+# l2 = Lambda2.new
+# 100000.times { f.foo }
+# 100000.times { b.foo }
+# 100000.times { s.foo }
+# 100000.times { l.foo }
+# 100000.times { l2.foo }
+#
+# x.report {100000.times { f.foo } }
+# x.report {100000.times { b.foo } }
+# x.report {100000.times { s.foo } }
+# x.report {100000.times { l.foo } }
+# x.report {100000.times { l2.foo } }
+# end
+#
+# # jruby -J-server module_test.rb
+# # user system total real
+# # 0.269000 0.000000 0.269000 ( 0.269000)
+# # 0.188000 0.000000 0.188000 ( 0.188000)
+# # 0.147000 0.000000 0.147000 ( 0.146000)
+# # 0.173000 0.000000 0.173000 ( 0.173000)
+# # 0.177000 0.000000 0.177000 ( 0.177000)
View
181 src/main.rb
@@ -15,177 +15,56 @@ def initialize
end
end
-class Demo < PApplet
- LOGIC_TICKS_PER_SECOND = 60
- LOGIC_TICK_TIME = 1000/LOGIC_TICKS_PER_SECOND
-
+
+class Scene < PApplet
def setup
- size(600,200)
- background(100,200,100)
- @player = Player.new(self, "data/duke.png", 0, 0)
- @enemy = Enemy.new(self, "data/DotNetLogo.jpg", 400,rand(200), 100,60)
- @sprites = [@enemy, @player]
- @last_frame_time = millis
- end
-
- def update
- delta = (millis - @last_frame_time) / LOGIC_TICK_TIME
- if delta > 1.0
- @sprites.each{|sprite| sprite.update(delta)}
- @player.shots.each do |shot|
- if @enemy.collided_with? shot
- @enemy.hit
- shot.x += 600
- end
- end
- end
+
end
def draw
- background(100,200,100)
- update
- @sprites.each {|sprite| sprite.draw}
+
end
end
-class Entity
- attr_accessor :x, :y, :width, :height
-
- def initialize(parent, source, x, y, width = nil, height = nil)
- @parent = parent
- if source.kind_of? String
- @sprite = parent.load_image(source)
- else
- @sprite = source
- end
- @x, @y = x, y
- @width = width || @sprite.width
- @height = height || @sprite.height
-
+class GameObject
+ @@subclasses = Hash.new {|h,k| h[k] = []}
+ def subclass(name)
+ @@subclasses[name]
end
- def draw
- @parent.image(@sprite, @x, @y, @width, @height)
- end
-
- def collided_with?(other)
- if ((other.x <= @x + @width) && ((other.x + other.width) >= @x)) &&
- ((other.y <= @y + @height) && ((other.y + other.height) >= @y))
- true
- else
- false
- end
+ def self.has_behavior(behavior)
+ # require target behavior
+ # require dependant behaviors
+ # mix in dependant behaviors
+ # generate callback registration methods
end
-
- def update(delta); end
end
-class Player < Entity
- attr_reader :shots
- MOVE_RATE = 3
-
- def initialize(*args)
- super
- @shots = []
- @last_shot_fired = @parent.millis
- end
-
- def update(delta)
- case @parent.key
- when " "[0]
- fire
- when "w"[0]
- move :up
- when "a"[0]
- move :left
- when "s"[0]
- move :down
- when "d"[0]
- move :right
- end
- @parent.key = 0
-
- @shots.each{|shot| shot.update(delta)}
- end
+class View
- def fire
- if((@parent.millis - @last_shot_fired) > 500)
- @shots << Shot.new(@parent, nil, @x + 110, @y + 22, 25, 25)
- @last_shot_fired = @parent.millis
- end
- end
-
- def move(direction)
- case direction
- when :up
- @y -= MOVE_RATE
- when :left
- @x -= MOVE_RATE
- when :right
- @x += MOVE_RATE
- when :down
- @y += MOVE_RATE
- end
- end
-
- def draw
- super
- @shots.delete_if{|shot| shot.dead?}.each{|shot| shot.draw}
- end
end
-class Enemy < Entity
- MOVE_SPEED = 2
-
- def initialize(*args)
- super
- @scale = 1
- @original_width = @width
- @original_height = @height
- @direction = :up
- end
-
- def draw
- @width = (@original_width * @scale.to_f).to_i
- @height = (@original_height * @scale.to_f).to_i
- super
- end
-
- def hit
- @scale -= 0.2
- if @scale <= 0
- puts "You win!"
- end
- end
-
- def update(delta)
- @scale += 0.001
- if :up == @direction
- @y -= MOVE_SPEED
- else
- @y += MOVE_SPEED
- end
- @direction = :down if @y < 0
- @direction = :up if @y > (200 - @height)
- end
+class PacMan < GameObject
+ has_behavior :Movable2D
+ before_move {"foo bar baz"}
end
-class Shot < Entity
- MOVEMENT_SPEED = 8
+class Movable2D
+ depends_on :Position2D
+ has_callback :before_move
+ has_callback :after_move
- def initialize(parent, source, x, y, width = nil, height = nil)
- @@shot_image = parent.load_image("data/jruby.png") unless self.class.class_variables.member?("@@shot_image")
- super(parent, @@shot_image, x, y, width, height)
+ def move(x, y)
+ notify :before_move
+ @x, @y = x, y
+ notify :after_move
end
- def update(delta)
- @x += MOVEMENT_SPEED
+ def x=(x)
+ move(x, @y)
end
- def dead?
- @x > 620
+ def y=(y)
+ move(@x, y)
end
-end
-
-demo = DemoFrame.new
-demo.show
+end
View
191 src/test_game.rb
@@ -0,0 +1,191 @@
+require 'jruby'
+
+include_class 'processing.core.PApplet'
+include_class 'java.awt.Frame'
+include_class 'java.awt.BorderLayout'
+
+class DemoFrame < Frame
+ def initialize
+ super("Demo of embedded Processing PApplet")
+ set_layout(BorderLayout.new)
+ demo = Demo.new
+ add(demo, BorderLayout::CENTER)
+ demo.init
+ pack
+ end
+end
+
+class Demo < PApplet
+ LOGIC_TICKS_PER_SECOND = 60
+ LOGIC_TICK_TIME = 1000/LOGIC_TICKS_PER_SECOND
+
+ def setup
+ size(600,200)
+ background(100,200,100)
+ @player = Player.new(self, "data/duke.png", 0, 0)
+ @enemy = Enemy.new(self, "data/DotNetLogo.jpg", 400,rand(200), 100,60)
+ @sprites = [@enemy, @player]
+ @last_frame_time = millis
+ end
+
+ def update
+ delta = (millis - @last_frame_time) / LOGIC_TICK_TIME
+ if delta > 1.0
+ @sprites.each{|sprite| sprite.update(delta)}
+ @player.shots.each do |shot|
+ if @enemy.collided_with? shot
+ @enemy.hit
+ shot.x += 600
+ end
+ end
+ end
+ end
+
+ def draw
+ background(100,200,100)
+ update
+ @sprites.each {|sprite| sprite.draw}
+ end
+end
+
+class Entity
+ attr_accessor :x, :y, :width, :height
+
+ def initialize(parent, source, x, y, width = nil, height = nil)
+ @parent = parent
+ if source.kind_of? String
+ @sprite = parent.load_image(source)
+ else
+ @sprite = source
+ end
+ @x, @y = x, y
+ @width = width || @sprite.width
+ @height = height || @sprite.height
+
+ end
+
+ def draw
+ @parent.image(@sprite, @x, @y, @width, @height)
+ end
+
+ def collided_with?(other)
+ if ((other.x <= @x + @width) && ((other.x + other.width) >= @x)) &&
+ ((other.y <= @y + @height) && ((other.y + other.height) >= @y))
+ true
+ else
+ false
+ end
+ end
+
+ def update(delta); end
+end
+
+class Player < Entity
+ attr_reader :shots
+ MOVE_RATE = 3
+
+ def initialize(*args)
+ super
+ @shots = []
+ @last_shot_fired = @parent.millis
+ end
+
+ def update(delta)
+ case @parent.key
+ when " "[0]
+ fire
+ when "w"[0]
+ move :up
+ when "a"[0]
+ move :left
+ when "s"[0]
+ move :down
+ when "d"[0]
+ move :right
+ end
+ @parent.key = 0
+
+ @shots.each{|shot| shot.update(delta)}
+ end
+
+ def fire
+ if((@parent.millis - @last_shot_fired) > 500)
+ @shots << Shot.new(@parent, nil, @x + 110, @y + 22, 25, 25)
+ @last_shot_fired = @parent.millis
+ end
+ end
+
+ def move(direction)
+ case direction
+ when :up
+ @y -= MOVE_RATE
+ when :left
+ @x -= MOVE_RATE
+ when :right
+ @x += MOVE_RATE
+ when :down
+ @y += MOVE_RATE
+ end
+ end
+
+ def draw
+ super
+ @shots.delete_if{|shot| shot.dead?}.each{|shot| shot.draw}
+ end
+end
+
+class Enemy < Entity
+ MOVE_SPEED = 2
+
+ def initialize(*args)
+ super
+ @scale = 1
+ @original_width = @width
+ @original_height = @height
+ @direction = :up
+ end
+
+ def draw
+ @width = (@original_width * @scale.to_f).to_i
+ @height = (@original_height * @scale.to_f).to_i
+ super
+ end
+
+ def hit
+ @scale -= 0.2
+ if @scale <= 0
+ puts "You win!"
+ end
+ end
+
+ def update(delta)
+ @scale += 0.001
+ if :up == @direction
+ @y -= MOVE_SPEED
+ else
+ @y += MOVE_SPEED
+ end
+ @direction = :down if @y < 0
+ @direction = :up if @y > (200 - @height)
+ end
+end
+
+class Shot < Entity
+ MOVEMENT_SPEED = 8
+
+ def initialize(parent, source, x, y, width = nil, height = nil)
+ @@shot_image = parent.load_image("data/jruby.png") unless self.class.class_variables.member?("@@shot_image")
+ super(parent, @@shot_image, x, y, width, height)
+ end
+
+ def update(delta)
+ @x += MOVEMENT_SPEED
+ end
+
+ def dead?
+ @x > 620
+ end
+end
+
+demo = DemoFrame.new
+demo.show

0 comments on commit 5fc8c11

Please sign in to comment.
Something went wrong with that request. Please try again.