

Building = Entity:createSubClass();

function Building:init()
   
   self:setTexture("shmup1.tga");
   self:setGravityInfluence(0);
   self:setSpriteFrame(0, 224, 64, 288);
   self:setDepth(-1);

   self:setSize(64, 64);

   self.hp = 10;
   self.flashCountDown = 0;

end

function Building:collided(otherEnt)
   
   local ent = entitiesByName[otherEnt];

   if self.hp < 0 then
	  -- Already dead.
	  return;
   end

   if ent.isPlayerShot then

	  -- Make it think it hit the world, which causes it to die and
	  -- play its animation.
	  ent:collidedWithWorld();

	  self:setFlashing(true);
	  self.flashCountDown = 2;
   
	  self.hp = self.hp - 1;

	  if self.hp < 0 then
		 -- We just died.

		 local x,y = self:getPos();

		 addSplosion(x - 16, y - 16).subsplosions = 5;
		 addSplosion(x + 16, y - 16).subsplosions = 5;
		 addSplosion(x + 16, y + 16).subsplosions = 5;
		 addSplosion(x - 16, y + 16).subsplosions = 5;

		 local sx1,sy1,sx2,sy2 = self:getSpriteFrame();
		 local dx = sx2 - sx1;
		 
		 self:setSpriteFrame(sx1 + dx, sy1, sx2 + dx, sy2);

		 playerScore = playerScore + 10;

	  end

   end

end

function Building:update()
   self.flashCountDown = self.flashCountDown - 1;
   if self.flashCountDown <= 0 then
	  self:setFlashing(false);
   end
end

function Building:save()

   self:init();

   return {};

end

registerSpawn("Building");

