

metalManScale = 2;

MetalBlade = Entity:createSubClass();

function MetalBlade:init()
	self:setTexture("data/metalsheet.png");
	self:setGravityInfluence(0);
	self:setSpriteFrame(336, 0, 352, 16);
	self.scale = metalManScale;
	self:setSize(16 * self.scale, 16 * self.scale);
	self:setSpriteScale(self.scale);
	self:setBlockable(false);
	self:setForceUpdate(true);
end

function MetalBlade:update()
	if self:getAge() > 200 then
		self:die();
	end
end





MetalMan = Entity:createSubClass();

function MetalMan:init()
	self:setTexture("data/metalsheet.png");

	self:setGravityInfluence(1);
	self:setSpriteFrame(0, 0, 32, 48);
	self:setSpriteOffset(0, -12);
	self:setBlockable(true);
	
	self.animationState = 0;
	self.animationFrame = 0;
	self.animationSpeed = 0.25;
	self.facing = 0;
	self.scale = 2;
	
	self.alreadyThrew = false;
	self.alreadyJumped = false;

	self:setSpriteScale(self.scale);
	self:setSize(16 * self.scale, 24 * self.scale);
end

function MetalMan:save()
	return {
		animState = self.animationState,
		animFrame = self.animationFrame,
		animSpeed = self.animationSpeed,
		facing = self.facing,
		scale = self.scale
	};
end

function MetalMan:initFromSave(saveData)
	self.animationState = saveData.animState;
	self.animationFrame = saveData.animFrame;
	self.animationSpeed = saveData.animSpeed;
	self.facing = saveData.facing;
	self.scale = saveData.scale;
end

function MetalMan:update()
	--self:setGravityInfluence(self.scale);
	self:setSize(16 * self.scale, 24 * self.scale);
	self:setSpriteScale(self.scale);

	-- Get a bunch of information
	local dx, dy = self:getVel();
	local x, y = self:getPos();
	
	if dx < 0 then 
		self.facing = 0;
	elseif dx > 0 then
		self.facing = 1;
	end


	-- Deal with AI, actions, and whatever.
	local axis0 = getJoyAxis(0);
	local axis1 = getJoyAxis(1);
	local jumpButton = getJoyButton(1);
	local throwButton = getJoyButton(0);
		
	if math.abs(axis0) > 32 then
		if axis0 > 0 then
			dx = 6 * self.scale;
		else
			dx = -6 * self.scale;
		end
	else
		dx = 0;
	end
	
	if jumpButton and self:getOnGround() and not self.alreadyJumped then
		dy = 16 * self.scale;
	end
	
	self.alreadyJumped = jumpButton;
	
	if not jumpButton and dy > 0 and not self:getOnGround() then
		dy = 0;
	end
	
	if throwButton then
		if not self.alreadyThrew then
			blade = MetalBlade:create();
			blade.isPlayerBullet = true;
			
			local bdx, bdy;
			local bspeed = 8 * self.scale;
			
			if axis0 < -32 then
				bdx = -bspeed;
			elseif axis0 > 32 then
				bdx = bspeed;
			else
				bdx = 0;
			end
			
			if axis1 < -32 then
				bdy = bspeed;
			elseif axis1 > 32 then
				bdy = -bspeed;
			else
				bdy = 0;
			end
			
			if math.abs(axis0) < 32 and math.abs(axis1) < 32 then
				bdy = 0;
				if self.facing == 1 then
					bdx = bspeed;
				else
					bdx = -bspeed;
				end
			end
			
			blade:setVel(bdx, bdy);
			blade:setPos(x + bdx * 2, y + bdy * 2);
			
		end
		self.alreadyThrew = true;
	else
		self.alreadyThrew = false;
	end
	
	--self:setVel(dx * 0.95, dy * 0.95);
	self:setVel(dx, dy);

	-- Set the animation state depending on real state.
	if self:getOnGround() then
		-- On the ground
		if math.abs(dx) > 1 then
			-- Moving - walking anim
			self.animationState = 1;
		else
			-- Not moving - standing frame
			if self.alreadyThrew then
				self.animationState = 3;
			else
				self.animationState = 0;
			end
		end
	else
		-- Flying in the air.
			-- Throwing?
			-- Not?
		self.animationState = 2;
	end
	

	-- Handle animation, and setting the correct frame.
	local frameNumber = 0;
	local frameRow = 0;

	self.animationFrame = self.animationFrame + self.animationSpeed;

	if self.animationState == 0 then
		-- 0 = Standing.
		frameNumber = 1;
	elseif self.animationState == 1 then
		-- 1 = Walking.
		
		if self.animationFrame > 3.99 then
			self.animationFrame = 0;
		end
		
		frameNumber = math.floor(self.animationFrame) + 4;
	
	elseif self.animationState == 2 then
		-- 2 = Mid-air
		
		frameNumber = 0;
		
	elseif self.animationState == 3 then
		-- 3 = Standing, threw
		
		frameNumber = 2;
		
	end
	
	if self.facing == 1 then
		frameRow = 1;
	end

	self:setSpriteFrame(frameNumber * 32, frameRow * 48, frameNumber * 32 + 32, frameRow * 48 + 48);

end

registerSpawn("MetalMan");
createSpawnType("MetalManSpawn", MetalMan, "data/metalsheet.png", {x1=0, y1=0, x2=32, y2=48}, {scale=2});
