mmbshmup/collisiontest.p8

142 lines
2.8 KiB
Lua

pico-8 cartridge // http://www.pico-8.com
version 42
__lua__
bx0=0
by0=0
bx1=127
by1=127
lx0=0
ly0=0
lx1=127
ly1=127
-->8
function collides()
local tmin=0
local tmax=1
local ldx=lx1-lx0
local ldy=ly1-ly0
-- x
if ldx==0 then
if (lx0<bx0 or lx0>bx1) return
else
local tx0=(bx0-lx0)/ldx
local tx1=(bx1-lx0)/ldx
if (tx0 > tx1) tx0,tx1=tx1,tx0
if (tmin < tx0) tmin=tx0
if (tmax > tx1) tmax=tx1
end
if ldy==0 then
if (ly0<by0 or ly0>by1) return
else
local ty0=(by0-ly0)/ldy
local ty1=(by1-ly0)/ldy
if (ty0 > ty1) ty0,ty1=ty1,ty0
if (tmin < ty0) tmin=ty0
if (tmax > ty1) tmax=ty1
end
if (tmax < tmin) return
return tmin,tmax
end
-->8
function _init()
poke(0x5f2d,1) -- enable mouse
end
function _bounce_screen(x)
return _bounce(x*128,128)
end
function _bounce(x,mx)
x=x%(mx * 2)
if (x>=mx)return mx-(x-mx)
return x
end
function _to_halfopen(x0,x1)
-- turn two numbers into a
-- half-open integer range
x0=flr(x0)
x1=flr(x1)
local lo=min(x0,x1)
local hi=max(x0,x1)
if (hi==lo) return lo,hi
return lo,hi-1
end
function _update60()
local t=time()/16
local bx0_=_bounce_screen(t*1)
local by0_=_bounce_screen(t*2)
local bx1_=_bounce_screen(t*3)
local by1_=_bounce_screen(t*4)
bx0,bx1=_to_halfopen(bx0_,bx1_)
by0,by1=_to_halfopen(by0_,by1_)
update_line()
--[[
local lx0_=_bounce_screen(t*1.5)
local ly0_=_bounce_screen(t*2.5)
local lx1_=_bounce_screen(t*3.5)
local ly1_=_bounce_screen(t*4.5)
lx0,lx1=lx0_,lx1_
ly0,ly1=ly0_,ly1_
]]--
end
was_down=false
last_mx,last_my=nil,nil
function update_line()
local mx,my=stat(32),stat(33)
local is_down=stat(34)!=0
if is_down then
if was_down then
lx1,ly1=mx,my
else
lx0,ly0=mx,my
end
end
was_down=is_down
last_mx,last_my=mx,my
end
-->8
function _draw()
cls(0)
rect(bx0,by0,bx1,by1,6)
line(lx0,ly0,lx1,ly1,2)
local cmin, cmax = collides()
if cmin then
local dx,dy=lx1-lx0,ly1-ly0
line(lx0 + dx*cmin,
ly0 + dy*cmin,
lx0 + dx*cmax,
ly0 + dy*cmax,
8)
pset(lx0 + dx*cmin,
ly0 + dy*cmin,
12)
end
pset(last_mx,last_my,7)
end
__gfx__
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000