sonic-radiance/sonic-radiance.love/scenes/overworld/map.lua
2021-03-24 11:57:42 +01:00

140 lines
3.8 KiB
Lua

local Parent = require "core.modules.world.maps.parent"
local TiledMultiMap = Parent:extend()
local StiWrapper = require "core.modules.world.maps.tiled.stiwrapper"
local TiledMixins = require "core.modules.world.maps.tiled.mixins"
TiledMultiMap:implement(TiledMixins)
local mapFolder = "datas/gamedata/maps/sti/"
local areaFolder = "datas.gamedata.maps.area."
function TiledMultiMap:new(world, subworld, file)
self.wrappers = {}
self.data = require(areaFolder .. subworld .. "." .. file)
self.w, self.h = 0, 0
self:loadMaps()
TiledMultiMap.super.new(self, world)
self.supportTileCollision = true
self:setBackgroundColorFromTable(self.data.color)
self.mapname = self.data.areaName
end
function TiledMultiMap:getMapName()
return self.data.areaName
end
function TiledMultiMap:getDimensions()
return self.w, self.h
end
function TiledMultiMap:loadObjects()
for i, wrapper in ipairs(self.wrappers) do
wrapper:loadObjects()
end
end
-- MAP HANDLING FUNCTIONS
-- Handle maps
function TiledMultiMap:loadMaps()
for i, mapData in ipairs(self.data.maps) do
self:addMap(i, mapData)
end
end
function TiledMultiMap:addMap(id, mapData)
print("Loading map " .. mapData.name)
local mapFile = mapFolder .. mapData.folder .. "/" .. mapData.map .. ".lua"
local wrapper = StiWrapper(self, mapFile, mapData.x, mapData.y)
wrapper.id = id
wrapper.music = mapData.music
wrapper.name = mapData.name
table.insert(self.wrappers, wrapper)
self:pushDimensions(wrapper)
end
function TiledMultiMap:pushDimensions(wrapper)
local x, y, w, h = wrapper:getRect()
self.w = math.max(self.w, x + w)
self.h = math.max(self.h, y + h)
end
function TiledMultiMap:getMapAtPoint(x, y)
for i, wrapper in ipairs(self.wrappers) do
local recx, recy, recw, rech = wrapper:getRect()
local relx, rely = x - recx, y - recy
if (relx <= recw and relx >= 0) then
if (rely <= rech and rely >= 0) then
return wrapper
end
end
end
end
function TiledMultiMap:getMapsInRect(x, y, w, h)
local listWrapper = {}
for i, wrapper in ipairs(self.wrappers) do
local recx, recy, recw, rech = wrapper:getRect()
local relx, rely = x - recx, y - recy
if (relx <= recw and relx + w >= 0) then
if (rely <= rech and rely + h >= 0) then
table.insert(listWrapper, wrapper)
end
end
end
return listWrapper
end
-- TILE FUNCTIONS
-- Get tiles
function TiledMultiMap:haveTileTypeInRect(x, y, w, h, type)
local listWrapper = self:getMapsInRect(x, y, w, h)
for i, wrapper in ipairs(listWrapper) do
local haveTile = wrapper:haveTileTypeInRect(x, y, w, h, type)
if (haveTile) then
return true
end
return false
end
end
-- UPDATE FUNCTION
-- Update or modify the map
function TiledMultiMap:resize(w, h)
for i, wrapper in ipairs(self.wrappers) do
wrapper:resize(w, h)
end
end
function TiledMultiMap:update(dt)
local x, y, w, h = self.world.cameras:getViewCoordinate(1)
local listWrapper = self:getMapsInRect(x, y, w, h)
for i, wrapper in ipairs(listWrapper) do
wrapper:update(dt)
end
end
-- DRAW FUNCTIONS
-- Handle drawing the wrapper
function TiledMultiMap:drawUpperLayers()
local x, y, w, h = self.world.cameras:getViewCoordinate(1)
local listWrapper = self:getMapsInRect(x, y, w, h)
for i, wrapper in ipairs(listWrapper) do
wrapper:drawUpperLayers()
end
end
function TiledMultiMap:drawLowerLayers()
local x, y, w, h = self.world.cameras:getViewCoordinate(1)
local listWrapper = self:getMapsInRect(x, y, w, h)
for i, wrapper in ipairs(listWrapper) do
wrapper:drawLowerLayers()
end
end
return TiledMultiMap