

Zombie = Entity:createSubClass();
function Zombie:init()
	self:setBlockable(true);
	self:setSize(68, 128);
	self:setSpriteScale(4);
	self:setSpriteFrame(0, 0, 17, 32);
	self:setTexture("data/castlevania2stuff.png");
	self.moveDir = 0;
	self.animFrame = 0;
	self.isBaddy = true;
	self.damage = 0;
	self.dyingAnimFrame = 0;
	self.hitLastFrame = false;
	self:setForceUpdate(true);
	self:setGravityInfluence(1);
	self:setBounciness(0.5);
	
	self.editorProperties = {isLoadedFromXml="Nope"};
end

function Zombie:update()

	self:setColor(
		(math.sin(self:getAge() / 16) + 1) / 2,
		(math.sin(self:getAge() / 32) + 1) / 2,
		0);
		
	if self.hitLastFrame then
		self:setFlashing(true);
	else
		self:setFlashing(false);
	end

	--if self.hitLastFrame then
	--	self:setAlpha(0.5);
	--else
	--	self:setAlpha(1);
	--end
	self.hitLastFrame = false;

	if self.dyingAnimFrame > 0 then
		self.dyingAnimFrame = self.dyingAnimFrame + 1;
		self:setAlpha(1.0 - (self.dyingAnimFrame / 10.0));
		self:setSpriteScale(4 + (self.dyingAnimFrame / 10.0) * 4);
		if self.dyingAnimFrame > 10 then
			if entitiesByName[self.spawnerName] then
				entitiesByName[self.spawnerName]:triggerSpawn();
			end
			self:die();
		end
		--writeConsole(self.dyingAnimFrame.."\n");
		return;
	end

	local vx, vy = self:getVel();
	if(math.abs(vx) < 1) then
		if self.moveDir == 0 then
			vx = 8;
			self.moveDir = 0;
		else
			vx = -8;
			self.moveDir = 1;
		end
	end
	if vx < 0 then
		vx = -8;
		self.moveDir = 0;
		self:setSpriteAngle(self:getSpriteAngle() + 5);
	else
		vx = 8;
		self.moveDir = 1;
		self:setSpriteAngle(self:getSpriteAngle() - 5);
	end
	self:setVel(vx, vy);
	
	self.animFrame = self.animFrame + 0.25;
	
	if math.floor(self.animFrame) >= 2 then
		self.animFrame = 0;
	end
	
	local spritex = math.floor(self.animFrame) * 17;
	
	if self.moveDir == 1 then
		spritex = spritex + 34;
	end
	
	self:setSpriteFrame(spritex, 0, spritex + 17, 32);
end

function Zombie:collided(otherEntName)
	--writeConsole("Zombie hit "..otherEntName.."\n");
	local otherEnt = entitiesByName[otherEntName];
	if otherEnt then
		if otherEnt.isPlayerBullet then
			otherEnt:die();
			--self:setAlpha(0.5);
			self.damage = self.damage + 1;
			self.hitLastFrame = true;
		end
	end
	
	if self.damage > 16 and self.dyingAnimFrame < 1 then
		self.dyingAnimFrame = 1;
		self:setBlockable(false);
		if self.deathSpeak ~= nil then
			writeConsole(self.deathSpeak.."\n");
		end
	end
end

function Zombie:destruct()
	writeConsole("Zombie "..self:getName().." dying!\n");
	--for i = 0, 6, 1 do
	--	clearLayer(i, "player", "camera");
	--end
	--clearTiles();

    local links = self:getAllLinkTargets();
    local k, v;
    for k, v in pairs(links) do
       local ent = entitiesByName[v];
       ent.activationOverride = true;
    end

end

function Zombie:save()
	writeConsole("Zombie saving...\n");
	
	return {foo="bar", wark="moo", cheese="awesome"};
end

function Zombie:initFromSave(loadTable)
	writeConsole("Zombie loading...\n");
	for k,v in pairs(loadTable) do
		writeConsole(k.." = "..v.."\n");
	end
	
	self.moveDir = 0;
	self.animFrame = 0;
	self.isBaddy = true;
	self.damage = 0;
	self.dyingAnimFrame = 0;
	self.hitLastFrame = false;

	self.editorProperties = {isLoadedFromXml="Yes"};

end

ZombieSpawner =	createSpawnType(
	"ZombieSpawn",
	Zombie,
	"data/castlevania2stuff.png",
	{x1=0, y1=0, x2=17, y2=32},
	{a=1, c=true, deathSpeak="foobar"});

registerSpawn("Zombie");

