

-- TODO: Move this to another file.
maxControlNum = 0;
function addControl(name)
   
   local ret = maxControlNum;
   setControlName(maxControlNum, name);
   maxControlNum = maxControlNum + 1;
   return ret;

end


-- TODO: Move these to another file.
gameScreenWidth = 1024;
gameScreenHeight = 768;

-- Define the size of all our levels.
levelActiveArea = {
   1024, 1024
};

levelEdgeMarkerEnts = {};

include("autogen_spritedefs.lua");
include("spritefuncs.lua");
include("actor.lua");
include("player.lua");
include("light.lua");
include("projectile.lua");
include("explosion.lua");
include("camera.lua");
include("npc.lua");
include("pathnode.lua");

include("chaser.lua");
include("spawner.lua");

include("ui.lua");

include("activatable.lua");
include("teleporter.lua");

include("inventoryui.lua");

include("tempmarker.lua");

--loadTiles("test.xml");
--loadEntities("test.xml");


-- Location in the world map.
worldMapCell = { 10, 10, 10 };

function moveToCell(x, y, z, playerEnt)

   -- Clear everything.

   local playerName = "";

   if playerEnt then 
      playerName = playerEnt:getName();
   end

   local i;

   for i = 1, getNumLayers() do
      clearLayer(i-1, playerName);
   end

   unnameDeadEntities();

   worldMapCell = { x, y, z };

   local newMapName = x.."_"..y.."_"..z..".xml";

   loadTiles(newMapName);
   loadEntities(newMapName);

   local mainLayer = getLayerByName("Main");
   if not (mainLayer == -1) then
      playerEnt:setLayer(mainLayer);
   end

   --camera = Camera:create();
   --camera:setLinkTarget(playerName);

end

player = Player:create();
moveToCell(10, 10, 10, player);

--camera:setLinkTarget(player:getName());

-- TODO: Move all fade-related stuff into its own file.
fadeCommandQueue = {};
fadeValue = 1;
function fadeOutAndDo(str)
   fadeCommandQueue[#fadeCommandQueue + 1] = str;
   writeConsole(str.."\n");
end

function updateGlobalFade()
   setSubtractiveFade(fadeValue, fadeValue, fadeValue);

   if #fadeCommandQueue > 0 then
      fadeValue = fadeValue - 0.1;

      if fadeValue <= 0 then
         
         for k,v in pairs(fadeCommandQueue) do
            local cmdStr = v;
            writeConsole(cmdStr.."\n");
            loadstring(cmdStr)();
         end

         fadeCommandQueue = {};

      end

   else
      fadeValue = fadeValue + 0.1;
   end

   if fadeValue < 0 then fadeValue = 0; end
   if fadeValue > 1 then fadeValue = 1; end
end



function worldUpdate()

   updateLetterBox();
   updateGlobalFade();

   updateTempMarkers();

end


-- TODO: Move all this UI crap into its own file.

widgetStack = {};

function ui_push(widget)
   widgetStack[#widgetStack + 1] = widget;
   widgetSetRoot(widgetStack[#widgetStack]);
end

function ui_popAndDestroy()

   if #widgetStack > 0 then
      Widget:delete(widgetStack[#widgetStack]);
      widgetStack[#widgetStack] = nil;
   end

   if #widgetStack > 0 then
      WidgetSetRoot(widgetStack[#widgetStack]);
   end

end





