Player = Entity:createSubClass();

function Player:init()
   self:setTexture("shmup1.tga");
   self:setGravityInfluence(0);
   self:setSpriteFrame(0, 0, 32, 32);
   self:setSize(16, 16);
   self:setForceUpdate(true);

   self.angle = 0;
   --self.thrust = 0;
   self.speed = 1;

   self.shotCooldown = 0;

   --self:setSpriteScale(2);

   local myShadow = Shadow:create();
   myShadow:assign(self);

   self.hp = 20;

   self.hpRestoreCountdown = 0;

   mainPlayer = self;
end

function Player:initFromSave(saveData)
   Player:init();
end

function Player:update()
   local axis0 = getJoyAxis(0);
   local axis1 = getJoyAxis(1);
   local turnRate = 4;
   local acceleration = 1;
   local x, y = self:getPos();

   self.angle = self.angle - (axis0 / 32767.0) * turnRate;

   self:setSpriteAngle(self.angle);

   self.speed = self.speed - (axis1 / 32767.0) * acceleration;
   if self.speed < 1 then
	  self.speed = 1;
   end

   if self.speed > 10 then
	  self.speed = 10;
   end

   local convertedAngle = (self.angle + 90) * 3.14159/180.0;

   local dx = math.cos(convertedAngle);
   local dy = math.sin(convertedAngle);

   self:setVel(dx * self.speed, dy * self.speed);


   local screenShakeX = math.cos(screenShakeAngle) * screenShakeMag;
   local screenShakeY = math.sin(screenShakeAngle) * screenShakeMag;

   setCameraPos(x + screenShakeX, y + screenShakeY);


   local b0 = getJoyButton(0) or getMouseButton(0);

   if b0 and self.shotCooldown <= 0 then

	  local shotSpeed = 15;
	  local shotOffset = 32;

	  local newShot = PlayerShot:create();
      newShot:setLayer(3);

	  newShot:setSpriteAngle(self.angle);

      local mx, my = getMousePos();
      local mdx = mx - x;
      local mdy = my - y;
      local mdist = math.sqrt(mdx * mdx + mdy * mdy);
      mdx = mdx / mdist;
      mdy = mdy / mdist;

      if getJoyButton(0) then
         x = x + dx;
         y = y + dy;
         newShot:setPos(x + dx * shotOffset, y + dy * shotOffset);
         newShot:setVel(dx * (shotSpeed + self.speed), dy * (shotSpeed + self.speed));
      else
         x = x + mdx;
         y = y + mdy;
         newShot:setPos(x + mdx * shotOffset, y + mdy * shotOffset);
         newShot:setVel(mdx * (shotSpeed + self.speed), mdy * (shotSpeed + self.speed));
         newShot:setSpriteAngle(math.atan2(mdx, mdy) * -(180 / 3.14));
     end

      self.shotCooldown = 2;

	  --screenShakeMag = screenShakeMag + 20;
	  --screenShakeAngle = screenShakeAngle + 90;

   else

	  self.shotCooldown = self.shotCooldown - 1;

   end

   local b1 = getJoyButton(1);

   --if b1 and self.shotCooldown <= 0 then
	--  local x, y = self:getPos();
	--  addSplosion(x, y).subsplosions = 10;
	--  --addSplosion(0, 0).subsplosions = 10;
   --end

   if self.hpRestoreCountdown <= 0 and self.hp < 20 then

	  local hpRestoreDelay = 10;

	  writeConsole("Regenerating 1 hp... ");

	  self.hp = self.hp + 1;
	  writeConsole("HP: "..self.hp.."\n");	  

	  self.hpRestoreCountdown = hpRestoreDelay;
   else
	  self.hpRestoreCountdown = self.hpRestoreCountdown - 1;
   end

end

function Player:collided(otherEnt)

   local x,y = self:getPos();
   local ent = entitiesByName[otherEnt];
   
   if not (ent == nil) then

	  if ent.isBadShot then
		 
		 addSplosion(x, y).subsplosions = 2;

		 --self:die();

		 self.hp = self.hp - 1;

		 writeConsole("HP: "..self.hp.."\n");
		 
		 screenShakeMag = screenShakeMag + 10;

		 if self.hp < 0 then
			self:die();

			mainPlayer = nil;

			addSplosion(x, y).subsplosions = 10;

		 end

		 ent:die();

		 self.hpRestoreCountdown = 90;

	  end

   end

end

registerSpawn("Player");

for key,val in pairs(Entity) do
   writeConsole(key.."\n");
end
