Instantly share code, notes, and snippets.

@Fortyseven Fortyseven/lines.p8
Last active Jan 22, 2019

Embed
What would you like to do?
pico-8 cartridge // http://www.pico-8.com
version 16
__lua__
-- lines (classic)
-- by fortyseven
SPACING = 4
SPEED = 1
function buildPolyVerts()
local verts = {
x=rnd(128),
y=rnd(128),
xvel= SPEED,
yvel= SPEED
};
if (rnd(1) > 0.5) verts.xvel = -verts.xvel
if (rnd(1) > 0.5) verts.yvel = -verts.yvel
return verts;
end;
-------------------------------------------------------------
poly = {}
function poly:init(i, color)
self.color = color
self.verts = {}
-- ensure we use the same seed for each poly object, so it will always
-- create the same intial x/y/velocity directions
for i = 1, 4 do
verts = buildPolyVerts()
add(self.verts,verts)
end
-- run the update a couple times to space them out
for i = 1, i*SPACING do self:update() end
end
function poly:update()
foreach(self.verts, function(vert)
vert.x += vert.xvel
if ((vert.x >= 128) or (vert.x <= 0)) vert.xvel = -vert.xvel
vert.y += vert.yvel
if ((vert.y >= 128) or (vert.y <= 0)) vert.yvel = -vert.yvel
end)
end
function poly:draw()
for i = 1, 3 do
color(self.color);
line(self.verts[i].x, self.verts[i].y, self.verts[i+1].x, self.verts[i+1].y)
end
line(self.verts[4].x, self.verts[4].y, self.verts[1].x, self.verts[1].y)
end
function poly:new(o)
self.__index = self
return setmetatable(o or {}, self)
end
-------------------------------------------------------------
polyCluster = {}
function polyCluster:init(col_array)
self.objects = {}
seed = flr(rnd(1000))
-- generate three unique polys
for i = 1, 3 do
srand(seed)
local o = poly:new()
o:init(i, col_array[i])
add(self.objects, o )
end
end
function polyCluster:update()
foreach(self.objects, function(obj)
obj:update()
end)
end
function polyCluster:draw()
foreach(self.objects, function(obj)
obj:draw()
end)
end
function polyCluster:new(o)
self.__index = self
return setmetatable(o or {}, self)
end
-------------------------------------------------------------
clusters = {}
function _init()
local c = polyCluster:new()
c:init({2,8,14})
add(clusters, c)
c = polyCluster:new()
c:init({1,6,12})
add(clusters, c)
c = polyCluster:new()
c:init({13,6,7})
add(clusters, c)
end
function _draw()
foreach(clusters, function(c)
c:update()
end)
end
function _update()
cls()
foreach(clusters, function(c)
c:draw()
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment