
Player = Actor:createSubClass();

registerSpawn("Player");
player_leftControl  = addControl("Move left");
player_rightControl = addControl("Move right");
player_upControl    = addControl("Move up");
player_downControl  = addControl("Move down");
player_activateControl = addControl("Activate");
--player_attackControl = addControl("Attack");

player_shootLeftControl = addControl("Shoot left");
player_shootRightControl = addControl("Shoot right");
player_shootUpControl = addControl("Shoot up");
player_shootDownControl = addControl("Shoot down");

player_inventoryButton = addControl("Inventory");
player_inventoryButton_wait = false;


function Player:init()

   Actor.init(self);

   self:setBlockable(true);

   self:setForceUpdate(true);
   self:setSize(32, 32);

   self.animFrame = 0;
   self.direction = 0;

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

   self:setDepth(1);

   self.shotCooldown = 0;

   self.isPlayer = true;

end

function Player:update()

   Actor.update(self);

   local leftRight = getControlValue(player_rightControl) - getControlValue(player_leftControl);
   local upDown    = getControlValue(player_upControl) - getControlValue(player_downControl);
   local attack    = getMouseButton(0);

   local dx,dy = self:getVel();
   local x, y = self:getPos();

   local playerAccel = 1;
   local playerDrag = 0.1;
   local playerAnimSpeed = 0.3;
   local playerAttackAnimSpeed = 0.5;

   dx = dx + (leftRight / 32768) * playerAccel;
   dy = dy + (upDown    / 32768) * playerAccel;

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

   self:setVel(dx, dy);

   if #widgetStack == 0 or widgetStack[#widgetStack]:getName() == "hud" then

      local shootLeftRight = getControlValue(player_shootRightControl) - getControlValue(player_shootLeftControl);
      local shootUpDown    = getControlValue(player_shootUpControl) - getControlValue(player_shootDownControl);
      local shootMag       = math.sqrt(shootLeftRight * shootLeftRight + shootUpDown * shootUpDown);
      
      if shootMag > 8192 then
         -- TODO: Look in the direction given.
      end

      if shootMag > 16384 then
         -- Shoot in the direction given.
         attack = true;
      end

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

         if leftRight < 0 then
            self:setFlipHorizontal(false);
            self.direction = 0;
         elseif leftRight > 0 then
            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 shotSpeed = 32;
            local shotOffset = 32;

            local newShot = Bullet:create();
            newShot.editorProperties.faction = self.editorProperties.faction;

            local mx, my;
            local mdx;
            local mdy;

            if shootMag > 16384 then

               -- Shoot in the stick direction.

               mdx = shootLeftRight;
               mdy = shootUpDown;

            else

               mx, my = getMousePos();
               mdx = mx - x;
               mdy = my - y;

            end

            local mdist = math.sqrt(mdx * mdx + mdy * mdy);
            mdx = mdx / mdist;
            mdy = mdy / mdist;
            x = x + mdx;
            y = y + mdy;

            newShot:setPos(x + mdx * shotOffset, y + mdy * shotOffset);
            newShot:setVel(mdx * (shotSpeed), mdy * (shotSpeed));
            newShot:setSpriteAngle(90 + math.atan2(mdx, mdy) * -(180 / 3.14));
            newShot:setLayer(self:getLayer());

            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

   -- Inventory control
   local inventoryButtonDown = getControlBoolValue(player_inventoryButton);
   if inventoryButtonDown then
      if not player_inventoryButton_wait then
         player_inventoryButton_wait = true;
         toggleInventoryScreen();
      end
   else
      player_inventoryButton_wait = false;
   end



   -- Edge of screen level transition stuff.

   local levelChanged = false;

   if x < levelActiveArea[1] * -1 then

      -- Left edge

      moveToCell(
         worldMapCell[1] - 1,
         worldMapCell[2],
         worldMapCell[3],
         self);

      x = levelActiveArea[1];

      levelChanged = true;

   elseif x > levelActiveArea[1] then

      -- Right edge

      moveToCell(
         worldMapCell[1] + 1,
         worldMapCell[2],
         worldMapCell[3],
         self);

      x = levelActiveArea[1] * -1;

      levelChanged = true;

   end

   if y < levelActiveArea[2] * -1 then

      -- Top edge

      moveToCell(
         worldMapCell[1],
         worldMapCell[2] - 1,
         worldMapCell[3],
         self);

      y = levelActiveArea[2];

      levelChanged = true;

   elseif y > levelActiveArea[2] then

      -- Bottom edge

      moveToCell(
         worldMapCell[1],
         worldMapCell[2] + 1,
         worldMapCell[3],
         self);

      y = levelActiveArea[2] * -1;

      levelChanged = true;

   end

   if levelChanged then
      self:setPos(x, y);
   end

end

function Player:postUpdate()

   -- Camera adjustment stuff.
   local x, y = self:getPos();
   local sx, sy = getScreenSize();

   local currentTop = y + (sy / 2);
   local currentLeft = x - (sx / 2);

   local currentRight = x + (sx / 2);
   local currentBottom = y - (sy / 2);

   if currentTop > levelActiveArea[2] then
      y = levelActiveArea[2] - (sy/2);
   elseif currentBottom < -1 * levelActiveArea[2] then
      y = -1 * levelActiveArea[2] + (sy/2);
   end

   if currentLeft < -1 * levelActiveArea[1] then
      x = -1 * levelActiveArea[1] + (sx/2);
   elseif currentRight > levelActiveArea[1] then
      x = levelActiveArea[1] - (sx/2);
   end

   setCameraPos(x, y);


end


