Skip to content

Commit

Permalink
Added autoact, collect & interact to platformer and topdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart Zaalberg committed May 31, 2022
1 parent 169a73e commit 25f10e6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 25 deletions.
28 changes: 27 additions & 1 deletion Source/core/cotton/PlayerTypes/PlayerBase.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ local gfx <const> = playdate.graphics

class("PlayerBase", {
isFrozen = false,
sprite = nil
sprite = nil,
currentCollisions = {},
}).extends()

function PlayerBase:Init()
Expand Down Expand Up @@ -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
24 changes: 1 addition & 23 deletions Source/core/cotton/PlayerTypes/PlayerGrid.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class("PlayerGrid", {
transitionMovement = true,
transitionSpeed = 2,
allowDiagonalMovement = false,
currentCollisions = {},
faceDirection = nil
}).extends(PlayerBase)

Expand Down Expand Up @@ -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()
Expand Down
12 changes: 12 additions & 0 deletions Source/core/cotton/PlayerTypes/PlayerPlatformer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -53,6 +64,7 @@ function PlayerPlatformer:update()
end

self:doBasicInputChecks()
self:checkIfStillColliding(self.sprite)

local dt = 1 / playdate.display.getRefreshRate()

Expand Down
13 changes: 12 additions & 1 deletion Source/core/cotton/PlayerTypes/PlayerTopdown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ local gfx <const> = 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)
Expand All @@ -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

Expand All @@ -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)
Expand Down

0 comments on commit 25f10e6

Please sign in to comment.