feat: initial terrain data loading

This commit is contained in:
Kazhnuz 2021-04-18 10:53:35 +02:00
parent a88b30dd9f
commit 15973e491a
3 changed files with 154 additions and 0 deletions

View file

@ -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,
}
}
}

View file

@ -39,6 +39,7 @@ end
function Player:updateStart(dt)
self.tweens:update(dt)
self:updateTerrain()
self:actionMove()
self:actionJump()

View file

@ -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