-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathisometrictilemap.lua
118 lines (101 loc) · 3.15 KB
/
isometrictilemap.lua
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
---with TILE FLIP SUPPORT
IsometricTileMap = Core.class(Sprite)
function IsometricTileMap:init(width, height,
texture,
tilewidth, tileheight,
spacingx, spacingy,
marginx, marginy,
displaywidth, displayheight)
spacingx = spacingx or 0
spacingy = spacingy or 0
marginx = marginx or 0
marginy = marginy or 0
displaywidth = displaywidth or tilewidth
displayheight = displayheight or tileheight
self.width = width
self.height = height
self.texture = texture
self.tilewidth = tilewidth
self.tileheight = tileheight
self.spacingx = spacingx
self.spacingy = spacingy
self.marginx = marginx
self.marginy = marginy
self.displaywidth = displaywidth
self.displayheight = displayheight
local height2 = width + height - 1
local width2 = math.min(width, height)
local rows = {}
self.rows = rows
for j=1,height2 do
local mesh = Mesh.new()
mesh:setTexture(texture)
self:addChild(mesh)
rows[#rows + 1] = mesh
end
for j=1,height2 do
local width3
if j < width2 then
width3 = j
elseif j > height2 - width2 + 1 then
width3 = height2 + 1 - j
else
width3 = width2
end
local ind = 1
local vert = 1
local mesh = rows[j]
mesh:resizeVertexArray(width3 * 4)
mesh:resizeTextureCoordinateArray(width3 * 4)
for i=1,width3 do
mesh:setIndices(ind, vert,
ind + 1, vert + 1,
ind + 2, vert + 2,
ind + 3, vert,
ind + 4, vert + 2,
ind + 5, vert + 3)
ind = ind + 6
vert = vert + 4
end
end
end
function IsometricTileMap:setTile(x, y, tx, ty, flip)
local tilewidth = self.tilewidth
local tileheight = self.tileheight
local displaywidth = self.displaywidth
local displayheight = self.displayheight
local j = x + y - 1
local mesh = self.rows[j]
local index = math.min(0, self.height - j) + x
local vert = (index - 1) * 4 + 1
local px = (x - y - 1) * displaywidth / 2
local py = (x + y - 2) * displayheight / 2 - (tileheight - displayheight)
mesh:setVertices(vert, px, py,
vert + 1, px + tilewidth, py,
vert + 2, px + tilewidth, py + tileheight,
vert + 3, px, py + tileheight)
local tx2 = self.marginx + (tx - 1) * (tilewidth + self.spacingx)
local ty2 = self.marginy + (ty - 1) * (tileheight + self.spacingy)
-- print("FLIP:",flip)
if flip == 4 then --FLIP.HORIZONTAL
mesh:setTextureCoordinates(vert, tx2 + tilewidth, ty2,
vert + 1, tx2, ty2,
vert + 2, tx2, ty2 + tileheight,
vert + 3, tx2 + tilewidth, ty2 + tileheight)
elseif flip == 2 then --FLIP.VERTICAL
mesh:setTextureCoordinates(vert, tx2, ty2 + tileheight,
vert + 1, tx2 + tilewidth, ty2 + tileheight,
vert + 2, tx2 + tilewidth, ty2,
vert + 3, tx2, ty2)
elseif flip == 6 then --FLIP.DIAGONAL
mesh:setTextureCoordinates(vert, tx2 + tilewidth, ty2 + tileheight,
vert + 1, tx2, ty2 + tileheight,
vert + 2, tx2, ty2,
vert + 3, tx2 + tilewidth, ty2 )
else --NO FLIP
mesh:setTextureCoordinates(vert, tx2, ty2,
vert + 1, tx2 + tilewidth, ty2,
vert + 2, tx2 + tilewidth, ty2 + tileheight,
vert + 3, tx2, ty2 + tileheight)
end
end