sonic-radiance/sonic-radiance.love/scenes/overworld/actors/player/interactions.lua
2021-04-18 10:53:35 +02:00

146 lines
3.6 KiB
Lua

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()
if (self.haveCollided == false) then
self.lastCollision = nil
end
self.haveCollided = false
end
function PlayerInteractions:collideWithGizmo(other)
if (other.needButton) then
if (self.keys["A"].isPressed) then
other:doAction()
self.haveCollided = true
self.lastCollision = other.creationID
end
else
if (self.lastCollision ~= other.creationID) then
other:doAction()
end
self.haveCollided = true
self.lastCollision = other.creationID
end
end
function PlayerInteractions:talkToGizmo(other)
if (self.keys["A"].isPressed and (self:faceRightDirection(other))) then
other:doAction()
self.haveCollided = true
self.lastCollision = other.creationID
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
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