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,37 @@
1
+ require 'helper'
2
+
3
+ describe Backstage do
4
+
5
+
6
+ it 'should construct' do
7
+ subject.should_not be_nil
8
+ end
9
+
10
+ it 'should return nil if not found' do
11
+ subject.get(:foo).should be_nil
12
+ end
13
+
14
+ it 'should return the value if found' do
15
+ subject.set(:foo,:foo_val).should == :foo_val
16
+ subject.get(:foo).should == :foo_val
17
+ end
18
+
19
+ it 'should replace with new values' do
20
+ subject.set(:foo,:foo_val).should == :foo_val
21
+ subject.get(:foo).should == :foo_val
22
+
23
+ subject.set(:foo,:other_val).should == :other_val
24
+ subject.get(:foo).should == :other_val
25
+ end
26
+
27
+ it 'should properly offer bracket notation' do
28
+ subject[:foo] = :val
29
+ subject[:foo].should == :val
30
+ end
31
+
32
+ it 'should raise if an Actor strolls off backstage' do
33
+ foo = create_actor
34
+ lambda{ subject[:foo] = foo }.should raise_error
35
+ end
36
+
37
+ end
@@ -0,0 +1,50 @@
1
+ require 'helper'
2
+
3
+ describe BehaviorFactory do
4
+ let(:some_behavior) { stub('some behavior', required_behaviors: []) }
5
+ let(:object_context) { mock('object context') }
6
+ let(:some_actor) { stub('some actor', add_behavior: nil, this_object_context: object_context) }
7
+
8
+ before do
9
+ Behavior.define :shootable
10
+
11
+ object_context.stubs(:[]).with(:behavior).returns(some_behavior)
12
+ object_context.stubs(:in_subcontext).yields(object_context)
13
+ some_behavior.stubs(:configure)
14
+ end
15
+
16
+ describe "#add_behavior" do
17
+ it 'creates the behavior based on the actor and symbol behavior_def' do
18
+ some_behavior.expects(:configure).with({})
19
+
20
+ subject.add_behavior some_actor, :shootable
21
+ end
22
+
23
+ it 'adds the behavior to the actor' do
24
+ some_actor.expects(:add_behavior).with(:shootable, some_behavior)
25
+ subject.add_behavior some_actor, :shootable
26
+ end
27
+
28
+ it 'configures the behavior with the given opts' do
29
+ opts = {some: 'opts'}
30
+ some_behavior.expects(:configure).with(opts)
31
+
32
+ subject.add_behavior some_actor, :shootable, opts
33
+ end
34
+
35
+ it 'raises on nil actor' do
36
+ lambda { subject.add_behavior nil, {} }.should raise_exception(/nil actor/)
37
+ end
38
+
39
+ it 'raises on nil behavior def' do
40
+ lambda { subject.add_behavior some_actor, nil }.should raise_exception(/nil behavior definition/)
41
+ end
42
+
43
+ it 'raises for missing behavior' do
44
+ lambda { subject.add_behavior actor, :do_not_exist }.should raise_exception
45
+ end
46
+
47
+ it 'creates all required behaviors'
48
+ it 'mixes in helpers'
49
+ end
50
+ end
@@ -0,0 +1,8 @@
1
+ require 'helper'
2
+
3
+ describe Behavior do
4
+ let(:actor) { create_actor }
5
+
6
+ it 'should auto-require behaviors that it depends on'
7
+
8
+ end
@@ -0,0 +1,8 @@
1
+ require 'helper'
2
+
3
+ describe Gamebox::Configuration do
4
+ describe ".add_setting" do
5
+ it 'works with all the gamebox settings'
6
+ it 'can add custom settings'
7
+ end
8
+ end
@@ -0,0 +1,13 @@
1
+ require 'helper'
2
+
3
+ describe Gamebox do
4
+ describe '.configure' do
5
+ it 'yields the configuration object to the given block'
6
+ it 'raises if no block is given'
7
+ end
8
+
9
+ describe '.configuration' do
10
+ it 'returns a new configuration object on first call'
11
+ it 'returns the same configuration object on subsequent calls'
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ require 'helper'
2
+
3
+ describe FontStyleFactory do
4
+ describe "#build" do
5
+ inject_mocks :this_object_context
6
+ let(:style) { stub }
7
+ before do
8
+ @this_object_context.stubs(:in_subcontext).yields(@this_object_context)
9
+ @this_object_context.stubs(:[]).with('font_style').returns style
10
+ end
11
+
12
+ it 'pulls the font style from the context and configures it' do
13
+ style.expects(:configure).with('arial', 22, :purple, 2, 0.5)
14
+ subject.build 'arial', 22, :purple, 2, 0.5
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,41 @@
1
+ require 'helper'
2
+
3
+ describe FontStyle do
4
+ inject_mocks :resource_manager
5
+ let(:font) { mock }
6
+
7
+ before do
8
+ @resource_manager.expects(:load_font).with("FooFace", 24).returns font
9
+ subject.configure "FooFace", 24, :aquamarine, 1, 1
10
+ end
11
+
12
+ it 'constructs a font from style specifications' do
13
+ subject.name.should == "FooFace"
14
+ subject.size.should == 24
15
+ subject.color.should == :aquamarine
16
+ subject.x_scale.should == 1
17
+ subject.y_scale.should == 1
18
+ end
19
+
20
+ it 'calculates the width of the given text' do
21
+ font.expects(:text_width).with("Blah").returns 56
22
+
23
+ subject.calc_width("Blah").should == 56
24
+ end
25
+
26
+ it 'has a height' do
27
+ font.expects(:height).returns 42
28
+ subject.height.should == 42
29
+ end
30
+
31
+ it 'updates the font when the face or size change' do
32
+ subject.name = "Bob"
33
+ subject.size = 21
34
+ @resource_manager.expects(:load_font).with("Bob", 21).returns :newfont
35
+
36
+ subject.reload
37
+ subject.font.should == :newfont
38
+ end
39
+ end
40
+
41
+
@@ -0,0 +1,75 @@
1
+ require 'helper'
2
+
3
+ describe HookedGosuWindow do
4
+ class Gosu::Window
5
+ def initialize(*args)
6
+ # TODO not sure how to handle this for travis-ci breakage..
7
+ # hopefully travis ci starts working again some day...
8
+ # sometimes causes seg faults if running bundle exec rake
9
+ # autorelease garbage in output if I don't do this
10
+ end
11
+ end
12
+
13
+ subject { HookedGosuWindow.new 2, 3, false }
14
+
15
+ it 'should inherit from Gosu::Window' do
16
+ described_class.is_a? Gosu::Window
17
+ end
18
+
19
+ describe "#needs_cursor?" do
20
+ it 'defaults to nil' do
21
+ subject.needs_cursor.should be_nil
22
+ end
23
+
24
+ it 'returns the instance variable' do
25
+ subject.needs_cursor = :foopy
26
+ subject.needs_cursor.should == :foopy
27
+ subject.needs_cursor?.should == :foopy
28
+ end
29
+ end
30
+
31
+ describe "#update" do
32
+ it 'fires initial update with full millis' do
33
+ Gosu.stubs(:milliseconds).returns 58
34
+
35
+ expects_event subject, :update, [[58]] do
36
+ subject.update
37
+ end
38
+ end
39
+
40
+ it 'fires initial update with deltas' do
41
+ subject.instance_variable_set('@last_millis', 30)
42
+ Gosu.stubs(:milliseconds).returns 58
43
+
44
+ expects_event subject, :update, [[28]] do
45
+ subject.update
46
+ end
47
+
48
+ end
49
+ end
50
+
51
+ describe "#draw" do
52
+ it 'fires the draw event' do
53
+ expects_event subject, :draw, [[]] do
54
+ subject.draw
55
+ end
56
+ end
57
+ end
58
+
59
+ describe "#button_down" do
60
+ it 'fires the button_down event' do
61
+ expects_event subject, :button_down, [[44]] do
62
+ subject.button_down 44
63
+ end
64
+ end
65
+ end
66
+
67
+ describe "#button_up" do
68
+ it 'fires the button_up event' do
69
+ expects_event subject, :button_up, [[44]] do
70
+ subject.button_up 44
71
+ end
72
+ end
73
+ end
74
+
75
+ end
@@ -0,0 +1,285 @@
1
+ require 'helper'
2
+
3
+ describe InputManager do
4
+ inject_mocks :config_manager, :wrapped_screen
5
+ before do
6
+ @config_manager.stubs(:[] => nil)
7
+ @window = evented_stub(stub('window', mouse_x: 43, mouse_y: 12))
8
+ @wrapped_screen.stubs(screen: @window)
9
+ subject.register game
10
+ end
11
+ let(:game) { stub('game') }
12
+
13
+ describe "mouse drag event" do
14
+ it "fires after a mouse down, mouse motion, mouse up" do
15
+ from_x = 40
16
+ from_y = 20
17
+ to_x = 140
18
+ to_y = 120
19
+ @window.stubs(:mouse_x).returns(from_x)
20
+ @window.stubs(:mouse_y).returns(from_y)
21
+
22
+ event_data = {:from => [from_x, from_y],:to => [to_x, to_y]}
23
+ exp_event = {
24
+ :type => :mouse,
25
+ :id => MsLeft,
26
+ :action => :up,
27
+ :callback_key => :mouse_drag,
28
+ :data => event_data
29
+ }
30
+
31
+ subject.stubs(:fire).with(:event_received, any_parameters)
32
+
33
+ subject.expects(:fire).with(:event_received, exp_event)
34
+
35
+ fire_event(subject, MsLeft, :down)
36
+ pretend_event(subject, nil, :motion)
37
+ @window.stubs(:mouse_x).returns(to_x)
38
+ @window.stubs(:mouse_y).returns(to_y)
39
+ fire_event(subject, MsLeft, :up)
40
+
41
+ end
42
+ end
43
+
44
+ describe "non-id specific events" do
45
+ it 'calls callback for all keyboard events' do
46
+ key_pressed = 0
47
+ subject.reg :keyboard_down do
48
+ key_pressed += 1
49
+ end
50
+
51
+ fire_event(subject, KbT, :up)
52
+ key_pressed.should == 0
53
+
54
+ # fire_event(subject, MsLeft, :down)
55
+ # key_pressed.should == 0
56
+
57
+ fire_event(subject, KbT, :down)
58
+ key_pressed.should == 1
59
+ end
60
+
61
+ it 'calls callback for all mouse events' do
62
+ mouse_pressed = 0
63
+ subject.reg :mouse_down do
64
+ mouse_pressed += 1
65
+ end
66
+
67
+ fire_event(subject, MsLeft, :up)
68
+ mouse_pressed.should == 0
69
+
70
+ # fire_event(subject, KbT, :down)
71
+ # mouse_pressed.should == 0
72
+
73
+ fire_event(subject, MsLeft, :down)
74
+ mouse_pressed.should == 1
75
+ end
76
+ end
77
+
78
+ describe "standard keyboard events" do
79
+ it 'calls the callbacks for the correct events' do
80
+ r_pressed = 0
81
+ y_pressed = 0
82
+ subject.reg :keyboard_down, KbR do
83
+ r_pressed += 1
84
+ end
85
+ subject.reg :keyboard_down, KbY do
86
+ y_pressed += 1
87
+ end
88
+
89
+ fire_event(subject, KbT, :down)
90
+ r_pressed.should == 0
91
+ y_pressed.should == 0
92
+
93
+ fire_event(subject, KbR, :up)
94
+ r_pressed.should == 0
95
+ y_pressed.should == 0
96
+
97
+ fire_event(subject, KbR, :down)
98
+ r_pressed.should == 1
99
+ y_pressed.should == 0
100
+
101
+ fire_event(subject, KbY, :down)
102
+ r_pressed.should == 1
103
+ y_pressed.should == 1
104
+
105
+ subject.clear_hooks self
106
+
107
+ # has now been unregistered
108
+ fire_event(subject, KbR, :down)
109
+ r_pressed.should == 1
110
+
111
+ fire_event(subject, KbY, :down)
112
+ y_pressed.should == 1
113
+ end
114
+
115
+ it 'passes along args'
116
+ end
117
+
118
+ describe "mapping an event id to a boolean" do
119
+ it "sets an ivar to true as long as the event id is down" do
120
+ listener = Struct.new(:left).new
121
+ subject.while_pressed KbLeft, listener, :left
122
+
123
+ listener.left.should be_false
124
+
125
+ fire_event(subject, KbLeft, :down)
126
+ listener.left.should be_true
127
+
128
+ fire_event(subject, GpLeft, :down)
129
+ listener.left.should be_true
130
+
131
+ fire_event(subject, GpLeft, :up)
132
+ listener.left.should be_true
133
+
134
+ fire_event(subject, KbT, :up)
135
+ listener.left.should be_true
136
+
137
+ fire_event(subject, KbLeft, :up)
138
+ listener.left.should be_false
139
+ end
140
+ end
141
+
142
+ describe "mapping multiple keys to a boolean" do
143
+ it "sets an ivar to true as long as ANY of the keys are down" do
144
+ listener = Struct.new(:left).new
145
+ subject.while_pressed [KbLeft, GpLeft], listener, :left
146
+
147
+ listener.left.should be_false
148
+
149
+ fire_event(subject, KbLeft, :down)
150
+ listener.left.should be_true
151
+
152
+ fire_event(subject, GpLeft, :down)
153
+ listener.left.should be_true
154
+
155
+ fire_event(subject, KbT, :up)
156
+ listener.left.should be_true
157
+
158
+ fire_event(subject, KbP, :down)
159
+ listener.left.should be_true
160
+
161
+ fire_event(subject, GpLeft, :up)
162
+ listener.left.should be_true
163
+
164
+ fire_event(subject, KbLeft, :up)
165
+ listener.left.should be_false
166
+ end
167
+ end
168
+
169
+ describe 'clearing events' do
170
+ it 'clears an event by listener when registered by button id' do
171
+ pressed = false
172
+ subject.reg :keyboard_down, KbLeft do
173
+ pressed = true
174
+ end
175
+
176
+ subject.clear_hooks self
177
+ fire_event(subject, KbLeft, :down)
178
+ pressed.should == false
179
+ end
180
+
181
+ it 'clears an event by listener when registered without button id' do
182
+ pressed = false
183
+ subject.reg :keyboard_down do
184
+ pressed = true
185
+ end
186
+
187
+ subject.clear_hooks self
188
+ fire_event(subject, KbLeft, :down)
189
+ pressed.should == false
190
+ end
191
+
192
+ it 'clears all events if no listener is provided' do
193
+ pressed = false
194
+ subject.reg :keyboard_down do
195
+ pressed = true
196
+ end
197
+
198
+ subject.clear_hooks
199
+ fire_event(subject, KbLeft, :down)
200
+ pressed.should == false
201
+ end
202
+
203
+ end
204
+
205
+ describe "#show" do
206
+ before do
207
+ @window.stubs(:show)
208
+ @wrapped_screen.stubs(screen: @window)
209
+ end
210
+
211
+ it 'shows the window' do
212
+ @window.expects(:show)
213
+ subject.show
214
+ end
215
+ end
216
+
217
+ describe "#register" do
218
+ it 'handles an update event' do
219
+ game.expects(:update).with(:millis)
220
+ @window.fire :update, :millis
221
+ end
222
+
223
+ it 'handles a draw event' do
224
+ game.expects(:draw)
225
+ @window.fire :draw
226
+ end
227
+ end
228
+
229
+ describe 'registering actors' do
230
+ it 'unregisters actors on death' do
231
+ subject
232
+ tom_cruise = create_actor :actor
233
+ called = false
234
+ subject.send :_register_hook, tom_cruise, :down, KbSpace do
235
+ called = true
236
+ end
237
+
238
+ tom_cruise.send :fire, :remove_me, tom_cruise
239
+ fire_event(subject, KbSpace, :down)
240
+ called.should be_false
241
+ end
242
+ end
243
+
244
+ it 'tracks the mouse motion events'
245
+ it 'cleanly removes handlers not using ids'
246
+ describe 'pausing' do
247
+ it 'pause/unpause' do
248
+ key_pressed = 0
249
+ subject.reg :keyboard_down do
250
+ key_pressed += 1
251
+ end
252
+
253
+ fire_event(subject, KbT, :down)
254
+ key_pressed.should == 1
255
+
256
+ subject.pause
257
+
258
+ fire_event(subject, KbT, :down)
259
+ key_pressed.should == 1
260
+
261
+ paused_pressed = 0
262
+ subject.reg :keyboard_down do
263
+ paused_pressed += 1
264
+ end
265
+
266
+ fire_event(subject, KbT, :down)
267
+ paused_pressed.should == 1
268
+
269
+ subject.unpause
270
+ fire_event(subject, KbT, :down)
271
+ paused_pressed.should == 1
272
+ key_pressed.should == 2
273
+ end
274
+ end
275
+
276
+ private
277
+ def fire_event(target, id, direction)
278
+ @window.fire "button_#{direction}".to_sym, id
279
+ end
280
+
281
+ def pretend_event(target, id, direction)
282
+ target.send :handle_event, id, direction
283
+ end
284
+
285
+ end