minigl 1.3.6 → 1.3.7

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
  SHA1:
3
- metadata.gz: d35ec0ab57189caa5cc2edd7e61764845968db0e
4
- data.tar.gz: 8d065a06a3a5bdac7a2a67ca316eefd114350971
3
+ metadata.gz: 334e4fb063765088eb6bc85cb3d67b68d28b1867
4
+ data.tar.gz: ce24cfb378fe102dd475e32ff636b542b9b9f841
5
5
  SHA512:
6
- metadata.gz: a0f06503c0348c89094a8717e5075d969dc3dc4ed7b20ba052a731e1115c3d137ce8ebbfac982560b4d60353c68a03fa98fce53d6418ee1c12e92e0be20223dd
7
- data.tar.gz: 88dc0365857bf9162587db91fd85181d37a51d072eac998457e5f7ef1094b6952f2fdf9a310055425b2d1cf92439b687cac240155967fac93f949b66e6fb9185
6
+ metadata.gz: d9d84abe68d022276574fc48baeea9c41a1188b1bc9b359dd88716384f0b9760c8ee28f0f1e7f9a911cfc25983c2d07d234bbfee63b9079987b7e442ce2ef8b4
7
+ data.tar.gz: 30392926054fc4e1567647dcc72f14c2479ad48bbbd66b2285b5424258b0eed0b73983f4b956eb99a62b4ea61064cc41f1f2dbb15c5230878982ee9e6400b29e
data/README.md CHANGED
@@ -32,6 +32,8 @@ this [working game example](https://github.com/victords/aventura-do-saber).
32
32
  * An auxiliary, tutorial-like documentation is under construction
33
33
  [here](https://github.com/victords/minigl/wiki).
34
34
 
35
- **Version 1.3.6**
35
+ **Version 1.3.7**
36
36
 
37
- * Changed license to MIT.
37
+ * Minor adjustment and exposure of `prefix` attribute of `Res`.
38
+ * Removed debug message that was being output when starting the game.
39
+ * Improved the `Vector` class with operations, comparisons, etc.
data/lib/minigl/global.rb CHANGED
@@ -2,9 +2,88 @@ require 'gosu'
2
2
 
3
3
  # The main module of the library, used only as a namespace.
4
4
  module AGL
5
- # A Struct with two attributes, x and y (in this order), representing a point
6
- # in a bidimensional space.
7
- Vector = Struct.new :x, :y
5
+ # This class represents a point or vector in a bidimensional space.
6
+ class Vector
7
+ # The x coordinate of the vector
8
+ attr_accessor :x
9
+
10
+ # The y coordinate of the vector
11
+ attr_accessor :y
12
+
13
+ # Creates a new bidimensional vector.
14
+ #
15
+ # Parameters:
16
+ # [x] The x coordinate of the vector
17
+ # [y] The y coordinate of the vector
18
+ def initialize x, y
19
+ @x = x
20
+ @y = y
21
+ end
22
+
23
+ # Returns +true+ if both coordinates of this vector are equal to the
24
+ # corresponding coordinates of +other_vector+, with +precision+ decimal
25
+ # places of precision.
26
+ def == other_vector, precision = 6
27
+ @x.round(precision) == other_vector.x.round(precision) and
28
+ @y.round(precision) == other_vector.y.round(precision)
29
+ end
30
+
31
+ # Returns +true+ if at least one coordinate of this vector is different from
32
+ # the corresponding coordinate of +other_vector+, with +precision+ decimal
33
+ # places of precision.
34
+ def != other_vector, precision = 6
35
+ @x.round(precision) != other_vector.x.round(precision) or
36
+ @y.round(precision) != other_vector.y.round(precision)
37
+ end
38
+
39
+ # Sums this vector with +other_vector+, i.e., sums each coordinate of this
40
+ # vector with the corresponding coordinate of +other_vector+.
41
+ def + other_vector
42
+ Vector.new @x + other_vector.x, @y + other_vector.y
43
+ end
44
+
45
+ # Subtracts +other_vector+ from this vector, i.e., subtracts from each
46
+ # coordinate of this vector the corresponding coordinate of +other_vector+.
47
+ def - other_vector
48
+ Vector.new @x - other_vector.x, @y - other_vector.y
49
+ end
50
+
51
+ # Multiplies this vector by a scalar, i.e., each coordinate is multiplied by
52
+ # the given number.
53
+ def * scalar
54
+ Vector.new @x * scalar, @y * scalar
55
+ end
56
+
57
+ # Divides this vector by a scalar, i.e., each coordinate is divided by the
58
+ # given number.
59
+ def / scalar
60
+ Vector.new @x / scalar.to_f, @y / scalar.to_f
61
+ end
62
+
63
+ # Returns the euclidean distance between this vector and +other_vector+.
64
+ def distance other_vector
65
+ dx = @x - other_vector.x
66
+ dy = @y - other_vector.y
67
+ Math.sqrt(dx ** 2 + dy ** 2)
68
+ end
69
+
70
+ # Returns a vector corresponding to the rotation of this vector around the
71
+ # origin (0, 0) by +radians+ radians.
72
+ def rotate radians
73
+ sin = Math.sin radians
74
+ cos = Math.cos radians
75
+ Vector.new cos * @x - sin * @y, sin * @x + cos * @y
76
+ end
77
+
78
+ # Rotates this vector by +radians+ radians around the origin (0, 0).
79
+ def rotate! radians
80
+ sin = Math.sin radians
81
+ cos = Math.cos radians
82
+ prev_x = @x
83
+ @x = cos * @x - sin * @y
84
+ @y = sin * prev_x + cos * @y
85
+ end
86
+ end
8
87
 
9
88
  # This class represents a rectangle by its x and y coordinates and width and
10
89
  # height.
@@ -328,12 +407,16 @@ module AGL
328
407
  @@global_songs = Hash.new
329
408
  @@fonts = Hash.new
330
409
  @@global_fonts = Hash.new
331
- @@prefix = File.expand_path(File.dirname($0)) + '/'
332
- puts @@prefix
410
+ @@prefix = File.expand_path(File.dirname($0)) + '/data/'
333
411
  end
334
412
 
413
+ # Get the current prefix for searching data files. This is the directory
414
+ # under which 'img', 'sound', 'song', etc. folders are located.
415
+ def self.prefix; @@prefix; end
416
+
335
417
  # Set a custom prefix for loading resources. By default, the prefix is the
336
- # directory of the game script.
418
+ # directory of the game script. The prefix is the directory under which
419
+ # 'img', 'sound', 'song', etc. folders are located.
337
420
  def self.prefix= value
338
421
  value += '/' if value[-1] != '/'
339
422
  @@prefix = value
@@ -359,7 +442,7 @@ module AGL
359
442
  def self.img id, global = false, tileable = false, ext = ".png"
360
443
  if global; a = @@global_imgs; else; a = @@imgs; end
361
444
  return a[id] if a[id]
362
- s = @@prefix + "data/img/" + id.to_s.split('_').join('/') + ext
445
+ s = @@prefix + "img/" + id.to_s.split('_').join('/') + ext
363
446
  img = Gosu::Image.new Game.window, s, tileable
364
447
  a[id] = img
365
448
  end
@@ -382,7 +465,7 @@ module AGL
382
465
  def self.imgs id, sprite_cols, sprite_rows, global = false, ext = ".png"
383
466
  if global; a = @@global_imgs; else; a = @@imgs; end
384
467
  return a[id] if a[id]
385
- s = @@prefix + "data/img/" + id.to_s.split('_').join('/') + ext
468
+ s = @@prefix + "img/" + id.to_s.split('_').join('/') + ext
386
469
  imgs = Gosu::Image.load_tiles Game.window, s, -sprite_cols, -sprite_rows, false
387
470
  a[id] = imgs
388
471
  end
@@ -406,7 +489,7 @@ module AGL
406
489
  def self.tileset id, tile_width = 32, tile_height = 32, global = false, ext = ".png"
407
490
  if global; a = @@global_tilesets; else; a = @@tilesets; end
408
491
  return a[id] if a[id]
409
- s = @@prefix + "data/tileset/" + id.to_s.split('_').join('/') + ext
492
+ s = @@prefix + "tileset/" + id.to_s.split('_').join('/') + ext
410
493
  tileset = Gosu::Image.load_tiles Game.window, s, tile_width, tile_height, true
411
494
  a[id] = tileset
412
495
  end
@@ -426,7 +509,7 @@ module AGL
426
509
  def self.sound id, global = false, ext = ".wav"
427
510
  if global; a = @@global_sounds; else; a = @@sounds; end
428
511
  return a[id] if a[id]
429
- s = @@prefix + "data/sound/" + id.to_s.split('_').join('/') + ext
512
+ s = @@prefix + "sound/" + id.to_s.split('_').join('/') + ext
430
513
  sound = Gosu::Sample.new Game.window, s
431
514
  a[id] = sound
432
515
  end
@@ -446,7 +529,7 @@ module AGL
446
529
  def self.song id, global = false, ext = ".ogg"
447
530
  if global; a = @@global_songs; else; a = @@songs; end
448
531
  return a[id] if a[id]
449
- s = @@prefix + "data/song/" + id.to_s.split('_').join('/') + ext
532
+ s = @@prefix + "song/" + id.to_s.split('_').join('/') + ext
450
533
  song = Gosu::Song.new Game.window, s
451
534
  a[id] = song
452
535
  end
@@ -470,7 +553,7 @@ module AGL
470
553
  if global; a = @@global_fonts; else; a = @@fonts; end
471
554
  id_size = "#{id}_#{size}"
472
555
  return a[id_size] if a[id_size]
473
- s = @@prefix + "data/font/" + id.to_s.split('_').join('/') + ext
556
+ s = @@prefix + "font/" + id.to_s.split('_').join('/') + ext
474
557
  font = Gosu::Font.new Game.window, s, size
475
558
  a[id_size] = font
476
559
  end
data/lib/minigl/map.rb CHANGED
@@ -5,6 +5,11 @@ module AGL
5
5
  # a grid of equally sized tiles. It also provides viewport control, through
6
6
  # its camera property and methods.
7
7
  class Map
8
+ # :nodoc:
9
+ Sqrt2Div2 = Math.sqrt(2) / 2
10
+ # :nodoc:
11
+ MinusPiDiv4 = -Math::PI / 4
12
+
8
13
  # A Vector where x is the tile width and y is the tile height.
9
14
  attr_reader :tile_size
10
15
 
@@ -145,8 +150,6 @@ module AGL
145
150
  end
146
151
 
147
152
  private
148
- Sqrt2Div2 = Math.sqrt(2) / 2
149
- MinusPiDiv4 = -Math::PI / 4
150
153
 
151
154
  def set_bounds
152
155
  if @isometric
@@ -242,20 +245,14 @@ module AGL
242
245
  center.y *= @tile_ratio
243
246
 
244
247
  # Rotaciona o vetor posição -45°
245
- position = rotate position, MinusPiDiv4
248
+ position.rotate! MinusPiDiv4
246
249
 
247
250
  # Retorna a referência da posição para o canto da tela
248
- position.x += center.x; position.y += center.y
251
+ position += center
249
252
 
250
253
  # O mapa quadrado está centralizado no centro do losango, precisa retornar ao canto da tela
251
254
  position.x -= @isometric_offset_x; position.y -= @isometric_offset_y
252
255
  position
253
256
  end
254
-
255
- def rotate v, angle
256
- sin = Math.sin angle
257
- cos = Math.cos angle
258
- Vector.new cos * v.x - sin * v.y, sin * v.x + cos * v.y
259
- end
260
257
  end
261
258
  end
@@ -6,7 +6,7 @@ class SpriteTest < Test::Unit::TestCase
6
6
  def setup
7
7
  @window = Gosu::Window.new 800, 600, false
8
8
  Game.initialize @window
9
- Res.prefix = File.expand_path(File.dirname(__FILE__))
9
+ Res.prefix = File.expand_path(File.dirname(__FILE__)) + '/data'
10
10
  end
11
11
 
12
12
  def test_sprite_position
@@ -33,7 +33,7 @@ class GameObjectTest < Test::Unit::TestCase
33
33
  def setup
34
34
  @window = Gosu::Window.new 800, 600, false
35
35
  Game.initialize @window
36
- Res.prefix = File.expand_path(File.dirname(__FILE__))
36
+ Res.prefix = File.expand_path(File.dirname(__FILE__)) + '/data'
37
37
  end
38
38
 
39
39
  def test_game_object_attributes
data/test/res_tests.rb CHANGED
@@ -6,7 +6,7 @@ class ResTest < Test::Unit::TestCase
6
6
  def setup
7
7
  @window = Gosu::Window.new 800, 600, false
8
8
  Game.initialize @window
9
- Res.prefix = File.expand_path(File.dirname(__FILE__))
9
+ Res.prefix = File.expand_path(File.dirname(__FILE__)) + '/data'
10
10
  end
11
11
 
12
12
  def test_tileset
@@ -0,0 +1,56 @@
1
+ require 'test/unit'
2
+ require_relative '../lib/minigl'
3
+ include AGL
4
+
5
+ class VectorTest < Test::Unit::TestCase
6
+ def test_vector_comparison
7
+ v1 = Vector.new 1, 2
8
+ v2 = v1.clone
9
+ assert v1 == v2
10
+ assert !(v1 != v2)
11
+
12
+ v1 = Vector.new 1.37, 4.56
13
+ v2 = Vector.new 1.373, 4.562
14
+ assert v1.==(v2, 2)
15
+ assert v1.!=(v2, 3)
16
+ end
17
+
18
+ def test_vector_operations
19
+ v1 = Vector.new 1, 1
20
+ v2 = Vector.new 2, 3
21
+
22
+ v3 = v1 + v2
23
+ assert_equal 3, v3.x
24
+ assert_equal 4, v3.y
25
+
26
+ v3 = v1 - v2
27
+ assert_equal -1, v3.x
28
+ assert_equal -2, v3.y
29
+
30
+ v3 = v2 * 3
31
+ assert_equal 6, v3.x
32
+ assert_equal 9, v3.y
33
+
34
+ v3 = v2 / 2
35
+ assert_equal 1, v3.x
36
+ assert_equal 1.5, v3.y
37
+
38
+ v3 = Vector.new 0, Math.sqrt(2)
39
+ assert v1.rotate(Math::PI / 4) == v3
40
+
41
+ v1.rotate! Math::PI / 4
42
+ assert v1 == v3
43
+ end
44
+
45
+ def test_vector_distance
46
+ v1 = Vector.new 1, 1
47
+ v2 = Vector.new 2, 3
48
+ d = v1.distance v2
49
+ assert_equal Math.sqrt(5), d
50
+
51
+ v1 = Vector.new 0, 3
52
+ v2 = Vector.new 4, 0
53
+ d = v1.distance v2
54
+ assert_equal 5, d
55
+ end
56
+ 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: 1.3.6
4
+ version: 1.3.7
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: 2014-10-20 00:00:00.000000000 Z
11
+ date: 2014-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gosu
@@ -59,6 +59,7 @@ files:
59
59
  - test/map_tests.rb
60
60
  - test/movement_tests.rb
61
61
  - test/res_tests.rb
62
+ - test/vector_tests.rb
62
63
  homepage: https://github.com/victords/minigl
63
64
  licenses:
64
65
  - MIT
@@ -84,6 +85,7 @@ signing_key:
84
85
  specification_version: 4
85
86
  summary: MiniGL
86
87
  test_files:
88
+ - test/vector_tests.rb
87
89
  - test/movement_tests.rb
88
90
  - test/map_tests.rb
89
91
  - test/game.rb