
Actor = Entity:createSubClass();

registerSpawn("Actor");

function Actor:init()

   self:setGravityInfluence(0.25);
   self:setForceUpdate(true);
   self:setBlockable(true);

   self.isActor = true;

   self:setBounciness(0.25);

   self:setTexture("joust1.tga");

   setSpriteFromDef(self, 1, 1, true);

   self.walkAnimFrame = 0;
   self.flapAnimFrame = 0;

   self:setSpriteScale(2);

   self:setSpriteOffset(0, -2);
   self:setSize(48, 48);

   self:setLayer(3);

end

function Actor:startFlapAnim()

   self.flapAnimFrame = 1;

end

function Actor:update()

   -- Screen wrapping.

   local x, y = self:getPos();
   local dx, dy = self:getVel();
   
   if x < -512 then
      x = 512;
   end

   if x > 512 then
      x = -512;
   end

   self:setPos(x, y);

   if y < screenBottom then
      self:die();
   end


   -- Left/right sprite.
   if not self.directionOverride then

      if dx < 0 then
         self:setFlipHorizontal(true);
      end

      if dx > 0 then
         self:setFlipHorizontal(false);
      end

   else

      if self.directionOverride < 0 then
         self:setFlipHorizontal(true);
      else
         self:setFlipHorizontal(false);
      end

   end

   self.walkAnimFrame = self.walkAnimFrame + (math.abs(dx) * 0.2);
   if self.walkAnimFrame > 5.99 then
      self.walkAnimFrame = 0;
   end

   if self:getOnGround() then
      
      -- Ground walk animation or stand still frame.
      if (math.abs(dx) < 0.1) then
         setSpriteFromDef(self, 1, 1, true);
      else
         setSpriteFromDef(self, 2, 1 + math.floor(self.walkAnimFrame), true);
      end

   else

      setSpriteFromDef(self, 3, 1 + math.floor(self.flapAnimFrame), true);

      if self.flapAnimFrame > 0 then
         self.flapAnimFrame = self.flapAnimFrame + 0.5;
         if self.flapAnimFrame > 3.99 then
            self.flapAnimFrame = 0;
         end
      end

   end

end

function Actor:collidedWithWorld()

end

function Actor:collided(otherEnt)

   local ent = entitiesByName[otherEnt];
   
   if ent then

      local myx, myy = self:getPos();
      local otx, oty = ent:getPos();

      local bounceBoost = 8;
      local killBoost = 8;
   
      if ent.isActor then

         -- TODO: Add middle "safe" zone where they just bounce off.

         local dist = math.abs(myy - oty);

         if(dist > 2) then

            -- Big difference. Someone will die.

            if myy > oty then
               
               -- Hit another player from top.

               local dx, dy = self:getVel();
               --dy = dy + killBoost;
               dy = killBoost;
               self:setVel(dx, dy);
               
            end

            if myy < oty then

               -- Hit by another player.

               if self.gotHit then
                  self:gotHit(otx - myx, oty - myy)
               else
                  self:die();
               end

            end

         else

            -- No significant y value difference. Just bounce off each
            -- other.

            local dx, dy = self:getVel();

            if myx < otx then
               dx = dx - bounceBoost;
            else
               dx = dx + bounceBoost;
            end

            self:setVel(dx, dy);

         end

      end

   end

end


