Separate logic into class files
This commit is contained in:
@@ -1,62 +1,6 @@
|
||||
pico-8 cartridge // http://www.pico-8.com
|
||||
version 34
|
||||
__lua__
|
||||
ball_count = 16
|
||||
ball_radius = 5
|
||||
|
||||
function _init()
|
||||
balls = {}
|
||||
collisions = {}
|
||||
for i=1,ball_count do
|
||||
add(balls, ball:new({
|
||||
pos=vec2:new(rnd(127),rnd(127)),
|
||||
vel=vec2:new(rnd(2)-1,rnd(2)-1):normal(),
|
||||
col=rnd(15)+1
|
||||
}))
|
||||
end
|
||||
end
|
||||
|
||||
function _update60()
|
||||
for b in all(balls) do
|
||||
b:move()
|
||||
b:edge_bounce()
|
||||
end
|
||||
for b in all(balls) do
|
||||
b:check_collisions(balls)
|
||||
end
|
||||
handle_collisions(balls)
|
||||
collisions = {}
|
||||
end
|
||||
|
||||
function _draw()
|
||||
cls()
|
||||
for b in all(balls) do
|
||||
b:draw()
|
||||
end
|
||||
end
|
||||
|
||||
function handle_collisions()
|
||||
for source,colls in pairs(collisions) do
|
||||
for target,diff in pairs(colls) do
|
||||
local n = diff:normal()
|
||||
local steps = 0
|
||||
local rewind = 0
|
||||
local s_xpoint = source.pos
|
||||
local t_xpoint = target.pos
|
||||
repeat
|
||||
steps += 1
|
||||
rewind += 0.1 * steps
|
||||
s_xpoint = source.pos - source.vel * rewind
|
||||
t_xpoint = target.pos - target.vel * rewind
|
||||
until (s_xpoint - t_xpoint):mag() >= source.rad + target.rad
|
||||
source.vel = collision_vel(s_xpoint, source.vel, t_xpoint, target.vel)
|
||||
target.vel = collision_vel(t_xpoint, target.vel, s_xpoint, source.vel)
|
||||
source.pos = s_xpoint + source.vel * rewind
|
||||
target.pos = t_xpoint + target.vel * rewind
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
vec2 = {
|
||||
x = 0,
|
||||
y = 0,
|
||||
@@ -78,7 +22,7 @@ vec2 = {
|
||||
local b = b or 1
|
||||
local n = n:normal()
|
||||
return (n * -2 * self:dot(n) + self) * b
|
||||
end
|
||||
end,
|
||||
}
|
||||
|
||||
vec2.__eq = function(v1,v2) return v1.x == v2.x and v1.y == v2.y end
|
||||
@@ -109,12 +53,14 @@ function vec2:print_debug(v)
|
||||
print("inv: "..vec2.__tostring(-self))
|
||||
end
|
||||
|
||||
|
||||
-->8
|
||||
ball = {
|
||||
__count = 0,
|
||||
id = nil,
|
||||
pos = vec2:new(0,0),
|
||||
vel = vec2:new(0,0),
|
||||
rad = ball_radius,
|
||||
rad = 1,
|
||||
col = 7,
|
||||
|
||||
draw = function(self)
|
||||
@@ -166,6 +112,66 @@ function ball:new(o)
|
||||
return o
|
||||
end
|
||||
|
||||
-->8
|
||||
ball_count = 16
|
||||
ball_radius = 5
|
||||
|
||||
function _init()
|
||||
balls = {}
|
||||
collisions = {}
|
||||
for i=1,ball_count do
|
||||
add(balls, ball:new({
|
||||
pos=vec2:new(rnd(127),rnd(127)),
|
||||
vel=vec2:new(rnd(2)-1,rnd(2)-1):normal(),
|
||||
rad=ball_radius,
|
||||
col=rnd(15)+1
|
||||
}))
|
||||
end
|
||||
end
|
||||
|
||||
function _update60()
|
||||
for b in all(balls) do
|
||||
b:move()
|
||||
b:edge_bounce()
|
||||
end
|
||||
for b in all(balls) do
|
||||
b:check_collisions(balls)
|
||||
end
|
||||
handle_collisions(balls)
|
||||
collisions = {}
|
||||
end
|
||||
|
||||
function _draw()
|
||||
cls()
|
||||
for b in all(balls) do
|
||||
b:draw()
|
||||
end
|
||||
end
|
||||
|
||||
function handle_collisions()
|
||||
for source,colls in pairs(collisions) do
|
||||
for target,diff in pairs(colls) do
|
||||
local n = diff:normal()
|
||||
local steps = 0
|
||||
local rewind = 0
|
||||
local s_xpoint = source.pos
|
||||
local t_xpoint = target.pos
|
||||
repeat
|
||||
steps += 1
|
||||
rewind += 0.1 * steps
|
||||
s_xpoint = source.pos - source.vel * rewind
|
||||
t_xpoint = target.pos - target.vel * rewind
|
||||
until (s_xpoint - t_xpoint):mag() >= source.rad + target.rad
|
||||
local s_newvel = collision_vel(s_xpoint, source.vel, t_xpoint, target.vel)
|
||||
local t_newvel = collision_vel(t_xpoint, target.vel, s_xpoint, source.vel)
|
||||
source.pos = s_xpoint + source.vel * rewind
|
||||
target.pos = t_xpoint + target.vel * rewind
|
||||
source.vel = s_newvel
|
||||
target.vel = t_newvel
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function collision_vel(x1,v1,x2,v2)
|
||||
local x_diff = x1 - x2
|
||||
local v_diff = v1 - v2
|
||||
|
||||
Reference in New Issue
Block a user