minigl 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +339 -0
- data/README.md +12 -0
- data/Rakefile +7 -0
- data/data/img/image.png +0 -0
- data/lib/minigl/forms.rb +365 -0
- data/lib/minigl/game_object.rb +100 -0
- data/lib/minigl/global.rb +220 -0
- data/lib/minigl/map.rb +81 -0
- data/lib/minigl/movement.rb +290 -0
- data/lib/minigl/text.rb +94 -0
- data/lib/minigl.rb +4 -0
- data/test/game_object_tests.rb +61 -0
- data/test/map_tests.rb +57 -0
- data/test/movement_tests.rb +86 -0
- metadata +80 -0
data/lib/minigl/forms.rb
ADDED
@@ -0,0 +1,365 @@
|
|
1
|
+
require_relative 'global'
|
2
|
+
|
3
|
+
module AGL
|
4
|
+
class Button
|
5
|
+
def initialize x, y, font, text, img, center = true, margin_x = 0, margin_y = 0, &action
|
6
|
+
@x = x
|
7
|
+
@y = y
|
8
|
+
@font = font
|
9
|
+
@text = text
|
10
|
+
@img = Res.imgs img, 1, 3, true
|
11
|
+
@w = @img[0].width
|
12
|
+
@h = @img[0].height
|
13
|
+
if center
|
14
|
+
@text_x = x + @w / 2
|
15
|
+
@text_y = y + @h / 2
|
16
|
+
else
|
17
|
+
@text_x = x + margin_x
|
18
|
+
@text_y = y + margin_y
|
19
|
+
end
|
20
|
+
@center = center
|
21
|
+
@action = Proc.new &action
|
22
|
+
|
23
|
+
@state = :up
|
24
|
+
@img_index = 0
|
25
|
+
end
|
26
|
+
|
27
|
+
def update
|
28
|
+
mouse_over = Mouse.over? @x, @y, @w, @h
|
29
|
+
mouse_press = Mouse.button_pressed? :left
|
30
|
+
mouse_rel = Mouse.button_released? :left
|
31
|
+
|
32
|
+
if @state == :up
|
33
|
+
if mouse_over
|
34
|
+
@img_index = 1
|
35
|
+
@state = :over
|
36
|
+
end
|
37
|
+
elsif @state == :over
|
38
|
+
if not mouse_over
|
39
|
+
@img_index = 0
|
40
|
+
@state = :up
|
41
|
+
elsif mouse_press
|
42
|
+
@img_index = 2
|
43
|
+
@state = :down
|
44
|
+
end
|
45
|
+
elsif @state == :down
|
46
|
+
if not mouse_over
|
47
|
+
@img_index = 0
|
48
|
+
@state = :down_out
|
49
|
+
elsif mouse_rel
|
50
|
+
@img_index = 0
|
51
|
+
@state = :up
|
52
|
+
@action.call
|
53
|
+
end
|
54
|
+
elsif @state == :down_out
|
55
|
+
if mouse_over
|
56
|
+
@img_index = 2
|
57
|
+
@state = :down
|
58
|
+
elsif mouse_rel
|
59
|
+
@img_index = 0
|
60
|
+
@state = :up
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def draw
|
66
|
+
@img[@img_index].draw @x, @y, 0
|
67
|
+
if @center
|
68
|
+
@font.draw_rel @text, @text_x, @text_y, 0, 0.5, 0.5, 1, 1, 0xff000000
|
69
|
+
else
|
70
|
+
@font.draw @text, @text_x, @text_y, 0, 1, 1, 0xff000000
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
class TextField
|
76
|
+
attr_reader :text
|
77
|
+
|
78
|
+
def initialize x, y, font, img, cursor_img = nil, text = "", margin_x = 0, margin_y = 0, max_length = 100
|
79
|
+
@x = x
|
80
|
+
@y = y
|
81
|
+
@font = font
|
82
|
+
@img = Res.img img
|
83
|
+
@w = @img.width
|
84
|
+
@h = @img.height
|
85
|
+
@cursor_img = Res.img(cursor_img) if cursor_img
|
86
|
+
@text = text
|
87
|
+
@text_x = x + margin_x
|
88
|
+
@text_y = y + margin_y
|
89
|
+
@max_length = max_length
|
90
|
+
|
91
|
+
@nodes = [x + margin_x]
|
92
|
+
@cur_node = 0
|
93
|
+
@cursor_visible = true
|
94
|
+
@cursor_timer = 0
|
95
|
+
|
96
|
+
@k = [
|
97
|
+
Gosu::KbA, Gosu::KbB, Gosu::KbC, Gosu::KbD, Gosu::KbE, Gosu::KbF,
|
98
|
+
Gosu::KbG, Gosu::KbH, Gosu::KbI, Gosu::KbJ, Gosu::KbK, Gosu::KbL,
|
99
|
+
Gosu::KbM, Gosu::KbN, Gosu::KbO, Gosu::KbP, Gosu::KbQ, Gosu::KbR,
|
100
|
+
Gosu::KbS, Gosu::KbT, Gosu::KbU, Gosu::KbV, Gosu::KbW, Gosu::KbX,
|
101
|
+
Gosu::KbY, Gosu::KbZ, Gosu::Kb1, Gosu::Kb2, Gosu::Kb3, Gosu::Kb4,
|
102
|
+
Gosu::Kb5, Gosu::Kb6, Gosu::Kb7, Gosu::Kb8, Gosu::Kb9, Gosu::Kb0,
|
103
|
+
Gosu::KbNumpad1, Gosu::KbNumpad2, Gosu::KbNumpad3, Gosu::KbNumpad4,
|
104
|
+
Gosu::KbNumpad5, Gosu::KbNumpad6, Gosu::KbNumpad7, Gosu::KbNumpad8,
|
105
|
+
Gosu::KbNumpad9, Gosu::KbNumpad0, Gosu::KbSpace, Gosu::KbBackspace,
|
106
|
+
Gosu::KbDelete, Gosu::KbLeft, Gosu::KbRight, Gosu::KbHome,
|
107
|
+
Gosu::KbEnd, Gosu::KbLeftShift, Gosu::KbRightShift,
|
108
|
+
Gosu::KbBacktick, Gosu::KbMinus, Gosu::KbEqual, Gosu::KbBracketLeft,
|
109
|
+
Gosu::KbBracketRight, Gosu::KbBackslash, Gosu::KbApostrophe,
|
110
|
+
Gosu::KbComma, Gosu::KbPeriod, Gosu::KbSlash
|
111
|
+
]
|
112
|
+
@chars = "abcdefghijklmnopqrstuvwxyz1234567890 ABCDEFGHIJKLMNOPQRSTUVWXYZ'-=/[]\\,.;\"_+?{}|<>:!@#$%6&*()"
|
113
|
+
end
|
114
|
+
|
115
|
+
def text= value
|
116
|
+
@text = value[0...max_length]
|
117
|
+
@nodes.clear; @nodes << (@x + @margin_x)
|
118
|
+
x = @nodes[0]
|
119
|
+
for char in @text
|
120
|
+
x += @font.text_width char
|
121
|
+
@nodes << x
|
122
|
+
end
|
123
|
+
@cur_node = @nodes.size - 1
|
124
|
+
@anchor1 = nil
|
125
|
+
@anchor2 = nil
|
126
|
+
set_cursor_visible
|
127
|
+
end
|
128
|
+
|
129
|
+
def selected_text
|
130
|
+
return "" if @anchor2.nil?
|
131
|
+
min = @anchor1 < @anchor2 ? @anchor1 : @anchor2
|
132
|
+
max = min == @anchor1 ? @anchor2 : @anchor1
|
133
|
+
@text[min..max]
|
134
|
+
end
|
135
|
+
|
136
|
+
def update
|
137
|
+
################################ Mouse ################################
|
138
|
+
if Mouse.over? @x, @y, @w, @h
|
139
|
+
if Mouse.double_click? :left
|
140
|
+
@anchor1 = 0
|
141
|
+
@anchor2 = @nodes.size - 1
|
142
|
+
@cur_node = @anchor2
|
143
|
+
set_cursor_visible
|
144
|
+
elsif Mouse.button_pressed? :left
|
145
|
+
set_node_by_mouse
|
146
|
+
@anchor1 = @cur_node
|
147
|
+
@anchor2 = nil
|
148
|
+
set_cursor_visible
|
149
|
+
end
|
150
|
+
end
|
151
|
+
if Mouse.button_down? :left
|
152
|
+
if @anchor1
|
153
|
+
set_node_by_mouse
|
154
|
+
if @cur_node != @anchor1; @anchor2 = @cur_node
|
155
|
+
else; @anchor2 = nil; end
|
156
|
+
set_cursor_visible
|
157
|
+
end
|
158
|
+
elsif Mouse.button_released? :left
|
159
|
+
if @anchor1
|
160
|
+
if @cur_node != @anchor1; @anchor2 = @cur_node
|
161
|
+
else; @anchor1 = nil; end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
@cursor_timer += 1
|
166
|
+
if @cursor_timer >= 30
|
167
|
+
@cursor_visible = (not @cursor_visible)
|
168
|
+
@cursor_timer = 0
|
169
|
+
end
|
170
|
+
|
171
|
+
############################### Keyboard ##############################
|
172
|
+
shift = KB.key_down?(@k[53]) or KB.key_down?(@k[54])
|
173
|
+
if KB.key_pressed?(@k[53]) or KB.key_pressed?(@k[54]) # shift
|
174
|
+
@anchor1 = @cur_node if @anchor1.nil?
|
175
|
+
elsif KB.key_released?(@k[53]) or KB.key_released?(@k[54])
|
176
|
+
@anchor1 = nil if @anchor2.nil?
|
177
|
+
end
|
178
|
+
inserted = false
|
179
|
+
for i in 0..46 # alnum
|
180
|
+
if KB.key_pressed?(@k[i]) or KB.key_held?(@k[i])
|
181
|
+
remove_interval if @anchor1 and @anchor2
|
182
|
+
if i < 26
|
183
|
+
# bool capsLock = Console.CapsLock;
|
184
|
+
if shift
|
185
|
+
# if (capsLock) insert_char(@chars[i]);
|
186
|
+
# else
|
187
|
+
insert_char @chars[i + 37]
|
188
|
+
else
|
189
|
+
# if (capsLock) insert_char(@chars[i + 37]);
|
190
|
+
# else
|
191
|
+
insert_char @chars[i]
|
192
|
+
end
|
193
|
+
elsif i < 36
|
194
|
+
if shift
|
195
|
+
insert_char @chars[i + 57]
|
196
|
+
else; insert_char @chars[i]; end
|
197
|
+
elsif shift
|
198
|
+
insert_char(@chars[i + 47]);
|
199
|
+
else; insert_char(@chars[i - 10]); end
|
200
|
+
inserted = true
|
201
|
+
break
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
return if inserted
|
206
|
+
for i in 55..64 # special
|
207
|
+
if KB.key_pressed?(@k[i]) or KB.key_held?(@k[i])
|
208
|
+
if shift; insert_char @chars[i + 18]
|
209
|
+
else; insert_char @chars[i + 8]; end
|
210
|
+
inserted = true
|
211
|
+
break
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
return if inserted
|
216
|
+
if KB.key_pressed?(@k[47]) or KB.key_held?(@k[47]) # back
|
217
|
+
if @anchor1 and @anchor2
|
218
|
+
remove_interval
|
219
|
+
elsif @cur_node > 0
|
220
|
+
remove_char true
|
221
|
+
end
|
222
|
+
elsif KB.key_pressed?(@k[48]) or KB.key_held?(@k[48]) # del
|
223
|
+
if @anchor1 and @anchor2
|
224
|
+
remove_interval
|
225
|
+
elsif @cur_node < @nodes.size - 1
|
226
|
+
remove_char false
|
227
|
+
end
|
228
|
+
elsif KB.key_pressed?(@k[49]) or KB.key_held?(@k[49]) # left
|
229
|
+
if @anchor1
|
230
|
+
if shift
|
231
|
+
if @cur_node > 0
|
232
|
+
@cur_node -= 1
|
233
|
+
@anchor2 = @cur_node
|
234
|
+
set_cursor_visible
|
235
|
+
end
|
236
|
+
elsif @anchor2
|
237
|
+
@cur_node = @anchor1 < @anchor2 ? @anchor1 : @anchor2
|
238
|
+
@anchor1 = nil
|
239
|
+
@anchor2 = nil
|
240
|
+
set_cursor_visible
|
241
|
+
end
|
242
|
+
elsif @cur_node > 0
|
243
|
+
@cur_node -= 1
|
244
|
+
set_cursor_visible
|
245
|
+
end
|
246
|
+
elsif KB.key_pressed?(@k[50]) or KB.key_held?(@k[50]) # right
|
247
|
+
if @anchor1
|
248
|
+
if shift
|
249
|
+
if @cur_node < @nodes.size - 1
|
250
|
+
@cur_node += 1
|
251
|
+
@anchor2 = @cur_node
|
252
|
+
set_cursor_visible
|
253
|
+
end
|
254
|
+
elsif @anchor2
|
255
|
+
@cur_node = @anchor1 > @anchor2 ? @anchor1 : @anchor2
|
256
|
+
@anchor1 = nil
|
257
|
+
@anchor2 = nil
|
258
|
+
set_cursor_visible
|
259
|
+
end
|
260
|
+
elsif @cur_node < @nodes.size - 1
|
261
|
+
@cur_node += 1
|
262
|
+
set_cursor_visible
|
263
|
+
end
|
264
|
+
elsif KB.key_pressed?(@k[51]) # home
|
265
|
+
@cur_node = 0
|
266
|
+
if shift; @anchor2 = @cur_node
|
267
|
+
else
|
268
|
+
@anchor1 = nil
|
269
|
+
@anchor2 = nil
|
270
|
+
end
|
271
|
+
set_cursor_visible
|
272
|
+
elsif KB.key_pressed?(@k[52]) # end
|
273
|
+
@cur_node = @nodes.size - 1
|
274
|
+
if shift; @anchor2 = @cur_node
|
275
|
+
else
|
276
|
+
@anchor1 = nil
|
277
|
+
@anchor2 = nil
|
278
|
+
end
|
279
|
+
set_cursor_visible
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
def set_cursor_visible
|
284
|
+
@cursor_visible = true
|
285
|
+
@cursor_timer = 0
|
286
|
+
end
|
287
|
+
|
288
|
+
def set_node_by_mouse
|
289
|
+
index = @nodes.size - 1
|
290
|
+
@nodes.each_with_index do |n, i|
|
291
|
+
if n >= Mouse.x
|
292
|
+
index = i
|
293
|
+
break
|
294
|
+
end
|
295
|
+
end
|
296
|
+
if index > 0
|
297
|
+
d1 = @nodes[index] - Mouse.x; d2 = Mouse.x - @nodes[index - 1]
|
298
|
+
index -= 1 if d1 > d2
|
299
|
+
end
|
300
|
+
@cur_node = index
|
301
|
+
end
|
302
|
+
|
303
|
+
def insert_char char
|
304
|
+
if @text.length < @max_length
|
305
|
+
@text.insert @cur_node, char
|
306
|
+
@nodes.insert @cur_node + 1, @nodes[@cur_node] + @font.text_width(char)
|
307
|
+
for i in (@cur_node + 2)..(@nodes.size - 1)
|
308
|
+
@nodes[i] += @font.text_width(char)
|
309
|
+
end
|
310
|
+
@cur_node += 1
|
311
|
+
set_cursor_visible
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
315
|
+
def remove_interval
|
316
|
+
min = @anchor1 < @anchor2 ? @anchor1 : @anchor2
|
317
|
+
max = min == @anchor1 ? @anchor2 : @anchor1
|
318
|
+
interval_width = 0
|
319
|
+
for i in min...max
|
320
|
+
interval_width += @font.text_width(@text[i])
|
321
|
+
@nodes.delete_at min + 1
|
322
|
+
end
|
323
|
+
@text[min...max] = ""
|
324
|
+
for i in (min + 1)..(@nodes.size - 1)
|
325
|
+
@nodes[i] -= interval_width
|
326
|
+
end
|
327
|
+
@cur_node = min
|
328
|
+
@anchor1 = nil
|
329
|
+
@anchor2 = nil
|
330
|
+
set_cursor_visible
|
331
|
+
end
|
332
|
+
|
333
|
+
def remove_char back
|
334
|
+
@cur_node -= 1 if back
|
335
|
+
char_width = @font.text_width(@text[@cur_node])
|
336
|
+
@text[@cur_node] = ""
|
337
|
+
@nodes.delete_at @cur_node + 1
|
338
|
+
for i in (@cur_node + 1)..(@nodes.size - 1)
|
339
|
+
@nodes[i] -= char_width
|
340
|
+
end
|
341
|
+
set_cursor_visible
|
342
|
+
end
|
343
|
+
|
344
|
+
def draw
|
345
|
+
@img.draw @x, @y, 0
|
346
|
+
if @cursor_visible
|
347
|
+
if @cursor_img; @cursor_img.draw @text_x, @text_y, 0
|
348
|
+
else
|
349
|
+
Game.window.draw_quad @nodes[@cur_node], @text_y, 0xff000000,
|
350
|
+
@nodes[@cur_node] + 1, @text_y, 0xff000000,
|
351
|
+
@nodes[@cur_node] + 1, @text_y + @font.height, 0xff000000,
|
352
|
+
@nodes[@cur_node], @text_y + @font.height, 0xff000000, 0
|
353
|
+
end
|
354
|
+
end
|
355
|
+
@font.draw @text, @text_x, @text_y, 0, 1, 1, 0xff000000
|
356
|
+
|
357
|
+
if @anchor1 and @anchor2
|
358
|
+
Game.window.draw_quad @nodes[@anchor1], @text_y, 0x80000000,
|
359
|
+
@nodes[@anchor2] + 1, @text_y, 0x80000000,
|
360
|
+
@nodes[@anchor2] + 1, @text_y + @font.height, 0x80000000,
|
361
|
+
@nodes[@anchor1], @text_y + @font.height, 0x80000000, 0
|
362
|
+
end
|
363
|
+
end
|
364
|
+
end
|
365
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require_relative 'movement'
|
2
|
+
|
3
|
+
module AGL
|
4
|
+
class Sprite
|
5
|
+
attr_reader :img_index
|
6
|
+
attr_accessor :x, :y
|
7
|
+
|
8
|
+
def initialize x, y, img, sprite_cols = nil, sprite_rows = nil
|
9
|
+
@x = x; @y = y
|
10
|
+
@img =
|
11
|
+
if sprite_cols.nil?
|
12
|
+
[Res.img(img)]
|
13
|
+
else
|
14
|
+
Res.imgs img, sprite_cols, sprite_rows
|
15
|
+
end
|
16
|
+
@anim_counter = 0
|
17
|
+
@img_index = 0
|
18
|
+
@index_index = 0
|
19
|
+
end
|
20
|
+
|
21
|
+
def animate indices, interval
|
22
|
+
@anim_counter += 1
|
23
|
+
if @anim_counter >= interval
|
24
|
+
@index_index += 1
|
25
|
+
@index_index = 0 if @index_index == indices.length
|
26
|
+
@img_index = indices[@index_index]
|
27
|
+
@anim_counter = 0
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def draw map = nil
|
32
|
+
if map
|
33
|
+
@img[@img_index].draw @x.round - map.cam.x, @y.round - map.cam.y, 0
|
34
|
+
else
|
35
|
+
@img[@img_index].draw @x.round, @y.round, 0
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class GameObject < Sprite
|
41
|
+
include Movement
|
42
|
+
|
43
|
+
def initialize x, y, w, h, img, img_gap = nil, sprite_cols = nil, sprite_rows = nil
|
44
|
+
super x, y, img, sprite_cols, sprite_rows
|
45
|
+
@w = w; @h = h
|
46
|
+
@img_gap =
|
47
|
+
if img_gap.nil?
|
48
|
+
Vector.new 0, 0
|
49
|
+
else
|
50
|
+
img_gap
|
51
|
+
end
|
52
|
+
@speed = Vector.new 0, 0
|
53
|
+
@min_speed = Vector.new 0.01, 0.01
|
54
|
+
@max_speed = Vector.new 15, 15
|
55
|
+
@stored_forces = Vector.new 0, 0
|
56
|
+
end
|
57
|
+
|
58
|
+
def set_animation index
|
59
|
+
@anim_counter = 0
|
60
|
+
@img_index = index
|
61
|
+
@index_index = 0
|
62
|
+
end
|
63
|
+
|
64
|
+
def is_visible map
|
65
|
+
return map.cam.intersects @active_bounds if @active_bounds
|
66
|
+
false
|
67
|
+
end
|
68
|
+
|
69
|
+
def draw map = nil
|
70
|
+
if map
|
71
|
+
@img[@img_index].draw @x.round + @img_gap.x - map.cam.x,
|
72
|
+
@y.round + @img_gap.y - map.cam.y, 0 if @img
|
73
|
+
else
|
74
|
+
@img[@img_index].draw @x.round + @img_gap.x, @y.round + @img_gap.y, 0 if @img
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
class Effect < Sprite
|
80
|
+
def initialize x, y, life_time, img, sprite_cols = nil, sprite_rows = nil, indices = nil, interval = 1
|
81
|
+
super x, y, img, sprite_cols, sprite_rows
|
82
|
+
@life_time = life_time
|
83
|
+
@timer = 0
|
84
|
+
if indices
|
85
|
+
@indices = indices
|
86
|
+
else
|
87
|
+
@indices = *(0..(@img.length - 1))
|
88
|
+
end
|
89
|
+
@interval = interval
|
90
|
+
end
|
91
|
+
|
92
|
+
def update
|
93
|
+
animate @indices, @interval
|
94
|
+
@timer += 1
|
95
|
+
if @timer == @life_time
|
96
|
+
@dead = true
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,220 @@
|
|
1
|
+
require 'gosu'
|
2
|
+
|
3
|
+
module AGL
|
4
|
+
Vector = Struct.new :x, :y
|
5
|
+
|
6
|
+
class Rectangle
|
7
|
+
attr_accessor :x, :y, :w, :h
|
8
|
+
|
9
|
+
def initialize x, y, w, h
|
10
|
+
@x = x; @y = y; @w = w; @h = h
|
11
|
+
end
|
12
|
+
|
13
|
+
def intersects r
|
14
|
+
@x < r.x + r.w && @x + @w > r.x && @y < r.y + r.h && @y + @h > r.y
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class Game
|
19
|
+
def self.initialize window, gravity = Vector.new(0, 1)
|
20
|
+
@@window = window
|
21
|
+
@@gravity = gravity
|
22
|
+
|
23
|
+
KB.initialize
|
24
|
+
Mouse.initialize
|
25
|
+
Res.initialize
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.window; @@window; end
|
29
|
+
def self.gravity; @@gravity; end
|
30
|
+
end
|
31
|
+
|
32
|
+
#class JSHelper
|
33
|
+
|
34
|
+
class KB
|
35
|
+
def self.initialize
|
36
|
+
@@keys = [
|
37
|
+
Gosu::KbUp, Gosu::KbDown,
|
38
|
+
Gosu::KbReturn, Gosu::KbEscape,
|
39
|
+
Gosu::KbLeftControl, Gosu::KbRightControl,
|
40
|
+
Gosu::KbA, Gosu::KbB, Gosu::KbC, Gosu::KbD, Gosu::KbE, Gosu::KbF,
|
41
|
+
Gosu::KbG, Gosu::KbH, Gosu::KbI, Gosu::KbJ, Gosu::KbK, Gosu::KbL,
|
42
|
+
Gosu::KbM, Gosu::KbN, Gosu::KbO, Gosu::KbP, Gosu::KbQ, Gosu::KbR,
|
43
|
+
Gosu::KbS, Gosu::KbT, Gosu::KbU, Gosu::KbV, Gosu::KbW, Gosu::KbX,
|
44
|
+
Gosu::KbY, Gosu::KbZ, Gosu::Kb1, Gosu::Kb2, Gosu::Kb3, Gosu::Kb4,
|
45
|
+
Gosu::Kb5, Gosu::Kb6, Gosu::Kb7, Gosu::Kb8, Gosu::Kb9, Gosu::Kb0,
|
46
|
+
Gosu::KbNumpad1, Gosu::KbNumpad2, Gosu::KbNumpad3, Gosu::KbNumpad4,
|
47
|
+
Gosu::KbNumpad5, Gosu::KbNumpad6, Gosu::KbNumpad7, Gosu::KbNumpad8,
|
48
|
+
Gosu::KbNumpad9, Gosu::KbNumpad0, Gosu::KbSpace, Gosu::KbBackspace,
|
49
|
+
Gosu::KbDelete, Gosu::KbLeft, Gosu::KbRight, Gosu::KbHome,
|
50
|
+
Gosu::KbEnd, Gosu::KbLeftShift, Gosu::KbRightShift,
|
51
|
+
Gosu::KbBacktick, Gosu::KbMinus, Gosu::KbEqual, Gosu::KbBracketLeft,
|
52
|
+
Gosu::KbBracketRight, Gosu::KbBackslash, Gosu::KbApostrophe,
|
53
|
+
Gosu::KbComma, Gosu::KbPeriod, Gosu::KbSlash
|
54
|
+
]
|
55
|
+
@@down = []
|
56
|
+
@@prev_down = []
|
57
|
+
@@held_timer = {}
|
58
|
+
@@held_interval = {}
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.update
|
62
|
+
@@held_timer.each do |k, v|
|
63
|
+
if v < 40; @@held_timer[k] += 1
|
64
|
+
else
|
65
|
+
@@held_interval[k] = 0
|
66
|
+
@@held_timer.delete k
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
@@held_interval.each do |k, v|
|
71
|
+
if v < 5; @@held_interval[k] += 1
|
72
|
+
else; @@held_interval[k] = 0; end
|
73
|
+
end
|
74
|
+
|
75
|
+
@@prev_down = @@down.clone
|
76
|
+
@@down.clear
|
77
|
+
@@keys.each do |k|
|
78
|
+
if Game.window.button_down? k
|
79
|
+
@@down << k
|
80
|
+
@@held_timer[k] = 0 if @@prev_down.index(k).nil?
|
81
|
+
elsif @@prev_down.index(k)
|
82
|
+
@@held_timer.delete k
|
83
|
+
@@held_interval.delete k
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def self.key_pressed? key
|
89
|
+
@@prev_down.index(key).nil? and @@down.index(key)
|
90
|
+
end
|
91
|
+
|
92
|
+
def self.key_down? key
|
93
|
+
@@down.index(key)
|
94
|
+
end
|
95
|
+
|
96
|
+
def self.key_released? key
|
97
|
+
@@prev_down.index(key) and @@down.index(key).nil?
|
98
|
+
end
|
99
|
+
|
100
|
+
def self.key_held? key
|
101
|
+
@@held_interval[key] == 5
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
class Mouse
|
106
|
+
def self.initialize
|
107
|
+
@@down = {}
|
108
|
+
@@prev_down = {}
|
109
|
+
@@dbl_click = {}
|
110
|
+
@@dbl_click_timer = {}
|
111
|
+
end
|
112
|
+
|
113
|
+
def self.update
|
114
|
+
@@prev_down = @@down.clone
|
115
|
+
@@down.clear
|
116
|
+
@@dbl_click.clear
|
117
|
+
|
118
|
+
@@dbl_click_timer.each do |k, v|
|
119
|
+
if v < 8; @@dbl_click_timer[k] += 1
|
120
|
+
else; @@dbl_click_timer.delete k; end
|
121
|
+
end
|
122
|
+
|
123
|
+
k1 = [Gosu::MsLeft, Gosu::MsMiddle, Gosu::MsRight]
|
124
|
+
k2 = [:left, :middle, :right]
|
125
|
+
for i in 0..2
|
126
|
+
if Game.window.button_down? k1[i]
|
127
|
+
@@down[k2[i]] = true
|
128
|
+
@@dbl_click[k2[i]] = true if @@dbl_click_timer[k2[i]]
|
129
|
+
@@dbl_click_timer.delete k2[i]
|
130
|
+
elsif @@prev_down[k2[i]]
|
131
|
+
@@dbl_click_timer[k2[i]] = 0
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
@@x = Game.window.mouse_x.round
|
136
|
+
@@y = Game.window.mouse_y.round
|
137
|
+
end
|
138
|
+
|
139
|
+
def self.x; @@x; end
|
140
|
+
def self.y; @@y; end
|
141
|
+
|
142
|
+
def self.button_pressed? btn
|
143
|
+
@@down[btn] and not @@prev_down[btn]
|
144
|
+
end
|
145
|
+
|
146
|
+
def self.button_down? btn
|
147
|
+
@@down[btn]
|
148
|
+
end
|
149
|
+
|
150
|
+
def self.button_released? btn
|
151
|
+
@@prev_down[btn] and not @@down[btn]
|
152
|
+
end
|
153
|
+
|
154
|
+
def self.double_click? btn
|
155
|
+
@@dbl_click[btn]
|
156
|
+
end
|
157
|
+
|
158
|
+
def self.over? x, y, w, h
|
159
|
+
@@x >= x and @@x < x + w and @@y >= y and @@y < y + h
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
class Res
|
164
|
+
def self.initialize
|
165
|
+
@@imgs = Hash.new
|
166
|
+
@@global_imgs = Hash.new
|
167
|
+
@@sounds = Hash.new
|
168
|
+
@@global_sounds = Hash.new
|
169
|
+
end
|
170
|
+
|
171
|
+
def self.img id, global = false, tileable = false, ext = ".png"
|
172
|
+
if global; a = @@global_imgs; else; a = @@imgs; end
|
173
|
+
return a[id] if a[id]
|
174
|
+
s = "data/img/" + id.to_s.split('_').join('/') + ext
|
175
|
+
img = Gosu::Image.new Game.window, s, tileable
|
176
|
+
a[id] = img
|
177
|
+
end
|
178
|
+
|
179
|
+
def self.imgs id, sprite_cols, sprite_rows, global = false, ext = ".png"
|
180
|
+
if global; a = @@global_imgs; else; a = @@imgs; end
|
181
|
+
return a[id] if a[id]
|
182
|
+
s = "data/img/" + id.to_s.split('_').join('/') + ext
|
183
|
+
imgs = Gosu::Image.load_tiles Game.window, s, -sprite_cols, -sprite_rows, false
|
184
|
+
a[id] = imgs
|
185
|
+
end
|
186
|
+
|
187
|
+
def self.tileset id, global = false
|
188
|
+
if global; a = @@global_imgs; else; a = @@imgs; end
|
189
|
+
return a[id] if a[id]
|
190
|
+
s = "data/img/tileset/#{id}.png"
|
191
|
+
tileset = Gosu::Image.load_tiles Game.window, s, C::TileSize, C::TileSize, true
|
192
|
+
a[id] = tileset
|
193
|
+
end
|
194
|
+
|
195
|
+
def self.sound id, global = false
|
196
|
+
if global; a = @@global_sounds; else; a = @@sounds; end
|
197
|
+
return a[id] if a[id]
|
198
|
+
s = "data/sound/se/" + id.to_s.split('_').join('/') + ".wav"
|
199
|
+
sound = Gosu::Sample.new Game.window, s
|
200
|
+
a[id] = sound
|
201
|
+
end
|
202
|
+
|
203
|
+
def self.song id, global = false
|
204
|
+
if global; a = @@global_sounds; else; a = @@sounds; end
|
205
|
+
return a[id] if a[id]
|
206
|
+
s = "data/sound/bgm/" + id.to_s.split('_').join('/') + ".ogg"
|
207
|
+
song = Gosu::Song.new Game.window, s
|
208
|
+
a[id] = song
|
209
|
+
end
|
210
|
+
|
211
|
+
# def self.text id
|
212
|
+
# G.texts[G.lang][id.to_sym]
|
213
|
+
# end
|
214
|
+
|
215
|
+
def self.clear
|
216
|
+
@imgs.clear
|
217
|
+
@sounds.clear
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|