minigl 1.3.7 → 1.3.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,8 +2,8 @@ require 'gosu'
2
2
 
3
3
  # The main module of the library, used only as a namespace.
4
4
  module AGL
5
- # This class represents a point or vector in a bidimensional space.
6
- class Vector
5
+ # This class represents a point or vector in a bidimensional space.
6
+ class Vector
7
7
  # The x coordinate of the vector
8
8
  attr_accessor :x
9
9
 
@@ -85,329 +85,329 @@ module AGL
85
85
  end
86
86
  end
87
87
 
88
- # This class represents a rectangle by its x and y coordinates and width and
89
- # height.
90
- class Rectangle
91
- # The x-coordinate of the rectangle.
92
- attr_accessor :x
93
-
94
- # The y-coordinate of the rectangle.
95
- attr_accessor :y
96
-
97
- # The width of the rectangle.
98
- attr_accessor :w
99
-
100
- # The height of the rectangle.
101
- attr_accessor :h
102
-
103
- # Creates a new rectangle.
104
- #
105
- # Parameters:
106
- # [x] The x-coordinate of the rectangle.
107
- # [y] The y-coordinate of the rectangle.
108
- # [w] The width of the rectangle.
109
- # [h] The height of the rectangle.
110
- def initialize x, y, w, h
111
- @x = x; @y = y; @w = w; @h = h
112
- end
113
-
114
- # Returns whether this rectangle intersects another.
115
- #
116
- # Parameters:
117
- # [r] The rectangle to check intersection with.
118
- def intersects r
119
- @x < r.x + r.w && @x + @w > r.x && @y < r.y + r.h && @y + @h > r.y
120
- end
121
- end
122
-
123
- # The main class for a MiniGL game, holds references to globally accessible
124
- # objects and constants.
125
- class Game
126
- # Initializes a MiniGL game. This method must be called before any feature
127
- # provided by the library can be used.
128
- #
129
- # Parameters:
130
- # [window] An instance of a class which inherits from
131
- # <code>Gosu::Window</code>. This will be the game window, used
132
- # to draw everything and capture user input.
133
- # [gravity] A Vector object representing the horizontal and vertical
134
- # components of the force of gravity. Essentially, this force
135
- # will be applied to every object which calls +move+, from the
136
- # Movement module.
137
- # [kb_held_delay] The number of frames a key must be held by the user
138
- # before the "held" event (that can be checked with
139
- # <code>KB.key_held?</code>) starts to trigger.
140
- # [kb_held_interval] The interval, in frames, between each triggering of
141
- # the "held" event, after the key has been held for
142
- # more than +kb_held_delay+ frames.
143
- # [double_click_delay] The maximum interval, in frames, between two
144
- # clicks, to trigger the "double click" event
145
- # (checked with <code>Mouse.double_click?</code>).
146
- def self.initialize window, gravity = Vector.new(0, 1),
147
- kb_held_delay = 40, kb_held_interval = 5,
148
- double_click_delay = 8
149
- @@window = window
150
- @@gravity = gravity
151
- @@kb_held_delay = kb_held_delay
152
- @@kb_held_interval = kb_held_interval
153
- @@double_click_delay = double_click_delay
154
-
155
- KB.initialize
156
- Mouse.initialize
157
- Res.initialize
158
- end
159
-
160
- # Returns a reference to the game window.
161
- def self.window; @@window; end
162
-
163
- # Returns a Vector representing the force of gravity. See +initialize+ for
164
- # details.
165
- def self.gravity; @@gravity; end
166
-
167
- # Returns the value of kb_held_delay. See +initialize+ for details.
168
- def self.kb_held_delay; @@kb_held_delay; end
169
-
170
- # Returns the value of kb_held_interval. See +initialize+ for details.
171
- def self.kb_held_interval; @@kb_held_interval; end
172
-
173
- # Returns the value of double_click_delay. See +initialize+ for details.
174
- def self.double_click_delay; @@double_click_delay; end
175
- end
176
-
177
- #class JSHelper
178
-
179
- # Exposes methods for controlling keyboard events.
180
- class KB
181
- # This is called by <code>Game.initialize</code>. Don't call it
182
- # explicitly.
183
- def self.initialize
184
- @@keys = [
185
- Gosu::KbUp, Gosu::KbDown,
186
- Gosu::KbReturn, Gosu::KbEscape,
187
- Gosu::KbLeftControl, Gosu::KbRightControl,
188
- Gosu::KbLeftAlt, Gosu::KbRightAlt,
189
- Gosu::KbA, Gosu::KbB, Gosu::KbC, Gosu::KbD, Gosu::KbE, Gosu::KbF,
190
- Gosu::KbG, Gosu::KbH, Gosu::KbI, Gosu::KbJ, Gosu::KbK, Gosu::KbL,
191
- Gosu::KbM, Gosu::KbN, Gosu::KbO, Gosu::KbP, Gosu::KbQ, Gosu::KbR,
192
- Gosu::KbS, Gosu::KbT, Gosu::KbU, Gosu::KbV, Gosu::KbW, Gosu::KbX,
193
- Gosu::KbY, Gosu::KbZ, Gosu::Kb1, Gosu::Kb2, Gosu::Kb3, Gosu::Kb4,
194
- Gosu::Kb5, Gosu::Kb6, Gosu::Kb7, Gosu::Kb8, Gosu::Kb9, Gosu::Kb0,
195
- Gosu::KbNumpad1, Gosu::KbNumpad2, Gosu::KbNumpad3, Gosu::KbNumpad4,
196
- Gosu::KbNumpad5, Gosu::KbNumpad6, Gosu::KbNumpad7, Gosu::KbNumpad8,
197
- Gosu::KbNumpad9, Gosu::KbNumpad0, Gosu::KbSpace, Gosu::KbBackspace,
198
- Gosu::KbDelete, Gosu::KbLeft, Gosu::KbRight, Gosu::KbHome,
199
- Gosu::KbEnd, Gosu::KbLeftShift, Gosu::KbRightShift, Gosu::KbTab,
200
- Gosu::KbBacktick, Gosu::KbMinus, Gosu::KbEqual, Gosu::KbBracketLeft,
201
- Gosu::KbBracketRight, Gosu::KbBackslash, Gosu::KbApostrophe,
202
- Gosu::KbComma, Gosu::KbPeriod, Gosu::KbSlash
203
- ]
204
- @@down = []
205
- @@prev_down = []
206
- @@held_timer = {}
207
- @@held_interval = {}
208
- end
209
-
210
- # Updates the state of all keys.
211
- def self.update
212
- @@held_timer.each do |k, v|
213
- if v < Game.kb_held_delay; @@held_timer[k] += 1
214
- else
215
- @@held_interval[k] = 0
216
- @@held_timer.delete k
217
- end
218
- end
219
-
220
- @@held_interval.each do |k, v|
221
- if v < Game.kb_held_interval; @@held_interval[k] += 1
222
- else; @@held_interval[k] = 0; end
223
- end
224
-
225
- @@prev_down = @@down.clone
226
- @@down.clear
227
- @@keys.each do |k|
228
- if Game.window.button_down? k
229
- @@down << k
230
- @@held_timer[k] = 0 if @@prev_down.index(k).nil?
231
- elsif @@prev_down.index(k)
232
- @@held_timer.delete k
233
- @@held_interval.delete k
234
- end
235
- end
236
- end
237
-
238
- # Returns whether the given key is down in the current frame and was not
239
- # down in the frame before.
240
- #
241
- # Parameters:
242
- # [key] Code of the key to be checked. The available codes are <code>
243
- # Gosu::KbUp, Gosu::KbDown, Gosu::KbReturn, Gosu::KbEscape,
244
- # Gosu::KbLeftControl, Gosu::KbRightControl,
245
- # Gosu::KbLeftAlt, Gosu::KbRightAlt,
246
- # Gosu::KbA, Gosu::KbB, Gosu::KbC, Gosu::KbD, Gosu::KbE, Gosu::KbF,
247
- # Gosu::KbG, Gosu::KbH, Gosu::KbI, Gosu::KbJ, Gosu::KbK, Gosu::KbL,
248
- # Gosu::KbM, Gosu::KbN, Gosu::KbO, Gosu::KbP, Gosu::KbQ, Gosu::KbR,
249
- # Gosu::KbS, Gosu::KbT, Gosu::KbU, Gosu::KbV, Gosu::KbW, Gosu::KbX,
250
- # Gosu::KbY, Gosu::KbZ, Gosu::Kb1, Gosu::Kb2, Gosu::Kb3, Gosu::Kb4,
251
- # Gosu::Kb5, Gosu::Kb6, Gosu::Kb7, Gosu::Kb8, Gosu::Kb9, Gosu::Kb0,
252
- # Gosu::KbNumpad1, Gosu::KbNumpad2, Gosu::KbNumpad3, Gosu::KbNumpad4,
253
- # Gosu::KbNumpad5, Gosu::KbNumpad6, Gosu::KbNumpad7, Gosu::KbNumpad8,
254
- # Gosu::KbNumpad9, Gosu::KbNumpad0, Gosu::KbSpace, Gosu::KbBackspace,
255
- # Gosu::KbDelete, Gosu::KbLeft, Gosu::KbRight, Gosu::KbHome,
256
- # Gosu::KbEnd, Gosu::KbLeftShift, Gosu::KbRightShift, Gosu::KbTab,
257
- # Gosu::KbBacktick, Gosu::KbMinus, Gosu::KbEqual, Gosu::KbBracketLeft,
258
- # Gosu::KbBracketRight, Gosu::KbBackslash, Gosu::KbApostrophe,
259
- # Gosu::KbComma, Gosu::KbPeriod, Gosu::KbSlash</code>.
260
- def self.key_pressed? key
261
- @@prev_down.index(key).nil? and @@down.index(key)
262
- end
263
-
264
- # Returns whether the given key is down in the current frame.
265
- #
266
- # Parameters:
267
- # [key] Code of the key to be checked. See +key_pressed?+ for details.
268
- def self.key_down? key
269
- @@down.index(key)
270
- end
271
-
272
- # Returns whether the given key is not down in the current frame but was
273
- # down in the frame before.
274
- #
275
- # Parameters:
276
- # [key] Code of the key to be checked. See +key_pressed?+ for details.
277
- def self.key_released? key
278
- @@prev_down.index(key) and @@down.index(key).nil?
279
- end
280
-
281
- # Returns whether the given key is being held down. See
282
- # <code>Game.initialize</code> for details.
283
- #
284
- # Parameters:
285
- # [key] Code of the key to be checked. See +key_pressed?+ for details.
286
- def self.key_held? key
287
- @@held_interval[key] == Game.kb_held_interval
288
- end
289
- end
290
-
291
- # Exposes methods for controlling mouse events.
292
- class Mouse
293
- # This is called by <code>Game.initialize</code>. Don't call it
294
- # explicitly.
295
- def self.initialize
296
- @@down = {}
297
- @@prev_down = {}
298
- @@dbl_click = {}
299
- @@dbl_click_timer = {}
300
- end
301
-
302
- # Updates the mouse position and the state of all buttons.
303
- def self.update
304
- @@prev_down = @@down.clone
305
- @@down.clear
306
- @@dbl_click.clear
307
-
308
- @@dbl_click_timer.each do |k, v|
309
- if v < Game.double_click_delay; @@dbl_click_timer[k] += 1
310
- else; @@dbl_click_timer.delete k; end
311
- end
312
-
313
- k1 = [Gosu::MsLeft, Gosu::MsMiddle, Gosu::MsRight]
314
- k2 = [:left, :middle, :right]
315
- for i in 0..2
316
- if Game.window.button_down? k1[i]
317
- @@down[k2[i]] = true
318
- @@dbl_click[k2[i]] = true if @@dbl_click_timer[k2[i]]
319
- @@dbl_click_timer.delete k2[i]
320
- elsif @@prev_down[k2[i]]
321
- @@dbl_click_timer[k2[i]] = 0
322
- end
323
- end
324
-
325
- @@x = Game.window.mouse_x.round
326
- @@y = Game.window.mouse_y.round
327
- end
328
-
329
- # Returns the x-coordinate of the mouse cursor in the screen.
330
- def self.x; @@x; end
331
-
332
- # Returns the y-coordinate of the mouse cursor in the screen.
333
- def self.y; @@y; end
334
-
335
- # Returns whether the given button is down in the current frame and was
336
- # not down in the frame before.
337
- #
338
- # Parameters:
339
- # [btn] Button to be checked. Valid values are +:left+, +:middle+ and
340
- # +:right+
341
- def self.button_pressed? btn
342
- @@down[btn] and not @@prev_down[btn]
343
- end
344
-
345
- # Returns whether the given button is down in the current frame.
346
- #
347
- # Parameters:
348
- # [btn] Button to be checked. Valid values are +:left+, +:middle+ and
349
- # +:right+
350
- def self.button_down? btn
351
- @@down[btn]
352
- end
353
-
354
- # Returns whether the given button is not down in the current frame, but
355
- # was down in the frame before.
356
- #
357
- # Parameters:
358
- # [btn] Button to be checked. Valid values are +:left+, +:middle+ and
359
- # +:right+
360
- def self.button_released? btn
361
- @@prev_down[btn] and not @@down[btn]
362
- end
363
-
364
- # Returns whether the given button has just been double clicked.
365
- #
366
- # Parameters:
367
- # [btn] Button to be checked. Valid values are +:left+, +:middle+ and
368
- # +:right+
369
- def self.double_click? btn
370
- @@dbl_click[btn]
371
- end
372
-
373
- # Returns whether the mouse cursor is currently inside the given area.
374
- #
375
- # Parameters:
376
- # [x] The x-coordinate of the top left corner of the area.
377
- # [y] The y-coordinate of the top left corner of the area.
378
- # [w] The width of the area.
379
- # [h] The height of the area.
380
- def self.over? x, y, w, h
381
- @@x >= x and @@x < x + w and @@y >= y and @@y < y + h
382
- end
383
- end
384
-
385
- # This class is responsible for resource management. It keeps references to
386
- # all loaded resources until a call to +clear+ is made. Resources can be
387
- # loaded as global, so that their references won't be removed even when
388
- # +clear+ is called.
389
- #
390
- # It also provides an easier syntax for loading resources, assuming a
391
- # particular folder structure. All resources must be inside subdirectories
392
- # of a 'data' directory, so that you will only need to specify the type of
393
- # resource being loaded and the file name (either as string or as symbol).
394
- # There are default extensions for each type of resource, so the extension
395
- # must be specified only if the file is in a format other than the default.
396
- class Res
397
- # This is called by <code>Game.initialize</code>. Don't call it
398
- # explicitly.
399
- def self.initialize
400
- @@imgs = Hash.new
401
- @@global_imgs = Hash.new
402
- @@tilesets = Hash.new
403
- @@global_tilesets = Hash.new
404
- @@sounds = Hash.new
405
- @@global_sounds = Hash.new
406
- @@songs = Hash.new
407
- @@global_songs = Hash.new
408
- @@fonts = Hash.new
409
- @@global_fonts = Hash.new
410
- @@prefix = File.expand_path(File.dirname($0)) + '/data/'
88
+ # This class represents a rectangle by its x and y coordinates and width and
89
+ # height.
90
+ class Rectangle
91
+ # The x-coordinate of the rectangle.
92
+ attr_accessor :x
93
+
94
+ # The y-coordinate of the rectangle.
95
+ attr_accessor :y
96
+
97
+ # The width of the rectangle.
98
+ attr_accessor :w
99
+
100
+ # The height of the rectangle.
101
+ attr_accessor :h
102
+
103
+ # Creates a new rectangle.
104
+ #
105
+ # Parameters:
106
+ # [x] The x-coordinate of the rectangle.
107
+ # [y] The y-coordinate of the rectangle.
108
+ # [w] The width of the rectangle.
109
+ # [h] The height of the rectangle.
110
+ def initialize x, y, w, h
111
+ @x = x; @y = y; @w = w; @h = h
112
+ end
113
+
114
+ # Returns whether this rectangle intersects another.
115
+ #
116
+ # Parameters:
117
+ # [r] The rectangle to check intersection with.
118
+ def intersects r
119
+ @x < r.x + r.w && @x + @w > r.x && @y < r.y + r.h && @y + @h > r.y
120
+ end
121
+ end
122
+
123
+ # The main class for a MiniGL game, holds references to globally accessible
124
+ # objects and constants.
125
+ class Game
126
+ # Initializes a MiniGL game. This method must be called before any feature
127
+ # provided by the library can be used.
128
+ #
129
+ # Parameters:
130
+ # [window] An instance of a class which inherits from
131
+ # <code>Gosu::Window</code>. This will be the game window, used
132
+ # to draw everything and capture user input.
133
+ # [gravity] A Vector object representing the horizontal and vertical
134
+ # components of the force of gravity. Essentially, this force
135
+ # will be applied to every object which calls +move+, from the
136
+ # Movement module.
137
+ # [kb_held_delay] The number of frames a key must be held by the user
138
+ # before the "held" event (that can be checked with
139
+ # <code>KB.key_held?</code>) starts to trigger.
140
+ # [kb_held_interval] The interval, in frames, between each triggering of
141
+ # the "held" event, after the key has been held for
142
+ # more than +kb_held_delay+ frames.
143
+ # [double_click_delay] The maximum interval, in frames, between two
144
+ # clicks, to trigger the "double click" event
145
+ # (checked with <code>Mouse.double_click?</code>).
146
+ def self.initialize window, gravity = Vector.new(0, 1),
147
+ kb_held_delay = 40, kb_held_interval = 5,
148
+ double_click_delay = 8
149
+ @@window = window
150
+ @@gravity = gravity
151
+ @@kb_held_delay = kb_held_delay
152
+ @@kb_held_interval = kb_held_interval
153
+ @@double_click_delay = double_click_delay
154
+
155
+ KB.initialize
156
+ Mouse.initialize
157
+ Res.initialize
158
+ end
159
+
160
+ # Returns a reference to the game window.
161
+ def self.window; @@window; end
162
+
163
+ # Returns a Vector representing the force of gravity. See +initialize+ for
164
+ # details.
165
+ def self.gravity; @@gravity; end
166
+
167
+ # Returns the value of kb_held_delay. See +initialize+ for details.
168
+ def self.kb_held_delay; @@kb_held_delay; end
169
+
170
+ # Returns the value of kb_held_interval. See +initialize+ for details.
171
+ def self.kb_held_interval; @@kb_held_interval; end
172
+
173
+ # Returns the value of double_click_delay. See +initialize+ for details.
174
+ def self.double_click_delay; @@double_click_delay; end
175
+ end
176
+
177
+ #class JSHelper
178
+
179
+ # Exposes methods for controlling keyboard events.
180
+ class KB
181
+ # This is called by <code>Game.initialize</code>. Don't call it
182
+ # explicitly.
183
+ def self.initialize
184
+ @@keys = [
185
+ Gosu::KbUp, Gosu::KbDown,
186
+ Gosu::KbReturn, Gosu::KbEscape,
187
+ Gosu::KbLeftControl, Gosu::KbRightControl,
188
+ Gosu::KbLeftAlt, Gosu::KbRightAlt,
189
+ Gosu::KbA, Gosu::KbB, Gosu::KbC, Gosu::KbD, Gosu::KbE, Gosu::KbF,
190
+ Gosu::KbG, Gosu::KbH, Gosu::KbI, Gosu::KbJ, Gosu::KbK, Gosu::KbL,
191
+ Gosu::KbM, Gosu::KbN, Gosu::KbO, Gosu::KbP, Gosu::KbQ, Gosu::KbR,
192
+ Gosu::KbS, Gosu::KbT, Gosu::KbU, Gosu::KbV, Gosu::KbW, Gosu::KbX,
193
+ Gosu::KbY, Gosu::KbZ, Gosu::Kb1, Gosu::Kb2, Gosu::Kb3, Gosu::Kb4,
194
+ Gosu::Kb5, Gosu::Kb6, Gosu::Kb7, Gosu::Kb8, Gosu::Kb9, Gosu::Kb0,
195
+ Gosu::KbNumpad1, Gosu::KbNumpad2, Gosu::KbNumpad3, Gosu::KbNumpad4,
196
+ Gosu::KbNumpad5, Gosu::KbNumpad6, Gosu::KbNumpad7, Gosu::KbNumpad8,
197
+ Gosu::KbNumpad9, Gosu::KbNumpad0, Gosu::KbSpace, Gosu::KbBackspace,
198
+ Gosu::KbDelete, Gosu::KbLeft, Gosu::KbRight, Gosu::KbHome,
199
+ Gosu::KbEnd, Gosu::KbLeftShift, Gosu::KbRightShift, Gosu::KbTab,
200
+ Gosu::KbBacktick, Gosu::KbMinus, Gosu::KbEqual, Gosu::KbBracketLeft,
201
+ Gosu::KbBracketRight, Gosu::KbBackslash, Gosu::KbApostrophe,
202
+ Gosu::KbComma, Gosu::KbPeriod, Gosu::KbSlash
203
+ ]
204
+ @@down = []
205
+ @@prev_down = []
206
+ @@held_timer = {}
207
+ @@held_interval = {}
208
+ end
209
+
210
+ # Updates the state of all keys.
211
+ def self.update
212
+ @@held_timer.each do |k, v|
213
+ if v < Game.kb_held_delay; @@held_timer[k] += 1
214
+ else
215
+ @@held_interval[k] = 0
216
+ @@held_timer.delete k
217
+ end
218
+ end
219
+
220
+ @@held_interval.each do |k, v|
221
+ if v < Game.kb_held_interval; @@held_interval[k] += 1
222
+ else; @@held_interval[k] = 0; end
223
+ end
224
+
225
+ @@prev_down = @@down.clone
226
+ @@down.clear
227
+ @@keys.each do |k|
228
+ if Game.window.button_down? k
229
+ @@down << k
230
+ @@held_timer[k] = 0 if @@prev_down.index(k).nil?
231
+ elsif @@prev_down.index(k)
232
+ @@held_timer.delete k
233
+ @@held_interval.delete k
234
+ end
235
+ end
236
+ end
237
+
238
+ # Returns whether the given key is down in the current frame and was not
239
+ # down in the frame before.
240
+ #
241
+ # Parameters:
242
+ # [key] Code of the key to be checked. The available codes are <code>
243
+ # Gosu::KbUp, Gosu::KbDown, Gosu::KbReturn, Gosu::KbEscape,
244
+ # Gosu::KbLeftControl, Gosu::KbRightControl,
245
+ # Gosu::KbLeftAlt, Gosu::KbRightAlt,
246
+ # Gosu::KbA, Gosu::KbB, Gosu::KbC, Gosu::KbD, Gosu::KbE, Gosu::KbF,
247
+ # Gosu::KbG, Gosu::KbH, Gosu::KbI, Gosu::KbJ, Gosu::KbK, Gosu::KbL,
248
+ # Gosu::KbM, Gosu::KbN, Gosu::KbO, Gosu::KbP, Gosu::KbQ, Gosu::KbR,
249
+ # Gosu::KbS, Gosu::KbT, Gosu::KbU, Gosu::KbV, Gosu::KbW, Gosu::KbX,
250
+ # Gosu::KbY, Gosu::KbZ, Gosu::Kb1, Gosu::Kb2, Gosu::Kb3, Gosu::Kb4,
251
+ # Gosu::Kb5, Gosu::Kb6, Gosu::Kb7, Gosu::Kb8, Gosu::Kb9, Gosu::Kb0,
252
+ # Gosu::KbNumpad1, Gosu::KbNumpad2, Gosu::KbNumpad3, Gosu::KbNumpad4,
253
+ # Gosu::KbNumpad5, Gosu::KbNumpad6, Gosu::KbNumpad7, Gosu::KbNumpad8,
254
+ # Gosu::KbNumpad9, Gosu::KbNumpad0, Gosu::KbSpace, Gosu::KbBackspace,
255
+ # Gosu::KbDelete, Gosu::KbLeft, Gosu::KbRight, Gosu::KbHome,
256
+ # Gosu::KbEnd, Gosu::KbLeftShift, Gosu::KbRightShift, Gosu::KbTab,
257
+ # Gosu::KbBacktick, Gosu::KbMinus, Gosu::KbEqual, Gosu::KbBracketLeft,
258
+ # Gosu::KbBracketRight, Gosu::KbBackslash, Gosu::KbApostrophe,
259
+ # Gosu::KbComma, Gosu::KbPeriod, Gosu::KbSlash</code>.
260
+ def self.key_pressed? key
261
+ @@prev_down.index(key).nil? and @@down.index(key)
262
+ end
263
+
264
+ # Returns whether the given key is down in the current frame.
265
+ #
266
+ # Parameters:
267
+ # [key] Code of the key to be checked. See +key_pressed?+ for details.
268
+ def self.key_down? key
269
+ @@down.index(key)
270
+ end
271
+
272
+ # Returns whether the given key is not down in the current frame but was
273
+ # down in the frame before.
274
+ #
275
+ # Parameters:
276
+ # [key] Code of the key to be checked. See +key_pressed?+ for details.
277
+ def self.key_released? key
278
+ @@prev_down.index(key) and @@down.index(key).nil?
279
+ end
280
+
281
+ # Returns whether the given key is being held down. See
282
+ # <code>Game.initialize</code> for details.
283
+ #
284
+ # Parameters:
285
+ # [key] Code of the key to be checked. See +key_pressed?+ for details.
286
+ def self.key_held? key
287
+ @@held_interval[key] == Game.kb_held_interval
288
+ end
289
+ end
290
+
291
+ # Exposes methods for controlling mouse events.
292
+ class Mouse
293
+ # This is called by <code>Game.initialize</code>. Don't call it
294
+ # explicitly.
295
+ def self.initialize
296
+ @@down = {}
297
+ @@prev_down = {}
298
+ @@dbl_click = {}
299
+ @@dbl_click_timer = {}
300
+ end
301
+
302
+ # Updates the mouse position and the state of all buttons.
303
+ def self.update
304
+ @@prev_down = @@down.clone
305
+ @@down.clear
306
+ @@dbl_click.clear
307
+
308
+ @@dbl_click_timer.each do |k, v|
309
+ if v < Game.double_click_delay; @@dbl_click_timer[k] += 1
310
+ else; @@dbl_click_timer.delete k; end
311
+ end
312
+
313
+ k1 = [Gosu::MsLeft, Gosu::MsMiddle, Gosu::MsRight]
314
+ k2 = [:left, :middle, :right]
315
+ for i in 0..2
316
+ if Game.window.button_down? k1[i]
317
+ @@down[k2[i]] = true
318
+ @@dbl_click[k2[i]] = true if @@dbl_click_timer[k2[i]]
319
+ @@dbl_click_timer.delete k2[i]
320
+ elsif @@prev_down[k2[i]]
321
+ @@dbl_click_timer[k2[i]] = 0
322
+ end
323
+ end
324
+
325
+ @@x = Game.window.mouse_x.round
326
+ @@y = Game.window.mouse_y.round
327
+ end
328
+
329
+ # Returns the x-coordinate of the mouse cursor in the screen.
330
+ def self.x; @@x; end
331
+
332
+ # Returns the y-coordinate of the mouse cursor in the screen.
333
+ def self.y; @@y; end
334
+
335
+ # Returns whether the given button is down in the current frame and was
336
+ # not down in the frame before.
337
+ #
338
+ # Parameters:
339
+ # [btn] Button to be checked. Valid values are +:left+, +:middle+ and
340
+ # +:right+
341
+ def self.button_pressed? btn
342
+ @@down[btn] and not @@prev_down[btn]
343
+ end
344
+
345
+ # Returns whether the given button is down in the current frame.
346
+ #
347
+ # Parameters:
348
+ # [btn] Button to be checked. Valid values are +:left+, +:middle+ and
349
+ # +:right+
350
+ def self.button_down? btn
351
+ @@down[btn]
352
+ end
353
+
354
+ # Returns whether the given button is not down in the current frame, but
355
+ # was down in the frame before.
356
+ #
357
+ # Parameters:
358
+ # [btn] Button to be checked. Valid values are +:left+, +:middle+ and
359
+ # +:right+
360
+ def self.button_released? btn
361
+ @@prev_down[btn] and not @@down[btn]
362
+ end
363
+
364
+ # Returns whether the given button has just been double clicked.
365
+ #
366
+ # Parameters:
367
+ # [btn] Button to be checked. Valid values are +:left+, +:middle+ and
368
+ # +:right+
369
+ def self.double_click? btn
370
+ @@dbl_click[btn]
371
+ end
372
+
373
+ # Returns whether the mouse cursor is currently inside the given area.
374
+ #
375
+ # Parameters:
376
+ # [x] The x-coordinate of the top left corner of the area.
377
+ # [y] The y-coordinate of the top left corner of the area.
378
+ # [w] The width of the area.
379
+ # [h] The height of the area.
380
+ def self.over? x, y, w, h
381
+ @@x >= x and @@x < x + w and @@y >= y and @@y < y + h
382
+ end
383
+ end
384
+
385
+ # This class is responsible for resource management. It keeps references to
386
+ # all loaded resources until a call to +clear+ is made. Resources can be
387
+ # loaded as global, so that their references won't be removed even when
388
+ # +clear+ is called.
389
+ #
390
+ # It also provides an easier syntax for loading resources, assuming a
391
+ # particular folder structure. All resources must be inside subdirectories
392
+ # of a 'data' directory, so that you will only need to specify the type of
393
+ # resource being loaded and the file name (either as string or as symbol).
394
+ # There are default extensions for each type of resource, so the extension
395
+ # must be specified only if the file is in a format other than the default.
396
+ class Res
397
+ # This is called by <code>Game.initialize</code>. Don't call it
398
+ # explicitly.
399
+ def self.initialize
400
+ @@imgs = Hash.new
401
+ @@global_imgs = Hash.new
402
+ @@tilesets = Hash.new
403
+ @@global_tilesets = Hash.new
404
+ @@sounds = Hash.new
405
+ @@global_sounds = Hash.new
406
+ @@songs = Hash.new
407
+ @@global_songs = Hash.new
408
+ @@fonts = Hash.new
409
+ @@global_fonts = Hash.new
410
+ @@prefix = File.expand_path(File.dirname($0)) + '/data/'
411
411
  end
412
412
 
413
413
  # Get the current prefix for searching data files. This is the directory
@@ -422,149 +422,149 @@ module AGL
422
422
  @@prefix = value
423
423
  end
424
424
 
425
- # Returns a <code>Gosu::Image</code> object.
426
- #
427
- # Parameters:
428
- # [id] A string or symbol representing the path to the image. If the file
429
- # is inside 'data/img', only the file name is needed. If it's inside
430
- # a subdirectory, the id must be prefixed by each subdirectory name
431
- # followed by an underscore. Example: to load
432
- # 'data/img/sprite/1.png', provide +:sprite_1+ or "sprite_1".
433
- # [global] Set to true if you want to keep the image in memory until the
434
- # game execution is finished. If false, the image will be
435
- # released when you call +clear+.
436
- # [tileable] Whether the image should be loaded in tileable mode, which is
437
- # proper for images that will be used as a tile, i.e., that
438
- # will be drawn repeated times, side by side, forming a
439
- # continuous composition.
440
- # [ext] The extension of the file being loaded. Specify only if it is
441
- # other than ".png".
442
- def self.img id, global = false, tileable = false, ext = ".png"
443
- if global; a = @@global_imgs; else; a = @@imgs; end
444
- return a[id] if a[id]
445
- s = @@prefix + "img/" + id.to_s.split('_').join('/') + ext
446
- img = Gosu::Image.new Game.window, s, tileable
447
- a[id] = img
448
- end
449
-
450
- # Returns an array of <code>Gosu::Image</code> objects, using the image as
451
- # a spritesheet. The image with index 0 will be the top left sprite, and
452
- # the following indices raise first from left to right and then from top
453
- # to bottom.
454
- #
455
- # Parameters:
456
- # [id] A string or symbol representing the path to the image. See +img+
457
- # for details.
458
- # [sprite_cols] Number of columns in the spritesheet.
459
- # [sprite_rows] Number of rows in the spritesheet.
460
- # [global] Set to true if you want to keep the image in memory until the
461
- # game execution is finished. If false, the image will be
462
- # released when you call +clear+.
463
- # [ext] The extension of the file being loaded. Specify only if it is
464
- # other than ".png".
465
- def self.imgs id, sprite_cols, sprite_rows, global = false, ext = ".png"
466
- if global; a = @@global_imgs; else; a = @@imgs; end
467
- return a[id] if a[id]
468
- s = @@prefix + "img/" + id.to_s.split('_').join('/') + ext
469
- imgs = Gosu::Image.load_tiles Game.window, s, -sprite_cols, -sprite_rows, false
470
- a[id] = imgs
471
- end
472
-
473
- # Returns an array of <code>Gosu::Image</code> objects, using the image as
474
- # a tileset. Works the same as +imgs+, except you must provide the tile
475
- # size instead of the number of columns and rows, and that the images will
476
- # be loaded as tileable.
477
- #
478
- # Parameters:
479
- # [id] A string or symbol representing the path to the image. It must be
480
- # specified the same way as in +img+, but the base directory is
481
- # 'data/tileset'.
482
- # [tile_width] Width of each tile, in pixels.
483
- # [tile_height] Height of each tile, in pixels.
484
- # [global] Set to true if you want to keep the image in memory until the
485
- # game execution is finished. If false, the image will be
486
- # released when you call +clear+.
487
- # [ext] The extension of the file being loaded. Specify only if it is
488
- # other than ".png".
489
- def self.tileset id, tile_width = 32, tile_height = 32, global = false, ext = ".png"
490
- if global; a = @@global_tilesets; else; a = @@tilesets; end
491
- return a[id] if a[id]
492
- s = @@prefix + "tileset/" + id.to_s.split('_').join('/') + ext
493
- tileset = Gosu::Image.load_tiles Game.window, s, tile_width, tile_height, true
494
- a[id] = tileset
495
- end
496
-
497
- # Returns a <code>Gosu::Sample</code> object. This should be used for
498
- # simple and short sound effects.
499
- #
500
- # Parameters:
501
- # [id] A string or symbol representing the path to the sound. It must be
502
- # specified the same way as in +img+, but the base directory is
503
- # 'data/sound'.
504
- # [global] Set to true if you want to keep the sound in memory until the
505
- # game execution is finished. If false, the sound will be
506
- # released when you call +clear+.
507
- # [ext] The extension of the file being loaded. Specify only if it is
508
- # other than ".wav".
509
- def self.sound id, global = false, ext = ".wav"
510
- if global; a = @@global_sounds; else; a = @@sounds; end
511
- return a[id] if a[id]
512
- s = @@prefix + "sound/" + id.to_s.split('_').join('/') + ext
513
- sound = Gosu::Sample.new Game.window, s
514
- a[id] = sound
515
- end
516
-
517
- # Returns a <code>Gosu::Song</code> object. This should be used for the
518
- # background musics of your game.
519
- #
520
- # Parameters:
521
- # [id] A string or symbol representing the path to the song. It must be
522
- # specified the same way as in +img+, but the base directory is
523
- # 'data/song'.
524
- # [global] Set to true if you want to keep the song in memory until the
525
- # game execution is finished. If false, the song will be released
526
- # when you call +clear+.
527
- # [ext] The extension of the file being loaded. Specify only if it is
528
- # other than ".ogg".
529
- def self.song id, global = false, ext = ".ogg"
530
- if global; a = @@global_songs; else; a = @@songs; end
531
- return a[id] if a[id]
532
- s = @@prefix + "song/" + id.to_s.split('_').join('/') + ext
533
- song = Gosu::Song.new Game.window, s
534
- a[id] = song
535
- end
536
-
537
- # Returns a <code>Gosu::Font</code> object. Fonts are needed to draw text
538
- # and used by MiniGL elements like buttons, text fields and TextHelper
539
- # objects.
540
- #
541
- # Parameters:
542
- # [id] A string or symbol representing the path to the song. It must be
543
- # specified the same way as in +img+, but the base directory is
544
- # 'data/font'.
545
- # [size] The size of the font, in pixels. This will correspond,
546
- # approximately, to the height of the tallest character when drawn.
547
- # [global] Set to true if you want to keep the font in memory until the
548
- # game execution is finished. If false, the font will be released
549
- # when you call +clear+.
550
- # [ext] The extension of the file being loaded. Specify only if it is
551
- # other than ".ttf".
552
- def self.font id, size, global = true, ext = ".ttf"
553
- if global; a = @@global_fonts; else; a = @@fonts; end
554
- id_size = "#{id}_#{size}"
555
- return a[id_size] if a[id_size]
556
- s = @@prefix + "font/" + id.to_s.split('_').join('/') + ext
557
- font = Gosu::Font.new Game.window, s, size
558
- a[id_size] = font
559
- end
560
-
561
- # Releases the memory used by all non-global resources.
562
- def self.clear
563
- @@imgs.clear
564
- @@tilesets.clear
565
- @@sounds.clear
566
- @@songs.clear
567
- @@fonts.clear
568
- end
569
- end
425
+ # Returns a <code>Gosu::Image</code> object.
426
+ #
427
+ # Parameters:
428
+ # [id] A string or symbol representing the path to the image. If the file
429
+ # is inside 'data/img', only the file name is needed. If it's inside
430
+ # a subdirectory, the id must be prefixed by each subdirectory name
431
+ # followed by an underscore. Example: to load
432
+ # 'data/img/sprite/1.png', provide +:sprite_1+ or "sprite_1".
433
+ # [global] Set to true if you want to keep the image in memory until the
434
+ # game execution is finished. If false, the image will be
435
+ # released when you call +clear+.
436
+ # [tileable] Whether the image should be loaded in tileable mode, which is
437
+ # proper for images that will be used as a tile, i.e., that
438
+ # will be drawn repeated times, side by side, forming a
439
+ # continuous composition.
440
+ # [ext] The extension of the file being loaded. Specify only if it is
441
+ # other than ".png".
442
+ def self.img id, global = false, tileable = false, ext = ".png"
443
+ if global; a = @@global_imgs; else; a = @@imgs; end
444
+ return a[id] if a[id]
445
+ s = @@prefix + "img/" + id.to_s.split('_').join('/') + ext
446
+ img = Gosu::Image.new Game.window, s, tileable
447
+ a[id] = img
448
+ end
449
+
450
+ # Returns an array of <code>Gosu::Image</code> objects, using the image as
451
+ # a spritesheet. The image with index 0 will be the top left sprite, and
452
+ # the following indices raise first from left to right and then from top
453
+ # to bottom.
454
+ #
455
+ # Parameters:
456
+ # [id] A string or symbol representing the path to the image. See +img+
457
+ # for details.
458
+ # [sprite_cols] Number of columns in the spritesheet.
459
+ # [sprite_rows] Number of rows in the spritesheet.
460
+ # [global] Set to true if you want to keep the image in memory until the
461
+ # game execution is finished. If false, the image will be
462
+ # released when you call +clear+.
463
+ # [ext] The extension of the file being loaded. Specify only if it is
464
+ # other than ".png".
465
+ def self.imgs id, sprite_cols, sprite_rows, global = false, ext = ".png"
466
+ if global; a = @@global_imgs; else; a = @@imgs; end
467
+ return a[id] if a[id]
468
+ s = @@prefix + "img/" + id.to_s.split('_').join('/') + ext
469
+ imgs = Gosu::Image.load_tiles Game.window, s, -sprite_cols, -sprite_rows, false
470
+ a[id] = imgs
471
+ end
472
+
473
+ # Returns an array of <code>Gosu::Image</code> objects, using the image as
474
+ # a tileset. Works the same as +imgs+, except you must provide the tile
475
+ # size instead of the number of columns and rows, and that the images will
476
+ # be loaded as tileable.
477
+ #
478
+ # Parameters:
479
+ # [id] A string or symbol representing the path to the image. It must be
480
+ # specified the same way as in +img+, but the base directory is
481
+ # 'data/tileset'.
482
+ # [tile_width] Width of each tile, in pixels.
483
+ # [tile_height] Height of each tile, in pixels.
484
+ # [global] Set to true if you want to keep the image in memory until the
485
+ # game execution is finished. If false, the image will be
486
+ # released when you call +clear+.
487
+ # [ext] The extension of the file being loaded. Specify only if it is
488
+ # other than ".png".
489
+ def self.tileset id, tile_width = 32, tile_height = 32, global = false, ext = ".png"
490
+ if global; a = @@global_tilesets; else; a = @@tilesets; end
491
+ return a[id] if a[id]
492
+ s = @@prefix + "tileset/" + id.to_s.split('_').join('/') + ext
493
+ tileset = Gosu::Image.load_tiles Game.window, s, tile_width, tile_height, true
494
+ a[id] = tileset
495
+ end
496
+
497
+ # Returns a <code>Gosu::Sample</code> object. This should be used for
498
+ # simple and short sound effects.
499
+ #
500
+ # Parameters:
501
+ # [id] A string or symbol representing the path to the sound. It must be
502
+ # specified the same way as in +img+, but the base directory is
503
+ # 'data/sound'.
504
+ # [global] Set to true if you want to keep the sound in memory until the
505
+ # game execution is finished. If false, the sound will be
506
+ # released when you call +clear+.
507
+ # [ext] The extension of the file being loaded. Specify only if it is
508
+ # other than ".wav".
509
+ def self.sound id, global = false, ext = ".wav"
510
+ if global; a = @@global_sounds; else; a = @@sounds; end
511
+ return a[id] if a[id]
512
+ s = @@prefix + "sound/" + id.to_s.split('_').join('/') + ext
513
+ sound = Gosu::Sample.new Game.window, s
514
+ a[id] = sound
515
+ end
516
+
517
+ # Returns a <code>Gosu::Song</code> object. This should be used for the
518
+ # background musics of your game.
519
+ #
520
+ # Parameters:
521
+ # [id] A string or symbol representing the path to the song. It must be
522
+ # specified the same way as in +img+, but the base directory is
523
+ # 'data/song'.
524
+ # [global] Set to true if you want to keep the song in memory until the
525
+ # game execution is finished. If false, the song will be released
526
+ # when you call +clear+.
527
+ # [ext] The extension of the file being loaded. Specify only if it is
528
+ # other than ".ogg".
529
+ def self.song id, global = false, ext = ".ogg"
530
+ if global; a = @@global_songs; else; a = @@songs; end
531
+ return a[id] if a[id]
532
+ s = @@prefix + "song/" + id.to_s.split('_').join('/') + ext
533
+ song = Gosu::Song.new Game.window, s
534
+ a[id] = song
535
+ end
536
+
537
+ # Returns a <code>Gosu::Font</code> object. Fonts are needed to draw text
538
+ # and used by MiniGL elements like buttons, text fields and TextHelper
539
+ # objects.
540
+ #
541
+ # Parameters:
542
+ # [id] A string or symbol representing the path to the song. It must be
543
+ # specified the same way as in +img+, but the base directory is
544
+ # 'data/font'.
545
+ # [size] The size of the font, in pixels. This will correspond,
546
+ # approximately, to the height of the tallest character when drawn.
547
+ # [global] Set to true if you want to keep the font in memory until the
548
+ # game execution is finished. If false, the font will be released
549
+ # when you call +clear+.
550
+ # [ext] The extension of the file being loaded. Specify only if it is
551
+ # other than ".ttf".
552
+ def self.font id, size, global = true, ext = ".ttf"
553
+ if global; a = @@global_fonts; else; a = @@fonts; end
554
+ id_size = "#{id}_#{size}"
555
+ return a[id_size] if a[id_size]
556
+ s = @@prefix + "font/" + id.to_s.split('_').join('/') + ext
557
+ font = Gosu::Font.new Game.window, s, size
558
+ a[id_size] = font
559
+ end
560
+
561
+ # Releases the memory used by all non-global resources.
562
+ def self.clear
563
+ @@imgs.clear
564
+ @@tilesets.clear
565
+ @@sounds.clear
566
+ @@songs.clear
567
+ @@fonts.clear
568
+ end
569
+ end
570
570
  end