
ExplodingBarrel = Entity:createSubClass();
registerSpawn("ExplodingBarrel");


function ExplodingBarrel:init()

   self:setTexture("oldsprites.tga");

   -- TODO: Make a more general function for setting these things
   -- based off spriteDefs.

   local scale = 3;
   self:setSpriteScale(scale);

   setSpriteFromDef(self, 12, 1);

   -- So that we actually collide with bullets.
   self:setBlockable(true);

   self.hp = 5;

   self:setGravityInfluence(1);
   self:setBounciness(0.5);
   self:setBlocker(true);

end



function ExplodingBarrel:collided(otherEntName)

   local otherEnt = entitiesByName[otherEntName];

   if otherEnt and otherEnt.isPlayerBullet then

      self.hp = self.hp - 1;
      otherEnt:die();

      self.hitThisFrame = true;

      if self.hp < 1 then
         self:die();
      end

   end

   -- TODO: Speed-based damage?

end

function ExplodingBarrel:update()
   
   local dx, dy = self:getVel();
   dx = dx * 0.9;
   self:setVel(dx, dy);

   self:setFlashing(false);

   if self.hitThisFrame then
      self.hitThisFrame = false;
      self:setFlashing(true);
   end
end

