54 lines
1.7 KiB
Lua
54 lines
1.7 KiB
Lua
local Object = require("object")
|
|
local Tile = require("tile")
|
|
local helpers = require("helpers")
|
|
local inspect = require("inspect/inspect")
|
|
|
|
local Map = Object:extend()
|
|
|
|
function Map:init(filename)
|
|
Map.__super.init(self)
|
|
local map = dofile(filename) -- love.filesystem.load(filename)
|
|
local platform_layers = helpers.filter(map.layers, function (layer) return layer.name == "platforms" end)
|
|
self.width = map.width
|
|
self.height = map.height
|
|
self.tilewidth = map.tilewidth
|
|
self.tileheight = map.tileheight
|
|
self.data = platform_layers[1].data
|
|
self.max_pixel = {x=map.width*map.tilewidth, y=map.height*map.tileheight}
|
|
end
|
|
|
|
function Map:itertiles()
|
|
local function iterator(map, index)
|
|
index = index + 1
|
|
local value = map.data[index]
|
|
if value ~= nil then
|
|
local tx = ((index - 1) % map.width) + 1
|
|
local ty = math.floor((index - 1) / map.width) + 1
|
|
local tile = Tile:new(tx, ty, map.tilewidth, map.tileheight, value)
|
|
return index, tile
|
|
end
|
|
end
|
|
return iterator, self, 0
|
|
end
|
|
|
|
function Map:draw()
|
|
for i, tile in self:itertiles() do
|
|
love.graphics.setColor(0.9, 0.8, 0.7)
|
|
if tile.solid then
|
|
love.graphics.setColor(0.2, 0.2, 0.2)
|
|
end
|
|
love.graphics.rectangle("fill", tile.pixel_rect.x, tile.pixel_rect.y, tile.w, tile.h)
|
|
love.graphics.setColor(0.9, 0.1, 0.3)
|
|
love.graphics.rectangle("line", tile.pixel_rect.x, tile.pixel_rect.y, tile.w, tile.h)
|
|
end
|
|
end
|
|
|
|
function Map:get_tile_from_pixel(pixel)
|
|
local tile_x = math.floor(pixel.x/self.tilewidth) + 1
|
|
local tile_y = math.floor(pixel.y/self.tileheight) + 1
|
|
local value = self.data[tile_x + (tile_y - 1)*self.width]
|
|
return Tile:new(tile_x, tile_y, self.tilewidth, self.tileheight, value)
|
|
end
|
|
|
|
return Map
|