diff --git a/Source/core/cotton/PlayerTypes/PlayerBase.lua b/Source/core/cotton/PlayerTypes/PlayerBase.lua index 3ac15cd..d90e145 100644 --- a/Source/core/cotton/PlayerTypes/PlayerBase.lua +++ b/Source/core/cotton/PlayerTypes/PlayerBase.lua @@ -2,7 +2,8 @@ local gfx = playdate.graphics class("PlayerBase", { isFrozen = false, - sprite = nil + sprite = nil, + currentCollisions = {}, }).extends() function PlayerBase:Init() @@ -78,3 +79,28 @@ function PlayerBase:readd(x, y) self.sprite:add() self:moveTo(x, y) end + +function PlayerBase:checkIfStillColliding(sprite) + local collisions = sprite:overlappingSprites() + local removedKeys = {} + + for key, value in pairs(game.player.currentCollisions) do + local isStillColliding = false + for i = 1, #collisions do + if collisions[i] then + if collisions[i].id == key then + isStillColliding = true + end + end + end + + if not isStillColliding then + table.insert(removedKeys, key) + end + end + + for i, v in pairs(removedKeys) do + game.player.currentCollisions[v]:onTileExit() + game.player.currentCollisions[v] = nil + end +end diff --git a/Source/core/cotton/PlayerTypes/PlayerGrid.lua b/Source/core/cotton/PlayerTypes/PlayerGrid.lua index 58c723c..8ac9460 100644 --- a/Source/core/cotton/PlayerTypes/PlayerGrid.lua +++ b/Source/core/cotton/PlayerTypes/PlayerGrid.lua @@ -5,7 +5,6 @@ class("PlayerGrid", { transitionMovement = true, transitionSpeed = 2, allowDiagonalMovement = false, - currentCollisions = {}, faceDirection = nil }).extends(PlayerBase) @@ -35,28 +34,7 @@ end function PlayerGrid:hasMoved() self.shouldMove = false - local collisions = self.tempSprite:overlappingSprites() - local removedKeys = {} - - for key, value in pairs(self.currentCollisions) do - local isStillColliding = false - for i = 1, #collisions do - if collisions[i].entity then - if collisions[i].entity.id == key then - isStillColliding = true - end - end - end - - if not isStillColliding then - table.insert(removedKeys, key) - end - end - - for i, v in pairs(removedKeys) do - self.currentCollisions[v]:onTileExit() - self.currentCollisions[v] = nil - end + self:checkIfStillColliding(self.tempSprite) end function PlayerGrid:moveTowards() diff --git a/Source/core/cotton/PlayerTypes/PlayerPlatformer.lua b/Source/core/cotton/PlayerTypes/PlayerPlatformer.lua index 87aad64..80a2460 100644 --- a/Source/core/cotton/PlayerTypes/PlayerPlatformer.lua +++ b/Source/core/cotton/PlayerTypes/PlayerPlatformer.lua @@ -36,6 +36,17 @@ function PlayerPlatformer:Init(ldtk_entity) function sprite:collisionResponse(other) if other.collisionType then + if other.collisionType == collisionTypes.overlap then + if game.player.currentCollisions[other.id] == nil then + game.player.currentCollisions[other.id] = other + other:onTileEnter() + end + end + + if config.autoAct then + other:interact() + end + return collisionTypes[other.collisionType] end @@ -53,6 +64,7 @@ function PlayerPlatformer:update() end self:doBasicInputChecks() + self:checkIfStillColliding(self.sprite) local dt = 1 / playdate.display.getRefreshRate() diff --git a/Source/core/cotton/PlayerTypes/PlayerTopdown.lua b/Source/core/cotton/PlayerTypes/PlayerTopdown.lua index d0eea06..5135d75 100644 --- a/Source/core/cotton/PlayerTypes/PlayerTopdown.lua +++ b/Source/core/cotton/PlayerTypes/PlayerTopdown.lua @@ -3,7 +3,8 @@ local gfx = playdate.graphics class("PlayerTopdown", { player_speed = 4, player_acc = 1, - player_ground_friction = 0.8 + player_ground_friction = 0.8, + currentCollisions = {} }).extends(PlayerBase) function PlayerTopdown:Init(ldtk_entity) @@ -23,6 +24,15 @@ function PlayerTopdown:Init(ldtk_entity) function sprite:collisionResponse(other) if other.collisionType then + if other.collisionType == collisionTypes.overlap then + game.player.currentCollisions[other.id] = other + other:onTileEnter() + end + + if config.autoAct then + other:interact() + end + return collisionTypes[other.collisionType] end @@ -40,6 +50,7 @@ function PlayerTopdown:update() end self:doBasicInputChecks() + self:checkIfStillColliding(self.sprite) if input.x() == 0 or input.y() == 0 then self.velocity.x = math.approach(self.velocity.x, 0, self.player_ground_friction)