gosu_extensions 0.2.9 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.9
1
+ 0.3.0
@@ -14,6 +14,7 @@ class Background
14
14
  end
15
15
 
16
16
  def functionify path_or_color
17
+ return lambda {} unless path_or_color
17
18
  Gosu::Color === path_or_color ? color_draw_function(path_or_color) : image_draw_function(path_or_color)
18
19
  end
19
20
 
@@ -9,12 +9,12 @@ class Collision
9
9
 
10
10
  #
11
11
  #
12
- attr_reader :window, :this, :that, :definition
12
+ attr_reader :things, :this, :that, :definition
13
13
 
14
+ # Things can collide.
14
15
  #
15
- #
16
- def initialize window, this, that = this, &definition
17
- @window = window # TODO Remove.
16
+ def initialize things, this, that = this, &definition
17
+ @things = things
18
18
  @this = this
19
19
  @that = that
20
20
  @definition = definition && package(definition)
@@ -35,9 +35,9 @@ class Collision
35
35
  #
36
36
  def simple_package definition
37
37
  lambda do |this_shape, _|
38
- window.moveables.each do |moveable|
39
- if moveable.shape == this_shape
40
- moveable.instance_eval &definition
38
+ things.each do |thing|
39
+ if thing.shape == this_shape
40
+ thing.instance_eval &definition
41
41
  break
42
42
  end
43
43
  end
@@ -49,13 +49,13 @@ class Collision
49
49
  def complex_package definition
50
50
  lambda do |this_shape, that_shape|
51
51
  this_that = Array.new 2
52
- window.moveables.each do |moveable|
53
- if moveable.shape == this_shape
54
- this_that[0] = moveable
52
+ things.each do |thing|
53
+ if thing.shape == this_shape
54
+ this_that[0] = thing
55
55
  break if this_that.all?
56
56
  end
57
- if moveable.shape == that_shape
58
- this_that[1] = moveable
57
+ if thing.shape == that_shape
58
+ this_that[1] = thing
59
59
  break if this_that.all?
60
60
  end
61
61
  end
@@ -2,7 +2,8 @@ class Environment < CP::Space
2
2
 
3
3
  # Remove body and shape from self.
4
4
  #
5
- def remove shape
5
+ def remove thing
6
+ shape = thing.shape
6
7
  self.remove_body shape.body
7
8
  self.remove_shape shape
8
9
  end
@@ -28,17 +28,24 @@ class GameWindow < Gosu::Window
28
28
  :gravity_vector,
29
29
  :background_options
30
30
  attr_reader :environment,
31
- :moveables,
31
+ :sprites,
32
+ :things,
33
+ :objects,
32
34
  :font,
33
- :scheduling,
34
- :collisions
35
+ :scheduling
35
36
  attr_accessor :stop_condition,
36
- :proceed_condition
37
+ :proceed_condition,
38
+ :collisions
37
39
 
38
40
  def initialize
39
41
  setup_window
40
- setup_moveables
41
- setup_remove_shapes
42
+
43
+ setup_environment
44
+
45
+ setup_sprites
46
+ setup_things
47
+ setup_objects
48
+
42
49
  setup_controls
43
50
  setup_window_control # e.g. ESC => exits
44
51
 
@@ -57,14 +64,14 @@ class GameWindow < Gosu::Window
57
64
 
58
65
  setup_containers
59
66
 
60
- setup_environment
61
-
62
67
  setup_enemies
63
68
  setup_players
64
69
  setup_waves
65
70
 
66
71
  setup_collisions
67
72
 
73
+ setup_apply_damping
74
+
68
75
  install_main_loop
69
76
 
70
77
  after_setup
@@ -77,10 +84,10 @@ class GameWindow < Gosu::Window
77
84
  # Smoother, but slower: GC.start if rand > 0.98
78
85
  next_step
79
86
  SUBSTEPS.times do
80
- remove_shapes
81
87
  move
82
88
  targeting
83
89
  handle_input
90
+ remove_marked
84
91
  step_physics
85
92
  end
86
93
  end
@@ -102,7 +109,7 @@ class GameWindow < Gosu::Window
102
109
  @font_size || 20
103
110
  end
104
111
  def damping
105
- @damping || 0.001
112
+ @damping || 0.5
106
113
  end
107
114
  def caption
108
115
  @caption || ""
@@ -117,15 +124,42 @@ class GameWindow < Gosu::Window
117
124
  @gravity_vector || @gravity_vector = CP::Vec2.new(0, 0.98/SUBSTEPS)
118
125
  end
119
126
  def background_options
120
- Gosu::Color::WHITE
127
+ # default is no background
121
128
  end
122
129
 
123
130
  class << self
131
+
132
+ # Sets the Esc button to close the window.
133
+ #
134
+ def default_controls
135
+ it_is Controllable
136
+ controls Gosu::Button::KbEscape => :close
137
+ end
138
+
139
+ # Gravity acting.
140
+ #
141
+ # If you want to set the gravity_vector on the window,
142
+ # use its writer: window.gravity_vector = CP::Vec2.new(1, 2)
143
+ #
124
144
  def gravity amount = 0.98
125
145
  InitializerHooks.register self do
126
146
  self.gravity_vector = CP::Vec2.new(0, amount.to_f/SUBSTEPS)
127
147
  end
128
148
  end
149
+ # How much is movement damped?
150
+ #
151
+ def damping amount = 0.0
152
+ InitializerHooks.register self do
153
+ self.damping = amount
154
+ end
155
+ end
156
+
157
+ # Size of the window. Use either width, height, or just size.
158
+ #
159
+ def size w = DEFAULT_SCREEN_WIDTH, h = DEFAULT_SCREEN_HEIGHT
160
+ width w
161
+ height h
162
+ end
129
163
  def width value = DEFAULT_SCREEN_WIDTH
130
164
  InitializerHooks.register self do
131
165
  self.screen_width = value
@@ -136,16 +170,15 @@ class GameWindow < Gosu::Window
136
170
  self.screen_height = value
137
171
  end
138
172
  end
173
+
174
+ # Window caption.
175
+ #
139
176
  def caption text = ""
140
177
  InitializerHooks.register self do
141
178
  self.caption = text
142
179
  end
143
180
  end
144
- def damping amount = 0.0
145
- InitializerHooks.register self do
146
- self.damping = amount
147
- end
148
- end
181
+
149
182
  def font name = Gosu::default_font_name, size = 20
150
183
  InitializerHooks.register self do
151
184
  self.font_name = name
@@ -164,18 +197,17 @@ class GameWindow < Gosu::Window
164
197
  end
165
198
  end
166
199
 
167
- attr_accessor :collisions
168
200
  def no_collision this, that = this
169
201
  # The next line doesn't work, as &nil == nil
170
202
  # collision this, that, &Collision::None
171
203
  InitializerHooks.register self do
172
- self.collisions << Collision.new(self, this, that)
204
+ self.collisions << Collision.new(things, this, that)
173
205
  end
174
206
  end
175
207
  def collision this, that = this, &definition
176
208
  definition ||= Collision::Simple
177
209
  InitializerHooks.register self do
178
- self.collisions << Collision.new(self, this, that, &definition)
210
+ self.collisions << Collision.new(things, this, that, &definition)
179
211
  end
180
212
  end
181
213
 
@@ -210,11 +242,14 @@ class GameWindow < Gosu::Window
210
242
  def setup_background
211
243
  @background = Background.new self
212
244
  end
213
- def setup_moveables
214
- @moveables = Moveables.new
245
+ def setup_sprites
246
+ @sprites = Sprites.new
215
247
  end
216
- def setup_remove_shapes
217
- @remove_shapes = RemoveShapes.new
248
+ def setup_things
249
+ @things = Things.new @environment
250
+ end
251
+ def setup_objects
252
+ @objects = Objects.new things, sprites
218
253
  end
219
254
  def setup_controls
220
255
  @controls = Controls.new
@@ -223,8 +258,7 @@ class GameWindow < Gosu::Window
223
258
  add_controls_for self
224
259
  end
225
260
  def setup_containers
226
- @players = []
227
- @waves = Waves.new self, @scheduling
261
+ @waves = Waves.new self, @scheduling
228
262
  end
229
263
  def setup_steps
230
264
  @step = 0
@@ -245,7 +279,11 @@ class GameWindow < Gosu::Window
245
279
  attr_accessor :window
246
280
  end
247
281
  @environment.window = self
248
- @environment.damping = -self.damping + 1 # recalculate the damping such that 0.0 has no damping.
282
+ end
283
+ def setup_apply_damping
284
+ # recalculate the damping such that 0.0 has no damping.
285
+ #
286
+ @environment.damping = -self.damping + 1
249
287
  end
250
288
  # Callbacks.
251
289
  #
@@ -259,10 +297,6 @@ class GameWindow < Gosu::Window
259
297
 
260
298
  #
261
299
  #
262
- # Example:
263
- # collisions do
264
- # add_collision_func ...
265
- #
266
300
  def setup_collisions
267
301
  self.collisions.each { |collision| collision.install_on(environment) }
268
302
  end
@@ -324,43 +358,21 @@ class GameWindow < Gosu::Window
324
358
  # Moves each moveable.
325
359
  #
326
360
  def move
327
- @moveables.move
361
+ @objects.move
328
362
  end
329
363
  # Handles the targeting process.
330
364
  #
331
365
  def targeting
332
- @moveables.targeting
366
+ @things.targeting
333
367
  end
334
368
  # Remove the shapes that are marked for removal.
335
369
  #
336
- def remove_shapes
337
- @remove_shapes.remove_from @environment, @moveables
370
+ def remove_marked
371
+ @objects.remove_marked
338
372
  end
339
373
 
340
374
  # Adding things.
341
375
  #
342
-
343
- # Moveables register themselves here.
344
- #
345
- def register moveable
346
- @moveables.register moveable
347
- moveable.add_to @environment
348
- end
349
- # Things unregister themselves here.
350
- #
351
- # Note: Use as follows in a Thing.
352
- #
353
- # def destroy
354
- # threaded do
355
- # 5.times { sleep 0.1; animate_explosion }
356
- # @window.unregister self
357
- # end
358
- # end
359
- #
360
- def unregister thing
361
- # explicitly call unregister_ui thing if you want it
362
- remove thing.shape
363
- end
364
376
  # Register a user interfaceable object.
365
377
  #
366
378
  def register_ui thing
@@ -370,19 +382,6 @@ class GameWindow < Gosu::Window
370
382
  @uis.delete thing
371
383
  end
372
384
 
373
- # Remove this shape the next turn.
374
- #
375
- # Note: Internal use. Use unregister to properly remove a moveable.
376
- #
377
- def remove shape
378
- @remove_shapes.add shape
379
- end
380
- # Is the thing registered?
381
- #
382
- def registered? thing
383
- @moveables.registered? thing
384
- end
385
-
386
385
  # Scheduling
387
386
  #
388
387
 
@@ -396,6 +395,8 @@ class GameWindow < Gosu::Window
396
395
  # destroy!
397
396
  # end
398
397
  #
398
+ # Note: You can also use after instead of threaded.
399
+ #
399
400
  def threaded time = 1, &code
400
401
  @scheduling.add time, &code
401
402
  end
@@ -407,26 +408,25 @@ class GameWindow < Gosu::Window
407
408
 
408
409
  # Example:
409
410
  # * x, y = uniform_random_position
411
+ # * warp_to *uniform_random_position
410
412
  #
411
413
  def uniform_random_position
412
414
  [rand(self.width), rand(self.height)]
413
415
  end
414
416
  # Randomly adds a Thing to a uniform random position.
415
417
  #
418
+ # Returns the new thing
419
+ #
416
420
  def add type, x = nil, y = nil, &random_function
417
421
  thing = type.new self
418
422
  position = x && y && [x, y] || random_function && random_function[]
419
423
  thing.warp_to *position
420
- register thing
424
+ thing.show
425
+ thing
421
426
  end
422
427
  def randomly_add type
423
428
  add type, *uniform_random_position
424
429
  end
425
- # Revives the player if not already in.
426
- #
427
- def revive player
428
- register player unless registered?(player) # player.registered?
429
- end
430
430
 
431
431
  # Drawing methods
432
432
  #
@@ -436,7 +436,7 @@ class GameWindow < Gosu::Window
436
436
  def draw
437
437
  draw_background
438
438
  draw_ambient
439
- draw_moveables
439
+ draw_objects
440
440
  draw_ui
441
441
  end
442
442
  # Draws a background image.
@@ -449,13 +449,12 @@ class GameWindow < Gosu::Window
449
449
  def draw_ambient
450
450
 
451
451
  end
452
- # Draw the moveables.
452
+ # Draw the things.
453
453
  #
454
- def draw_moveables
455
- @moveables.draw
454
+ def draw_objects
455
+ @objects.draw
456
456
  end
457
- # Override for example with
458
- # @font.draw "P1 Score: ", 10, 10, ZOrder::UI, 1.0, 1.0, 0xffff0000
457
+ #
459
458
  #
460
459
  def draw_ui
461
460
  @uis.each(&:draw_ui)
@@ -0,0 +1,46 @@
1
+ # Objects is an aggregator of things and sprites.
2
+ #
3
+ class Objects
4
+
5
+ attr_reader :things, :sprites
6
+
7
+ #
8
+ #
9
+ def initialize things, sprites
10
+ @things, @sprites = things, sprites
11
+ end
12
+
13
+ # # TODO Not used?
14
+ # #
15
+ # def registered? thing_or_sprite
16
+ # @things.registered?(thing_or_sprite) || @sprites.registered?(thing_or_sprite)
17
+ # end
18
+ #
19
+ # #
20
+ # #
21
+ # def register thing_or_sprite
22
+ # Thing === thing_or_sprite ? @things.register(thing_or_sprite) : @sprites.register(thing_or_sprite)
23
+ # end
24
+
25
+ #
26
+ #
27
+ def move
28
+ @things.move
29
+ @sprites.move
30
+ end
31
+
32
+ #
33
+ #
34
+ def draw
35
+ @things.draw
36
+ @sprites.draw
37
+ end
38
+
39
+ #
40
+ #
41
+ def remove_marked
42
+ @things.remove_marked
43
+ @sprites.remove_marked
44
+ end
45
+
46
+ end