Skip to content

Instantly share code, notes, and snippets.

@JettMonstersGoBoom
Last active December 7, 2017 13:19
Show Gist options
  • Save JettMonstersGoBoom/525b6d24e7c0fb4e74def45064319f2e to your computer and use it in GitHub Desktop.
Save JettMonstersGoBoom/525b6d24e7c0fb4e74def45064319f2e to your computer and use it in GitHub Desktop.
Time_Tripper Particle Test , with some optimizations
-- title: Particle test 2
-- author: Time_Tripper
-- desc: Particle test 2
-- script: lua
-- input: mouse
-- Optimization from MonstersGoBoom
-- create a single table for everything , reducing lookups
local particle = {}
function particle:new (o,x,y,vx,vy,c,l)
o = o or {} -- create object if user does not provide one
o.x = x or 0
o.y = y or 0
o.vx = vx or 0
o.vy = vy or 0
o.c = c or 15
o.l = l or 10
setmetatable(o, self)
self.__index = self
return o
end
-- a list of them
particles = {}
bmx=0
bmy=0
scr={}
ti=0
-- note // is Integer division, so //1 is faster yet similar enough to floor to work for this
local random=math.random -- this is faster than math.random
function pallet()
i=0x3FC0
cr=random(256)-1
cg=random(256)-1
cb=random(256)-1
cr2=random(256)-1
cg2=random(256)-1
cb2=random(256)-1
for k=8,1,-1 do
poke(i ,(cr-(cr/8)*k)//1)
poke(i+1,(cg-(cg/8)*k)//1)
poke(i+2,(cb-(cb/8)*k)//1)
i=i+3
end
for k=1,8 do
poke(i ,(cr+((cr2-cr)/8)*k)//1)
poke(i+1,(cg+((cg2-cg)/8)*k)//1)
poke(i+2,(cb+((cb2-cb)/8)*k)//1)
i=i+3
end
poke(0x3FC0,0)
poke(0x3FC1,0)
poke(0x3FC2,0)
end
-- movement
function move()
for i=#particles,1,-1 do
local p = particles[i]
p.x = p.x + p.vx
p.y = p.y + p.vy
if p.x>1 and p.x<238 and p.y>1 and p.y<135 then
if random(2)==1 then
scr[(p.x//1)+(p.y//1)*240]=p.c
end
p.vx=p.vx*0.98+(random(3)-2)*0.1
p.vy=p.vy*0.98+(random(3)-2)*0.1
p.l=p.l-1
else
p.l=0
end
if p.l<1 then
table.remove(particles,i)
end
end
end
-- rendering
function draw()
for y=1,134 do
local y240 = y*240
local scr_addr=0x0000*2 + (y240) -- address of the screen for each line
for x=1,239 do
local i=x+y240
local scri = scr[i] -- we use this twice so reduce lookup
poke4(scr_addr+x,scri//1) -- poke is faster than pix
scr[i]=(scri+scr[i-1]+scr[i+1]+scr[i-240]+scr[i+240])/5.05
end
end
end
--
function reset()
for i=1,32640 do
scr[i]=0
end
end
--
function init()
cls()
pallet()
reset()
end
--------------------
init()
--------------------
function TIC()
mx,my,md=mouse()
if md then
for i=1,5 do
p = particle:new( nil,
mx+(random(20)-10),
my+(random(20)-10),
(mx-bmx)*0.5,
(my-bmy)*0.5,
15,
random(150)+150)
table.insert(particles,p)
end
bmx=mx
bmy=my
end
if random(1)==1 then
p = particle:new(nil, (random(230)+5),
(random(130)+5),
0,0,15,random(150)+150)
table.insert(particles,p)
end
if ti%200==0 then pallet() end
move()
draw()
ti=ti+1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment