2021-04-10 17:14:41 +02:00
|
|
|
local PlayerInteractions = Object:extend()
|
|
|
|
|
2021-04-18 10:53:35 +02:00
|
|
|
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},
|
|
|
|
}
|
|
|
|
|
2021-04-10 17:14:41 +02:00
|
|
|
function PlayerInteractions:initInteractions()
|
|
|
|
self.lastCollision = -1
|
|
|
|
self.haveCollided = false
|
2021-04-18 10:53:35 +02:00
|
|
|
self.terrain = {}
|
|
|
|
self.terrain.name = ""
|
|
|
|
self.terrain.data = {}
|
2021-04-21 16:46:41 +02:00
|
|
|
self.terrain.new = false
|
2021-04-18 10:53:35 +02:00
|
|
|
self.lastPos = {}
|
2021-04-18 12:34:30 +02:00
|
|
|
self.lastPos.x = self.x
|
|
|
|
self.lastPos.y = self.y
|
2021-04-10 17:14:41 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function PlayerInteractions:updateInteraction()
|
|
|
|
if (self.haveCollided == false) then
|
|
|
|
self.lastCollision = nil
|
|
|
|
end
|
|
|
|
self.haveCollided = false
|
|
|
|
end
|
|
|
|
|
|
|
|
function PlayerInteractions:collideWithGizmo(other)
|
|
|
|
if (other.needButton) then
|
2021-12-05 17:52:27 +01:00
|
|
|
self.scene.gui:getElement("interactions"):setInteractionName(other.interactionName)
|
2021-04-10 17:14:41 +02:00
|
|
|
if (self.keys["A"].isPressed) then
|
|
|
|
other:doAction()
|
|
|
|
self.haveCollided = true
|
|
|
|
self.lastCollision = other.creationID
|
2021-12-05 17:52:27 +01:00
|
|
|
self.scene.gui:getElement("interactions"):setInteractionName("")
|
2021-04-10 17:14:41 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
if (self.lastCollision ~= other.creationID) then
|
|
|
|
other:doAction()
|
|
|
|
end
|
|
|
|
self.haveCollided = true
|
|
|
|
self.lastCollision = other.creationID
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function PlayerInteractions:talkToGizmo(other)
|
2021-04-21 18:36:20 +02:00
|
|
|
if (self:faceRightDirection(other)) then
|
2021-12-05 17:52:27 +01:00
|
|
|
self.scene.gui:getElement("interactions"):setInteractionName(other.interactionName)
|
2021-04-21 18:36:20 +02:00
|
|
|
if (self.keys["A"].isPressed) then
|
|
|
|
other:doAction()
|
|
|
|
self.haveCollided = true
|
|
|
|
self.lastCollision = other.creationID
|
2021-12-05 17:52:27 +01:00
|
|
|
self.scene.gui:getElement("interactions"):setInteractionName("")
|
2021-04-21 18:36:20 +02:00
|
|
|
end
|
2021-04-10 17:14:41 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function PlayerInteractions:faceRightDirection(other)
|
|
|
|
if (self.charDir == "up") then
|
|
|
|
return (self.y >= other.y + other.h)
|
|
|
|
end
|
|
|
|
if (self.charDir == "down") then
|
|
|
|
return (self.y + self.h <= other.y)
|
|
|
|
end
|
|
|
|
if (self.charDir == "left") then
|
|
|
|
return (self.x >= other.x + other.w)
|
|
|
|
end
|
|
|
|
if (self.charDir == "right") then
|
|
|
|
return (self.x + self.w <= other.x)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-18 10:53:35 +02:00
|
|
|
function PlayerInteractions:updateTerrain()
|
|
|
|
local newTerrain = self:getCurrentTerrain()
|
2021-04-21 16:46:41 +02:00
|
|
|
if (newTerrain ~= self.terrain.name) then
|
|
|
|
self.terrain.new = true
|
|
|
|
end
|
2021-04-18 12:34:30 +02:00
|
|
|
self.terrain.name = newTerrain
|
|
|
|
self:updateTerrainData()
|
2021-04-18 10:53:35 +02:00
|
|
|
if (newTerrain == "non-solid") then
|
|
|
|
self:updateLastPosition()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function PlayerInteractions:updateLastPosition()
|
|
|
|
local canUpdate = true
|
|
|
|
for _, coord in ipairs(TERRAIN_CHECKER) do
|
2021-04-18 12:34:30 +02:00
|
|
|
local xx, yy = coord.x, coord.y
|
|
|
|
if (xx == MIN_X) then xx=0 else xx=16 end
|
|
|
|
if (yy == MIN_Y) then yy=0 else yy=16 end
|
|
|
|
local newTerrain = self.world:getTileTypeAtPoint(self.x + xx, self.y + yy)
|
2021-04-18 10:53:35 +02:00
|
|
|
if (newTerrain ~= "non-solid") then
|
2021-04-18 12:34:30 +02:00
|
|
|
canUpdate = false
|
2021-04-18 10:53:35 +02:00
|
|
|
end
|
|
|
|
end
|
2021-04-18 12:34:30 +02:00
|
|
|
if (canUpdate and self.onGround) then
|
2021-04-18 10:53:35 +02:00
|
|
|
self.lastPos = {}
|
2021-04-18 12:34:30 +02:00
|
|
|
self.lastPos.x = self.x
|
|
|
|
self.lastPos.y = self.y
|
2021-04-18 10:53:35 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function PlayerInteractions:updateTerrainData()
|
2021-04-18 12:34:30 +02:00
|
|
|
local dataPack = terrainData.list[self.terrain.name]
|
|
|
|
local newData = terrainData.list["non-solid"][1]
|
2021-04-18 10:53:35 +02:00
|
|
|
|
|
|
|
if (dataPack == nil) then
|
2021-04-18 12:34:30 +02:00
|
|
|
dataPack = terrainData.list["non-solid"]
|
2021-04-18 10:53:35 +02:00
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self:setTerrainData(newData)
|
|
|
|
end
|
|
|
|
|
|
|
|
function PlayerInteractions:setTerrainData(data)
|
2021-04-18 12:34:30 +02:00
|
|
|
self.groundLevel = data.level or 0
|
|
|
|
self.groundHeight = data.height or 0
|
|
|
|
if (self.onGround) then
|
|
|
|
self.speedFactor = data.speedFactor or 1
|
|
|
|
self.canJump = data.canJump ~= false
|
2021-04-19 18:04:29 +02:00
|
|
|
self.canAct = data.canAct ~= false
|
2021-04-18 12:34:30 +02:00
|
|
|
self.forceAction = data.forceAction
|
|
|
|
end
|
2021-04-21 16:46:41 +02:00
|
|
|
if (self.terrain.new and (self.z < 0)) then
|
|
|
|
self.terrain.new = false
|
|
|
|
if (not utils.string.isEmpty(data.sound)) then
|
|
|
|
self.assets:playSFX(data.sound)
|
|
|
|
end
|
|
|
|
end
|
2021-04-19 18:04:29 +02:00
|
|
|
self.fallDamage = data.fallDamage or 0
|
2021-04-21 16:46:41 +02:00
|
|
|
self.fallSound = data.fallSound or ""
|
2021-04-18 12:34:30 +02:00
|
|
|
self.terrain.data = data
|
2021-04-18 10:53:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function PlayerInteractions:getCurrentTerrain()
|
2021-04-18 12:34:30 +02:00
|
|
|
local terrain = ""
|
|
|
|
local highpriority = terrainData.highpriority
|
2021-04-18 10:53:35 +02:00
|
|
|
for _, coord in ipairs(TERRAIN_CHECKER) do
|
|
|
|
local newTerrain = self.world:getTileTypeAtPoint(self.x + coord.x, self.y + coord.y)
|
2021-04-18 12:34:30 +02:00
|
|
|
if (utils.table.contain(highpriority, newTerrain)) then
|
|
|
|
terrain = newTerrain
|
|
|
|
elseif (newTerrain == "non-solid") then
|
|
|
|
if (not utils.table.contain(highpriority, terrain)) then
|
|
|
|
terrain = newTerrain
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if (not (utils.table.contain(highpriority, terrain) or terrain == "non-solid")) then
|
|
|
|
terrain = newTerrain
|
|
|
|
end
|
2021-04-18 10:53:35 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return terrain
|
|
|
|
end
|
|
|
|
|
2021-04-10 17:14:41 +02:00
|
|
|
return PlayerInteractions
|