Permalink
Browse files

netris refactorings

  • Loading branch information...
1 parent 29968bc commit 58f65ca16d647372478953b26e800c134a86a348 @shawn42 committed Jan 18, 2013
@@ -1,7 +1,9 @@
define_actor :bar do
- has_behavior :positioned
- has_attributes image: nil,
- blocks: [[
+ has_behaviors do
+ positioned
+ colored color: 'light_blue.png'
+ end
+ has_attributes blocks: [[
[-1, 0],
[0, 0],
[1, 0],
@@ -13,19 +15,6 @@
[0, 1]
]],
current_rotation: 0,
- grid_position: Struct.new(:x, :y).new(0, 0)
-
- view do
- draw do |target, x_off, y_off, z|
- actor.blocks[actor.current_rotation].each do |b|
- x = (BLOCK_SIZE * b[0]) + actor.x + x_off
- y = (BLOCK_SIZE * b[1]) + actor.y + x_off
- target.draw_image actor.image, x, y, 0
- end
- end
-
- setup do
- actor.image = resource_manager.load_image("light_blue.png")
- end
- end
+ grid_position: Struct.new(:x, :y).new(0, 0),
+ view: :piece_view
end
@@ -1,14 +1,12 @@
define_actor :block do
has_behavior :positioned
- has_attributes image: nil,
- grid_offset_x: nil,
- grid_offset_y: nil
view do
draw do |target, x_off, y_off, z|
+ image = actor.do_or_do_not(:image)
x = actor.x + x_off
y = actor.y + x_off
- target.draw_image actor.image, x, y, 0
+ target.draw_image image, x, y, z if image
end
end
end
@@ -1,26 +1,15 @@
define_actor :j do
- has_behavior :positioned
- has_attributes image: nil,
- blocks: [
+ has_behaviors do
+ positioned
+ colored color: 'dark_blue.png'
+ end
+ has_attributes blocks: [
[[0 , 0],[0,-1],[1, 0],[2, 0]],
[[0 , 0],[1, 0],[0, 1],[0, 2]],
[[0 , 0],[-1, 0],[-2, 0],[0, 1]],
[[0 , 0],[0, -1],[0, -2],[-1, 0]]
],
current_rotation: 0,
- grid_position: Struct.new(:x, :y).new(0, 0)
-
- view do
- draw do |target, x_off, y_off, z|
- actor.blocks[actor.current_rotation].each do |b|
- x = (BLOCK_SIZE * b[0]) + actor.x + x_off
- y = (BLOCK_SIZE * b[1]) + actor.y + x_off
- target.draw_image actor.image, x, y, 0
- end
- end
-
- setup do
- actor.image = resource_manager.load_image("dark_blue.png")
- end
- end
-end
+ grid_position: Struct.new(:x, :y).new(0, 0),
+ view: :piece_view
+end
@@ -1,7 +1,11 @@
define_actor :l do
- has_behavior :positioned
- has_attributes image: nil,
- blocks: [ [
+
+ has_behaviors do
+ positioned
+ colored color: 'orange.png'
+ end
+
+ has_attributes blocks: [ [
[0 , 0],
[0,-1],
[-1, 0],
@@ -23,19 +27,7 @@
[0, 2]
]],
current_rotation: 0,
- grid_position: Struct.new(:x, :y).new(0, 0)
+ grid_position: Struct.new(:x, :y).new(0, 0),
+ view: :piece_view
- view do
- draw do |target, x_off, y_off, z|
- actor.blocks[actor.current_rotation].each do |b|
- x = (BLOCK_SIZE * b[0]) + actor.x + x_off
- y = (BLOCK_SIZE * b[1]) + actor.y + x_off
- target.draw_image actor.image, x, y, 0
- end
- end
-
- setup do
- actor.image = resource_manager.load_image("orange.png")
- end
- end
end
@@ -1,7 +1,9 @@
define_actor :s do
- has_behavior :positioned
- has_attributes image: nil,
- blocks: [[
+ has_behaviors do
+ positioned
+ colored color: 'green.png'
+ end
+ has_attributes blocks: [[
[0, 0],
[-1, 0],
[0, -1],
@@ -13,19 +15,7 @@
[-1, -1]
]],
current_rotation: 0,
- grid_position: Struct.new(:x, :y).new(0, 0)
-
- view do
- draw do |target, x_off, y_off, z|
- actor.blocks[actor.current_rotation].each do |b|
- x = (BLOCK_SIZE * b[0]) + actor.x + x_off
- y = (BLOCK_SIZE * b[1]) + actor.y + x_off
- target.draw_image actor.image, x, y, 0
- end
- end
+ grid_position: Struct.new(:x, :y).new(0, 0),
+ view: :piece_view
- setup do
- actor.image = resource_manager.load_image("green.png")
- end
- end
end
@@ -1,25 +1,15 @@
define_actor :square do
- has_behavior :positioned
- has_attributes image: nil,
- blocks: [[
+ has_behaviors do
+ positioned
+ colored color: 'orange.png'
+ end
+
+ has_attributes blocks: [[
[0, 0],[1, 0],
[1, -1],[0, -1]
]],
current_rotation: 0,
- grid_position: Struct.new(:x, :y).new(0, 0)
-
- view do
- draw do |target, x_off, y_off, z|
- actor.blocks[actor.current_rotation].each do |b|
- x = (BLOCK_SIZE * b[0]) + actor.x + x_off
- y = (BLOCK_SIZE * b[1]) + actor.y + x_off
- target.draw_image actor.image, x, y, 0
- end
- end
-
- setup do
- actor.image = resource_manager.load_image("yellow.png")
- end
- end
+ grid_position: Struct.new(:x, :y).new(0, 0),
+ view: :piece_view
-end
+end
@@ -1,7 +1,9 @@
define_actor :t do
- has_behavior :positioned
- has_attributes image: nil,
- blocks: [[
+ has_behaviors do
+ positioned
+ colored color: 'purple.png'
+ end
+ has_attributes blocks: [[
[0, 0],
[-1, 0],
[1, 0],
@@ -23,19 +25,6 @@
[-1, 0]
]],
current_rotation: 0,
- grid_position: Struct.new(:x, :y).new(0, 0)
-
- view do
- draw do |target, x_off, y_off, z|
- actor.blocks[actor.current_rotation].each do |b|
- x = (BLOCK_SIZE * b[0]) + actor.x + x_off
- y = (BLOCK_SIZE * b[1]) + actor.y + x_off
- target.draw_image actor.image, x, y, 0
- end
- end
-
- setup do
- actor.image = resource_manager.load_image("purple.png")
- end
- end
+ grid_position: Struct.new(:x, :y).new(0, 0),
+ view: :piece_view
end
@@ -1,7 +1,9 @@
define_actor :z do
- has_behavior :positioned
- has_attributes image: nil,
- blocks: [[
+ has_behaviors do
+ positioned
+ colored color: 'green.png'
+ end
+ has_attributes blocks: [[
[0, 0],
[1, 0],
[0, -1],
@@ -13,19 +15,7 @@
[1, -1]
]],
current_rotation: 0,
- grid_position: Struct.new(:x, :y).new(0, 0)
-
- view do
- draw do |target, x_off, y_off, z|
- actor.blocks[actor.current_rotation].each do |b|
- x = (BLOCK_SIZE * b[0]) + actor.x + x_off
- y = (BLOCK_SIZE * b[1]) + actor.y + x_off
- target.draw_image actor.image, x, y, 0
- end
- end
+ grid_position: Struct.new(:x, :y).new(0, 0),
+ view: :piece_view
- setup do
- actor.image = resource_manager.load_image("green.png")
- end
- end
end
@@ -0,0 +1,7 @@
+define_behavior :colored do
+ requires :resource_manager
+
+ setup do
+ actor.has_attributes image: resource_manager.load_image(opts[:color])
+ end
+end
@@ -2,8 +2,7 @@
requires :director, :input_manager, :stage
setup do
- actor.has_attributes grid: Grid.new(10, 21),
- drop_after: 500,
+ actor.has_attributes drop_after: 500,
time_lapsed: 0
actor.grid.when(:game_over) do
@@ -53,9 +52,15 @@
end
end
end
+
+ reacts_with :remove
end
helpers do
+ def remove
+ director.unsubscribe_all self
+ input_manager.unsubscribe_all self
+ end
end
end
@@ -1,13 +1,16 @@
define_stage :demo do
curtain_up do
sound_manager.play_music :current_rider
- @game_field = create_actor :game_field, x: 50, y: 80
+
+ create_actor :game_field, x: 50, y: 80,
+ grid: Grid.new(10, 20)
+
create_actor :label,
text: "Press N to start a new game",
x: 100,
- y: 10,
- font_name: "Asimov.ttf",
- font_size: 30,
- color: [250, 250, 250, 255]
+ y: 10
+ # font_name: "Asimov.ttf",
+ # font_size: 30,
+ # color: [250, 250, 250, 255]
end
end
@@ -0,0 +1,12 @@
+define_actor_view :piece_view do
+ draw do |target, x_off, y_off, z|
+ image = actor.do_or_do_not(:image)
+ if image
+ actor.blocks[actor.current_rotation].each do |b|
+ x = (BLOCK_SIZE * b[0]) + actor.x + x_off
+ y = (BLOCK_SIZE * b[1]) + actor.y + x_off
+ target.draw_image actor.image, x, y, 0
+ end
+ end
+ end
+end

0 comments on commit 58f65ca

Please sign in to comment.