minigl 1.3.7 → 1.3.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,259 +1,254 @@
1
1
  require_relative 'movement'
2
2
 
3
3
  module AGL
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
- def is_visible map # :nodoc:
159
- return map.cam.intersects @active_bounds if @active_bounds
160
- false
161
- end
162
-
163
- # Draws the game object in the screen.
164
- #
165
- # Parameters:
166
- # [map] A Map object, relative to which the object will be drawn (the x
167
- # and y coordinates of the image will be changed according to the
168
- # position of the camera).
169
- # [scale_x] A scale factor to be applied horizontally to the image.
170
- # [scale_y] A scale factor to be applied vertically to the image.
171
- # [alpha] The opacity with which the image will be drawn. Valid values
172
- # vary from 0 (fully transparent) to 255 (fully opaque).
173
- # [color] A color filter to apply to the image. A white (0xffffff) filter
174
- # will keep all colors unchanged, while a black (0x000000) filter
175
- # will turn all colors to black. A red (0xff0000) filter will keep
176
- # reddish colors with slight or no change, whereas bluish colors
177
- # will be darkened, for example.
178
- # [angle] A rotation, in degrees, to be applied to the image, relative to
179
- # its center.
180
- # [z_index] The z-order to draw the object. Objects with larger z-orders
181
- # will be drawn on top of the ones with smaller z-orders.
182
- def draw map = nil, scale_x = 1, scale_y = 1, alpha = 0xff, color = 0xffffff, angle = nil, z_index = 0
183
- color = (alpha << 24) | color
184
- if map
185
- if angle
186
- @img[@img_index].draw_rot @x.round + @img_gap.x - map.cam.x,
187
- @y.round + @img_gap.y - map.cam.y,
188
- z_index, angle, 0.5, 0.5, scale_x, scale_y, color
189
- else
190
- @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
191
- end
192
- elsif angle
193
- @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
194
- else
195
- @img[@img_index].draw @x.round + @img_gap.x, @y.round + @img_gap.y, z_index, scale_x, scale_y, color
196
- end
197
- end
198
- end
199
-
200
- # Represents a visual effect, i.e., a graphic - usually animated - that shows
201
- # up in the screen, lasts for a given time and "disappears". You should
202
- # explicitly dispose of references to effects whose attribute +dead+ is set
203
- # to +true+.
204
- class Effect < Sprite
205
- # This is +true+ when the effect's lifetime has already passed.
206
- attr_reader :dead
207
-
208
- # Creates a new Effect.
209
- #
210
- # Parameters:
211
- # [x] The x-coordinate in the screen (or map) where the effect will be
212
- # drawn. This can be modified later via the +x+ attribute.
213
- # [y] The y-coordinate in the screen (or map) where the effect will be
214
- # drawn. This can be modified later via the +y+ attribute.
215
- # [img] The image or spritesheet to use for this effect (see Sprite for
216
- # details on spritesheets).
217
- # [sprite_cols] (see Sprite)
218
- # [sprite_rows] (see Sprite)
219
- # [interval] The interval between steps of the animation, in updates.
220
- # [indices] The indices to use in the animation. See Sprite#animate for
221
- # details. If +nil+, it will be the sequence from 0 to
222
- # <code>sprite_cols * sprite_rows - 1</code>.
223
- # [lifetime] The lifetime of the effect, in updates. After +update+ is
224
- # called this number of times, the effect will no longer
225
- # be visible, even when +draw+ is called, and the +dead+ flag
226
- # will be set to +true+, so you get to know when to dispose
227
- # of the Effect object. If +nil+, it will be set to
228
- # <code>@indices.length * interval</code>, i.e., the exact time
229
- # needed for one animation cycle to complete.
230
- def initialize x, y, img, sprite_cols = nil, sprite_rows = nil, interval = 10, indices = nil, lifetime = nil
231
- super x, y, img, sprite_cols, sprite_rows
232
- @timer = 0
233
- if indices
234
- @indices = indices
235
- else
236
- @indices = *(0..(@img.length - 1))
237
- end
238
- @interval = interval
239
- if lifetime
240
- @lifetime = lifetime
241
- else
242
- @lifetime = @indices.length * interval
243
- end
244
- end
245
-
246
- # Updates the effect, animating and counting its remaining lifetime.
247
- def update
248
- unless @dead
249
- animate @indices, @interval
250
- @timer += 1
251
- @dead = true if @timer == @lifetime
252
- end
253
- end
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