forked from gideros/Isometric-Tilemap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
132 lines (104 loc) · 3.67 KB
/
main.lua
File metadata and controls
132 lines (104 loc) · 3.67 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
127
128
129
130
local function load(filename)
local tilemap = Sprite.new()
-- Bits on the far end of the 32-bit global tile ID are used for tile flags (flip, rotate)
local FLIPPED_HORIZONTALLY_FLAG = 0x80000000;
local FLIPPED_VERTICALLY_FLAG = 0x40000000;
local FLIPPED_DIAGONALLY_FLAG = 0x20000000;
local map = loadfile(filename)()
for i=1, #map.tilesets do
local tileset = map.tilesets[i]
tileset.sizex = math.floor((tileset.imagewidth - tileset.margin + tileset.spacing) / (tileset.tilewidth + tileset.spacing))
tileset.sizey = math.floor((tileset.imageheight - tileset.margin + tileset.spacing) / (tileset.tileheight + tileset.spacing))
tileset.lastgid = tileset.firstgid + (tileset.sizex * tileset.sizey) - 1
tileset.texture = Texture.new(tileset.image)
end
local function gid2tileset(map, gid)
for i=1, #map.tilesets do
local tileset = map.tilesets[i]
if tileset.firstgid <= gid and gid <= tileset.lastgid then
return tileset
end
end
end
for i=1, #map.layers do
local tileset = map.tilesets[i]
local layer = map.layers[i]
local tilemaps = {}
local group = Sprite.new()
for y=1,layer.height do
for x=1,layer.width do
-- Variables to let us know if the tile is flipped or rotated
local flipHor, flipVer, flipDia = 0, 0, 0
local i = x + (y - 1) * layer.width
local gid = layer.data[i]
-- If not empty tile
if gid ~= 0 then
-- Read flipping flags
flipHor = gid&FLIPPED_HORIZONTALLY_FLAG
flipVer = gid&FLIPPED_VERTICALLY_FLAG
flipDia = gid&FLIPPED_DIAGONALLY_FLAG
-- Convert flags to gideros style
if(flipHor ~= 0) then flipHor = 4 end --TileMap.FLIP_HORIZONTAL end
if(flipVer ~= 0) then flipVer = 2 end --TileMap.FLIP_VERTICAL end
if(flipDia ~= 0) then flipDia = 1 end --TileMap.FLIP_DIAGONAL end
-- Clear the flags from gid so other information is healthy
gid = gid&(~(FLIPPED_HORIZONTALLY_FLAG|FLIPPED_VERTICALLY_FLAG|FLIPPED_DIAGONALLY_FLAG))
end
local tileset = gid2tileset(map, gid)
if tileset then
local tilemap = nil
if tilemaps[tileset] then
tilemap = tilemaps[tileset]
else
tilemap = IsometricTileMap.new(layer.width,
layer.height,
tileset.texture,
tileset.tilewidth,
tileset.tileheight,
tileset.spacing,
tileset.spacing,
tileset.margin,
tileset.margin,
map.tilewidth,
map.tileheight)
tilemaps[tileset] = tilemap
group:addChild(tilemap)
end
local tx = (gid - tileset.firstgid) % tileset.sizex + 1
local ty = math.floor((gid - tileset.firstgid) / tileset.sizex) + 1
-- Set the tile with flip info
tilemap:setTile(x, y, tx, ty, flipHor|flipVer|flipDia)
-- Reset vars, so they dont confuse us in the next iteration
flipHor, flipVer, flipDia = 0, 0, 0
end
end
end
group:setAlpha(layer.opacity)
tilemap:addChild(group)
end
return tilemap
end
local tilemap = load("iso-test-vertexz.lua")
stage:addChild(tilemap)
local dragging, startx, starty
local function onMouseDown(event)
dragging = true
startx = event.x
starty = event.y
end
local function onMouseMove(event)
if dragging then
local dx = event.x - startx
local dy = event.y - starty
tilemap:setX(tilemap:getX() + dx)
tilemap:setY(tilemap:getY() + dy)
startx = event.x
starty = event.y
end
end
local function onMouseUp(event)
dragging = false
end
stage:addEventListener(Event.MOUSE_DOWN, onMouseDown)
stage:addEventListener(Event.MOUSE_MOVE, onMouseMove)
stage:addEventListener(Event.MOUSE_UP, onMouseUp)