//---------------------------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//---------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------
// startGame
// All game logic should be set up here. This will be called by the level builder when you
// select "Run Game" or by the startup process of your game to load the first level.
//---------------------------------------------------------------------------------------------
function startGame (%level)
{//echo("startGame()");echo("%level =" SPC %level);
	Canvas.setContent(sceneWindow2D);
	
	new ActionMap(moveMap);   
	moveMap.push();
	$enableDirectInput = true;
	activateDirectInput();
	enableJoystick();
	
	sceneWindow2D.loadLevel(%level);
	
	Canvas.showCursor();
	
	initiateGlobalVariables();
	stopBGMOgg();
	$gameInProgress = true;
}

// Handles all global variables at start.
function initiateGlobalVariables ()
{//echo("initiateGlobalVariables()");
	// Game mechanic globals.
	// NOTE: Should be set by function _loadGameConfigurationData.
	$cameraMountForce = $Game::CameraTrackSpeed;
	$autoTargetCameraMountForce = $cameraMountForce;//4;	// Hard-coded!
	$MonsterTurnSpeed = $Game::MonsterTurnSpeed;
	
	// Squad globals.
	$maxSquadSize = 4;
	$currentSquadSize = 0;
	$playerSquad[$maxSquadSize] = 0;
	$activeCharacter = 0;
	$leaderPersist = false;
	$scoutPersist = false;
	$grenadierPersist = false;
	$supportPersist = false;
	// Should the long list of globals in spawnPersistentCharacter() be here?
	// For consistency they should, but it shouldn't cause any problems if they are not.
	
	// Monster globals.
	$monsterList = new SimSet();
	
	// Dungeon globals.
	$mazeDimension = 32;
	$mazeRoom[$mazeDimension, $mazeDimension] = 0;		// Just a 1x1 array as a placeholder.
	$mazeLength = 1;
	
	// Control globals.
	$currentCharacterActionFlag = false;
	unlockFireControls();
}

function endGame ()
{//echo("endGame()");
	sceneWindow2D.endLevel();
	moveMap.pop();
	moveMap.delete();
}

function levelCleanup (%scenegraph)//levelCleanup(levelSceneGraph);
{//echo("levelCleanup()");
	echo("Starting level cleanup : " @ %scenegraph);
	if (isObject(%scenegraph))
	{	 
		echo("******* Cleanup Level *******");  
		%sceneObjectList = %scenegraph.getSceneObjectList();
		for (%i = 0; %i < getWordCount(%sceneObjectList); %i++)
		{
			%sceneObject = getWord(%sceneObjectList, %i);
			if(isObject(%sceneObject))
			{
				echo("["@%i@"] object deleting: "@%sceneObject.getClassName());
			   	%sceneObject.delete();			 
			}   
		}
	  	echo("******* Cleanup Level ended *******");
	}
	else
	{
		echo (%scenegraph @ " is not an object");
	}
}

// Called from loadOnlySave() in fileManager.cs.
function beginNewGame ()
{//echo("beginNewGame()");
	Canvas.popDialog(mainScreenGui);
	popMainMenu();
	
	Canvas.hideCursor();


	// Seriously...
	$leaderPersist = true;		// Wtf?
	$scoutPersist = true;		// Why?!?
	// ... wtf is this?

	
	nextDungeonLevel();
	createNewSquad(levelSceneGraph);
	
	createSquadInventory();
	spawnItems();
	
	setPortraits();
	//initializeReadout();
	updateSquadInfoGui();
	Canvas.pushDialog(exploreGui);
	setupInputs();
}