Created
November 21, 2018 06:45
-
-
Save afk-mario/9408234af071a91e8baca246455eabab to your computer and use it in GitHub Desktop.
Sistema de colisiones para pico 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function solid(x, y) | |
-- checa el indice del sprite | |
-- en la posicion indicada | |
val=mget(x, y) | |
-- checa si el sprite tiene la | |
-- bandera 0 activada | |
return fget(val, 0) | |
end | |
-- checa una caja de colision | |
-- de temanio w,h en la posicion | |
-- [x,y] | |
function solid_area(x,y,w,h) | |
return | |
solid(x-w,y-h) or | |
solid(x+w,y-h) or | |
solid(x-w,y+h) or | |
solid(x+w,y+h) | |
end | |
-- checa si el actor (juagador) | |
-- colisionara si se mueve en | |
-- [dx] o [dy] | |
function is_solid(a,dx,dy) | |
return solid_area(a.x+dx,a.y+dy, | |
a.w,a.h) | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function _draw() | |
rectfill(0,0,128,128,3) | |
draw_map() | |
draw_player() | |
draw_debug() | |
end | |
-- dibuja 16 x 16 celdas del mapa | |
function draw_map() | |
map(0,0,0,0,16,16) | |
end | |
-- dibuja al jugador | |
function draw_player() | |
local sx = (p.x * 8) - 4 | |
local sy= (p.y * 8) - 4 | |
spr(p.s, sx, sy,1,1, p.flip, false) | |
end | |
-- si esta activada la variable | |
-- debug, dibuja el overlay | |
function draw_debug() | |
if not debug then return end | |
rectfill(0, 0, 127, 20, 0) | |
print( | |
"["..p.x..","..p.y.."]", | |
2, | |
2, | |
7) | |
if c_x then | |
print("coll_x", 2, 10, 7) | |
else | |
print("not coll_x", 2, 10, 7) | |
end | |
if c_y then | |
print("coll_y", 64, 10, 7) | |
else | |
print("not coll_y", 64, 10, 7) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function _init() | |
-- variables para saber si | |
-- colisiona en x o y | |
c_x = false | |
c_y = false | |
-- variable para activar info | |
-- de debug | |
debug = false | |
-- inicializar jugador | |
init_player() | |
end | |
function init_player() | |
-- initializa el jugador como un | |
-- objeto | |
p = {} | |
-- posicion inicial | |
p.x = 2 | |
p.y = 10 | |
-- indice de sprite | |
p.s = 0 | |
-- diferencia en x con que se | |
-- movera | |
p.dx = 0 | |
-- diferencia en y con que se | |
-- movera | |
p.dy = 0 | |
-- ancho de la colision | |
p.w = 0.4 | |
-- alto de la colision | |
p.h = 0.4 | |
-- variable para definir el rebote | |
-- del jugador | |
p.b = 0.8 | |
-- variable para determinar | |
-- si el jugador esta volteando | |
p.flip = false | |
-- el valor que se le quitara | |
-- cuando se deje de mover | |
p.inertia = 0.6 | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function _update() | |
ctrl_player() | |
move_player() | |
set_debug() | |
end | |
function ctrl_player() | |
-- aceleracion del jugador | |
accel = 0.3 | |
if btn(⬅️) then | |
p.dx -= accel | |
p.flip = true | |
end | |
if btn(➡️) then | |
p.dx += accel | |
p.flip = false | |
end | |
if btn(⬆️) then | |
p.dy -= accel | |
end | |
if btn(⬇️) then | |
p.dy += accel | |
end | |
end | |
function move_player() | |
-- checa si colisiona en x | |
c_x = is_solid(p, p.dx, 0) | |
-- checa si colisiona en y | |
c_y = is_solid(p, 0, p.dy) | |
-- si no colisiona en x | |
-- mueve al jugador | |
if not c_x then | |
p.x += p.dx | |
else | |
-- si coliciona rebota | |
p.dx *= -p.b | |
sfx(2) | |
end | |
if not c_y then | |
p.y += p.dy | |
else | |
p.dy *= -p.b | |
sfx(2) | |
end | |
-- le quita cada frame la inercia | |
p.dx *= p.inertia | |
p.dy *= p.inertia | |
end | |
function set_debug() | |
if btnp(❎) then | |
debug = not debug | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment