gosu_extensions 0.1.17 → 0.1.18

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.17
1
+ 0.1.18
@@ -34,7 +34,9 @@ class GameWindow < Gosu::Window
34
34
 
35
35
  def initialize
36
36
  setup_window
37
- setup_containers
37
+ setup_moveables
38
+ setup_remove_shapes
39
+ setup_controls
38
40
 
39
41
  after_initialize
40
42
 
@@ -49,6 +51,8 @@ class GameWindow < Gosu::Window
49
51
  setup_scheduling
50
52
  setup_font
51
53
 
54
+ setup_containers
55
+
52
56
  setup_environment
53
57
  setup_enemies
54
58
  setup_players
@@ -63,9 +67,8 @@ class GameWindow < Gosu::Window
63
67
  # Step the physics environment SUBSTEPS times each update.
64
68
  #
65
69
  SUBSTEPS.times do
66
- remove_shapes!
67
- reset_forces
68
- move_all
70
+ remove_shapes
71
+ move
69
72
  targeting
70
73
  handle_input
71
74
  step_physics
@@ -185,11 +188,17 @@ class GameWindow < Gosu::Window
185
188
  @background_image = Gosu::Image.new self, File.join(Resources.root, self.background_path), self.background_hard_borders
186
189
  end
187
190
  end
191
+ def setup_moveables
192
+ @moveables = Moveables.new
193
+ end
194
+ def setup_remove_shapes
195
+ @remove_shapes = RemoveShapes.new
196
+ end
197
+ def setup_controls
198
+ @controls = []
199
+ end
188
200
  def setup_containers
189
- @moveables = []
190
- @controls = []
191
- @remove_shapes = []
192
- @players = []
201
+ @players = []
193
202
  end
194
203
  def setup_steps
195
204
  @step = 0
@@ -266,8 +275,6 @@ class GameWindow < Gosu::Window
266
275
  # Does a single step.
267
276
  #
268
277
  def step_physics
269
- # Perform the step over @dt period of time
270
- # For best performance @dt should remain consistent for the game
271
278
  @environment.step @dt
272
279
  end
273
280
 
@@ -291,7 +298,7 @@ class GameWindow < Gosu::Window
291
298
  # Note: Internal use. Use unregister to properly remove a moveable.
292
299
  #
293
300
  def remove shape
294
- @remove_shapes << shape
301
+ @remove_shapes.add shape
295
302
  end
296
303
 
297
304
  # Run some code at relative time <time>.
@@ -310,21 +317,15 @@ class GameWindow < Gosu::Window
310
317
 
311
318
  # Moves each moveable.
312
319
  #
313
- def move_all
314
- @moveables.each &:move
320
+ def move
321
+ @moveables.move
315
322
  end
316
-
317
323
  # Handles the targeting process.
318
324
  #
319
325
  def targeting
320
- @moveables.select { |m| m.respond_to? :target }.each do |gun|
321
- gun.target *@moveables.select { |m| m.kind_of? Enemy }
322
- end
326
+ @moveables.targeting
323
327
  end
324
328
 
325
-
326
-
327
-
328
329
  # Utility Methods
329
330
  #
330
331
 
@@ -359,44 +360,25 @@ class GameWindow < Gosu::Window
359
360
  # Moveables register themselves here.
360
361
  #
361
362
  def register moveable
362
- @moveables << moveable
363
+ @moveables.register moveable
363
364
  moveable.add_to @environment
364
365
  end
365
366
 
366
- def remove_shapes!
367
- # This iterator makes an assumption of one Shape per Star making it safe to remove
368
- # each Shape's Body as it comes up
369
- # If our Stars had multiple Shapes, as would be required if we were to meticulously
370
- # define their true boundaries, we couldn't do this as we would remove the Body
371
- # multiple times
372
- # We would probably solve this by creating a separate @remove_bodies array to remove the Bodies
373
- # of the Stars that were gathered by the Player
374
- #
375
- # p @remove_shapes unless @remove_shapes.empty?
376
- @remove_shapes.each do |shape|
377
- @environment.remove_body shape.body
378
- @environment.remove_shape shape
379
- @moveables.delete_if { |moveable| moveable.shape == shape }
380
- end
381
- @remove_shapes.clear
367
+ def remove_shapes
368
+ @remove_shapes.remove_from @environment, @moveables
382
369
  end
383
370
 
384
- def reset_forces
385
- # When a force or torque is set on a Body, it is cumulative
386
- # This means that the force you applied last SUBSTEP will compound with the
387
- # force applied this SUBSTEP; which is probably not the behavior you want
388
- # We reset the forces on the Player each SUBSTEP for this reason
389
- #
390
- # @player1.shape.body.reset_forces
391
- # @player2.shape.body.reset_forces
392
- # @player3.shape.body.reset_forces
393
- # @players.each { |player| player.shape.body.reset_forces }
371
+ # Revives the player if not already in.
372
+ #
373
+ def revive player
374
+ register player unless registered?(player) # player.registered?
394
375
  end
395
376
 
396
- # def revive player
397
- # return if @moveables.find { |moveable| moveable == player }
398
- # register player
399
- # end
377
+ # Is the thing registered?
378
+ #
379
+ def registered? thing
380
+ @moveables.registered? thing
381
+ end
400
382
 
401
383
  # Drawing methods
402
384
  #
@@ -414,7 +396,7 @@ class GameWindow < Gosu::Window
414
396
 
415
397
  end
416
398
  def draw_moveables
417
- @moveables.each &:draw
399
+ @moveables.draw
418
400
  end
419
401
  def draw_ui
420
402
  # @font.draw "P1 Score: ", 10, 10, ZOrder::UI, 1.0, 1.0, 0xffff0000
@@ -0,0 +1,33 @@
1
+ # TODO optimize
2
+ #
3
+ class Moveables
4
+
5
+ def initialize
6
+ @elements = []
7
+ end
8
+
9
+ delegate :each, :to => :@elements
10
+
11
+ def register moveable
12
+ @elements << moveable
13
+ end
14
+ def registered? moveable
15
+ @elements.include? moveable
16
+ end
17
+ def remove shape
18
+ @elements.delete_if { |element| element.shape == shape }
19
+ end
20
+
21
+ def draw
22
+ @elements.each &:draw
23
+ end
24
+ def move
25
+ @elements.each &:move
26
+ end
27
+ def targeting
28
+ @elements.select { |m| m.respond_to? :target }.each do |gun|
29
+ gun.target *@elements.select { |m| m.kind_of? Enemy }
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,35 @@
1
+ #
2
+ #
3
+ class RemoveShapes
4
+
5
+ def initialize
6
+ @shapes = []
7
+ end
8
+
9
+ #
10
+ #
11
+ def add shape
12
+ @shapes << shape
13
+ end
14
+
15
+ #
16
+ #
17
+ def remove_from environment, moveables
18
+ # This iterator makes an assumption of one Shape per Star making it safe to remove
19
+ # each Shape's Body as it comes up
20
+ # If our Stars had multiple Shapes, as would be required if we were to meticulously
21
+ # define their true boundaries, we couldn't do this as we would remove the Body
22
+ # multiple times
23
+ # We would probably solve this by creating a separate @remove_bodies array to remove the Bodies
24
+ # of the Stars that were gathered by the Player
25
+ #
26
+ return if @shapes.empty?
27
+ @shapes.each do |shape|
28
+ environment.remove_body shape.body
29
+ environment.remove_shape shape
30
+ moveables.remove shape
31
+ end
32
+ @shapes.clear
33
+ end
34
+
35
+ end
@@ -28,6 +28,8 @@ require 'trait'
28
28
  require 'traits'
29
29
  require 'it_is_a'
30
30
 
31
+ require 'moveables'
32
+ require 'remove_shapes'
31
33
  require 'scheduling'
32
34
  require 'game_window'
33
35
  require 'controls'
@@ -18,7 +18,7 @@ describe GameWindow do
18
18
  @window = GameWindowTest.new
19
19
  end
20
20
 
21
- describe "gravity_vector" do
21
+ describe "gravity and gravity_vector" do
22
22
  context 'default' do
23
23
  it "should have a calculated value" do
24
24
  @window.gravity_vector.x.should == 0.0
@@ -27,14 +27,12 @@ describe GameWindow do
27
27
  end
28
28
  context 'user defined' do
29
29
  before(:each) do
30
- GameWindowTest.class_eval do
31
- gravity 100
32
- end
30
+ GameWindowTest.gravity 100
33
31
  @window = GameWindowTest.new
34
32
  end
35
33
  it "should have a user defined value" do
36
34
  @window.gravity_vector.x.should == 0.0
37
- @window.gravity_vector.y.should == 10.0
35
+ @window.gravity_vector.y.should == (100/SUBSTEPS)
38
36
  end
39
37
  end
40
38
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 17
9
- version: 0.1.17
8
+ - 18
9
+ version: 0.1.18
10
10
  platform: ruby
11
11
  authors:
12
12
  - Florian Hanke
@@ -58,6 +58,8 @@ files:
58
58
  - lib/core/initializer_hooks.rb
59
59
  - lib/core/it_is_a.rb
60
60
  - lib/core/layer.rb
61
+ - lib/core/moveables.rb
62
+ - lib/core/remove_shapes.rb
61
63
  - lib/core/resources.rb
62
64
  - lib/core/scheduling.rb
63
65
  - lib/core/trait.rb