
-----------------------------------------------------------------------
-- Bonus base class
-----------------------------------------------------------------------

Bonus = Entity:createSubClass();

function Bonus:init()
	self:setTexture("data/bonuses.png");
	self:setGravityInfluence(0);
	self.isTemporary = false;
	self.tempCountdown = 0;
end

function Bonus:save()
	return {
		temp = self.isTemporary,
		tempCountDown = self.tempCountDown
	};
end

function Bonus:initFromSave(saveData)
	self.isTemporary = saveData.temp;
	self.tempCountDown = self.tempCountDown;
end

function Bonus:collided(otherEnt)
   if otherEnt == "player" then
      self:die();
   end
   
   -- local targets = self:getAllLinkTargets();
   -- local k, v;
   -- for k, v in pairs(targets) do
   --    local link = entitiesByName[v];
   --    link:collided(otherEnt);
   -- end

end

function Bonus:update()
	local dx, dy = self:getVel();
	self:setVel(dx * 0.95, dy * 0.95);
	
	if self.isTemporary then
		self.tempCountdown = self.tempCountdown - 1;
		if self.tempCountdown < 30 then
			self:setAlpha(1 - ((30 - self.tempCountdown) / 30));
		end
		if self.tempCountdown < 0 then
			self:die();
		end
	end
end

-----------------------------------------------------------------------
-- Graphical effect for disappearing gems
-----------------------------------------------------------------------

GemFlash = Entity:createSubClass();

function GemFlash:init()
	self:setTexture("data/bonuses.png");
	self:setSpriteFrame(0, 96, 32, 160);
	self:setDepth(0.1);
	self.animFrame = 0;
	self.animSpeed = 0.25;
end

function GemFlash:update()
	self.animFrame = self.animFrame + self.animSpeed;
	if self.animFrame > 3.99 then
		self:die();
	end
	local spritex = math.floor(self.animFrame) * 32;
	self:setSpriteFrame(spritex, 96, spritex + 32, 160);
end

-----------------------------------------------------------------------
-- Gems
-----------------------------------------------------------------------

Gem = Bonus:createSubClass();

function Gem:init()
	Bonus.init(self);
	self:setSpriteFrame(0, 32, 32, 96);
	self:setSize(16, 32);
	self.animFrame = 0;
	self.animSpeed = 0.25;
end

function Gem:save()
	baseData = Bonus:save(self);
	baseData.animFrame = self.animFrame;
	baseData.animSpeed = self.animSpeed;
	return baseData;
end

function Gem:initFromSave(saveData)
	Bonus:initFromSave(self, saveData);
	self.animFrame = saveData.animFrame;
	self.animSpeed = saveData.animSpeed;
end

function Gem:update()
	Bonus.update(self);
	self.animFrame = self.animFrame + self.animSpeed;
	if self.animFrame > 5.99 then
		self.animFrame = 0;
	end
	local spritex = math.floor(self.animFrame) * 32;
	self:setSpriteFrame(spritex, 32, spritex + 32, 96);
end

function Gem:collided(otherEnt)
	if otherEnt == "player" then
		local flash = GemFlash:create();
		flash:setPos(self:getPos());
        flash:setLayer(self:getLayer());
		playSound("data/collect1.ogg");
	end
	Bonus.collided(self, otherEnt);
end

registerSpawn("Gem");
GemSpawner = createSpawnType("GemSpawn", Gem, "data/bonuses.png", {x1=0, y1=32, x2=32, y2=96});

-----------------------------------------------------------------------
-- Ammo (base class)
-----------------------------------------------------------------------

Ammo = Bonus:createSubClass();

function Ammo:init()
	Bonus.init(self);
	self:setGravityInfluence(1);
	self:setBlockable(1);
end

-----------------------------------------------------------------------
-- Molotov pickups
-----------------------------------------------------------------------

MolotovPickup = Ammo:createSubClass();

function MolotovPickup:init()
	Ammo.init(self);
	self:setSpriteFrame(64, 0, 96, 32);
	self:setSize(16, 32);
end

MolotovPickupSpawn = createSpawnType("MolotovPickupSpawn", MolotovPickup, "data/bonuses.png", {x1=64, y1=0, x2=96, y2=32});

-----------------------------------------------------------------------
-- Grenade pickups
-----------------------------------------------------------------------

GrenadePickup = Ammo:createSubClass();

function GrenadePickup:init()
	Ammo.init(self);
	self:setSpriteFrame(32, 0, 64, 32);
	self:setSize(16, 32);
end

GrenadePickupSpawn = createSpawnType("GrenadePickupSpawn", GrenadePickup, "data/bonuses.png", {x1=32, y1=0, x2=64, y2=32});

