gosu_extensions 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.2.3
@@ -0,0 +1,73 @@
1
+ class Collision
2
+
3
+ # Default collision functions
4
+ #
5
+ None = nil # do not collide
6
+ Simple = lambda {} # just collide
7
+ # Kill = lambda { kill! }
8
+ # Damage = lambda { damage! }
9
+
10
+ #
11
+ #
12
+ attr_reader :window, :this, :that, :definition
13
+
14
+ #
15
+ #
16
+ def initialize window, this, that = this, &definition
17
+ @window = window # TODO Remove.
18
+ @this = this
19
+ @that = that
20
+ @definition = definition && package(definition)
21
+ end
22
+
23
+ # TODO Extend the definition to incorporate this
24
+ # method. Or at least #complex, #simple.
25
+ #
26
+ def package definition
27
+ if definition.arity == 2
28
+ complex_package definition
29
+ else
30
+ simple_package definition
31
+ end
32
+ end
33
+
34
+ #
35
+ #
36
+ def simple_package definition
37
+ lambda do |this_shape, _|
38
+ # TODO break if found.
39
+ #
40
+ window.moveables.each do |movable|
41
+ if movable.shape == this_shape
42
+ movable.instance_eval &definition
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ #
49
+ #
50
+ def complex_package definition
51
+ lambda do |this_shape, that_shape|
52
+ this_that = Array.new(2)
53
+ # TODO break if found
54
+ #
55
+ window.moveables.each do |movable|
56
+ if movable.shape == this_shape
57
+ this_that[0] = movable
58
+ end
59
+ if movable.shape == that_shape
60
+ this_that[1] = movable
61
+ end
62
+ end
63
+ definition.call *this_that
64
+ end
65
+ end
66
+
67
+ # Install this collision on the given environment.
68
+ #
69
+ def install_on environment
70
+ environment.add_collision_func this, that, &definition
71
+ end
72
+
73
+ end
@@ -29,9 +29,12 @@ class GameWindow < Gosu::Window
29
29
  attr_reader :environment,
30
30
  :moveables,
31
31
  :font,
32
- :scheduling
32
+ :scheduling,
33
+ :collisions
33
34
  attr_accessor :background_path,
34
- :background_hard_borders
35
+ :background_hard_borders,
36
+ :stop_condition,
37
+ :proceed_condition
35
38
 
36
39
  def initialize
37
40
  setup_window
@@ -40,6 +43,8 @@ class GameWindow < Gosu::Window
40
43
  setup_controls
41
44
  setup_window_control # e.g. ESC => exits
42
45
 
46
+ @collisions = []
47
+
43
48
  after_initialize
44
49
 
45
50
  super self.screen_width, self.screen_height, self.full_screen, 16
@@ -61,6 +66,8 @@ class GameWindow < Gosu::Window
61
66
  setup_collisions
62
67
 
63
68
  install_main_loop
69
+
70
+ after_setup
64
71
  end
65
72
 
66
73
  # This is the main game loop.
@@ -153,12 +160,42 @@ class GameWindow < Gosu::Window
153
160
  self.full_screen = true
154
161
  end
155
162
  end
156
- def collisions &block
157
- raise "collisions are defined in a block" unless block_given?
163
+
164
+ attr_accessor :collisions
165
+ def no_collision this, that = this
166
+ # Doesn't work, as &nil == nil
167
+ # collision this, that, &Collision::None
168
+ InitializerHooks.register self do
169
+ self.collisions << Collision.new(self, this, that)
170
+ end
171
+ end
172
+ def collision this, that = this, &definition
173
+ definition ||= Collision::Simple
158
174
  InitializerHooks.register self do
159
- @collision_definitions = block
175
+ self.collisions << Collision.new(self, this, that, &definition)
160
176
  end
161
177
  end
178
+
179
+ # Stop the game if this condition is true.
180
+ #
181
+ # Block will instance eval in the window.
182
+ #
183
+ # Use the callback after_stopping.
184
+ #
185
+ def stop_on &condition
186
+ InitializerHooks.register self do
187
+ self.stop_condition = condition
188
+ end
189
+ end
190
+
191
+ # # Proceed if this condition is true.
192
+ # #
193
+ # def proceed_on &condition
194
+ # InitializerHooks.register self do
195
+ # self.proceed_condition = condition
196
+ # end
197
+ # end
198
+
162
199
  end
163
200
 
164
201
  # Setup methods
@@ -206,11 +243,16 @@ class GameWindow < Gosu::Window
206
243
  @environment.window = self
207
244
  @environment.damping = -self.damping + 1 # recalculate the damping such that 0.0 has no damping.
208
245
  end
209
- # Override.
246
+ # Callbacks:
210
247
  #
211
248
  def setup_players; end
212
249
  def setup_enemies; end
213
250
  def setup_waves; end
251
+ def after_setup; end
252
+ def after_stopping; end
253
+ def before_proceeding; end
254
+ def step; end # The most important callback
255
+
214
256
  #
215
257
  #
216
258
  # Example:
@@ -218,7 +260,7 @@ class GameWindow < Gosu::Window
218
260
  # add_collision_func ...
219
261
  #
220
262
  def setup_collisions
221
- self.environment.instance_eval &@collision_definitions if @collision_definitions
263
+ self.collisions.each { |collision| collision.install_on(environment) }
222
264
  end
223
265
 
224
266
  # Add controls for a player.
@@ -242,12 +284,29 @@ class GameWindow < Gosu::Window
242
284
  def update
243
285
  @current_loop.call
244
286
  end
287
+ def stop
288
+ @current_loop = lambda do
289
+ proceed if proceed_condition && instance_eval(&proceed_condition)
290
+ advance_step
291
+ # TODO stopped
292
+ end
293
+ after_stopping
294
+ end
295
+ def proceed
296
+ before_proceeding
297
+ @current_loop = main_loop
298
+ end
245
299
  # Advances to the next step in the game.
246
300
  #
247
- def next_step
301
+ def advance_step
248
302
  @step += 1
249
303
  @scheduling.step
250
304
  end
305
+ def next_step
306
+ stop if stop_condition && instance_eval(&stop_condition)
307
+ advance_step
308
+ step
309
+ end
251
310
  # Each step, this is called to handle any input.
252
311
  #
253
312
  def handle_input
@@ -326,6 +385,7 @@ class GameWindow < Gosu::Window
326
385
  def threaded time = 1, &code
327
386
  @scheduling.add time, &code
328
387
  end
388
+ alias after threaded
329
389
 
330
390
 
331
391
  # Utility Methods
@@ -339,11 +399,15 @@ class GameWindow < Gosu::Window
339
399
  end
340
400
  # Randomly adds a Thing to a uniform random position.
341
401
  #
342
- def randomly_add type
402
+ def add type, x = nil, y = nil, &random_function
343
403
  thing = type.new self
344
- thing.warp_to *uniform_random_position
404
+ position = x && y && [x, y] || random_function && random_function[]
405
+ thing.warp_to *position
345
406
  register thing
346
407
  end
408
+ def randomly_add type
409
+ add type, *uniform_random_position
410
+ end
347
411
  # Revives the player if not already in.
348
412
  #
349
413
  def revive player
@@ -33,6 +33,7 @@ require 'remove_shapes'
33
33
  require 'wave'
34
34
  require 'waves'
35
35
  require 'scheduling'
36
+ require 'collision'
36
37
  require 'environment'
37
38
  require 'game_window'
38
39
  require 'control'
@@ -0,0 +1,86 @@
1
+ require File.join(File.dirname(__FILE__), '/../../spec_helper')
2
+
3
+ describe Collision do
4
+
5
+ context 'with collision' do
6
+ before(:each) do
7
+ @some_function = lambda {}
8
+ @collision = Collision.new :some_window, :this, :that, &@some_function
9
+ end
10
+ describe "install_on" do
11
+ it "should install itself on the given thing correctly" do
12
+ environment = stub :environment
13
+ environment.should_receive(:add_collision_func).once.with :this, :that, &@some_function
14
+
15
+ @collision.install_on environment
16
+ end
17
+ end
18
+ end
19
+
20
+ context 'no collision' do
21
+ before(:each) do
22
+ @collision = Collision.new :some_window, :this, :that, &Collision::None
23
+ end
24
+ describe "install_on" do
25
+ it "should install itself on the given thing correctly" do
26
+ environment = stub :environment
27
+ environment.should_receive(:add_collision_func).once.with :this, :that, &Collision::None
28
+
29
+ @collision.install_on environment
30
+ end
31
+ end
32
+ end
33
+
34
+ describe "complex_package" do
35
+ before(:each) do
36
+ @collision = Collision.new :some_window, :this, :that
37
+ end
38
+ it "should return a lambda with two params" do
39
+ @collision.complex_package(lambda{}).arity.should == 2
40
+ end
41
+ end
42
+
43
+ describe "simple_package" do
44
+ before(:each) do
45
+ @collision = Collision.new :some_window, :this, :that
46
+ end
47
+ it "should return a lambda with two params" do
48
+ @collision.simple_package(lambda{}).arity.should == 2
49
+ end
50
+ end
51
+
52
+ describe "package" do
53
+ context 'definition with two params' do
54
+ before(:each) do
55
+ @definition = lambda { |one, two| }
56
+ @collision = Collision.new :some_window, :this, :that, &@definition
57
+ end
58
+ it "should use the complex packaging" do
59
+ @collision.should_receive(:complex_package).once.with @definition
60
+
61
+ @collision.package @definition
62
+ end
63
+ end
64
+ context 'definition with no params' do
65
+ before(:each) do
66
+ @definition = lambda { }
67
+ @collision = Collision.new :some_window, :this, :that, &@definition
68
+ end
69
+ it "should use the simple packaging" do
70
+ @collision.should_receive(:simple_package).once.with @definition
71
+
72
+ @collision.package @definition
73
+ end
74
+ end
75
+ end
76
+
77
+ describe "constants" do
78
+ it "should be nil" do
79
+ Collision::None.should == nil
80
+ end
81
+ it "should be an empty lambda" do
82
+ Collision::Simple.call # well, Collision should not receive anything
83
+ end
84
+ end
85
+
86
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 2
9
- version: 0.2.2
8
+ - 3
9
+ version: 0.2.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Florian Hanke
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-11 00:00:00 +02:00
17
+ date: 2010-04-12 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -53,6 +53,7 @@ files:
53
53
  - Rakefile
54
54
  - VERSION
55
55
  - generator/gogogosu.rb
56
+ - lib/core/collision.rb
56
57
  - lib/core/control.rb
57
58
  - lib/core/controls.rb
58
59
  - lib/core/environment.rb
@@ -122,6 +123,7 @@ signing_key:
122
123
  specification_version: 3
123
124
  summary: Default extensions built onto the popular Gosu Framework. Uses Chipmunk for game physics. That's it for now. I'm working on them. Anyway, GAME ON!
124
125
  test_files:
126
+ - spec/lib/core/collision_spec.rb
125
127
  - spec/lib/core/control_spec.rb
126
128
  - spec/lib/core/controls_spec.rb
127
129
  - spec/lib/core/initializer_hooks_spec.rb