This commit is contained in:
Simon Cambier 2025-04-07 13:16:26 +02:00
parent ac60c09612
commit 71e3f6157e
2 changed files with 43 additions and 22 deletions

18
aabb.p8
View File

@ -3,7 +3,7 @@ version 42
__lua__ __lua__
local player = { local player = {
-- position -- position
x = 64, x = 67,
y = 80, y = 80,
-- velocity -- velocity
@ -15,23 +15,23 @@ local player = {
self.dy = self.dy + 1 self.dy = self.dy + 1
self.dy = min(self.dy, 10) self.dy = min(self.dy, 10)
-- collision right
if self.dx > 0 and (isColliding(self.x + self.dx + 7, self.y) or isColliding(self.x + self.dx + 7, self.y + 7)) then
self.dx = 0
end
-- collision left -- collision left
if self.dx < 0 and (isColliding(self.x + self.dx, self.y) or isColliding(self.x + self.dx, self.y + 7)) then if self.dx < 0 and (isColliding(self.x + self.dx, self.y) or isColliding(self.x + self.dx, self.y + 7)) then
self.dx = 0 self.dx = 0
end end
-- collision right
-- collision down if self.dx > 0 and (isColliding(self.x + self.dx + 7, self.y) or isColliding(self.x + self.dx + 7, self.y + 7)) then
if self.dy > 0 and (isColliding(self.x, self.y + self.dy + 7) or isColliding(self.x + 7, self.y + self.dy + 7)) then self.dx = 0
self.dy = 0
end end
-- collision up -- collision up
if self.dy < 0 and (isColliding(self.x, self.y + self.dy) or isColliding(self.x + 7, self.y + self.dy)) then if self.dy < 0 and (isColliding(self.x, self.y + self.dy) or isColliding(self.x + 7, self.y + self.dy)) then
self.dy = 0 self.dy = 0
end end
-- collision down
if self.dy > 0 and (isColliding(self.x, self.y + self.dy + 7) or isColliding(self.x + 7, self.y + self.dy + 7)) then
self.dy = 0
end
-- apply movement -- apply movement
self.x = self.x + self.dx self.x = self.x + self.dx

View File

@ -3,7 +3,7 @@ version 42
__lua__ __lua__
local player = { local player = {
-- position -- position
x = 64, x = 67,
y = 50, y = 50,
-- velocity -- velocity
@ -16,20 +16,17 @@ local player = {
-- cap the falling speed to 7px/frame -- cap the falling speed to 7px/frame
self.dy = min(self.dy, 7) self.dy = min(self.dy, 7)
-- collision right
if self.dx > 0 and (isColliding(self.x + self.dx + 7, self.y) or isColliding(self.x + self.dx + 7, self.y + 7)) then
self.dx = 0
end
-- collision left -- collision left
if self.dx < 0 and (isColliding(self.x + self.dx, self.y) or isColliding(self.x + self.dx, self.y + 7)) then if self.dx < 0 and (isColliding(self.x + self.dx, self.y) or isColliding(self.x + self.dx, self.y + 7)) then
-- move the player against the left wall
self.x = self.x % 8 == 0 and self.x or flr(self.x / 8) * 8
self.dx = 0 self.dx = 0
end end
-- collision right
-- collision down if self.dx > 0 and (isColliding(self.x + self.dx + 7, self.y) or isColliding(self.x + self.dx + 7, self.y + 7)) then
if self.dy > 0 and (isColliding(self.x, self.y + self.dy + 7) or isColliding(self.x + 7, self.y + self.dy + 7)) then -- move the player against the right wall
-- clip the player to the nearest block down self.x = self.x % 8 == 0 and self.x or flr((self.x + 8) / 8) * 8
self.y = self.y % 8 == 0 and self.y or flr((self.y + 8) / 8) * 8 self.dx = 0
self.dy = 0
end end
-- collision up -- collision up
if self.dy < 0 and (isColliding(self.x, self.y + self.dy) or isColliding(self.x + 7, self.y + self.dy)) then if self.dy < 0 and (isColliding(self.x, self.y + self.dy) or isColliding(self.x + 7, self.y + self.dy)) then
@ -37,6 +34,22 @@ local player = {
self.y = self.y % 8 == 0 and self.y or flr(self.y / 8) * 8 self.y = self.y % 8 == 0 and self.y or flr(self.y / 8) * 8
self.dy = 0 self.dy = 0
end end
-- collision down
if self.dy > 0 and (isColliding(self.x, self.y + self.dy + 7) or isColliding(self.x + 7, self.y + self.dy + 7)) then
-- align the player with the nearest block down
self.y = self.y % 8 == 0 and self.y or flr((self.y + 8) / 8) * 8
self.dy = 0
end
-- diagonals
if self.dx ~= 0 and self.dy ~= 0 and isCollidingRect(self.x + self.dx, self.y + self.dy) then
self.dx = 0
self.dy = 0
-- align with the current cell
self.x = flr(self.x / 8) * 8
self.y = flr(self.y / 8) * 8
printh(self.x .. ", " .. self.y)
end
-- apply movement -- apply movement
self.x = self.x + self.dx self.x = self.x + self.dx
@ -52,6 +65,14 @@ function isColliding(x, y)
return fget(mget(flr(x / 8), flr(y / 8)), 0) return fget(mget(flr(x / 8), flr(y / 8)), 0)
end end
-- same as isColliding(), but will check the 4 corners at once
function isCollidingRect(x, y)
return isColliding(x, y)
or isColliding(x + 7, y)
or isColliding(x, y + 7)
or isColliding(x + 7, y + 7)
end
function getBlockCoords(x, y) function getBlockCoords(x, y)
return flr(x / 8) * 8, flr(y / 8) * 8 return flr(x / 8) * 8, flr(y / 8) * 8
end end
@ -62,12 +83,12 @@ end
function _update() function _update()
--left --left
if btn(0) then if btn(0) then
player.dx = -1 player.dx = -2
end end
-- right -- right
if btn(1) then if btn(1) then
player.dx = 1 player.dx = 2
end end
-- Up -- Up