
globalData = {};

writeConsole("Lily Project 3 - Creating classes.\n");

include("autogen_spritedefs.lua");

-- UI stuff

include("uijunk.lua");

-- Utilily stuff

include("spritefuncs.lua");

-- Entity types

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

include("hud.lua");

include("player.lua");
include("camera.lua");

include("zombie.lua");
include("teleporter.lua");
include("bonuses.lua");
include("projectiles.lua");

include("doorswitch.lua");

include("metalman.lua");
include("worldmaplily.lua");

include("checkpoint.lua");

include("infoboxpickup.lua");

include("fade.lua");

include("lights.lua");

include("explodingbarrel.lua");

-- Cutscenes

include("cutscene.lua");

-- Actual code...

updateFrameNumber = 0;

-- Set up control names in Engine UI.

setControlName(0, "Aim Up");
setControlName(1, "Aim Down");
setControlName(2, "Move Left");
setControlName(3, "Move Right");

setControlName(4, "Shoot");
setControlName(5, "Jump");
setControlName(6, "Grenade (hold)");

setControlName(7, "Activate");

setControlName(8, "Pause / Save slot menu");

setControlName(9, "Start level (world map)");

function worldUpdate()

   updateFrameNumber = updateFrameNumber + 1;
   
   -- FIXME: Won't work with an actual HUD as the root.
   if getControlBoolValue(8) and startLifted and not (widgetGetRoot()) then
      startLifted = false;
      startSaveMenu();
   end
   
   if not getControlBoolValue(8) then
      startLifted = true;
   end
   
   -- Right now this checks if there's any root widget at all, but in
   -- the future we should just check that it's NOT the main game UI
   -- (hp bars, etc.)
   if widgetGetRoot() then
      paused = true;
   else
      paused = false;
   end

   if paused then
      setExclusiveUpdatesThisFrame("");
   end

   fade_update();
   cutSceneUpdate();
end

--function worldPostUpdate()
--	writeConsole("Worldpostupdate!\n");
--end

function clearEverything()
	for i = 0, 6, 1 do
		clearLayer(i, "saveMenu", "saveMenuBG");
	end
end

-- Sets up an entirely new game.
function setupFreshGame()
	clearEverything();
	
	globalData = {};

    -- Start us on the world map.
    globalData.currentLevel = "worldmap1.xml"

    -- Set up world map.
    globalData.worldNodeCheckpoints = { };

    -- If we start in a level instead of world map, we'll need to set
    -- these to be accurate to the world map. (Just leave them nil
    -- otherwise.)
    globalData.worldNodeName = nil;
    globalData.worldMapLevel = nil;

    -- Load first level.
	loadTiles(globalData.currentLevel..".tiles.rle");
	loadEntities(globalData.currentLevel);
	
    -- FIXME: Right now, the player is saved in the starting world
    -- map. Maybe we should just spawn WorldMapPlayer here?

    -- Now that everything is set up, save the initial state.
	saveEntities(globalData.currentLevel);
	saveTable("globaldata.xml", globalData);
	
	setupHud();
end

function loadGame(saveName)
	clearEverything();
	
	setSaveName(saveName);
	
	globalData = loadTable("globaldata.xml");
	
	if globalData then
		loadTiles(globalData.currentLevel..".tiles.rle");
		loadEntities(globalData.currentLevel);
		setupHud();
	else
		setupFreshGame()
	end
	
	saveTable("globaldata.xml", globalData);
	
end

function startSaveMenu()

   if not widgetGetRoot() then
      paused = true;
      popUpNewSaveMenu();
   end

end

if initialRunDone == nil then
   --loadGame("testSave");

   --startMusic("data/blastermaster3.ogg");
   --startMusic("collect1.ogg");
   --startMusic("dung1.ogg");


   initialRunDone = true;
   paused = false;
   
   popUpNewSaveMenu();

else
   writeConsole("************** ERROR: Initial run going twice ********************\n");
end

loadEntities("titlescreen.xml");


--startCutscene();


