Skip to content

Instantly share code, notes, and snippets.

@Achie72
Created June 17, 2024 19:57
Show Gist options
  • Save Achie72/bc9a676d68f5a53d7580a5a52cfa7349 to your computer and use it in GitHub Desktop.
Save Achie72/bc9a676d68f5a53d7580a5a52cfa7349 to your computer and use it in GitHub Desktop.
PICO-8 Sliding Movement for PPJJGG
-- function to add characters to the game
-- at any point on the map
function add_character(_x, _y)
local p = {
x = _x,
y = _y,
direction = 5,
doReplaceOldTile = false,
replaceTileId = 0
}
-- set it's tile to the player tile
mset(p.x, p.y, 4)
-- add it to the collection
add(characters, p)
end
function _init()
-- have a direction table that shows how to move
-- on x and y to get to the next tile in said direction
-- it is always mapped to the buttons, so 0 is left, 1 is right
-- 2 is up, 3 is down, 4 is neutral
directionTable = {
{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {0, 0}
}
characters = {}
add_character(4,4)
add_character(10,4)
add_character(4,10)
add_character(10,10)
end
function _update()
-- check movement for each character
for i=1,#characters do
update_player(i)
end
end
function update_player(_index)
local character = characters[_index]
-- save pressed direction for character
-- only count if the character is already stopped
if character.direction == 5 then
if btnp(0) then character.direction = 1 end
if btnp(1) then character.direction = 2 end
if btnp(2) then character.direction = 3 end
if btnp(3) then character.direction = 4 end
end
-- see if next tile in that direction is walkable
local nextTile = mget(character.x + directionTable[character.direction][1], character.y + directionTable[character.direction][2])
-- if it is not empty void
-- move the character there
-- 0 is void, 2 is wall for now
if (nextTile != 0) and (nextTile != 2) and (nextTile != 4) then
local oldPosX, oldPosY = character.x, character.y
local newPosX, newPosY = character.x + directionTable[character.direction][1], character.y + directionTable[character.direction][2]
-- if the player was on a normal tile, reset it to that (1)
-- if the player was on a directional tile and has it's replace
-- flag set, then fetch the directional tile that I need to replace
-- this is what will preserve the arrow tiles instead of just replacing them with
-- normal empty tiles
local tileToReset = character.doReplaceOldTile and character.replaceTileId or 1
-- if I have fetched the new reset tile, then turn off the flag for it
if tileToReset != 1 then character.doReplaceOldTile = false end
-- move the character
character.x, character.y = newPosX, newPosY
-- check if new tile is redirectional
if (nextTile > 47) and (nextTile < 52) then
-- arrow tiles are 48,49,50,51
-- if I substract 47 then I conver the
-- tileId to the direction it represents
character.direction = nextTile - 47
-- set the flag so the character remembers to set it back
character.doReplaceOldTile = true
-- save what needs to be reset
character.replaceTileId = nextTile
end
-- reset the tile the character was on
mset(oldPosX, oldPosY, tileToReset)
-- this is what sets the character
mset(newPosX, newPosY, 4)
else
-- if movement is not possible do to void(0), wall(2)
-- or another player(4) then stop movement
character.direction = 5
end
end
function _draw()
cls()
map()
for character in all(characters) do
--print("웃", character.x*8, character.y*8, 7)
end
end
@Achie72
Copy link
Author

Achie72 commented Jun 17, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment