gamebox 0.3.4 → 0.4.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (184) hide show
  1. data/README.rdoc +38 -0
  2. data/Rakefile +1 -10
  3. data/TODO.txt +6 -6
  4. data/app_generators/gamebox_generator.rb +95 -0
  5. data/{lib/gamebox/templates/template_app → app_generators/templates}/.gitignore +0 -0
  6. data/app_generators/templates/Gemfile +7 -0
  7. data/app_generators/templates/NEXT_STEPS.txt +1 -0
  8. data/{lib/gamebox/templates/template_app/README → app_generators/templates/README.rdoc} +0 -0
  9. data/{lib/gamebox/templates/template_app → app_generators/templates}/Rakefile +0 -0
  10. data/{lib/gamebox/templates/template_app → app_generators/templates}/config/boot.rb +0 -0
  11. data/app_generators/templates/config/environment.rb +30 -0
  12. data/{lib/gamebox/templates/template_app → app_generators/templates}/config/game.yml +0 -0
  13. data/{lib/gamebox/templates/template_app → app_generators/templates}/data/fonts/FONTS_GO_HERE +0 -0
  14. data/{lib/gamebox/templates/template_app → app_generators/templates}/data/graphics/GRAPHICS_GO_HERE +0 -0
  15. data/{lib/gamebox/templates/template_app → app_generators/templates}/data/music/MUSIC_GOES_HERE +0 -0
  16. data/{lib/gamebox/templates/template_app → app_generators/templates}/data/sounds/SOUND_FX_GO_HERE +0 -0
  17. data/{lib/gamebox/templates → app_generators/templates/script}/actor.erb +0 -0
  18. data/{lib/gamebox/templates → app_generators/templates/script}/actor_spec.erb +0 -0
  19. data/{lib/gamebox/templates → app_generators/templates/script}/actor_view.erb +0 -0
  20. data/{lib/gamebox/templates → app_generators/templates/script}/actor_view_spec.erb +0 -0
  21. data/app_generators/templates/script/generate +12 -0
  22. data/{lib/gamebox/templates/template_app → app_generators/templates}/spec/helper.rb +0 -0
  23. data/app_generators/templates/src/actors/player.rb +8 -0
  24. data/{lib/gamebox/templates/template_app → app_generators/templates}/src/app.rb +0 -0
  25. data/app_generators/templates/src/demo_stage.rb +7 -0
  26. data/bin/gamebox +8 -70
  27. data/component_generators/actor_generator.rb +17 -0
  28. data/docs/CODE_REVIEW +1 -1
  29. data/docs/REFACTOR_NOTES.txt +25 -0
  30. data/docs/getting_started.rdoc +1 -1
  31. data/gamebox.gemspec +7 -4
  32. data/lib/gamebox.rb +6 -3
  33. data/lib/gamebox/actors/collidable_debugger.rb +13 -15
  34. data/lib/gamebox/actors/curtain.rb +44 -43
  35. data/lib/gamebox/actors/emitter.rb +3 -42
  36. data/lib/gamebox/actors/fps.rb +13 -6
  37. data/lib/gamebox/actors/label.rb +42 -34
  38. data/lib/gamebox/actors/logo.rb +2 -4
  39. data/lib/gamebox/actors/score.rb +37 -27
  40. data/lib/gamebox/actors/svg_actor.rb +45 -32
  41. data/lib/gamebox/behaviors/animated.rb +39 -59
  42. data/lib/gamebox/behaviors/audible.rb +14 -14
  43. data/lib/gamebox/behaviors/collidable.rb +65 -36
  44. data/lib/gamebox/behaviors/collidable/aabb_collidable.rb +2 -3
  45. data/lib/gamebox/behaviors/collidable/collidable_shape.rb +6 -4
  46. data/lib/gamebox/behaviors/collidable/polygon_collidable.rb +1 -1
  47. data/lib/gamebox/behaviors/emitting.rb +48 -0
  48. data/lib/gamebox/behaviors/graphical.rb +22 -56
  49. data/lib/gamebox/behaviors/layered.rb +8 -21
  50. data/lib/gamebox/behaviors/physical.rb +202 -213
  51. data/lib/gamebox/behaviors/positioned.rb +16 -0
  52. data/lib/gamebox/behaviors/projectile.rb +15 -0
  53. data/lib/gamebox/behaviors/visible.rb +16 -0
  54. data/lib/gamebox/core/aabb_helpers.rb +61 -0
  55. data/lib/gamebox/core/aabb_node.rb +118 -0
  56. data/lib/gamebox/core/aabb_tree.rb +137 -0
  57. data/lib/gamebox/core/actor.rb +102 -0
  58. data/lib/gamebox/core/actor_factory.rb +56 -0
  59. data/lib/gamebox/core/actor_view.rb +63 -0
  60. data/lib/gamebox/core/actor_view_factory.rb +40 -0
  61. data/lib/gamebox/{arbiter.rb → core/arbiter.rb} +31 -34
  62. data/lib/gamebox/{backstage.rb → core/backstage.rb} +0 -0
  63. data/lib/gamebox/core/behavior.rb +64 -0
  64. data/lib/gamebox/core/behavior_factory.rb +56 -0
  65. data/lib/gamebox/{class_finder.rb → core/class_finder.rb} +0 -0
  66. data/lib/gamebox/{config_manager.rb → core/config_manager.rb} +1 -1
  67. data/lib/gamebox/core/configuration.rb +39 -0
  68. data/lib/gamebox/core/core.rb +30 -0
  69. data/lib/gamebox/core/deprecated.rb +15 -0
  70. data/lib/gamebox/{director.rb → core/director.rb} +6 -11
  71. data/lib/gamebox/core/font_style.rb +26 -0
  72. data/lib/gamebox/core/font_style_factory.rb +11 -0
  73. data/lib/gamebox/core/game.rb +19 -0
  74. data/lib/gamebox/{hooked_gosu_window.rb → core/hooked_gosu_window.rb} +12 -6
  75. data/lib/gamebox/{input_manager.rb → core/input_manager.rb} +106 -99
  76. data/lib/gamebox/core/physics.rb +22 -0
  77. data/lib/gamebox/{physical_stage.rb → core/physics_manager.rb} +36 -30
  78. data/lib/gamebox/{resource_manager.rb → core/resource_manager.rb} +19 -18
  79. data/lib/gamebox/{sound_manager.rb → core/sound_manager.rb} +9 -7
  80. data/lib/gamebox/{stage.rb → core/stage.rb} +42 -80
  81. data/lib/gamebox/{stage_manager.rb → core/stage_manager.rb} +46 -53
  82. data/lib/gamebox/{stagehand.rb → core/stagehand.rb} +0 -0
  83. data/lib/gamebox/{svg_document.rb → core/svg_document.rb} +0 -0
  84. data/lib/gamebox/core/timer_manager.rb +50 -0
  85. data/lib/gamebox/{viewport.rb → core/viewport.rb} +2 -3
  86. data/lib/gamebox/{wrapped_screen.rb → core/wrapped_screen.rb} +12 -19
  87. data/lib/gamebox/gamebox_application.rb +7 -15
  88. data/lib/gamebox/lib/evented_attributes.rb +51 -0
  89. data/lib/gamebox/{ftor.rb → lib/ftor.rb} +0 -0
  90. data/lib/gamebox/lib/min_max_helpers.rb +10 -0
  91. data/lib/gamebox/lib/rect.rb +112 -54
  92. data/lib/gamebox/lib/yoda.rb +46 -0
  93. data/lib/gamebox/spec/helper.rb +317 -12
  94. data/lib/gamebox/stagehands/spatial_tree_stagehand.rb +61 -0
  95. data/lib/gamebox/version.rb +8 -3
  96. data/lib/gamebox/views/graphical_actor_view.rb +22 -29
  97. data/script/perf_aabb.rb +56 -0
  98. data/script/perf_array_access.rb +16 -0
  99. data/script/perf_collisions.rb +37 -18
  100. data/script/perf_struct_vs_array.rb +7 -7
  101. data/spec/acceptance/animation_spec.rb +65 -0
  102. data/spec/acceptance/basic_actor_lifecycle_spec.rb +92 -0
  103. data/spec/acceptance/built_in_collision_handling_spec.rb +55 -0
  104. data/spec/acceptance/chipmunk_collision_handling_spec.rb +83 -0
  105. data/spec/acceptance/fps_actor_spec.rb +40 -0
  106. data/spec/acceptance/pausing_spec.rb +61 -0
  107. data/spec/acceptance/timer_usage_spec.rb +53 -0
  108. data/spec/actors/emitter_spec.rb +5 -0
  109. data/spec/{label_spec.rb → actors/label_spec.rb} +1 -1
  110. data/spec/behaviors/animated_spec.rb +85 -0
  111. data/spec/behaviors/collidable_spec.rb +134 -0
  112. data/spec/{physical_spec.rb → behaviors/physical_spec.rb} +2 -1
  113. data/spec/behaviors/positioned_spec.rb +6 -0
  114. data/spec/behaviors/projectile_spec.rb +6 -0
  115. data/spec/core/aabb_tree_spec.rb +109 -0
  116. data/spec/core/actor_factory_spec.rb +44 -0
  117. data/spec/core/actor_spec.rb +78 -0
  118. data/spec/core/actor_view_spec.rb +53 -0
  119. data/spec/{arbiter_spec.rb → core/arbiter_spec.rb} +29 -30
  120. data/spec/core/backstage_spec.rb +37 -0
  121. data/spec/core/behavior_factory_spec.rb +50 -0
  122. data/spec/core/behavior_spec.rb +8 -0
  123. data/spec/core/configuration_spec.rb +8 -0
  124. data/spec/core/core_spec.rb +13 -0
  125. data/spec/core/font_style_factory_spec.rb +17 -0
  126. data/spec/core/font_style_spec.rb +41 -0
  127. data/spec/core/hooked_gosu_window_spec.rb +75 -0
  128. data/spec/core/input_manager_spec.rb +285 -0
  129. data/spec/core/physics_manager_spec.rb +11 -0
  130. data/spec/core/resource_manager_spec.rb +12 -0
  131. data/spec/core/stage_manager_spec.rb +140 -0
  132. data/spec/core/stage_spec.rb +73 -0
  133. data/spec/core/timer_manager_spec.rb +89 -0
  134. data/spec/{viewport_spec.rb → core/viewport_spec.rb} +6 -3
  135. data/spec/core/wrapped_screen_spec.rb +26 -0
  136. data/spec/fixtures/game.yml +7 -0
  137. data/spec/fixtures/snelpling/idle/1.png +0 -0
  138. data/spec/fixtures/snelpling/jump/1.png +0 -0
  139. data/spec/fixtures/snelpling/jump/2.png +0 -0
  140. data/spec/fixtures/snelpling/jump/3.png +0 -0
  141. data/spec/helper.rb +8 -0
  142. data/spec/{class_finder_spec.rb → lib/class_finder_spec.rb} +2 -1
  143. data/spec/stagehands/spatial_tree_stagehand_spec.rb +19 -0
  144. data/spec/views/graphical_actor_view_spec.rb +116 -0
  145. metadata +343 -144
  146. data/README.txt +0 -34
  147. data/lib/gamebox/actor.rb +0 -179
  148. data/lib/gamebox/actor_factory.rb +0 -57
  149. data/lib/gamebox/actor_view.rb +0 -44
  150. data/lib/gamebox/actors/spatial_debugger.rb +0 -62
  151. data/lib/gamebox/behavior.rb +0 -70
  152. data/lib/gamebox/behaviors/timed.rb +0 -33
  153. data/lib/gamebox/behaviors/updatable.rb +0 -12
  154. data/lib/gamebox/console_app.rb +0 -41
  155. data/lib/gamebox/gamebox_generator.rb +0 -32
  156. data/lib/gamebox/generators/actor_generator.rb +0 -43
  157. data/lib/gamebox/generators/view_generator.rb +0 -42
  158. data/lib/gamebox/physical_director.rb +0 -17
  159. data/lib/gamebox/physics.rb +0 -32
  160. data/lib/gamebox/spatial_bucket.rb +0 -9
  161. data/lib/gamebox/spatial_hash.rb +0 -194
  162. data/lib/gamebox/spatial_stagehand.rb +0 -80
  163. data/lib/gamebox/templates/template_app/Gemfile +0 -6
  164. data/lib/gamebox/templates/template_app/config/environment.rb +0 -23
  165. data/lib/gamebox/templates/template_app/config/stage_config.yml +0 -2
  166. data/lib/gamebox/templates/template_app/script/generate +0 -7
  167. data/lib/gamebox/templates/template_app/src/demo_stage.rb +0 -11
  168. data/lib/gamebox/templates/template_app/src/game.rb +0 -19
  169. data/lib/gamebox/templates/template_app/src/my_actor.rb +0 -14
  170. data/script/perf_spatial_hash.rb +0 -64
  171. data/spec/actor_factory_spec.rb +0 -61
  172. data/spec/actor_spec.rb +0 -71
  173. data/spec/actor_view_spec.rb +0 -61
  174. data/spec/animated_spec.rb +0 -83
  175. data/spec/backstage_spec.rb +0 -45
  176. data/spec/behavior_spec.rb +0 -28
  177. data/spec/collidable_spec.rb +0 -135
  178. data/spec/emitter_spec.rb +0 -20
  179. data/spec/input_manager_spec.rb +0 -134
  180. data/spec/resource_manager_spec.rb +0 -13
  181. data/spec/spatial_hash_spec.rb +0 -119
  182. data/spec/spatial_stagehand_spec.rb +0 -93
  183. data/spec/stage_manager_spec.rb +0 -25
  184. data/spec/stage_spec.rb +0 -65
@@ -0,0 +1,83 @@
1
+ require 'helper'
2
+
3
+ describe "Using chipmunks collision handling", acceptance: true do
4
+
5
+ define_actor :rock do
6
+ has_behavior :physical => {shape: :circle,
7
+ mass: 10,
8
+ friction: 1.7,
9
+ elasticity: 0.4,
10
+ radius: 10,
11
+ moment: 150
12
+ }
13
+ end
14
+
15
+ define_actor :hard_place do
16
+ has_behavior :physical => {shape: :circle,
17
+ mass: 10,
18
+ friction: 1.7,
19
+ elasticity: 0.4,
20
+ radius: 10,
21
+ moment: 150,
22
+ fixed: true
23
+ }
24
+ end
25
+
26
+ it 'shows that two overlapping objects collide' do
27
+ #scope cheatzys
28
+ rock = nil
29
+ hard_place = nil
30
+ game.stage do |stage| # instance of TestingStage
31
+ # TODO order issue here
32
+ require 'chipmunk'
33
+ load "#{GAMEBOX_PATH}core/physics.rb"
34
+
35
+ @physics_manager = this_object_context[:physics_manager]
36
+ @physics_manager.configure
37
+ # TODO move these optionally to configure?
38
+ @physics_manager.elastic_iterations = 4
39
+ @physics_manager.damping = 0.4
40
+
41
+ rock = create_actor :rock, x: 0, y: 0
42
+ hard_place = create_actor :hard_place, x: 0, y: 0
43
+ @physics_manager.add_collision_func :rock, :hard_place do |collision_rock, collision_hard_place|
44
+ @collision_rock = collision_rock
45
+ @collision_hard_place = collision_hard_place
46
+ end
47
+ end
48
+ see_stage_ivars collision_rock: nil, collision_hard_place: nil
49
+ update 1
50
+ # TODO not sure about this one..
51
+ update 1
52
+ see_stage_ivars collision_rock: rock, collision_hard_place: hard_place
53
+ end
54
+
55
+ it 'shows that an object in motion can collide and that gravity is moving things' do
56
+ rock = nil
57
+ hard_place = nil
58
+ game.stage do |stage| # instance of TestingStage
59
+ # TODO order issue here
60
+ require 'chipmunk'
61
+ load "#{GAMEBOX_PATH}core/physics.rb"
62
+
63
+ @physics_manager = this_object_context[:physics_manager]
64
+ @physics_manager.configure
65
+ # TODO move these optionally to configure?
66
+ @physics_manager.elastic_iterations = 4
67
+ @physics_manager.damping = 0.4
68
+ @physics_manager.gravity = vec2(0,500)
69
+
70
+ rock = create_actor :rock, x: 0, y: -20
71
+
72
+ hard_place = create_actor :hard_place, x: 0, y: 0
73
+ @physics_manager.add_collision_func :rock, :hard_place do |collision_rock, collision_hard_place|
74
+ @collision_rock = collision_rock
75
+ @collision_hard_place = collision_hard_place
76
+ end
77
+ end
78
+ see_stage_ivars collision_rock: nil, collision_hard_place: nil
79
+ update 500
80
+ see_stage_ivars collision_rock: rock, collision_hard_place: hard_place
81
+ end
82
+ end
83
+
@@ -0,0 +1,40 @@
1
+ require 'helper'
2
+
3
+ describe "Using fps actor", acceptance: true do
4
+ it 'draws and updates the fps label' do
5
+ game.stage do |stage| # instance of TestingStage
6
+ @fps = create_actor :fps, font_name: 'arial', font_size: 20
7
+ end
8
+
9
+ game.should have_actor(:fps)
10
+ game.should have_actor(:label)
11
+ draw
12
+ see_text_drawn "", in: arial_20
13
+
14
+ Gosu.stubs(:fps).returns(99)
15
+ draw
16
+ see_text_drawn "", in: arial_20
17
+
18
+ update 100
19
+ draw
20
+ see_text_drawn "99", in: arial_20
21
+
22
+ remove_fps
23
+
24
+ game.should_not have_actor(:fps)
25
+ game.should_not have_actor(:label)
26
+
27
+ pending "check color of drawn fonts"
28
+
29
+ end
30
+
31
+ let!(:arial_20) { mock_font('arial', 20) }
32
+
33
+ def remove_fps
34
+ game.current_stage.instance_variable_get("@fps").remove
35
+ # TODO is this needed?
36
+ update 1
37
+ end
38
+
39
+ end
40
+
@@ -0,0 +1,61 @@
1
+ require 'helper'
2
+
3
+ describe "pausing in gamebox", acceptance: true do
4
+
5
+ define_behavior :shoot_rock do |beh|
6
+ beh.requires :timer_manager
7
+ beh.setup do
8
+ actor.has_attributes rocks_shot: 0
9
+ timer_manager.add_timer 'shoot_rock', 1_000 do
10
+ actor.react_to :shoot_rock
11
+ end
12
+ end
13
+
14
+ react_to do |msg, *args|
15
+ actor.rocks_shot += 1 if msg == :shoot_rock
16
+ end
17
+ end
18
+
19
+ define_actor :volcano do
20
+ has_behavior :shoot_rock
21
+ end
22
+
23
+
24
+ it 'allows timers and all updates from the director to be paused / unpaused' do
25
+ game.stage do |stage| # instance of TestingStage
26
+ @counter = 0
27
+ timer_manager.add_timer 'stage_timer', 2000 do
28
+ @counter += 1
29
+ end
30
+ create_actor :volcano
31
+ end
32
+ see_actor_attrs :volcano, rocks_shot: 0
33
+ see_stage_ivars counter: 0
34
+
35
+ update 100
36
+ see_actor_attrs :volcano, rocks_shot: 0
37
+ see_stage_ivars counter: 0
38
+
39
+ update 901
40
+ see_actor_attrs :volcano, rocks_shot: 1
41
+ see_stage_ivars counter: 0
42
+
43
+ pause
44
+ update 2001
45
+ see_actor_attrs :volcano, rocks_shot: 1
46
+ see_stage_ivars counter: 0
47
+
48
+ update 2001
49
+ see_actor_attrs :volcano, rocks_shot: 1
50
+ see_stage_ivars counter: 0
51
+
52
+ unpause
53
+ update 2001
54
+ see_actor_attrs :volcano, rocks_shot: 2
55
+ see_stage_ivars counter: 1
56
+
57
+ pending "add more actors _during_ the pause"
58
+ end
59
+
60
+ end
61
+
@@ -0,0 +1,53 @@
1
+ require 'helper'
2
+
3
+ describe "Using timers", acceptance: true do
4
+
5
+ define_behavior :shoot_rock do |beh|
6
+ beh.requires :timer_manager
7
+ beh.setup do
8
+ actor.has_attributes rocks_shot: 0
9
+ timer_manager.add_timer 'shoot_rock', 1_000 do
10
+ actor.react_to :shoot_rock
11
+ end
12
+ end
13
+
14
+ react_to do |msg, *args|
15
+ actor.rocks_shot += 1 if msg == :shoot_rock
16
+ end
17
+ end
18
+
19
+ define_actor :volcano do
20
+ has_behavior :shoot_rock
21
+ end
22
+
23
+
24
+ it 'allows behaviors to get fired from timers' do
25
+ game.stage do |stage| # instance of TestingStage
26
+ @counter = 0
27
+ timer_manager.add_timer 'stage_timer', 2000 do
28
+ @counter += 1
29
+ end
30
+ create_actor :volcano
31
+ end
32
+ see_actor_attrs :volcano, rocks_shot: 0
33
+ see_stage_ivars counter: 0
34
+
35
+ update 100
36
+ see_actor_attrs :volcano, rocks_shot: 0
37
+ see_stage_ivars counter: 0
38
+
39
+ update 901
40
+ see_actor_attrs :volcano, rocks_shot: 1
41
+ see_stage_ivars counter: 0
42
+
43
+ update 2001
44
+ see_actor_attrs :volcano, rocks_shot: 2
45
+ see_stage_ivars counter: 1
46
+
47
+ update 1
48
+ see_actor_attrs :volcano, rocks_shot: 3
49
+ see_stage_ivars counter: 1
50
+ end
51
+
52
+ end
53
+
@@ -0,0 +1,5 @@
1
+ require 'helper'
2
+
3
+ describe :emitter do
4
+ it 'should be rewritten as and actual particle system'
5
+ end
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__),'helper')
1
+ require 'helper'
2
2
 
3
3
  describe 'a new Label' do
4
4
  before do
@@ -0,0 +1,85 @@
1
+ require 'helper'
2
+
3
+ describe :animated do
4
+ it 'needs updated tests'
5
+
6
+ let(:opts) { {} }
7
+ subject { subcontext[:behavior_factory].add_behavior actor, :animated, opts }
8
+ let(:director) { evented_stub(stub_everything('director')) }
9
+ let(:subcontext) do
10
+ it = nil
11
+ Conject.default_object_context.in_subcontext{|ctx|it = ctx};
12
+ _mocks = create_mocks *(Actor.object_definition.component_names + ActorView.object_definition.component_names - [:actor, :behavior, :this_object_context])
13
+ _mocks.each do |k,v|
14
+ it[k] = v
15
+ it[:director] = director
16
+ end
17
+ it
18
+ end
19
+ let!(:actor) { subcontext[:actor] }
20
+
21
+ let(:image0) { stub('image 1', width: 5, height: 6) }
22
+ let(:image1) { stub('image 2', width: 7, height: 8) }
23
+ let(:images) { [image0, image1] }
24
+
25
+ before do
26
+ @resource_manager.stubs(:load_animation_set).returns(images)
27
+ end
28
+
29
+ it 'should define attributes on @actor' do
30
+ subject
31
+ actor.should respond_to(:image)
32
+ actor.should respond_to(:action)
33
+ actor.should respond_to(:width)
34
+ actor.should respond_to(:height)
35
+ actor.should respond_to(:image=)
36
+ actor.should respond_to(:action=)
37
+ actor.should respond_to(:width=)
38
+ actor.should respond_to(:height=)
39
+ end
40
+
41
+ it 'shouldnt update frame for non-animating' do
42
+ subject
43
+ actor.animating = false
44
+ actor.image.should == image0
45
+ director.fire :update, 61
46
+
47
+ actor.image.should == image0
48
+ end
49
+
50
+ it 'should update frame for animating' do
51
+ subject
52
+ actor.image.should == image0
53
+
54
+ director.fire :update, 61
55
+ actor.image.should == image1
56
+
57
+ director.fire :update, 60
58
+ actor.image.should == image0
59
+
60
+ director.fire :update, 30
61
+ actor.image.should == image0
62
+ director.fire :update, 30
63
+ actor.image.should == image1
64
+
65
+ end
66
+
67
+ it 'should set the action and animate accordingly for single frame' do
68
+ subject
69
+ actor.animating = true
70
+ @resource_manager.expects(:load_animation_set).with(actor, :foo).returns([image1])
71
+ actor.action = :foo
72
+
73
+ actor.animating.should be_false
74
+ end
75
+
76
+ it 'should set the action and animate accordingly for many frames' do
77
+ subject
78
+ actor.animating = false
79
+ @resource_manager.expects(:load_animation_set).with(actor, :foo).returns(images)
80
+ actor.action = :foo
81
+
82
+ actor.animating.should be_true
83
+ end
84
+
85
+ end
@@ -0,0 +1,134 @@
1
+ require 'helper'
2
+
3
+ describe :collidable do
4
+ let(:opts) { {} }
5
+ subject { subcontext[:behavior_factory].add_behavior actor, :collidable, opts }
6
+
7
+ let(:director) { evented_stub(stub_everything('director')) }
8
+ let!(:subcontext) do
9
+ it = nil
10
+ Conject.default_object_context.in_subcontext{|ctx|it = ctx};
11
+ _mocks = create_mocks *(Actor.object_definition.component_names + ActorView.object_definition.component_names - [:actor, :behavior, :this_object_context])
12
+ _mocks.each do |k,v|
13
+ it[k] = v
14
+ it[:director] = director
15
+ end
16
+ it
17
+ end
18
+ let!(:actor) { subcontext[:actor] }
19
+
20
+ before do
21
+ @stage.stubs(:register_collidable)
22
+ end
23
+
24
+ describe "aabb shape" do
25
+ let(:opts) do
26
+ {:shape => :aabb,
27
+ :cw_world_points => [
28
+ [-15,10],[15,10],
29
+ [15,-10], [-15,10]
30
+ ]}
31
+ end
32
+
33
+ it "constructs based on points" do
34
+ subject
35
+ actor.shape_type.should == :aabb
36
+ end
37
+ end
38
+
39
+ describe "circle shape" do
40
+ let(:opts) do
41
+ {shape: :circle, radius: 20}
42
+ end
43
+
44
+ it 'should recalculate_collidable_cache on position_changed' do
45
+ subject
46
+ actor.shape.expects(:recalculate_collidable_cache)
47
+ actor.react_to :position_changed
48
+ end
49
+
50
+ it 'should calculate center point for circle' do
51
+ actor.has_attributes x: 3, y: 6
52
+ subject
53
+
54
+ actor.center_x.should be_within(0.001).of(3)
55
+ actor.center_y.should be_within(0.001).of(6)
56
+ end
57
+ end
58
+
59
+ describe "polygon shape" do
60
+ let(:opts) do
61
+ {shape: :polygon, points: [[0,0],[10,7],[20,10]]}
62
+ end
63
+
64
+ it 'should calculate center point for polygon' do
65
+ subject
66
+ actor.center_x.should be_within(0.001).of(10)
67
+ actor.center_y.should be_within(0.001).of(5)
68
+ end
69
+
70
+ it 'should translate points to world coords for poly' do
71
+ actor.has_attributes x: 10, y: 5
72
+ subject
73
+ actor.cw_world_points.should == [[10,5],[20,12],[30,15]]
74
+ end
75
+
76
+ it 'should translate lines to world coords lines' do
77
+ subject
78
+ actor.cw_world_lines.should == [
79
+ [[0,0],[10,7]],
80
+ [[10,7],[20,10]],
81
+ [[20,10],[0,0]]
82
+ ]
83
+ end
84
+
85
+ it 'should translate world lines to edge normals' do
86
+ subject
87
+
88
+ actor.cw_world_edge_normals.should == [
89
+ [-7,10], [-3,10], [10,-20]
90
+ ]
91
+ end
92
+
93
+ it 'should cache calcs until reset' do
94
+ subject
95
+ # prime the cache
96
+ actor.cw_world_points.should == [[0,0],[10,7],[20,10]]
97
+ actor.cw_world_lines.should == [
98
+ [[0,0],[10,7]],
99
+ [[10,7],[20,10]],
100
+ [[20,10],[0,0]]
101
+ ]
102
+ actor.cw_world_edge_normals.should == [
103
+ [-7,10], [-3,10], [10,-20]
104
+ ]
105
+
106
+ actor.x = 10
107
+ actor.y = 5
108
+ actor.cw_world_points.should == [[0,0],[10,7],[20,10]]
109
+ actor.cw_world_lines.should == [
110
+ [[0,0],[10,7]],
111
+ [[10,7],[20,10]],
112
+ [[20,10],[0,0]]
113
+ ]
114
+ actor.cw_world_edge_normals.should == [
115
+ [-7,10], [-3,10], [10,-20]
116
+ ]
117
+
118
+ # triggers the recalc cache
119
+ director.fire :update, 4
120
+
121
+ actor.cw_world_points.should == [[10,5],[20,12],[30,15]]
122
+ actor.cw_world_lines.should == [
123
+ [[10,5],[20,12]],
124
+ [[20,12],[30,15]],
125
+ [[30,15],[10,5]]
126
+ ]
127
+ actor.cw_world_edge_normals.should == [
128
+ [-7,10], [-3,10], [10,-20]
129
+ ]
130
+
131
+ end
132
+ end
133
+
134
+ end