gosu_extensions 0.1.7 → 0.1.8

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.1.7
1
+ 0.1.8
data/lib/game_window.rb CHANGED
@@ -7,6 +7,17 @@ class GameWindow < Gosu::Window
7
7
 
8
8
  include InitializerHooks
9
9
 
10
+ # TODO handle more elegantly
11
+ #
12
+ def window
13
+ self
14
+ end
15
+ def destroyed?
16
+ false
17
+ end
18
+
19
+ include ItIsA # Move up to standard object, also from thing
20
+
10
21
  attr_writer :full_screen,
11
22
  :font_name,
12
23
  :font_size,
@@ -22,13 +33,16 @@ class GameWindow < Gosu::Window
22
33
  :background_hard_borders
23
34
 
24
35
  def initialize
36
+ setup_window
37
+ setup_containers
38
+
25
39
  after_initialize
26
40
 
27
41
  super self.screen_width, self.screen_height, self.full_screen, 16
28
42
 
29
- setup_window
30
43
  setup_background
31
- setup_containers
44
+ setup_menu
45
+
32
46
  setup_steps
33
47
  setup_waves
34
48
  setup_scheduling
@@ -38,6 +52,45 @@ class GameWindow < Gosu::Window
38
52
  setup_enemies
39
53
  setup_players
40
54
  setup_collisions
55
+
56
+ install_main_loop
57
+ end
58
+
59
+ def main_loop
60
+ @main_loop ||= lambda do
61
+ next_step
62
+ # Step the physics environment SUBSTEPS times each update.
63
+ #
64
+ SUBSTEPS.times do
65
+ remove_shapes!
66
+ reset_forces
67
+ move_all
68
+ targeting
69
+ handle_input
70
+ step_physics
71
+ end
72
+ end
73
+ end
74
+
75
+ def install_main_loop
76
+ @current_loop = main_loop
77
+ end
78
+
79
+ def show_menu
80
+ suspend
81
+ end
82
+
83
+ def suspend
84
+ return if @suspended
85
+ @current_loop = @menu.loop
86
+ @suspended = Time.now
87
+ p "suspended"
88
+ end
89
+ def continue
90
+ return unless @suspended
91
+ @current_loop = main_loop
92
+ @suspended = false
93
+ p "continued"
41
94
  end
42
95
 
43
96
  def media_path
@@ -119,6 +172,10 @@ class GameWindow < Gosu::Window
119
172
  end
120
173
  end
121
174
 
175
+ def setup_menu
176
+ @menu = Menu.new self
177
+ end
178
+
122
179
  def setup_window
123
180
  self.caption = self.class.caption || ""
124
181
  end
@@ -181,6 +238,12 @@ class GameWindow < Gosu::Window
181
238
  @controls << Controls.new(self, object)
182
239
  end
183
240
 
241
+ def next_step
242
+ @step += 1
243
+ @waves.check @step # TODO maybe the waves should move into the scheduling
244
+ @scheduling.step
245
+ end
246
+
184
247
 
185
248
  # Core methods used by the extensions "framework"
186
249
  #
@@ -190,19 +253,7 @@ class GameWindow < Gosu::Window
190
253
  # TODO implement hooks.
191
254
  #
192
255
  def update
193
- @step += 1
194
- # Step the physics environment SUBSTEPS times each update.
195
- #
196
- SUBSTEPS.times do
197
- remove_shapes!
198
- reset_forces
199
- move_all
200
- targeting
201
- handle_input
202
- step_once
203
- end
204
- @waves.check @step
205
- @scheduling.step
256
+ @current_loop.call
206
257
  end
207
258
  # Each step, this is called to handle any input.
208
259
  #
@@ -211,7 +262,7 @@ class GameWindow < Gosu::Window
211
262
  end
212
263
  # Does a single step.
213
264
  #
214
- def step_once
265
+ def step_physics
215
266
  # Perform the step over @dt period of time
216
267
  # For best performance @dt should remain consistent for the game
217
268
  @environment.step @dt
@@ -43,6 +43,7 @@ $:.unshift File.join(File.dirname(__FILE__), '/units')
43
43
  require 'thing'
44
44
 
45
45
  require 'controls'
46
+ require 'menu'
46
47
  require 'game_window'
47
48
  require 'scheduling'
48
49
  require 'waves'
data/lib/menu.rb ADDED
@@ -0,0 +1,30 @@
1
+ class Menu < Thing
2
+
3
+ include Controllable
4
+
5
+ # TODO Move away
6
+ #
7
+ controls Gosu::Button::KbP => :continue
8
+
9
+ def initialize window
10
+ @controls = []
11
+ super window
12
+ end
13
+
14
+ # TODO duplicate
15
+ #
16
+ def handle_input
17
+ @controls.each &:handle
18
+ end
19
+
20
+ def continue
21
+ window.continue
22
+ end
23
+
24
+ def loop
25
+ @loop ||= lambda do
26
+ # self.handle_input
27
+ end
28
+ end
29
+
30
+ end
@@ -13,13 +13,10 @@ module Generator
13
13
  til = options[:until]
14
14
  offset = options[:starting_at]
15
15
 
16
- define_method :generated_class do
17
- klass
18
- end
19
16
  class_eval <<-GENERATION
20
- def start_generating! every_rate = #{rate || 10}, til = #{til || false}, offset = #{offset || rate || 10}
17
+ def start_generating klass = #{klass.name}, every_rate = #{rate || 10}, til = #{til || false}, offset = #{offset || rate || 10}
21
18
  return if til && til <= 0
22
- threaded offset, &generation(generated_class, every_rate, til)
19
+ threaded offset, &generation(klass, every_rate, til)
23
20
  end
24
21
  GENERATION
25
22
 
@@ -27,7 +24,7 @@ end
27
24
  #
28
25
  if offset
29
26
  InitializerHooks.register self do
30
- start_generating!
27
+ start_generating
31
28
  end
32
29
  end
33
30
 
@@ -51,7 +48,7 @@ end
51
48
  lambda do
52
49
  self.generate klass
53
50
  til = til - every_rate if til
54
- self.start_generating! every_rate, til, every_rate
51
+ self.start_generating klass, every_rate, til, every_rate
55
52
  end
56
53
  end
57
54
 
data/lib/units/thing.rb CHANGED
@@ -92,10 +92,11 @@ class Thing
92
92
 
93
93
  # Plays a random sound of the given sounds.
94
94
  #
95
- def plays *paths
95
+ def plays paths, options = {}
96
+ paths = [*paths]
96
97
  InitializerHooks.register self do
97
98
  sound = Gosu::Sample.new self.window, File.join(Resources.root, paths[rand(paths.size)])
98
- sound.play
99
+ sound.play options[:volume] || 1.0
99
100
  end
100
101
  end
101
102
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 7
9
- version: 0.1.7
8
+ - 8
9
+ version: 0.1.8
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-03-19 00:00:00 +01:00
17
+ date: 2010-03-24 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -58,7 +58,7 @@ files:
58
58
  - lib/game_window.rb
59
59
  - lib/gosu_extensions.rb
60
60
  - lib/layer.rb
61
- - lib/old_game_window.rb
61
+ - lib/menu.rb
62
62
  - lib/resources.rb
63
63
  - lib/scheduling.rb
64
64
  - lib/traits/attachable.rb
@@ -1,249 +0,0 @@
1
- # Extend this class for your game.
2
- #
3
- # Example:
4
- # class MyGreatGame < GameWindow
5
- #
6
- class GameWindow < Gosu::Window
7
-
8
- attr_reader :space, :font
9
-
10
- def initialize
11
- # set to true for fullscreen
12
- super SCREEN_WIDTH, SCREEN_HEIGHT, true, 16
13
-
14
- init
15
- setup_battlefield
16
- setup_objects
17
- setup_collisions
18
- end
19
-
20
- def init
21
- self.caption = "Incredible WWII battles!"
22
-
23
- generate_landscape
24
-
25
- @font = Gosu::Font.new self, Gosu::default_font_name, 20
26
- @moveables = []
27
- @controls = []
28
- @remove_shapes = []
29
- @players = []
30
- @waves = Waves.new self
31
- @scheduling = Scheduling.new
32
- @step = 0
33
- @dt = 1.0 / 60.0
34
- end
35
-
36
- def paint_hill
37
- x, y = uniform_random_position
38
- [80, 40, 20, 10].each do |radius|
39
- circle x, y, radius, :fill => true, :color => [5.0/radius, 0.8, 5.0/radius, 1]
40
- end
41
- end
42
-
43
- def uniform_random_position
44
- [rand(SCREEN_WIDTH), rand(SCREEN_HEIGHT)]
45
- end
46
-
47
- def generate_landscape
48
- @background_image = Gosu::Image.new self, 'media/battlefield.png', true
49
- @background_image.paint do
50
- 20.times { paint_hill }
51
- end
52
- end
53
-
54
- def setup_battlefield
55
- @battlefield = CP::Space.new
56
- @battlefield.damping = 1.0 # 0.0 # full damping?
57
- end
58
-
59
- def imprint &block
60
- @background_image.paint &block
61
- end
62
-
63
- def threaded time, code
64
- @scheduling.add time, code
65
- end
66
-
67
- def randomly_add type
68
- thing = type.new self
69
-
70
- thing.warp_to SCREEN_WIDTH, rand*SCREEN_HEIGHT
71
-
72
- register thing
73
- end
74
-
75
- def setup_objects
76
- wave 10, Enemy, 100
77
- wave 10, Enemy, 400
78
- wave 10, Enemy, 700
79
- wave 10, Enemy, 1000
80
-
81
- # add_player_units
82
- end
83
-
84
- def wave amount, type, time
85
- @waves.add amount, type, time
86
- end
87
-
88
- def small_explosion shape
89
- explosion = SmallExplosion.new self
90
- explosion.warp shape.body.p
91
- remove shape
92
- register explosion
93
- end
94
-
95
- def setup_collisions
96
- # @space.add_collision_func :projectile, :projectile, &nil
97
- # @space.add_collision_func :projectile, :enemy do |projectile_shape, enemy_shape|
98
- # @moveables.each { |projectile| projectile.shape == projectile_shape && projectile.destroy }
99
- # end
100
- end
101
-
102
- # Moveables register themselves here.
103
- #
104
- def register moveable
105
- @moveables << moveable
106
- moveable.add_to @battlefield
107
- end
108
-
109
- # Moveables unregister themselves here.
110
- #
111
- # Note: Use as follows in a Moveable.
112
- #
113
- # def destroy
114
- # threaded do
115
- # 5.times { sleep 0.1; animate_explosion }
116
- # @window.unregister self
117
- # end
118
- # end
119
- #
120
- def unregister moveable
121
- remove moveable.shape
122
- end
123
-
124
- # Remove this shape the next turn.
125
- #
126
- # Note: Internal use. Use unregister to properly remove a moveable.
127
- #
128
- def remove shape
129
- @remove_shapes << shape
130
- end
131
-
132
- # # Adds the first player.
133
- # #
134
- # def add_admiral
135
- # @player1 = Cruiser.new self, 0x99ff0000
136
- # @player1.warp_to 400, 320
137
- #
138
- # @controls << Controls.new(self, @player1,
139
- # Gosu::Button::KbA => :left,
140
- # Gosu::Button::KbD => :right,
141
- # Gosu::Button::KbW => :full_speed_ahead,
142
- # Gosu::Button::KbS => :reverse,
143
- # Gosu::Button::Kb1 => :revive
144
- # )
145
- #
146
- # @players << @player1
147
- #
148
- # register @player1
149
- # end
150
-
151
- def remove_collided
152
- # This iterator makes an assumption of one Shape per Star making it safe to remove
153
- # each Shape's Body as it comes up
154
- # If our Stars had multiple Shapes, as would be required if we were to meticulously
155
- # define their true boundaries, we couldn't do this as we would remove the Body
156
- # multiple times
157
- # We would probably solve this by creating a separate @remove_bodies array to remove the Bodies
158
- # of the Stars that were gathered by the Player
159
- #
160
- @remove_shapes.each do |shape|
161
- @space.remove_body shape.body
162
- @space.remove_shape shape
163
- @moveables.delete_if { |moveable| moveable.shape == shape && moveable.destroy }
164
- end
165
- @remove_shapes.clear
166
- end
167
-
168
- def handle_input
169
- @controls.each &:handle
170
- end
171
-
172
- def reset_forces
173
- # When a force or torque is set on a Body, it is cumulative
174
- # This means that the force you applied last SUBSTEP will compound with the
175
- # force applied this SUBSTEP; which is probably not the behavior you want
176
- # We reset the forces on the Player each SUBSTEP for this reason
177
- #
178
- # @player1.shape.body.reset_forces
179
- # @player2.shape.body.reset_forces
180
- # @player3.shape.body.reset_forces
181
- # @players.each { |player| player.shape.body.reset_forces }
182
- end
183
-
184
- # Checks whether
185
- #
186
- def validate
187
- @moveables.each &:validate_position
188
- end
189
-
190
- def step_once
191
- # Perform the step over @dt period of time
192
- # For best performance @dt should remain consistent for the game
193
- @battlefield.step @dt
194
- end
195
-
196
- def targeting
197
- @moveables.select { |m| m.respond_to? :target }.each do |gun|
198
- gun.target *@moveables.select { |m| m.kind_of? Enemy }
199
- end
200
- end
201
-
202
- # def revive player
203
- # return if @moveables.find { |moveable| moveable == player }
204
- # register player
205
- # end
206
-
207
- #
208
- #
209
- def update
210
- @step += 1
211
- # Step the physics environment SUBSTEPS times each update.
212
- #
213
- SUBSTEPS.times do
214
- remove_collided
215
- reset_forces
216
- validate
217
- targeting
218
- handle_input
219
- step_once
220
- end
221
- @waves.check @step
222
- @scheduling.step
223
- end
224
-
225
- def draw_background
226
- @background_image.draw 0, 0, ZOrder::Background, 1.5, 1.2
227
- end
228
-
229
- def draw_moveables
230
- @moveables.each &:draw
231
- end
232
-
233
- def draw_ui
234
- # @font.draw "P1 Score: ", 10, 10, ZOrder::UI, 1.0, 1.0, 0xffff0000
235
- end
236
-
237
- def draw
238
- draw_background
239
- draw_moveables
240
- draw_ui
241
- end
242
-
243
- # Escape exits.
244
- #
245
- def button_down id
246
- close if id == Gosu::Button::KbEscape
247
- end
248
-
249
- end