minigl 2.2.9 → 2.3.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 79bd9440a172b26f147083ef8bd486af9adcaded814bf84473535c24704f2ded
4
- data.tar.gz: 667b535e408b7d3302e45c4b3d02fe5fa20d85c4e31b8af68eaa4f1023851a68
3
+ metadata.gz: cadd81a84bfdc1c4075f72b52b3ef6237bfeb2905646c6a539e754ba91ab9feb
4
+ data.tar.gz: 69c54b89125351d5248b9605d6e32b9ae281cb84d50c93ce26c87836f0fb80f5
5
5
  SHA512:
6
- metadata.gz: 6a26a33090716a53212cf084158ed623cb9d54e28407a990e8238f2784ca679a08d42908a03c23266836e830943afde4f5176f4d8946ba7302530f895d5b347a
7
- data.tar.gz: e60f740c8eec1033d01adb79af11d2960e709bd5a0df1165949622a6ba43ce49e9dc8fcb8d7f6c0ba93bd550fad1382dbf2ed73187038162561489b01f206399
6
+ metadata.gz: 01f2fdc4c5ee559a571e84a27a408787f5e10ae92e881a41f8304ed54ecb62a58f804b999f50761e2656d5457253c0832747c3697a97364e40da7dfa566b1d96
7
+ data.tar.gz: 3c5080247dc013cb1d5c44e4d37343938feb348b76bc8af0183471deb7e449798434345eabaf6cd0885b7c2a6cb39a0a043c7af38b6bcef00491e400a87abe38
data/README.md CHANGED
@@ -29,9 +29,9 @@ After installing the Gosu dependencies, you can just `gem install minigl`.
29
29
  * The [wiki](https://github.com/victords/minigl/wiki) is a work in progress with tutorials and examples.
30
30
  * Test package and examples aren't complete!
31
31
 
32
- ## Version 2.2.8
32
+ ## Version 2.3.4
33
33
 
34
- * Add `stop_time` parameter to the `cycle` method of `Movement` (included in `GameObject`).
34
+ * Attempt to fix bug: objects going through ramps sometimes.
35
35
 
36
36
  ## Contributing
37
37
 
@@ -111,7 +111,7 @@ module MiniGL
111
111
  # [obj] The object to check intersection with. It must have the +x+, +y+,
112
112
  # +w+ and +h+ accessible attributes determining its bounding box.
113
113
  def intersect?(obj)
114
- obj.x + obj.w > @x && obj.x < @x + @w && obj.y > get_y(obj) && obj.y <= @y + @h - obj.h
114
+ obj.x + obj.w > @x && obj.x < @x + @w && obj.y > get_y(obj) && obj.y < @y + @h
115
115
  end
116
116
 
117
117
  # :nodoc:
@@ -365,11 +365,14 @@ module MiniGL
365
365
  # +x+, +y+, +w+ and +h+.
366
366
  # [obstacles] Obstacles that should be considered for collision checking
367
367
  # with the carried objects, if they include the +Movement+
368
- # module, and with this object too, if moving with forces.
368
+ # module, and with this object too, if moving with forces and
369
+ # the +ignore_collision+ flag is false.
369
370
  # [ramps] Ramps that should be considered for the carried objects, if they
370
371
  # include the +Movement+ module, and for this object too, if moving
371
- # with forces.
372
- def move_carrying(arg, speed, carried_objs, obstacles, ramps)
372
+ # with forces and +ignore_collision+ is false.
373
+ # [ignore_collision] Set to true to make this object ignore collision even
374
+ # when moving with forces.
375
+ def move_carrying(arg, speed, carried_objs, obstacles, ramps, ignore_collision = false)
373
376
  if speed
374
377
  x_d = arg.x - @x; y_d = arg.y - @y
375
378
  distance = Math.sqrt(x_d**2 + y_d**2)
@@ -382,6 +385,9 @@ module MiniGL
382
385
  @speed.x = 1.0 * x_d * speed / distance
383
386
  @speed.y = 1.0 * y_d * speed / distance
384
387
  x_aim = @x + @speed.x; y_aim = @y + @speed.y
388
+ else
389
+ x_aim = @x + @speed.x + G.gravity.x + arg.x
390
+ y_aim = @y + @speed.y + G.gravity.y + arg.y
385
391
  end
386
392
 
387
393
  passengers = []
@@ -407,7 +413,7 @@ module MiniGL
407
413
  @y = y_aim
408
414
  end
409
415
  else
410
- move(arg, obstacles, ramps)
416
+ move(arg, ignore_collision ? [] : obstacles, ignore_collision ? [] : ramps)
411
417
  end
412
418
 
413
419
  forces = Vector.new @x - prev_x, @y - prev_y
@@ -507,6 +513,8 @@ module MiniGL
507
513
  unless @cycle_setup
508
514
  @cur_point = 0 if @cur_point.nil?
509
515
  if obstacles
516
+ obst_obstacles = [] if obst_obstacles.nil?
517
+ obst_ramps = [] if obst_ramps.nil?
510
518
  move_carrying points[@cur_point], speed, obstacles, obst_obstacles, obst_ramps
511
519
  else
512
520
  move_free points[@cur_point], speed
@@ -1,23 +1,127 @@
1
1
  module MiniGL
2
+ # This class represents a font and exposes most of the methods from +Gosu::Font+,
3
+ # but allows the font to be created from an image, allowing for better customization
4
+ # and also using the +retro+ option. Moreover, this class can be passed to the
5
+ # +TextHelper+ constructor as if it was a +Gosu::Font+.
6
+ #
7
+ # The image used to load the font must meet these criteria:
8
+ # * The characters should be laid out in lines of the same height in pixels.
9
+ # * The full image must have a height that is a multiple of that line height.
10
+ # * The characters should occupy the maximum available space in each line, i.e.,
11
+ # if a character fits in the current line it must not be placed in the next
12
+ # one. In the last line there can be any amount of free space at the end.
13
+ class ImageFont
14
+ # The height of this font in pixels.
15
+ attr_reader :height
16
+
17
+ # Creates an +ImageFont+.
18
+ #
19
+ # Parameters:
20
+ # [img_path] Identifier of an image fitting the description in the class documentation,
21
+ # as used in +Res.img+.
22
+ # [chars] A string containing all characters that will be present in the image, in the
23
+ # same order as they appear in the image. Do not include white space.
24
+ # [widths] An integer representing the width of the chars in pixels, if this is a fixed
25
+ # width font, or an array containing the width of each char, in the same order
26
+ # as they appear in the +chars+ string.
27
+ # [height] The height of the lines in the image (see description above).
28
+ # [space_width] The width of the white space character in this font.
29
+ # [global] Parameter that will be passed to +Res.img+ when loading the image.
30
+ # [ext] Parameter that will be passed to +Res.img+ when loading the image.
31
+ # [retro] Parameter that will be passed to +Res.img+ when loading the image.
32
+ def initialize(img_path, chars, widths, height, space_width, global = true, ext = '.png', retro = nil)
33
+ retro = Res.retro_images if retro.nil?
34
+ img = Res.img(img_path, global, false, ext, retro)
35
+ @chars = chars
36
+ @images = []
37
+ @height = height
38
+ @space_width = space_width
39
+ wa = widths.is_a?(Array)
40
+ if wa && widths.length != chars.length
41
+ raise 'Wrong widths array size!'
42
+ end
43
+ x = y = 0
44
+ (0...chars.length).each do |i|
45
+ @images.push(img.subimage(x, y, wa ? widths[i] : widths, height))
46
+ new_x = x + (wa ? widths[i] : widths)
47
+ if i < chars.length - 1 && new_x + (wa ? widths[i+1] : widths) > img.width
48
+ x = 0
49
+ y += height
50
+ else
51
+ x = new_x
52
+ end
53
+ end
54
+ end
55
+
56
+ # Returns the width, in pixels, of a given string written by this font.
57
+ # Note: Markup is not supported, this method is named this way to match
58
+ # <code>Gosu::Font</code>'s signature.
59
+ #
60
+ # Parameters:
61
+ # [text] The string to be measured
62
+ def markup_width(text)
63
+ text.chars.reduce(0) { |w, c| if c == ' '; w += @space_width; else; i = @chars.index(c); w += i ? @images[i].width : 0; end }
64
+ end
65
+
66
+ # See <code>Gosu::Font#draw_markup_rel</code> for details.
67
+ # Note: Markup is not supported, this method is named this way to match
68
+ # <code>Gosu::Font</code>'s signature.
69
+ def draw_markup_rel(text, x, y, z, rel_x, rel_y, scale_x, scale_y, color)
70
+ text = text.to_s unless text.is_a?(String)
71
+ if rel_x != 0
72
+ x -= scale_x * markup_width(text) * rel_x
73
+ end
74
+ if rel_y != 0
75
+ y -= scale_y * @height * rel_y
76
+ end
77
+ text.each_char do |c|
78
+ if c == ' '
79
+ x += scale_x * @space_width
80
+ next
81
+ end
82
+ i = @chars.index(c)
83
+ next if i.nil?
84
+ @images[i].draw(x, y, z, scale_x, scale_y, color)
85
+ x += scale_x * @images[i].width
86
+ end
87
+ end
88
+
89
+ # See <code>Gosu::Font#draw_markup</code> for details.
90
+ # Note: Markup is not supported, this method is named this way to match
91
+ # <code>Gosu::Font</code>'s signature.
92
+ def draw_markup(text, x, y, z, scale_x, scale_y, color)
93
+ draw_markup_rel(text, x, y, z, 0, 0, scale_x, scale_y, color)
94
+ end
95
+
96
+ alias :draw_text_rel :draw_markup_rel
97
+ alias :draw_text :draw_markup
98
+ alias :text_width :markup_width
99
+ end
100
+
2
101
  # This class provides methods for easily drawing one or multiple lines of
3
102
  # text, with control over the text alignment and coloring.
4
103
  class TextHelper
5
104
  # Creates a TextHelper.
6
105
  #
7
106
  # Parameters:
8
- # [font] A <code>Gosu::Font</code> that will be used to draw the text.
9
- # [line_spacing] When drawing multiple lines, the distance, in pixels,
10
- # between each line.
11
- def initialize(font, line_spacing = 0)
107
+ # [font] A <code>Gosu::Font</code> or <code>ImageFont</code> that will
108
+ # be used to draw the text.
109
+ # [line_spacing] When drawing multiple lines, the default distance, in
110
+ # pixels, between each line.
111
+ # [scale_x] The default horizontal scale of the font.
112
+ # [scale_y] The default vertical scale of the font.
113
+ def initialize(font, line_spacing = 0, scale_x = 1, scale_y = 1)
12
114
  @font = font
13
115
  @line_spacing = line_spacing
116
+ @scale_x = scale_x
117
+ @scale_y = scale_y
14
118
  end
15
119
 
16
120
  # Draws a single line of text.
17
121
  #
18
122
  # Parameters:
19
123
  # [text] The text to be drawn. No line breaks are allowed. You can use the
20
- # `<b>` tag for bold, `<i>` for italic and `<c=rrggbb>` for colors.
124
+ # `<b>` tag for bold, `<i>` for italic and `<c=rrggbb>` for colors.
21
125
  # [x] The horizontal reference for drawing the text. If +mode+ is +:left+,
22
126
  # all text will be drawn from this point to the right; if +mode+ is
23
127
  # +:right+, all text will be drawn from this point to the left; and if
@@ -43,12 +147,16 @@ module MiniGL
43
147
  # provide less than 255.
44
148
  # [z_index] The z-order to draw the object. Objects with larger z-orders
45
149
  # will be drawn on top of the ones with smaller z-orders.
150
+ # [scale_x] The horizontal scaling of the text. If +nil+, this instance's
151
+ # +@scale_x+ value will be used.
152
+ # [scale_y] The vertical scaling of the text. If +nil+, this instance's
153
+ # +@scale_y+ value will be used.
46
154
  #
47
155
  # *Obs.:* This method accepts named parameters, but +text+, +x+ and +y+ are
48
156
  # mandatory.
49
157
  def write_line(text, x = nil, y = nil, mode = :left, color = 0, alpha = 0xff,
50
158
  effect = nil, effect_color = 0, effect_size = 1, effect_alpha = 0xff,
51
- z_index = 0)
159
+ z_index = 0, scale_x = nil, scale_y = nil)
52
160
  if text.is_a? Hash
53
161
  x = text[:x]
54
162
  y = text[:y]
@@ -60,9 +168,13 @@ module MiniGL
60
168
  effect_size = text.fetch(:effect_size, 1)
61
169
  effect_alpha = text.fetch(:effect_alpha, 0xff)
62
170
  z_index = text.fetch(:z_index, 0)
171
+ scale_x = text.fetch(:scale_x, nil)
172
+ scale_y = text.fetch(:scale_y, nil)
63
173
  text = text[:text]
64
174
  end
65
175
 
176
+ scale_x = @scale_x if scale_x.nil?
177
+ scale_y = @scale_y if scale_y.nil?
66
178
  color = (alpha << 24) | color
67
179
  rel =
68
180
  case mode
@@ -74,19 +186,19 @@ module MiniGL
74
186
  if effect
75
187
  effect_color = (effect_alpha << 24) | effect_color
76
188
  if effect == :border
77
- @font.draw_markup_rel text, x - effect_size, y - effect_size, z_index, rel, 0, 1, 1, effect_color
78
- @font.draw_markup_rel text, x, y - effect_size, z_index, rel, 0, 1, 1, effect_color
79
- @font.draw_markup_rel text, x + effect_size, y - effect_size, z_index, rel, 0, 1, 1, effect_color
80
- @font.draw_markup_rel text, x + effect_size, y, z_index, rel, 0, 1, 1, effect_color
81
- @font.draw_markup_rel text, x + effect_size, y + effect_size, z_index, rel, 0, 1, 1, effect_color
82
- @font.draw_markup_rel text, x, y + effect_size, z_index, rel, 0, 1, 1, effect_color
83
- @font.draw_markup_rel text, x - effect_size, y + effect_size, z_index, rel, 0, 1, 1, effect_color
84
- @font.draw_markup_rel text, x - effect_size, y, z_index, rel, 0, 1, 1, effect_color
189
+ @font.draw_markup_rel text, x - effect_size, y - effect_size, z_index, rel, 0, scale_x, scale_y, effect_color
190
+ @font.draw_markup_rel text, x, y - effect_size, z_index, rel, 0, scale_x, scale_y, effect_color
191
+ @font.draw_markup_rel text, x + effect_size, y - effect_size, z_index, rel, 0, scale_x, scale_y, effect_color
192
+ @font.draw_markup_rel text, x + effect_size, y, z_index, rel, 0, scale_x, scale_y, effect_color
193
+ @font.draw_markup_rel text, x + effect_size, y + effect_size, z_index, rel, 0, scale_x, scale_y, effect_color
194
+ @font.draw_markup_rel text, x, y + effect_size, z_index, rel, 0, scale_x, scale_y, effect_color
195
+ @font.draw_markup_rel text, x - effect_size, y + effect_size, z_index, rel, 0, scale_x, scale_y, effect_color
196
+ @font.draw_markup_rel text, x - effect_size, y, z_index, rel, 0, scale_x, scale_y, effect_color
85
197
  elsif effect == :shadow
86
- @font.draw_markup_rel text, x + effect_size, y + effect_size, z_index, rel, 0, 1, 1, effect_color
198
+ @font.draw_markup_rel text, x + effect_size, y + effect_size, z_index, rel, 0, scale_x, scale_y, effect_color
87
199
  end
88
200
  end
89
- @font.draw_markup_rel text, x, y, z_index, rel, 0, 1, 1, color
201
+ @font.draw_markup_rel text, x, y, z_index, rel, 0, scale_x, scale_y, color
90
202
  end
91
203
 
92
204
  # Draws text, breaking lines when needed and when explicitly caused by the
@@ -94,7 +206,7 @@ module MiniGL
94
206
  #
95
207
  # Parameters:
96
208
  # [text] The text to be drawn. Line breaks are allowed. You can use the
97
- # `<b>` tag for bold, `<i>` for italic and `<c=rrggbb>` for colors.
209
+ # `<b>` tag for bold, `<i>` for italic and `<c=rrggbb>` for colors.
98
210
  # [x] The horizontal reference for drawing the text. Works like in
99
211
  # +write_line+ for the +:left+, +:right+ and +:center+ modes. For the
100
212
  # +:justified+ mode, works the same as for +:left+.
@@ -109,11 +221,20 @@ module MiniGL
109
221
  # transparent) to 255 (fully opaque).
110
222
  # [z_index] The z-order to draw the object. Objects with larger z-orders
111
223
  # will be drawn on top of the ones with smaller z-orders.
112
- def write_breaking(text, x, y, width, mode = :left, color = 0, alpha = 0xff, z_index = 0)
224
+ # [scale_x] The horizontal scaling of the text. If +nil+, this instance's
225
+ # +@scale_x+ value will be used.
226
+ # [scale_y] The vertical scaling of the text. If +nil+, this instance's
227
+ # +@scale_y+ value will be used.
228
+ # [line_spacing] The spacing between lines, in pixels. If +nil+, this
229
+ # instance's +@line_spacing+ value will be used.
230
+ def write_breaking(text, x, y, width, mode = :left, color = 0, alpha = 0xff, z_index = 0, scale_x = nil, scale_y = nil, line_spacing = nil)
231
+ line_spacing = @line_spacing if line_spacing.nil?
232
+ scale_x = @scale_x if scale_x.nil?
233
+ scale_y = @scale_y if scale_y.nil?
113
234
  color = (alpha << 24) | color
114
235
  text.split("\n").each do |p|
115
236
  if mode == :justified
116
- y = write_paragraph_justified p, x, y, width, color, z_index
237
+ y = write_paragraph_justified p, x, y, width, color, z_index, scale_x, scale_y, line_spacing
117
238
  else
118
239
  rel =
119
240
  case mode
@@ -122,40 +243,40 @@ module MiniGL
122
243
  when :right then 1
123
244
  else 0
124
245
  end
125
- y = write_paragraph p, x, y, width, rel, color, z_index
246
+ y = write_paragraph p, x, y, width, rel, color, z_index, scale_x, scale_y, line_spacing
126
247
  end
127
248
  end
128
249
  end
129
250
 
130
251
  private
131
252
 
132
- def write_paragraph(text, x, y, width, rel, color, z_index)
253
+ def write_paragraph(text, x, y, width, rel, color, z_index, scale_x, scale_y, line_spacing)
133
254
  line = ''
134
255
  line_width = 0
135
256
  text.split(' ').each do |word|
136
- w = @font.markup_width word
137
- if line_width + w > width
138
- @font.draw_markup_rel line.chop, x, y, z_index, rel, 0, 1, 1, color
257
+ w = @font.markup_width(word)
258
+ if line_width + w * scale_x > width
259
+ @font.draw_markup_rel line.chop, x, y, z_index, rel, 0, scale_x, scale_y, color
139
260
  line = ''
140
261
  line_width = 0
141
- y += @font.height + @line_spacing
262
+ y += (@font.height + line_spacing) * scale_y
142
263
  end
143
264
  line += "#{word} "
144
- line_width += @font.markup_width "#{word} "
265
+ line_width += @font.markup_width("#{word} ") * scale_x
145
266
  end
146
- @font.draw_markup_rel line.chop, x, y, z_index, rel, 0, 1, 1, color unless line.empty?
147
- y + @font.height + @line_spacing
267
+ @font.draw_markup_rel line.chop, x, y, z_index, rel, 0, scale_x, scale_y, color unless line.empty?
268
+ y + (@font.height + line_spacing) * scale_y
148
269
  end
149
270
 
150
- def write_paragraph_justified(text, x, y, width, color, z_index)
151
- space_width = @font.text_width ' '
271
+ def write_paragraph_justified(text, x, y, width, color, z_index, scale_x, scale_y, line_spacing)
272
+ space_width = @font.text_width(' ') * scale_x
152
273
  spaces = [[]]
153
274
  line_index = 0
154
275
  new_x = x
155
276
  words = text.split(' ')
156
277
  words.each do |word|
157
- w = @font.markup_width word
158
- if new_x + w > x + width
278
+ w = @font.markup_width(word)
279
+ if new_x + w * scale_x > x + width
159
280
  space = x + width - new_x + space_width
160
281
  index = 0
161
282
  while space > 0
@@ -170,7 +291,7 @@ module MiniGL
170
291
 
171
292
  new_x = x
172
293
  end
173
- new_x += @font.markup_width(word) + space_width
294
+ new_x += @font.markup_width(word) * scale_x + space_width
174
295
  spaces[line_index] << space_width
175
296
  end
176
297
 
@@ -178,11 +299,11 @@ module MiniGL
178
299
  spaces.each do |line|
179
300
  new_x = x
180
301
  line.each do |s|
181
- @font.draw_markup words[index], new_x, y, z_index, 1, 1, color
182
- new_x += @font.markup_width(words[index]) + s
302
+ @font.draw_markup(words[index], new_x, y, z_index, scale_x, scale_y, color)
303
+ new_x += @font.markup_width(words[index]) * scale_x + s
183
304
  index += 1
184
305
  end
185
- y += @font.height + @line_spacing
306
+ y += (@font.height + line_spacing) * scale_y
186
307
  end
187
308
  y
188
309
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minigl
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.9
4
+ version: 2.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor David Santos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-07 00:00:00.000000000 Z
11
+ date: 2021-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gosu
@@ -98,37 +98,37 @@ signing_key:
98
98
  specification_version: 4
99
99
  summary: MiniGL
100
100
  test_files:
101
+ - test/vector_tests.rb
101
102
  - test/res_tests.rb
102
- - test/iso_game.rb
103
103
  - test/mov_game.rb
104
+ - test/movement_tests.rb
104
105
  - test/map_tests.rb
105
- - test/game.rb
106
+ - test/iso_game.rb
106
107
  - test/game_object_tests.rb
107
- - test/vector_tests.rb
108
- - test/movement_tests.rb
108
+ - test/game.rb
109
109
  - test/test.png
110
+ - test/data/tileset/tileset1.png
110
111
  - test/data/font/font1.ttf
112
+ - test/data/sound/1.wav
113
+ - test/data/img/check.png
111
114
  - test/data/img/square.svg
112
- - test/data/img/barbg.png
113
- - test/data/img/img1.png
114
- - test/data/img/tile1.png
115
- - test/data/img/tile2b.png
116
- - test/data/img/text.png
117
- - test/data/img/btn.png
118
- - test/data/img/square2.svg
119
- - test/data/img/square2.png
115
+ - test/data/img/tile2.svg
120
116
  - test/data/img/barfg.png
121
- - test/data/img/tile1b.png
122
117
  - test/data/img/tile2.png
123
- - test/data/img/check.png
124
- - test/data/img/tile2.svg
125
- - test/data/img/square3.png
118
+ - test/data/img/tile1.svg
126
119
  - test/data/img/square3.svg
120
+ - test/data/img/text.png
121
+ - test/data/img/square2.svg
127
122
  - test/data/img/image.png
128
- - test/data/img/tile1.svg
129
- - test/data/img/square.png
123
+ - test/data/img/img1.png
124
+ - test/data/img/square2.png
130
125
  - test/data/img/barbg.svg
131
126
  - test/data/img/barfg.svg
132
- - test/data/tileset/tileset1.png
133
- - test/data/sound/1.wav
127
+ - test/data/img/tile1b.png
128
+ - test/data/img/barbg.png
129
+ - test/data/img/square.png
130
+ - test/data/img/btn.png
131
+ - test/data/img/square3.png
132
+ - test/data/img/tile2b.png
133
+ - test/data/img/tile1.png
134
134
  - test/data/img/sub/image.png