feat: stop hardcoding position in maps

This commit is contained in:
Kazhnuz 2021-04-02 23:26:22 +02:00
parent 001ae44eec
commit fbbe20e225
8 changed files with 58 additions and 21 deletions

View file

@ -1,11 +1,12 @@
local StiWrapper = Object:extend() local StiWrapper = Object:extend()
local STI = require "core.modules.world.maps.tiled.libs.sti" local STI = require "core.modules.world.maps.tiled.libs.sti"
function StiWrapper:new(owner, mapfile, x, y) function StiWrapper:new(owner, mapfile, x, y, canLoadPlayer)
self.sti = STI(mapfile) self.sti = STI(mapfile)
self.owner = owner self.owner = owner
self.x = x or 0 self.x = x or 0
self.y = y or 0 self.y = y or 0
self.canLoadPlayer = (canLoadPlayer ~= false)
self.objectlayer = 0 self.objectlayer = 0
self.isLoaded = false self.isLoaded = false
@ -84,6 +85,7 @@ end
function StiWrapper:loadPlayers() function StiWrapper:loadPlayers()
for k, objectlayer in pairs(self.sti.layers) do for k, objectlayer in pairs(self.sti.layers) do
if (objectlayer.name == "player") then if (objectlayer.name == "player") then
if (self.canLoadPlayer) then
local debugstring = local debugstring =
"loading at most " .. #objectlayer.objects .. " actors in " .. objectlayer.name .. " actor layer" "loading at most " .. #objectlayer.objects .. " actors in " .. objectlayer.name .. " actor layer"
core.debug:print("map/sti", debugstring) core.debug:print("map/sti", debugstring)
@ -92,6 +94,7 @@ function StiWrapper:loadPlayers()
self.owner:newPlayer(object, i, self.x, self.y) self.owner:newPlayer(object, i, self.x, self.y)
i = i + 1 i = i + 1
end end
end
self.sti:removeLayer(objectlayer.name) self.sti:removeLayer(objectlayer.name)
end end
end end

View file

@ -0,0 +1,8 @@
return {
position = {
x = 10,
y = 10,
area = "test.plain"
},
baseteam = {"sonic", "tails", "amy"}
}

View file

@ -26,12 +26,13 @@ local CharacterManager = Object:extend()
local charutils = require "game.utils.characters" local charutils = require "game.utils.characters"
local AbstractCharacter = require "game.abstractmobs.character" local AbstractCharacter = require "game.abstractmobs.character"
local startdata = require "datas.gamedata.startdata"
function CharacterManager:new(controller) function CharacterManager:new(controller)
self.controller = controller self.controller = controller
self.namelist = require "datas.gamedata.characters" self.namelist = require "datas.gamedata.characters"
self.list = {} self.list = {}
self.team = require "datas.gamedata.characters.baseteam" self.team = startdata.baseteam
self.active = 1 self.active = 1
self:init() self:init()
end end

View file

@ -32,6 +32,8 @@ local CBSCore = require "game.battle"
local binser = require "core.modules.gamesystem.libs.binser" local binser = require "core.modules.gamesystem.libs.binser"
local startdata = require "datas.gamedata.startdata"
Game.utils = require "game.modules.utils" Game.utils = require "game.modules.utils"
Game.gui = require "game.modules.gui" Game.gui = require "game.modules.gui"
@ -46,6 +48,8 @@ function Game:new()
self.loot = Loot(self) self.loot = Loot(self)
self.cbs = CBSCore(self) self.cbs = CBSCore(self)
self:initPosition()
self.flags = {} self.flags = {}
self.destroyedGizmo = {} self.destroyedGizmo = {}
self.variables = {} self.variables = {}
@ -53,12 +57,20 @@ function Game:new()
self.version = "0.0.0" self.version = "0.0.0"
end end
function Game:initPosition()
self.position = {}
self.position.x = startdata.position.x
self.position.y = startdata.position.y
self.position.area = startdata.position.area
end
function Game:setData(data) function Game:setData(data)
local data = data local data = data
self.gametime = data.gametime self.gametime = data.gametime
self.destroyedGizmo = data.destroyedGizmo self.destroyedGizmo = data.destroyedGizmo
self.variables = data.variables self.variables = data.variables
self.flags = data.flags self.flags = data.flags
self.position = data.position
self.characters:setData(data.characters) self.characters:setData(data.characters)
end end
@ -69,6 +81,7 @@ function Game:getData()
data.flags = self.flags data.flags = self.flags
data.destroyedGizmo = self.destroyedGizmo data.destroyedGizmo = self.destroyedGizmo
data.variables = self.variables data.variables = self.variables
data.position = self.position
return data return data
end end

View file

@ -36,7 +36,7 @@ local EventManager = require "game.events"
local PLAYER_MESSAGE = 240 - 32 local PLAYER_MESSAGE = 240 - 32
function OverWorld:new() function OverWorld:new(area, playerx, playery)
OverWorld.super.new(self) OverWorld.super.new(self)
self.charsetManager = CharsetManager(self) self.charsetManager = CharsetManager(self)
self.assets:batchImport("game.modules.gui.assets") self.assets:batchImport("game.modules.gui.assets")
@ -45,7 +45,7 @@ function OverWorld:new()
self.tweens = TweenManager(self) self.tweens = TweenManager(self)
self.screens = screens self.screens = screens
World(self, "test", "plain") World(self, area, playerx, playery)
self.world:setPlayerNumber(1) self.world:setPlayerNumber(1)
self.world:loadMap() self.world:loadMap()

View file

@ -8,10 +8,11 @@ TiledMultiMap:implement(TiledMixins)
local mapFolder = "datas/gamedata/maps/sti/" local mapFolder = "datas/gamedata/maps/sti/"
local areaFolder = "datas.gamedata.maps.area." local areaFolder = "datas.gamedata.maps.area."
function TiledMultiMap:new(world, subworld, file) function TiledMultiMap:new(world, area, playerx, playery)
self.wrappers = {} self.wrappers = {}
self.data = require(areaFolder .. subworld .. "." .. file) self.data = require(areaFolder .. area)
self.w, self.h = 0, 0 self.w, self.h = 0, 0
self.playerx, self.playery = playerx * 16, playery * 16
self:loadMaps() self:loadMaps()
TiledMultiMap.super.new(self, world) TiledMultiMap.super.new(self, world)
self.supportTileCollision = true self.supportTileCollision = true
@ -32,6 +33,11 @@ function TiledMultiMap:loadObjects()
for i, wrapper in ipairs(self.wrappers) do for i, wrapper in ipairs(self.wrappers) do
wrapper:loadObjects() wrapper:loadObjects()
end end
self:loadPlayer()
end
function TiledMultiMap:loadPlayer()
self.world:addPlayer(self.playerx, self.playery, 0, 1)
end end
-- MAP HANDLING FUNCTIONS -- MAP HANDLING FUNCTIONS
@ -46,7 +52,7 @@ end
function TiledMultiMap:addMap(id, mapData) function TiledMultiMap:addMap(id, mapData)
print("Loading map " .. mapData.name) print("Loading map " .. mapData.name)
local mapFile = mapFolder .. mapData.folder .. "/" .. mapData.map .. ".lua" local mapFile = mapFolder .. mapData.folder .. "/" .. mapData.map .. ".lua"
local wrapper = StiWrapper(self, mapFile, mapData.x, mapData.y) local wrapper = StiWrapper(self, mapFile, mapData.x, mapData.y, false)
wrapper.id = id wrapper.id = id
wrapper.music = mapData.music wrapper.music = mapData.music
wrapper.name = mapData.name wrapper.name = mapData.name

View file

@ -98,6 +98,7 @@ end
function SaveExitWidget:action() function SaveExitWidget:action()
if (self.save) then if (self.save) then
self.scene.world:savePosition()
game:write() game:write()
end end
if (self.exit) then if (self.exit) then

View file

@ -1,19 +1,24 @@
local World = require "core.modules.world.world2D" local World = require "core.modules.world.world2D"
local RPGWorld = World:extend() local RPGWorld = World:extend()
local objFile = "scenes.overworld.actors" local objFile = "scenes.overworld.actors"
local mapFolder = "datas/gamedata/maps/sti/"
local RPGMap = require "scenes.overworld.map" local RPGMap = require "scenes.overworld.map"
function RPGWorld:new(scene, folder, area, playerx, playery) function RPGWorld:new(scene, area, playerx, playery)
--local mapFile = mapFolder .. folder .. "/" .. map .. ".lua" self.area = area or game.position.area
self.folder = folder self.playerx = playerx or game.position.x
self.area = area self.playery = playery or game.position.y
RPGWorld.super.new(self, scene, objFile, nil, nil) RPGWorld.super.new(self, scene, objFile, nil, nil)
end end
function RPGWorld:createMapController() function RPGWorld:createMapController()
RPGMap(self, self.folder, self.area) RPGMap(self, self.area, self.playerx, self.playery)
end
function RPGWorld:savePosition()
local x, y = self.players[1].actor.x, self.players[1].actor.y
game.position.x = math.floor(x / 16)
game.position.y = math.floor(y / 16)
game.position.area = self.area
end end
function RPGWorld:restoreActions() function RPGWorld:restoreActions()