minigl 1.3.7 → 1.3.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -4
- data/lib/minigl/forms.rb +694 -698
- data/lib/minigl/game_object.rb +250 -255
- data/lib/minigl/global.rb +470 -470
- data/lib/minigl/map.rb +153 -155
- data/lib/minigl/movement.rb +467 -467
- data/lib/minigl/text.rb +141 -141
- data/test/game_object_tests.rb +48 -48
- data/test/iso_game.rb +27 -27
- data/test/map_tests.rb +51 -51
- data/test/movement_tests.rb +78 -78
- data/test/res_tests.rb +15 -15
- metadata +2 -2
data/lib/minigl/game_object.rb
CHANGED
@@ -1,259 +1,254 @@
|
|
1
1
|
require_relative 'movement'
|
2
2
|
|
3
3
|
module AGL
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
def draw map = nil, scale_x = 1, scale_y = 1, alpha = 0xff, color = 0xffffff, angle = nil, z_index = 0
|
256
|
-
super unless @dead
|
257
|
-
end
|
258
|
-
end
|
4
|
+
# This class represents an (optionally animated) image inside the game screen.
|
5
|
+
class Sprite
|
6
|
+
# The index of the current sprite in the spritesheet being drawn.
|
7
|
+
attr_reader :img_index
|
8
|
+
|
9
|
+
# The x-coordinate of the image in the screen.
|
10
|
+
attr_accessor :x
|
11
|
+
|
12
|
+
# The y-coordinate of the image in the screen.
|
13
|
+
attr_accessor :y
|
14
|
+
|
15
|
+
# Creates a new sprite.
|
16
|
+
#
|
17
|
+
# Parameters:
|
18
|
+
# [x] The x-coordinate in the screen (or map) where the sprite will be
|
19
|
+
# drawn. This can be modified later via the +x+ attribute.
|
20
|
+
# [y] The y-coordinate in the screen (or map) where the sprite will be
|
21
|
+
# drawn. This can be modified later via the +y+ attribute.
|
22
|
+
# [img] The path to a PNG image or spritesheet, following the MiniGL
|
23
|
+
# convention: images must be inside a 'data/img' directory, relative
|
24
|
+
# to the code file, and you must only provide the file name, without
|
25
|
+
# extension, in this case. If the image is inside a subdirectory of
|
26
|
+
# 'data/img', you must prefix the file name with each subdirectory
|
27
|
+
# name, followed by an underscore (so the file and directories names
|
28
|
+
# must not contain underscores). For example, if your image is
|
29
|
+
# 'data/img/sprite/1.png', you must provide <code>"sprite_1"</code>
|
30
|
+
# or +:sprite_1+.
|
31
|
+
# [sprite_cols] The number of columns in the spritesheet. Use +nil+ if the
|
32
|
+
# image is not a spritesheet.
|
33
|
+
# [sprite_rows] The number of rows in the spritesheet. Use +nil+ if the
|
34
|
+
# image is not a spritesheet.
|
35
|
+
def initialize x, y, img, sprite_cols = nil, sprite_rows = nil
|
36
|
+
@x = x; @y = y
|
37
|
+
@img =
|
38
|
+
if sprite_cols.nil?
|
39
|
+
[Res.img(img)]
|
40
|
+
else
|
41
|
+
Res.imgs img, sprite_cols, sprite_rows
|
42
|
+
end
|
43
|
+
@anim_counter = 0
|
44
|
+
@img_index = 0
|
45
|
+
@index_index = 0
|
46
|
+
end
|
47
|
+
|
48
|
+
# Performs time checking to update the image index according to the
|
49
|
+
# sequence of indices and the interval.
|
50
|
+
#
|
51
|
+
# Parameters:
|
52
|
+
# [indices] The sequence of image indices used in the animation. The
|
53
|
+
# indices are determined from left to right, and from top to
|
54
|
+
# bottom, inside the spritesheet. All indices must be in the
|
55
|
+
# interval <code>0..(sprite_cols * sprite_rows)</code>.
|
56
|
+
# [interval] The amount of frames between each change in the image index.
|
57
|
+
# A frame will usually represent 1/60 second (roughly 17
|
58
|
+
# milliseconds).
|
59
|
+
def animate indices, interval
|
60
|
+
@anim_counter += 1
|
61
|
+
if @anim_counter >= interval
|
62
|
+
@index_index += 1
|
63
|
+
@index_index = 0 if @index_index == indices.length
|
64
|
+
@img_index = indices[@index_index]
|
65
|
+
@anim_counter = 0
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# Draws the sprite in the screen
|
70
|
+
#
|
71
|
+
# Parameters:
|
72
|
+
# [map] A Map object, relative to which the sprite will be drawn (the x
|
73
|
+
# and y coordinates of the sprite will be changed according to the
|
74
|
+
# position of the camera).
|
75
|
+
# [scale_x] A scale factor to be applied horizontally to the image.
|
76
|
+
# [scale_y] A scale factor to be applied vertically to the image.
|
77
|
+
# [alpha] The opacity with which the image will be drawn. Valid values
|
78
|
+
# vary from 0 (fully transparent) to 255 (fully opaque).
|
79
|
+
# [color] A color filter to apply to the image. A white (0xffffff) filter
|
80
|
+
# will keep all colors unchanged, while a black (0x000000) filter
|
81
|
+
# will turn all colors to black. A red (0xff0000) filter will keep
|
82
|
+
# reddish colors with slight or no change, whereas bluish colors
|
83
|
+
# will be darkened, for example.
|
84
|
+
# [angle] A rotation, in degrees, to be applied to the image, relative to
|
85
|
+
# its center.
|
86
|
+
# [z_index] The z-order to draw the object. Objects with larger z-orders
|
87
|
+
# will be drawn on top of the ones with smaller z-orders.
|
88
|
+
def draw map = nil, scale_x = 1, scale_y = 1, alpha = 0xff, color = 0xffffff, angle = nil, z_index = 0
|
89
|
+
color = (alpha << 24) | color
|
90
|
+
if map
|
91
|
+
if angle
|
92
|
+
@img[@img_index].draw_rot @x.round - map.cam.x, @y.round - map.cam.y, z_index, angle, 0.5, 0.5, scale_x, scale_y, color
|
93
|
+
else
|
94
|
+
@img[@img_index].draw @x.round - map.cam.x, @y.round - map.cam.y, z_index, scale_x, scale_y, color
|
95
|
+
end
|
96
|
+
elsif angle
|
97
|
+
@img[@img_index].draw_rot @x.round, @y.round, z_index, angle, 0.5, 0.5, scale_x, scale_y, color
|
98
|
+
else
|
99
|
+
@img[@img_index].draw @x.round, @y.round, z_index, scale_x, scale_y, color
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# This class represents an object with a set of properties and methods
|
105
|
+
# commonly used in games. It defines an object with a rectangular bounding
|
106
|
+
# box, and having all the attributes required for using the Movement module.
|
107
|
+
class GameObject < Sprite
|
108
|
+
include Movement
|
109
|
+
|
110
|
+
# Creates a new game object.
|
111
|
+
#
|
112
|
+
# Parameters:
|
113
|
+
# [x] The x-coordinate of the object's bounding box. This can be modified
|
114
|
+
# later via the +x+ attribute.
|
115
|
+
# [y] The y-coordinate of the object's bounding box. This can be modified
|
116
|
+
# later via the +y+ attribute.
|
117
|
+
# [w] The width of the object's bounding box.
|
118
|
+
# [h] The height of the object's bounding box.
|
119
|
+
# [img] The image or spritesheet for the object.
|
120
|
+
# [img_gap] A Vector object representing the difference between the top
|
121
|
+
# left corner of the bounding box and the coordinates of the
|
122
|
+
# image. For example, an object with <code>x = 100</code>,
|
123
|
+
# <code>y = 50</code> and <code>img_gap = Vector.new(-5, -5)</code>
|
124
|
+
# will be drawn at position (95, 45) of the screen.
|
125
|
+
# [sprite_cols] The number of columns in the spritesheet. Use +nil+ if the
|
126
|
+
# image is not a spritesheet.
|
127
|
+
# [sprite_rows] The number of rows in the spritesheet. Use +nil+ if the
|
128
|
+
# image is not a spritesheet.
|
129
|
+
# [mass] The mass of the object. Details on how it is used can be found
|
130
|
+
# in the Movement module.
|
131
|
+
def initialize x, y, w, h, img, img_gap = nil, sprite_cols = nil, sprite_rows = nil, mass = 1.0
|
132
|
+
super x, y, img, sprite_cols, sprite_rows
|
133
|
+
@w = w; @h = h
|
134
|
+
@img_gap =
|
135
|
+
if img_gap.nil?
|
136
|
+
Vector.new 0, 0
|
137
|
+
else
|
138
|
+
img_gap
|
139
|
+
end
|
140
|
+
@mass = mass
|
141
|
+
@speed = Vector.new 0, 0
|
142
|
+
@min_speed = Vector.new 0.01, 0.01
|
143
|
+
@max_speed = Vector.new 15, 15
|
144
|
+
@stored_forces = Vector.new 0, 0
|
145
|
+
end
|
146
|
+
|
147
|
+
# Resets the animation timer and immediately changes the image index to
|
148
|
+
# the specified value.
|
149
|
+
#
|
150
|
+
# Parameters:
|
151
|
+
# [index] The image index to be set.
|
152
|
+
def set_animation index
|
153
|
+
@anim_counter = 0
|
154
|
+
@img_index = index
|
155
|
+
@index_index = 0
|
156
|
+
end
|
157
|
+
|
158
|
+
# Draws the game object in the screen.
|
159
|
+
#
|
160
|
+
# Parameters:
|
161
|
+
# [map] A Map object, relative to which the object will be drawn (the x
|
162
|
+
# and y coordinates of the image will be changed according to the
|
163
|
+
# position of the camera).
|
164
|
+
# [scale_x] A scale factor to be applied horizontally to the image.
|
165
|
+
# [scale_y] A scale factor to be applied vertically to the image.
|
166
|
+
# [alpha] The opacity with which the image will be drawn. Valid values
|
167
|
+
# vary from 0 (fully transparent) to 255 (fully opaque).
|
168
|
+
# [color] A color filter to apply to the image. A white (0xffffff) filter
|
169
|
+
# will keep all colors unchanged, while a black (0x000000) filter
|
170
|
+
# will turn all colors to black. A red (0xff0000) filter will keep
|
171
|
+
# reddish colors with slight or no change, whereas bluish colors
|
172
|
+
# will be darkened, for example.
|
173
|
+
# [angle] A rotation, in degrees, to be applied to the image, relative to
|
174
|
+
# its center.
|
175
|
+
# [z_index] The z-order to draw the object. Objects with larger z-orders
|
176
|
+
# will be drawn on top of the ones with smaller z-orders.
|
177
|
+
def draw map = nil, scale_x = 1, scale_y = 1, alpha = 0xff, color = 0xffffff, angle = nil, z_index = 0
|
178
|
+
color = (alpha << 24) | color
|
179
|
+
if map
|
180
|
+
if angle
|
181
|
+
@img[@img_index].draw_rot @x.round + @img_gap.x - map.cam.x,
|
182
|
+
@y.round + @img_gap.y - map.cam.y,
|
183
|
+
z_index, angle, 0.5, 0.5, scale_x, scale_y, color
|
184
|
+
else
|
185
|
+
@img[@img_index].draw @x.round + @img_gap.x - map.cam.x, @y.round + @img_gap.y - map.cam.y, z_index, scale_x, scale_y, color
|
186
|
+
end
|
187
|
+
elsif angle
|
188
|
+
@img[@img_index].draw_rot @x.round + @img_gap.x, @y.round + @img_gap.y, z_index, angle, 0.5, 0.5, scale_x, scale_y, color
|
189
|
+
else
|
190
|
+
@img[@img_index].draw @x.round + @img_gap.x, @y.round + @img_gap.y, z_index, scale_x, scale_y, color
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
# Represents a visual effect, i.e., a graphic - usually animated - that shows
|
196
|
+
# up in the screen, lasts for a given time and "disappears". You should
|
197
|
+
# explicitly dispose of references to effects whose attribute +dead+ is set
|
198
|
+
# to +true+.
|
199
|
+
class Effect < Sprite
|
200
|
+
# This is +true+ when the effect's lifetime has already passed.
|
201
|
+
attr_reader :dead
|
202
|
+
|
203
|
+
# Creates a new Effect.
|
204
|
+
#
|
205
|
+
# Parameters:
|
206
|
+
# [x] The x-coordinate in the screen (or map) where the effect will be
|
207
|
+
# drawn. This can be modified later via the +x+ attribute.
|
208
|
+
# [y] The y-coordinate in the screen (or map) where the effect will be
|
209
|
+
# drawn. This can be modified later via the +y+ attribute.
|
210
|
+
# [img] The image or spritesheet to use for this effect (see Sprite for
|
211
|
+
# details on spritesheets).
|
212
|
+
# [sprite_cols] (see Sprite)
|
213
|
+
# [sprite_rows] (see Sprite)
|
214
|
+
# [interval] The interval between steps of the animation, in updates.
|
215
|
+
# [indices] The indices to use in the animation. See Sprite#animate for
|
216
|
+
# details. If +nil+, it will be the sequence from 0 to
|
217
|
+
# <code>sprite_cols * sprite_rows - 1</code>.
|
218
|
+
# [lifetime] The lifetime of the effect, in updates. After +update+ is
|
219
|
+
# called this number of times, the effect will no longer
|
220
|
+
# be visible, even when +draw+ is called, and the +dead+ flag
|
221
|
+
# will be set to +true+, so you get to know when to dispose
|
222
|
+
# of the Effect object. If +nil+, it will be set to
|
223
|
+
# <code>@indices.length * interval</code>, i.e., the exact time
|
224
|
+
# needed for one animation cycle to complete.
|
225
|
+
def initialize x, y, img, sprite_cols = nil, sprite_rows = nil, interval = 10, indices = nil, lifetime = nil
|
226
|
+
super x, y, img, sprite_cols, sprite_rows
|
227
|
+
@timer = 0
|
228
|
+
if indices
|
229
|
+
@indices = indices
|
230
|
+
else
|
231
|
+
@indices = *(0..(@img.length - 1))
|
232
|
+
end
|
233
|
+
@interval = interval
|
234
|
+
if lifetime
|
235
|
+
@lifetime = lifetime
|
236
|
+
else
|
237
|
+
@lifetime = @indices.length * interval
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
# Updates the effect, animating and counting its remaining lifetime.
|
242
|
+
def update
|
243
|
+
unless @dead
|
244
|
+
animate @indices, @interval
|
245
|
+
@timer += 1
|
246
|
+
@dead = true if @timer == @lifetime
|
247
|
+
end
|
248
|
+
end
|
249
|
+
|
250
|
+
def draw map = nil, scale_x = 1, scale_y = 1, alpha = 0xff, color = 0xffffff, angle = nil, z_index = 0
|
251
|
+
super unless @dead
|
252
|
+
end
|
253
|
+
end
|
259
254
|
end
|