From 15973e491aaee64221fac87a9098f7354a8876eb Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 18 Apr 2021 10:53:35 +0200 Subject: [PATCH] feat: initial terrain data loading --- .../datas/gamedata/maps/terrains.lua | 62 +++++++++++++ .../scenes/overworld/actors/player/init.lua | 1 + .../overworld/actors/player/interactions.lua | 91 +++++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 sonic-radiance.love/datas/gamedata/maps/terrains.lua diff --git a/sonic-radiance.love/datas/gamedata/maps/terrains.lua b/sonic-radiance.love/datas/gamedata/maps/terrains.lua new file mode 100644 index 0000000..a924234 --- /dev/null +++ b/sonic-radiance.love/datas/gamedata/maps/terrains.lua @@ -0,0 +1,62 @@ +return { + ["non-solid"] = { + { + terrainType = "normal", + forceAction = nil, + level=0, + height=0, + canJump = true + } + }, + ice = { + { + terrainType = "forceAction", + forceAction = "slide", + level=0, + height=0, + canJump = false + } + }, + water = { + { + terrainType = "forceAction", + forceAction = "fall", + level=-32, + height=0, + damage = 10, + speedFactor = 0, + }, + { + terrainType = "normal", + forceAction = nil, + level=-10, + height=0, + mustHaveAction = "swim", + mustDoAction = nil, + speedFactor = 0.8, + canJump = true, + damage = 0, + }, + { + terrainType = "normal", + forceAction = nil, + level=0, + height=0, + mustHaveAction = "run", + mustDoAction = "run", + speedFactor = 1, + canJump = true, + damage = 0, + } + }, + grass = { + { + terrainType = "normal", + forceAction = nil, + level=0, + height=16, + speedFactor = 0.75, + canJump = false, + } + } +} \ No newline at end of file diff --git a/sonic-radiance.love/scenes/overworld/actors/player/init.lua b/sonic-radiance.love/scenes/overworld/actors/player/init.lua index 0ece93b..022b6f7 100644 --- a/sonic-radiance.love/scenes/overworld/actors/player/init.lua +++ b/sonic-radiance.love/scenes/overworld/actors/player/init.lua @@ -39,6 +39,7 @@ end function Player:updateStart(dt) self.tweens:update(dt) + self:updateTerrain() self:actionMove() self:actionJump() diff --git a/sonic-radiance.love/scenes/overworld/actors/player/interactions.lua b/sonic-radiance.love/scenes/overworld/actors/player/interactions.lua index 5f77016..fbd6d8a 100644 --- a/sonic-radiance.love/scenes/overworld/actors/player/interactions.lua +++ b/sonic-radiance.love/scenes/overworld/actors/player/interactions.lua @@ -1,8 +1,28 @@ local PlayerInteractions = Object:extend() +local terrainData = require "datas.gamedata.maps.terrains" + +local MIN_X = 2 +local MAX_X = 14 +local MIN_Y = 9 +local MAX_Y = 15 + +local TERRAIN_CHECKER = { + {x = MIN_X, y = MIN_Y}, + {x = MIN_X, y = MAX_Y}, + {x = MAX_X, y = MIN_Y}, + {x = MAX_X, y = MAX_Y}, +} + function PlayerInteractions:initInteractions() self.lastCollision = -1 self.haveCollided = false + self.terrain = {} + self.terrain.name = "" + self.terrain.data = {} + self.lastPos = {} + self.lastPos.x = math.floor(self.x/16) * 16 + self.lastPos.y = math.floor(self.y/16) * 16 end function PlayerInteractions:updateInteraction() @@ -51,4 +71,75 @@ function PlayerInteractions:faceRightDirection(other) end end +function PlayerInteractions:updateTerrain() + local newTerrain = self:getCurrentTerrain() + + if (self.terrain.name ~= newTerrain) then + print(newTerrain) + self.terrain.name = newTerrain + self:updateTerrainData() + end + if (newTerrain == "non-solid") then + self:updateLastPosition() + end +end + +function PlayerInteractions:updateLastPosition() + local canUpdate = true + for _, coord in ipairs(TERRAIN_CHECKER) do + local newTerrain = self.world:getTileTypeAtPoint(self.x + coord.x, self.y + coord.y) + if (newTerrain ~= "non-solid") then + self.canUpdate = false + break; + end + end + if (canUpdate) then + self.lastPos = {} + self.lastPos.x = math.floor(self.x/16) * 16 + self.lastPos.y = math.floor(self.y/16) * 16 + end +end + +function PlayerInteractions:updateTerrainData() + local dataPack = terrainData[self.terrain.name] + local newData = terrainData["non-solid"][1] + + if (dataPack == nil) then + dataPack = terrainData["non-solid"] + end + + for id, data in ipairs(dataPack) do + local useThisData = true + if (data.mustHaveAction ~= nil and not self:canDoAction(data.mustHaveAction)) then + useThisData = false + end + if (data.mustDoAction ~= nil and data.mustDoAction ~= self.currentAction) then + useThisData = false + end + + if (useThisData) then + newData = data + print(id) + end + end + + self:setTerrainData(newData) +end + +function PlayerInteractions:setTerrainData(data) + --TODO +end + +function PlayerInteractions:getCurrentTerrain() + local terrain = self.terrain.name + for _, coord in ipairs(TERRAIN_CHECKER) do + local newTerrain = self.world:getTileTypeAtPoint(self.x + coord.x, self.y + coord.y) + terrain = newTerrain + if (newTerrain == "non-solid") then + break; + end + end + return terrain +end + return PlayerInteractions