summaryrefslogtreecommitdiff
path: root/tilemap.lua
blob: f809598783e6e19ccd4b5c4c0c4f41f67981cb48 (plain)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
local Entity = require("entity")
local Liquid = require("liquid")
local LiquidSurface = require("liquidSurface")
local Spike = require("spike")
local TextTrigger = require("textTrigger")
local Pickup = require("pickup")

local Tilemap = {}
Tilemap.__index = Tilemap

local function loadMapData(mapPath)
    if type(mapPath) ~= "string" then
        return mapPath
    end
    if love and love.filesystem and love.filesystem.load then
        local chunk, err = love.filesystem.load(mapPath)
        if not chunk then
            error("Tilemap: failed to load '" .. tostring(mapPath) .. "': " .. tostring(err))
        end
        return chunk()
    end
    local mod = mapPath:gsub("%.lua$", ""):gsub("/", ".")
    local ok, data = pcall(require, mod)
    if not ok then
        error("Tilemap: failed to load '" .. tostring(mapPath) .. "': " .. tostring(data))
    end
    return data
end

local function objectToEntity(obj)
    local x = obj.x or 0
    local y = obj.y or 0
    local w = obj.width or 0
    local h = obj.height or 0
    local entity = Entity:new(x, y, w, h)
    entity:setPropertiesFromOptions({
        properties = obj.properties or {},
        name = obj.name,
        type = obj.type,
        id = obj.id,
        rotation = obj.rotation,
        visible = obj.visible,
        shape = obj.shape
    })
    entity.polygon = obj.polygon
    entity.polyline = obj.polyline
    return entity
end

function Tilemap:new(mapPath, tilesets)
    local self = setmetatable({}, Tilemap)

    self.entitiesTiles = {}
    self.entitiesSpawns = {}
    self.entitiesCameraBorders = {}
    self.entitiesLiquidPolygons = {}
    self.entitiesLiquidSurfaces = {}
    self.entitiesSpikes = {}
    self.entitiesTextTriggers = {}
    self.entitiesPickups = {}
    self.entityDoor = nil
    self.layerBackground = nil
    self.layerDecorationBackground = nil
    self.layerDecorationForeground = nil
    self.layerFarground = nil
    self.layerGround = nil
    self.layerForeground = nil
    self.tileWidth = 16
    self.tileHeight = 16
    self.mapWidth = 0
    self.mapHeight = 0
    self.tileGidInfo = {}
    self.layerTileDimensions = {}

    local mapData = loadMapData(mapPath)
    self.mapData = mapData

    self.tileWidth = mapData and (mapData.tilewidth or 16) or 16
    self.tileHeight = mapData and (mapData.tileheight or 16) or 16
    self.mapWidth = (mapData and mapData.width or 0) * self.tileWidth
    self.mapHeight = (mapData and mapData.height or 0) * self.tileHeight

    if mapData and mapData.tilesets then
        for _, ts in ipairs(mapData.tilesets) do
            local info = tilesets[ts.name]
            if info then
                local ok, img = pcall(love.graphics.newImage, info.path)
                if ok and img then
                    local tw = info.tilewidth or self.tileWidth
                    local th = info.tileheight or self.tileHeight
                    local imgW, imgH = img:getDimensions()
                    local cols = math.floor(imgW / tw)
                    local rows = math.floor(imgH / th)
                    for index = 0, (cols * rows) - 1 do
                        local qx = (index % cols) * tw
                        local qy = math.floor(index / cols) * th
                        local gid = ts.firstgid + index
                        local quad = love.graphics.newQuad(qx, qy, tw, th, imgW, imgH)
                        self.tileGidInfo[gid] = { image = img, quad = quad, tilewidth = tw, tileheight = th }
                    end
                end
            end
        end
    end

    if mapData and mapData.layers then
        for _, layer in ipairs(mapData.layers) do
            if layer.type == "tilelayer" and layer.name then
                local n = layer.name:lower()
                if n == "background" then self.layerBackground = layer
                elseif n == "farground" then self.layerFarground = layer
                elseif n == "decoration_background" then self.layerDecorationBackground = layer
                elseif n == "decoration_foreground" then self.layerDecorationForeground = layer
                elseif n == "ground" then self.layerGround = layer
                elseif n == "foreground" then self.layerForeground = layer
                end
                local dims = nil
                if layer.data then
                    for _, gid in ipairs(layer.data) do
                        if gid and gid ~= 0 then
                            local gi = self.tileGidInfo[gid]
                            if gi then
                                dims = { tilewidth = gi.tilewidth, tileheight = gi.tileheight }
                                break
                            end
                        end
                    end
                end
                self.layerTileDimensions[n] = dims or { tilewidth = self.tileWidth, tileheight = self.tileHeight }
            elseif layer.type == "objectgroup" and layer.objects then
                local name = (layer.name or ""):gsub("%s+", "_"):lower()
                for _, obj in ipairs(layer.objects) do
                    local entity = objectToEntity(obj)
                    if name == "tiles" or name == "tile" then
                        table.insert(self.entitiesTiles, entity)
                    elseif name == "spawn" then
                        table.insert(self.entitiesSpawns, entity)
                    elseif name == "spikes" then
                        table.insert(self.entitiesSpikes, Spike.new(entity))
                    elseif name == "text_trigger" then
                        table.insert(self.entitiesTextTriggers, TextTrigger.new(entity))
                    elseif name == "camera_border" then
                        table.insert(self.entitiesCameraBorders, entity)
                    elseif name == "liquid" then
                        table.insert(self.entitiesLiquidPolygons, Liquid:new(entity))
                    elseif name == "liquid_surface" then
                        table.insert(self.entitiesLiquidSurfaces, LiquidSurface:new(entity))
                    elseif name == "pickups" then
                        table.insert(self.entitiesPickups, Pickup.new(entity))
                    elseif name == "door" then
                        entity.isDoor = true
                        self.entityDoor = entity
                    end
                end
            end
        end
    end

    return self
end

function Tilemap:getEntitiesLiquidPolygons()
    return self.entitiesLiquidPolygons
end

function Tilemap:getEntitiesLiquidSurfaces()
    return self.entitiesLiquidSurfaces
end

function Tilemap:getEntitiesTiles()
    return self.entitiesTiles
end

function Tilemap:getEntitiesSpawns()
    return self.entitiesSpawns
end

function Tilemap:getEntitiesSpikes()
    return self.entitiesSpikes
end

function Tilemap:getEntityDoor()
    return self.entityDoor
end

function Tilemap:getEntitiesTextTriggers()
    return self.entitiesTextTriggers
end

function Tilemap:getEntitiesPickups()
    return self.entitiesPickups
end

function Tilemap:getEntitiesCameraBorders()
    return self.entitiesCameraBorders
end

function Tilemap:getCameraLimits()
    local borders = self.entitiesCameraBorders
    if not borders or #borders == 0 then
        return nil, nil, nil, nil
    end
    local minX, maxX = math.huge, -math.huge
    local minY, maxY = math.huge, -math.huge
    for _, e in ipairs(borders) do
        local x, y = e.x or 0, e.y or 0
        local w, h = e.width or 0, e.height or 0
        minX = math.min(minX, x)
        maxX = math.max(maxX, x + w)
        minY = math.min(minY, y)
        maxY = math.max(maxY, y + h)
    end
    return minX, maxX, minY, maxY
end

function Tilemap:getMapData()
    return self.mapData
end

function Tilemap:getBackgroundLayer()
    return self.layerBackground
end

function Tilemap:getFargroundLayer()
    return self.layerFarground
end

function Tilemap:getDecorationBackgroundLayer()
    return self.layerDecorationBackground
end

function Tilemap:getDecorationForegroundLayer()
    return self.layerDecorationForeground
end

function Tilemap:getGroundLayer()
    return self.layerGround
end

function Tilemap:getForegroundLayer()
    return self.layerForeground
end

function Tilemap:getLayers()
    return {
        background = self.layerBackground,
        ground = self.layerGround,
        foreground = self.layerForeground
    }
end

function Tilemap:getTileWidth()
    return self.tileWidth
end

function Tilemap:getTileHeight()
    return self.tileHeight
end

function Tilemap:getLayerTileDimensions(layerName)
    return self.layerTileDimensions[layerName:lower()]
        or { tilewidth = self.tileWidth, tileheight = self.tileHeight }
end

function Tilemap:getMapWidth()
    return self.mapWidth
end

function Tilemap:getMapHeight()
    return self.mapHeight
end

function Tilemap:getTileGidInfo()
    return self.tileGidInfo
end

return Tilemap