Spacewar! 33 - Breaking up is hard to do.
Exploding better
While waiting for Tozier, I sketched a ship and divided it up into fragments into which it might break up:
SHIP SKETCH
Then I extended Fragment to draw those shapes:
Fragment = class()
Fragment.types = {}
Fragment.types[1] = function()
line(0,7,-3,-8)
line(-3,-8, 1, -4)
line(1,-4, 0,7)
end
Fragment.types[2] = function()
line(-2,2, 3,6)
line(3,6, 3,-2)
line(3,-2, -4,-4)
line(-4,-4, -2,2)
end
Fragment.types[3] = function()
line(-4,1, 3,3)
line(3,3, 3,1)
line(3,1, -1,-3)
line(-1,-3, -5,-3)
line(-5,-3, -4,1)
end
Fragment.types[4] = function()
line(-5,2, -1,2)
line(-1,2, 3,6)
line(3,6, 6,-2)
line(6,-2, -6,-2)
line(-6,-2, -5,2)
end
function Fragment:init(fragmentNumber, ship)
self.type = fragmentNumber
self.cannotCollide = true
self.pos = ship.pos
local outwardVelocity = vec2(0,1):rotate(math.random()*math.pi*2)
self.vel = ship.vel + outwardVelocity
self.angularVelocity = math.random()*10-5
self.heading = 0
U:addObject(self)
end
function Fragment:move()
self.pos = U:clip_to_screen(self.pos + self.vel)
self.heading = self.heading + self.angularVelocity
end
function Fragment:draw()
pushMatrix()
pushStyle()
translate(self.pos:unpack())
rotate(self.heading)
self:drawFragment(self.type)
popStyle()
popMatrix()
end
function Fragment:drawFragment(type)
strokeWidth(2)
if self.types[type] then self.types[type]() else
--line(0,0,10,6)
--line(0,0,0,20)
end
end
Note the little functions to draw the lines. I approximated the values by eyeballing my sketch. The explosion looks decent:
PICTURE HERE
Note that we have the “blammo!” still present, which we used as a visible test of explosion along the way, and that there are two of each Fragment. That’s because we kill everything twice in this loop in Universe:
function Universe:interactAll()
for _, a in pairs(U.Drawn) do
for _, b in pairs(U.Drawn) do
if U:colliding(a,b) then
a:die()
b:die()
end
end
end
end