gridmap 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +15 -0
  2. data/lib/gridmap.rb +162 -0
  3. metadata +45 -0
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NzcwOWY5YzRhYzY2YzhmMjAwMmYyMTk0OGFlOTRjZjE2M2E5MWMwMA==
5
+ data.tar.gz: !binary |-
6
+ Mjg1ZjU3OTFhNjE4ZjUxNjE3N2IwNzRiZmIzOWZhNWVhZDhiNjU0NQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ Y2Y4OTU0MDg0MDYwNTA0NmJmZDc4NGZhNmRjNDAxZGQ5MDYzZjgwZjEwY2E3
10
+ M2YwZjk5ZWRmMGIzOWJjM2MyZGU4OTg2NWU5ZGJjMmI0MGZkYjM1YTI0OWNi
11
+ MzljZTAyYjkyZjcyYzAxYzhiZTc2Njk4Y2I4ZTVmY2NlZWE5YjE=
12
+ data.tar.gz: !binary |-
13
+ YWY2NWYwNjkwZmZlNzY0N2MxZDI4YjZhNjBjYzM3NWQ2NDE3MzNkNjYzYzNh
14
+ ZGM2MTQ5NTgyZDUzYWQzOGJjYjZlZDM3NDM4MWU2NmFhMDUwMjEwZjE2Yzk0
15
+ NjM5ZDhhNGJlZjgxMWY5ZTIyMDFiODkyMGIzMTA3MzkzM2IzOWE=
@@ -0,0 +1,162 @@
1
+ class Gridmap < Array
2
+ attr_reader :tile_size, :grid_width, :grid_height, :pixel_width, :pixel_height, :connected_tiles, :orientated_tiles
3
+ def initialize(map_path, symbol_details, tile_size)
4
+ @tile_size = tile_size
5
+ @connected_tiles = []
6
+ @orientated_tiles = []
7
+
8
+ IO.readlines(map_path).each_with_index do |line, y|
9
+ gridline = []
10
+ line.chomp.split("").each_with_index do |symbol, x|
11
+ t = Tile.new(x, y, tile_size, symbol_details[symbol])
12
+ gridline << t
13
+ @connected_tiles << t if t.details[:connected]
14
+ @orientated_tiles << t if t.details[:orientated]
15
+ end
16
+ self << gridline
17
+ end
18
+
19
+ #Since all lines should contain the same number of characters, the width is set by counting the number of characters in the first line.
20
+ @grid_width = self.first.count - 1
21
+ @grid_height = self.count - 1
22
+
23
+ @pixel_width = (@grid_width + 1) * tile_size
24
+ @pixel_height = (@grid_height + 1) * tile_size
25
+ end
26
+
27
+ def define_tiles(window, images)
28
+ #The check method checks a tile with the given grid co-ordinates to see if it is connected or not.
29
+ #The logic here prevents a tile from being checked if it lies outside the grid.
30
+ def check(x, y)
31
+ if y < 0 || x < 0 || y > @grid_height || x > @grid_width
32
+ false
33
+ elsif self[y][x].details[:connected] || self[y][x].details[:orientated]
34
+ true
35
+ else
36
+ false
37
+ end
38
+ end
39
+
40
+ #Works out connections for connected or orientated tiles.
41
+ (@connected_tiles + @orientated_tiles).each do |tile|
42
+ connections = [false, false, false, false] # up right down left
43
+ connections[0] = check(tile.x, tile.y - 1) #up
44
+ connections[1] = check(tile.x + 1, tile.y) #right
45
+ connections[2] = check(tile.x, tile.y + 1) #down
46
+ connections[3] = check(tile.x - 1, tile.y) #left
47
+ tile.connections = connections
48
+ end
49
+
50
+ #Sets image and angle for connected tiles based on their connections.
51
+ @connected_tiles.each do |tile|
52
+ case tile.connections.count(true)
53
+ when 0
54
+ img_key = :block
55
+ when 1
56
+ img_key = :straight
57
+ tile.angle = 90 if tile.connections[1] || tile.connections[3]
58
+ when 2
59
+ if tile.connections == [true, false, true, false]
60
+ img_key = :straight
61
+ elsif tile.connections == [false, true, false, true]
62
+ img_key = :straight
63
+ tile.angle = 90
64
+ else
65
+ img_key = :corner
66
+ case tile.connections
67
+ when [false, true, true, false]
68
+ #angle does not need to change
69
+ when [false, false, true, true]
70
+ tile.angle = 90
71
+ when [true, false, false, true]
72
+ tile.angle = 180
73
+ when [true, true, false, false]
74
+ tile.angle = 270
75
+ end
76
+ end
77
+ when 3
78
+ img_key = :tri
79
+ for n in 0..3
80
+ tile.angle = 90 * n if tile.connections[n] == false
81
+ end
82
+ when 4
83
+ img_key = :quad
84
+ end
85
+
86
+ tile.image = Gosu::Image.new(window, images[img_key], true)
87
+ end
88
+
89
+ angle_orientated_tiles
90
+
91
+ #Sets image for all non-connected tiles.
92
+ self.each do |line|
93
+ line.each do |tile|
94
+ unless tile.details[:connected]
95
+ #img_key is inferred from the tile's type!
96
+ img_key = tile.details[:type].to_sym
97
+ tile.image = Gosu::Image.new(window, images[img_key], true)
98
+ end
99
+ end
100
+ end
101
+ end
102
+
103
+ def angle_orientated_tiles
104
+ #Sets angle for orientated tiles based on nearest connected tile in a clockwise direction.
105
+ #CHANGE THIS METHOD AS YOU WISH.
106
+ @orientated_tiles.each do |t1|
107
+ @connected_tiles.each do |t2|
108
+ if t2.y == t1.y - 1
109
+ #dont change from default angle
110
+ elsif t2.x == t1.x + 1
111
+ t1.angle = 90
112
+ elsif t2.y == t1.y + 1
113
+ t1.angle = 180
114
+ elsif t2.x == t1.x - 1
115
+ t1.angle = 270
116
+ else
117
+ #dont change from default angle
118
+ end
119
+ end
120
+ end
121
+ end
122
+
123
+ def draw_tiles
124
+ self.each {|line| line.each {|tile| tile.draw}}
125
+ end
126
+
127
+ def find_tile(x, y)
128
+ self[(y / @tile_size).floor][(x / @tile_size).floor]
129
+ end
130
+ end
131
+
132
+ class Tile
133
+ attr_reader :x, :y
134
+ attr_accessor :angle, :centre_point, :connections, :details, :image, :z
135
+ def initialize(x, y, size, details)
136
+ @x, @y, @size, @details = x, y, size, details
137
+
138
+ @offset_x = details[:offset_x] if details[:offset_x]
139
+ @offset_y = details[:offset_y] if details[:offset_y]
140
+ @z = details[:z] if details[:z]
141
+ @angle = details[:angle] if details[:angle]
142
+
143
+ @offset_x ||= 0
144
+ @offset_y ||= 0
145
+ @z ||= 0
146
+ @angle ||= 0
147
+
148
+ @centre_point = [@x * @size + @size / 2, @y * @size + @size / 2]
149
+ end
150
+
151
+ def draw
152
+ if @details[:connected] || @details[:orientated]
153
+ draw_rot
154
+ else
155
+ @image.draw @x * @size + @offset_x, @y * @size + @offset_y, @z
156
+ end
157
+ end
158
+
159
+ def draw_rot
160
+ @image.draw_rot @centre_point[0], @centre_point[1], @z, @angle
161
+ end
162
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gridmap
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Normangorman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Gridmap makes tile based maps easy. It offers 2 new classes - a gridmap
14
+ and a tile. It takes care of drawing tiles with connected textures (e.g. walls).
15
+ email: 8076bgorman@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/gridmap.rb
21
+ homepage: https://github.com/Normangorman/gridmap
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.1.9
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: An extension for the libgosu library to make tile based maps easy.
45
+ test_files: []