-- Generic teleporter class. Covers things like doors, stairs, level
-- transition stuff, and so on. Not just magicy or Star Trek
-- transporter crap.

Teleporter = Activatable:createSubClass();
registerSpawn("Teleporter");

function Teleporter:init()

   Activatable.init(self);

   self.editorProperties.teleportDestinationEnt = "";
   self.editorProperties.teleportDestinationLevel = "";

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

end

function finishTeleport_levelTransition(destLevel)

end

function finishTeleport_moveEnt(lastTouchEnt, destEnt)

   local entDest = entitiesByName[destEnt];
   if entDest then
      local entToMove = entitiesByName[lastTouchEnt];
      if entToMove then
         entToMove:setPos(entDest:getPos());


         -- In case we moved onto a teleporter or something...
         if not (entDest.waitingForPlayerToLetGo == nil) then
            entDest.waitingForPlayerToLetGo = true;
         end

      end
   end

end

function Teleporter:activate()

   if not (self.editorProperties.teleportDestinationLevel == "") then

      -- TODO: Trigger a map move.
      local cmdStr = "finishTeleport_levelTransition(\""..self.editorProperties.teleportDestinationLevel.."\");";
      
      if self.editorProperties.playerActivateOnly then
         fadeOutAndDo(cmdStr);
      else
         loadstring(cmdStr)();
      end

   end

   -- We can use link targets or destination ents here.
   local destEntName = self.editorProperties.teleportDestinationEnt;
   if destEntName == "" then
      destEntName = self:getLinkTarget();
   end

   if not (destEntName  == "") then

      -- Trigger a move to another ent. Must be after map move so we
      -- can move to a new ent on a new map.

      local cmdStr = "finishTeleport_moveEnt(\""..self.lastTouchedEnt.."\", \""..destEntName.."\");";
      if self.editorProperties.playerActivateOnly then
         fadeOutAndDo(cmdStr);
      else
         loadstring(cmdStr)();
      end

   end

end



