-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.lua
More file actions
126 lines (113 loc) · 3.34 KB
/
map.lua
File metadata and controls
126 lines (113 loc) · 3.34 KB
1
2
3
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
Tile = require 'tile'
local Map = class('Map')
local TILES_PER_ROW = 3
function Map:initialize( rowCount, pattern, mapLayer )
self.rowCount = rowCount
self.pattern = pattern
self.mapLayer = mapLayer
self.tiles = {}
self:draw()
end
function Map:flipAllTiles()
for k,tile in pairs(self.tiles) do
tile:flip()
end
end
function Map:draw()
--Draw the origin tile first
local drawstyle = "draw_origin"
local posx, posy = 0,0
local rposx, rposy = 0,0
local per_row = 1
if self.pattern == 'wedge' then
for row=1, self.rowCount do
--Do the first iteration of the Tile Drawing outside the for loop so we can store the
--position of the first thing we draw for later, when we start the next row
posx, posy = self:_draw_tile( drawstyle, rposx, rposy)
--Store the position of the first item in the row so we can hop down on the next row
rposx, rposy = posx, posy
drawstyle = 'draw_next_in_row'
--Draw the rest of the tiles in the row
for tile=2, per_row do
posx, posy = self:_draw_tile( drawstyle, posx, posy)
drawstyle = 'draw_next_in_row'
end
--Get ready to draw the next row, and increase row size by one on each iteration
drawstyle = 'draw_new_row'
per_row = per_row + 1
end
elseif self.pattern == 'column' then
--Basically the same above, except we always have 3 tiles per row and we start rows slightly differently
per_row = TILES_PER_ROW
for row=1, self.rowCount do
posx, posy = self:_draw_tile( drawstyle, rposx, rposy )
rposx, rposy = posx, posy
drawstyle = 'draw_next_in_row'
for tile=2, per_row do
posx, posy = self:_draw_tile( drawstyle, posx, posy )
drawstyle = 'draw_next_in_row'
end
drawstyle = 'draw_new_row'
end
end
self:flipAllTiles()
end
--Annoying helper class because i cant abuse references like python
function Map:_draw_tile( style, posx, posy )
if style == 'draw_origin' then
return self:draw_origin( posx, posy )
elseif style == 'draw_new_row' then
return self:draw_new_row( posx, posy )
elseif style == 'draw_next_in_row' then
return self:draw_next_in_row( posx, posy )
end
end
--Draw the first tile, and return the location you were given
function Map:draw_origin( posx, posy )
local tile = Tile:new(
posx
, posy
, self.mapLayer
)
table.insert(self.tiles, tile)
return tile.x, tile.y
end
--Draw a tile that starts a new row (so, south east in the wedge or south maybe in the column)
function Map:draw_new_row( posx, posy )
local tile
if self.pattern == 'wedge' then
tile = Tile:new(
5 * cos30deg * 25 + posx -- X
, -1.5 * 25 + posy -- Y
, self.mapLayer -- Render Layer
)
elseif self.pattern == 'column' then
tile = Tile:new(
cos30deg * 25 + posx -- X
, -4.5 * 25 + posy -- Y
, self.mapLayer -- Render Layer
)
end
table.insert(self.tiles, tile)
return tile.x, tile.y
end
--Draw a tile that continues the current row (so, south west)
function Map:draw_next_in_row( posx, posy )
local tile
if self.pattern == 'wedge' then
tile = Tile:new(
-4 * cos30deg * 25 + posx -- X
, -3 * 25 + posy -- Y
, self.mapLayer -- Render Layer
)
elseif self.pattern == 'column' then
tile = Tile:new(
-4 * cos30deg * 25 + posx -- X
, -3 * 25 + posy -- Y
, self.mapLayer -- Render Layer
)
end
table.insert(self.tiles, tile)
return tile.x, tile.y
end
return Map