player blocker

This commit is contained in:
Your Name 2026-08-11 20:29:35 -04:00
parent 0618cfc51d
commit a60f46d585
12 changed files with 1141 additions and 89 deletions

View file

@ -361,9 +361,12 @@ end
local function roomWith(player, entities, width, height)
gridWidth, gridHeight = width or 8, height or 8
local matrix = {}
local blockerMatrix = {}
for y = 1, gridHeight do
matrix[y] = {}
blockerMatrix[y] = {}
for x = 1, gridWidth do matrix[y][x] = false end
for x = 1, gridWidth do blockerMatrix[y][x] = false end
end
for _, entity in ipairs(entities) do
for _, cell in ipairs(entity:getOccupiedCells()) do
@ -376,12 +379,61 @@ local function roomWith(player, entities, width, height)
colorsPlayerHas = {"red"},
player = {red = player},
collidableMatrices = {red = matrix},
playerBlockerMatrices = {red = blockerMatrix},
}, {__index = Room})
room.playerJoinCheck = function() end
room.tick = function() end
return room
end
test("Room keeps player-only blockers out of block collision", function()
loadRoomClass()
local oldAssets, oldLoadSprite = globalAssetProperties, loadSprite
local sprite = {getWidth = function() return 16 end, getHeight = function() return 16 end}
globalAssetProperties = {
player_blocker = {
class = "player_blocker",
sprites = {red = "player_blocker_r.png"},
cells = {red = {{x = 1, y = 1}}},
},
}
loadSprite = function() return sprite end
local room = setmetatable({
collideables = {red = {}, green = {}, blue = {}},
playerBlockers = {red = {}, green = {}, blue = {}},
}, {__index = Room})
local blocker = room:createEntity("player_blocker", "red", 4, 3)
assertTrue(blocker:blocksPlayer())
assertTrue(#room.playerBlockers.red == 1)
assertTrue(#room.collideables.red == 0, "blockers must not enter block collision")
globalAssetProperties, loadSprite = oldAssets, oldLoadSprite
end)
test("Room player-only blockers stop the player but do not stop a box", function()
loadRoomClass()
local player = movable(2, 3)
local box = movable(3, 3)
local blocker = movable(4, 3)
function blocker:blocksPlayer() return true end
local room = roomWith(player, {box}, 8, 8)
local blockerMatrix = {}
for y = 1, gridHeight do
blockerMatrix[y] = {}
for x = 1, gridWidth do blockerMatrix[y][x] = false end
end
blockerMatrix[3][4] = blocker
room.playerBlockerMatrices.red = blockerMatrix
room:movePlayer("right")
assertTrue(box.x == 4, "box should move onto the player-only blocker")
assertTrue(player.x == 3, "player should move while pushing the box through it")
room:movePlayer("right")
assertTrue(player.x == 3, "player should be stopped by the player-only blocker")
end)
test("Room applies the sticky-regular-player pull", function()
loadRoomClass()
local sticky = movable(2, 3, true)