
Player = Actor:createSubClass();

registerSpawn("Player");
player_leftControl = addControl("Move left");
player_rightControl = addControl("Move right");
player_jumpControl = addControl("Jump");
player_attackControl = addControl("Attack");

function Player:init()

   Actor.init(self);

   --self:setGravityInfluence(1.5);
   self:setGravityInfluence(1);
   self:setBlockable(true);

   self:setForceUpdate(true);

   self:setSize(32, 48);
   --self:setSize(64, 96);

   self.jumpHeld = false;
   self.doubleJumped = false;
   self.hasDoubleJump = true;

   self.animFrame = 0;

   self.direction = 0;

   self:setTexture("oldsprites.tga");
   self:setSpriteScale(2);
   setSpriteFromDef(self, 3, 1, true);

   self:setDepth(1);

   self.shotCooldown = 0;
end

function Player:update()

   Actor.update(self);

   local movement = getControlValue(player_rightControl) - getControlValue(player_leftControl);
   local jump     = getControlBoolValue(player_jumpControl);
   local attack   = getControlBoolValue(player_attackControl);

   local dx,dy = self:getVel();

   local playerAccel = 1;
   local jumpAccel = 32;
   local playerDrag = 0.05;
   local playerGrav = 2;
   local playerAnimSpeed = 0.3;
   local playerAttackAnimSpeed = 0.5;

   dx = dx + (movement / 32768) * playerAccel;

   dx = dx * (1.0 - playerDrag);
   dy = dy * (1.0 - playerDrag);

   if not(jump) and self.jumpHeld then

      self.jumpHeld = false;

      -- Cut off vertical velocity when the jump button is released
      -- while we're still accelerating upwards.
      if dy > 0 then
         dy = 0;
      end

   end

   local onGround = self:getOnGround();
   if onGround then
      self.doubleJumped = false;
   end

   if not self.jumpHeld then
      
      if jump and (onGround or (not(self.doubleJumped) and self.hasDoubleJump)) then
         
         dy = jumpAccel;

         if not onGround then
            self.doubleJumped = true;
         end

      end

   end

   if jump then
      self.jumpHeld = true;
   end

   self:setVel(dx, dy);


   if math.abs(movement) > 0.1 then
      
      if not attack then
         self.animFrame = self.animFrame + playerAnimSpeed;
      end

      if movement < 0 then
         self:setFlipHorizontal(false);
         self.direction = 0;
      else
         self:setFlipHorizontal(true);
         self.direction = 1;
      end

   end

   self.shotCooldown = self.shotCooldown - 1;

   if attack then
      self.animFrame = self.animFrame + playerAttackAnimSpeed;
      if self.shotCooldown <= 0 then
         local newShot = Bullet:create();
         local shotdx = 1;
         local shotx, shoty = self:getPos(); 

         if self.direction < 1 then
            shotdx = -1;
         end

         shotx = shotx + 32 * shotdx;
         shoty = shoty - 6;

         newShot:setPos(shotx, shoty);
         newShot:setVel(shotdx * 32, 0);

         local newLight = Light_Basic:create();
         newLight:setLinkTarget(newShot:getName());

         self.shotCooldown = 4;
      end
   end

   if self.animFrame > 1.99 then
      self.animFrame = 0;
   end

   if attack then
      setSpriteFromDef(self, 3, 2 + math.floor(self.animFrame), true);
   else
      setSpriteFromDef(self, 3, 1 + math.floor(self.animFrame), true);
   end

end



