------------------------------------------------------------------------
-- Base projectile class
------------------------------------------------------------------------

Projectile = Entity:createSubClass();

function Projectile:init()
	self:setForceUpdate(true);
    self:setFullbright(true);
    self:setLayer(3);

    local light = Light_Basic:create();

    light:setLayer(2);

    self.myLight = light:getName();
end

function Projectile:postUpdate()

   local light = entitiesByName[self.myLight];
   if light then
      light:setPos(self:getPos());
   end

end

function Projectile:destruct()
   
   local light = entitiesByName[self.myLight];
   if light then
      light:die()
   end

end

function Projectile:update()

   if self:getAge() > 1000 then
      self:die();
   end

end

------------------------------------------------------------------------
-- Grenade class
------------------------------------------------------------------------

Grenade = Projectile:createSubClass();

function Grenade:init()
	Projectile.init(self);
	
	self.animFrame = 0;
	self.animSpeed = 1;
	self:setTexture("data/bonuses.png");
	
	self:setSpriteFrame(0, 288, 32, 320);
	self:setGravityInfluence(1);
	self:setBlockable(true);
	self:setBounciness(0.5);
	
	self.timer = 30 * 3;

    self:setLayer(3);
end

function Grenade:update()
	Projectile.update(self);

	local dx, dy = self:getVel();

	self.animFrame = self.animFrame + (self.animSpeed * (math.abs(dx)/16));
	
	if self.animFrame > 7.99 then
		self.animFrame = 0;
	end
	
	local spritex = math.floor(self.animFrame) * 32;
	local spritey = 288;
	
	if dx < 0 then
		spritey = spritey + 32;
	end

	self:setSpriteFrame(spritex, spritey, spritex + 32, spritey + 32);
	self:setVel(dx * 0.99, dy * 0.99);
	
	self.timer = self.timer - 1;
	if self.timer < 0 then
		self:die();

		for i = 0, 100, 1 do		
			local angle = math.random(628) / 100;
			local boomSpeed = 32;
			local newOb = CClass:create();
			newOb:setPos(self:getPos());
			newOb:setVel(math.cos(angle) * boomSpeed, math.sin(angle) * boomSpeed);
			newOb.isPlayerBullet = true;
			newOb:setBlockable(false);
		end
	end
end

------------------------------------------------------------------------
-- Molotov class
------------------------------------------------------------------------

Molotov = Projectile:createSubClass();

function Molotov:init()
	Projectile.init(self);
	
	self.animFrame = 0;
	self.animSpeed = 1;
	self:setTexture("data/bonuses.png");
	
	self:setSpriteFrame(0, 160, 64, 224);
	self:setGravityInfluence(1);
	self:setBlockable(true);
end

function Molotov:update()
	Projectile.update(self);

	self.animFrame = self.animFrame + self.animSpeed;
	
	if self.animFrame > 7.99 then
		self.animFrame = 0;
	end
	
	local spritex = math.floor(self.animFrame) * 64;
	local spritey = 160;
	
	local dx, dy = self:getVel();
	if dx > 0 then
		spritey = spritey + 64;
	end

	self:setSpriteFrame(spritex, spritey, spritex + 64, spritey + 64);
end

function Molotov:collided(otherEnt)
	local ent = entitiesByName[otherEnt];
	if ent then
		if ent:getBlocker() then
			self:collidedWithWorld();
		end
	end
end

function Molotov:collidedWithWorld()
	self:die();
	
	playSound("data/glassbreak.ogg");
	
	-- Temp stuff, just to mess around.
	local dx, dy = self:getVel();
	local x, y = self:getPos();

	--for i = 1, 10, 1 do
	--	newGem = Gem:create();
	--	newGem:setPos(x, y);
	--	local range = 64;
	--	newGem:setVel(math.random(range) - (range / 2), math.random(range) - (range / 2));
	--	newGem:setGravityInfluence(1);
	--	newGem:setBlockable(true);
	--	newGem:setBounciness(0.5);		
	--	newGem.isTemporary = true;
	--	newGem.tempCountdown = 30 * (10 + math.random(5));
	--end
end
