
Explosion = Entity:createSubClass();

function Explosion:init()
   
   self:setTexture("shmup1.tga");
   self:setGravityInfluence(0);
   self:setSpriteFrame(0, 96, 32, 128);

   self.animFrame = 0;
   self.subsplosions = 0;

   self:setBlockable(false);
   self:setLayer(4);

end

function Explosion:update()

   local animSpeed = 0.25;

   if self.animFrame == 0 then
	  -- First frame. Make some smoke.

	  local i;

	  for i = 0, 4 do

		 local x,y,dx,dy;
		 local smokeScatter = 1;
		 x,y = self:getPos();
		 dx,dy = self:getVel();

		 dx = dx + (math.random() - 0.5) * smokeScatter;
		 dy = dy + (math.random() - 0.5) * smokeScatter;

		 addSmoke(x, y, dx * 2, dy * 2);
	  end
   end

   self.animFrame = self.animFrame + animSpeed;

   if self.animFrame >= 4 then
	  self:die();
   end

   local realFrame = math.floor(self.animFrame) * 32;

   self:setSpriteFrame(realFrame, 96, realFrame + 32, 128);

   if self.subsplosions > 0 and math.floor(self.animFrame) > 0.5 then

	  while self.subsplosions > 0 do

		 self.subsplosions = self.subsplosions - 1;
		 local x, y = self:getPos();
		 local newsplosion = addSplosion(x, y);
		 newsplosion.subsplosions = self.subsplosions - 1;
		 local dx, dy = self:getVel();
		 local scatterSpeed = 20;
		 dx = dx + (math.random() - 0.5) * scatterSpeed;
		 dy = dy + (math.random() - 0.5) * scatterSpeed;

		 newsplosion:setVel(dx, dy);

	  end

   end

end

function addSplosion(x, y)
   local explosion = Explosion:create();
   explosion:setPos(x, y);

   screenShakeMag = screenShakeMag + 1;

   return explosion;
end


