-- Class for stuff that can be manually activated or activated upon
-- being touched.

Activatable = Entity:createSubClass();

function Activatable:init()

   if not self.editorProperties then
      self.editorProperties = {};
   end

   self.editorProperties.touchActivated = false;
   self.editorProperties.playerActivateOnly = true;
   self.editorProperties.singleActivations = true;

   self.playerTouchCountdown = 0;

   -- Initally set to true in case the player spawns on top of this or
   -- something silly.
   self.waitingForPlayerToLetGo = true;

   self:setLayer(3);

end

function Activatable:initFromSave(saveData)

   

end

function Activatable:save()

   return { };

end

function Activatable:collided(otherEnt)

   local ent = entitiesByName[otherEnt];
   local passPlayerOnlyCheck;

   if ent then
      if ent.isPlayer == true then
         passPlayerOnlyCheck = true;
      end
   end

   if not self.editorProperties.playerActivateOnly then
      passPlayerOnlyCheck = true;
   end

   if passPlayerOnlyCheck then

      self.lastTouchedEnt = otherEnt;
      
      if self.editorProperties.touchActivated then
         
         if self.editorProperties.singleActivations then

            if not self.waitingForPlayerToLetGo then
               self:activate();
               self.waitingForPlayerToLetGo = true;
            end

         else
            self:activate();
         end
         
      end

      self.playerTouchCountdown = 3;
         
   end


end

function Activatable:update()

   if self.playerTouchCountdown > 0 and not self.editorProperties.touchActivated then

      -- Player is standing on us and we're manually activated.

      -- Get the player controller value for this thing.

      -- TODO: If we ever go MP, make sure this is coming from the
      -- right player.

      local activateHeld = getControlBoolValue(player_activateControl);

      if self.editorProperties.singleActivations then

         -- Not waiting, do an activation if the button is pressed.
         if not self.waitingForPlayerToLetGo and activateHeld then
            self:activate();
            self.waitingForPlayerToLetGo = true;
         end

         -- Button not pressed but we're waiting for them to let go.
         if self.waitingForPlayerToLetGo and not activateHeld then
            self.waitingForPlayerToLetGo = false;
         end

      else

         if activateHeld then
            self:activate();
         end

      end

   end

   self.playerTouchCountdown = self.playerTouchCountdown - 1;
   
   if self.playerTouchCountdown < 0 then
      self.waitingForPlayerToLetGo = false;
   end

end

function Activatable:activate()

   -- In practice, this shouldn't happen.
   writeConsole("Activatable base class triggered.\n");

end

