
Player = Actor:createSubClass();
registerSpawn("Player");

function Player:init()

   Actor.init(self);

   local playerNumber = 1;

   while (entitiesByName["Player_"..playerNumber]) do
      playerNumber = playerNumber + 1;
   end

   if playerNumber > maxPlayerNum then
      writeConsole("Reached maximum number of players.\n");
      self:die();
   else
      self:setName("Player_"..playerNumber);
   end

   self.playerNumber = playerNumber;
   self.wasHoldingJump = false;
   self.isPlayer = true;

   self:setLabelFont("font1.tga");
   self:setLabel("Player "..self.playerNumber);

end


function Player:update()

   Actor.update(self);

   -- Static stuff.
   local playerAccel        = 0.25;
   local playerMaxSpeed     = 8;
   local playerJumpBoost    = 4;
   local playerMaxVertSpeed = 8;

   -- Handle player input.

   local controlSet = playerControls[self.playerNumber];
   local leftRight = getControlValue(controlSet["Right"]) - getControlValue(controlSet["Left"]);
   local dx,dy = self:getVel();
   
   if leftRight < 0 then
      self.directionOverride = -1;
   end

   if leftRight > 0 then
      self.directionOverride = 1;
   end

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

   if dx > playerMaxSpeed then
      dx = playerMaxSpeed;
   end

   if dx < -playerMaxSpeed then
      dx = -playerMaxSpeed
   end

   local jumpButton = getControlBoolValue(controlSet["Jump"]);

   if not self.wasHolding and jumpButton then
      -- Do boost.
      
      dy = dy + playerJumpBoost;
      self.wasHolding = true;
      self:startFlapAnim();
   end

   if not jumpButton then
      self.wasHolding = false;
   end

   if dy > playerMaxVertSpeed then
      dy = playerMaxVertSpeed;
   end

   if dy < -playerMaxVertSpeed then
      dy = -playerMaxVertSpeed
   end

   self:setVel(dx, dy);

end

function Player:collided(otherEnt)

   Actor.collided(self, otherEnt);

   if self:getDead() then

      writeConsole("OUCH!\n");

   end

end



