From TDN
//*******************************************************//
// Basic Game Setup Idea
// Rodney Rindels -- Programmer at Large
//*******************************************************//
//*******************************************************//
// package Game
// Implements Game Specific Functions
//*******************************************************//
package Game
{
//****************************************************//
// debug setup
// if (getDebug()){
// //do something requiring debug to be turned on
// }
//
// setDebug(1) to turn on
// setDebug(0) to turn off
//****************************************************//
function getDebug()
{
return $Debug;
}
function setDebug(%val)
{
$Debug = %val;
print("Debug Set to :"@ %val);
return getDebug();
}
//****************************************************//
// print setup
//
// Only print if $Debug = 1
//****************************************************//
function print(%string)
{
if (getDebug())
{
echo("Game:"SPC %string);
}
}
//****************************************************//
// function getGameName()
// Simply returns the $gameName variable.
//****************************************************//
function getGameName()
{
return $gameName;
}
//************************************************//
// function setGameName(%string)
// %string is a string containing the Name of the Game
// you want to set.
//************************************************//
function setGameName(%string)
{
$gameName=%string;
print("GameName set to: " @ %string);
return getGameName();
}
//****************************************************//
// getCaption
//
// Returns the currently set window caption.
//****************************************************//
function getCaption()
{
return $gameCaption;
}
//****************************************************//
// setCaption
//
// sets the Window Caption
//****************************************************//
function setCaption(%string)
{
if (getDebug())
{
$gameCaption = %string SPC "- Debug";
} else {
$gameCaption = %string;
}
print("Caption: " @ $gameCaption);
SetCanvasTitle(getCaption());
return getCaption();
}
//****************************************************//
// setScriptDir / getScriptDir
//
// Sets the Path to the directory containing scripts
//****************************************************//
function setScriptDir(%string)
{
$scriptDir="./"@ %string @"/";
print("Script Directory:" SPC $scriptDir);
return getScriptDir();
}
function getScriptDir()
{
return $scriptDir;
}
//****************************************************//
// setGuiDir / getGuiDir
//
// Gets / sets the path to the directory containing
// gui elements.
//****************************************************//
function setGuiDir(%string)
{
$guiDir = "./"@ %string @"/";
print("Gui Directory: " SPC $guiDir);
return getGuiDir();
}
function getGuiDir()
{
return $guiDir;
}
//****************************************************//
// setLevelDir / getLevelDir
//
// Gets / sets the paths where the levels are contained
//****************************************************//
function setLevelDir(%string)
{
$levelDir =$currentProject @ "/" @ %string @ "/";
print("Level Directory: " SPC $levelDir);
return getLevelDir();
}
function getLevelDir()
{
return $levelDir;
}
//****************************************************//
// defineGlobals
//
// set Initial startup values for things.
//****************************************************//
function defineGlobals()
{
setDebug(1);
setGameName("MyGame");
setGamePaused(0);
setScriptDir("gameScripts");
setLevelDir("data/levels");
setGuiDir("gui");
setDebugState(0);
}
//****************************************************//
// execGUI
//
//exec replacement for GUI's
//****************************************************//
function execGui(%string)
{
print("Exec GUI: " @ %string);
exec(getGuiDir() @ %string);
}
//****************************************************//
// execScript
//
// exec replacement for Scripts
//****************************************************//
function execScript(%string)
{
print("Exec Script: " @ %string);
exec(getScriptDir() @ %string);
}
//****************************************************//
// execLevel
//
// exec replacement for Levels
//****************************************************//
function execLevel(%string)
{
print("Exec Level: " @ %string);
exec(getLevelDir() @ %string);
}
//****************************************************//
// addKeyboardBind
//
// adds a keyboard binding to %obj
//****************************************************//
function addKeyboardBind(%obj,%key,%down,%up)
{
if(isObject(%obj))
{
print("Adding Binding for Key: " @ %key);
%obj.pop();
%obj.bindCmd(keyboard,%key,%down,%up);
%obj.push();
} else {
print("Must Pass ActionMap Object to addKeyboardBind");
return;
}
}
//****************************************************//
// getGamePaused
//
// returns the state of the pausing
//****************************************************//
function getGamePaused()
{
return $gamePaused;
}
//****************************************************//
// setGamePaused
//
// sets the state of the pausing
//****************************************************//
function setGamePaused(%val)
{
$gamePaused = %val;
print("gamePaused set to" SPC %val);
return getGamePaused();
}
//****************************************************//
// pauseGame
//
// Given a SceneGraph it will pause scene.
//****************************************************//
function pauseGame(%obj)
{
if(isObject(%obj))
{
if (getGamePaused())
{
print("Resuming Game");
setGamePaused(0);
pauseWindow.setVisible(false);
%obj.setScenePause(false);
setCaption(getGameName());
} else {
print("Pausing Game");
setGamePaused(1);
pauseWindow.setVisible(true);
setCaption(getGameName() SPC "- Paused");
%obj.setScenePause(true);
}
} else {
print("Non Object Reference Passed to pauseGame");
return;
}
}
//****************************************************//
// toggleDebug
//
// turns Scene Debugging on and off.
//****************************************************//
function toggleDebug(%obj)
{
if (isObject(%obj))
{
if (getDebugState())
{
print("Turning Debug Off");
%obj.setDebugOff(0);
setDebugState(0);
} else {
print("Turning Debug On");
%obj.setDebugOn(0);
setDebugState(1);
}
} else {
print("You Must Pass an Object to toggleDebug");
}
}
//****************************************************//
// getDebugState
//
// gets the state of Scene Debugging
//****************************************************//
function getDebugState()
{
return $debugState;
}
//****************************************************//
// setDebugState
//
// sets the $debugState
//****************************************************//
function setDebugState(%val)
{
$debugState = %val;
print("Debug State: " @ %val);
return getDebugState();
}
//****************************************************//
// setCurrentLevel
//
// Sets the $currentLevel to track which level were on
//****************************************************//
function setCurrentLevel(%string)
{
$currentLevel = %string;
print("Current Level: " SPC %string);
return getCurrentLevel();
}
//****************************************************//
// getCurrentLevel
//
// returns the current level $currentLevel
//****************************************************//
function getCurrentLevel()
{
return $currentLevel;
}
//****************************************************//
// setCurrentGui
//
// sets the $currentGui
//****************************************************//
function setCurrentGui(%obj)
{
if (isObject(%obj))
{
$currentGui = %obj;
print("Current Gui: " SPC %obj.getName());
} else {
$currentGui = mainScreenGui;
}
return getCurrentGui();
}
//****************************************************//
// getCurrentGui
//
// gets the $currentGui
//****************************************************//
function getCurrentGui()
{
return $currentGui;
}
//****************************************************//
// setCurrentCursor
//
// sets the $currentCursor
//****************************************************//
function setCurrentCursor(%obj)
{
if( isObject(%obj))
{
$currentCursor = %obj;
} else {
$currentCursor = DefaultCursor;
}
return getCurrentCursor();
}
//****************************************************//
// getCurrentCursor
//
// gets the $currentCursor
//****************************************************//
function getCurrentCursor()
{
return $currentCursor;
}
//****************************************************//
// getCurrentSceneWindow
//
// returns the current scene window $currentSceneWindow
//****************************************************//
function getCurrentSceneWindow()
{
return $currentSceneWindow;
}
//****************************************************//
// setCurrentSceneWindow
//
// sets the CurrentSceneWindow $currentSceneWindow
//****************************************************//
function setCurrentSceneWindow(%obj)
{
if (isObject(%obj))
{
print("Setting SceneWindow: " SPC %obj.getName());
$currentSceneWindow = %obj;
} else {
$currentSceneWindow = mainScreenGui;
}
return $currentSceneWindow;
}
//****************************************************//
// setCurrentSceneGraph
//
//sets the currentSceneGraph $currentSceneGraph
//****************************************************//
function setCurrentSceneGraph(%obj)
{
if (isObject(%obj))
{
print("Setting SceneGraph: " SPC %obj.getName());
$currentSceneGraph=%obj;
} else {
$currentSceneGraph = t2dScene;
}
return getCurrentSceneGraph();
}
//****************************************************//
// getCurrentSceneGraph
//
// returns the current SceneGraph
//****************************************************//
function getCurrentSceneGraph()
{
return $currentSceneGraph;
}
//****************************************************//
// startGame
//
// required StartGame, sets the level, then calls
// our loadGameLevel
//****************************************************//
function startGame(%level)
{
setCurrentLevel(%level);
loadGameLevel(%level);
}
//****************************************************//
// loadGameLevel
//
// given a level, will set the game to use that level
// this requires you've set
// setCurrentGui, setCurrentCursor, and setCurrentLevel
// previous to calling this.
//****************************************************//
function loadGameLevel(%level)
{
Canvas.setContent(getCurrentGui());
Canvas.setCursor(getCurrentCursor());
$currentSceneWindow.loadLevel(getCurrentLevel());
setCaption(getGameName());
}
};
//*******************************************************//
// initializeProject
//
// called on project activation.
//*******************************************************//
function initializeProject()
{
//*******************************************************//
// Activate the Game Package
//*******************************************************//
activatePackage(Game);
//*******************************************************//
// set up default values for globals.
//*******************************************************//
defineGlobals();
//*******************************************************//
// Execute our Startup files.
//*******************************************************//
execGui("mainScreen.gui");
execScript("game.cs");
execLevel("test.t2d");
//*******************************************************//
// Set the Starter Visual Elements
//*******************************************************//
setCurrentGui(mainScreenGui);
setCurrentCursor(DefaultCursor);
setCurrentSceneWindow(sceneWindow2D);
setCurrentSceneGraph(t2dScene);
//*******************************************************//
// Setup our Key Bindings
//*******************************************************//
addKeyboardBind( gameActionMap , "alt p" , "" , "pauseGame(t2dScene);" );
addKeyboardBind( gameActionMap , "e" , "" , "toggleLevelEditor();" );
addKeyBoardBind( gameActionMap , "d" , "" , "toggleDebug(t2dScene);" );
addKeyboardBind( gameActionMap , "ctrl q" , "" , "shutdownProject();" );
//*******************************************************//
// start up the game.
//*******************************************************//
startGame(getLevelDir() @ "test.t2d");
}
//*******************************************************//
// setupKeyBinds called from common/main.cs
// in our initializeProject we setup our package
// based bindings.
// so here lets just clear the ActionMap out
//*******************************************************//
function setupKeybinds()
{
//TODO: make this an array loop
// or some odd thing.
cleanActionMap(gameActionMap);
cleanActionMap(playerActionMap);
cleanActionMap(levelActionMap);
}
//*******************************************************//
// cleanActionMap
//
// dumps and action map, and resets it
//*******************************************************//
function cleanActionMap(%obj)
{
if(isObject(%obj))
{
%obj.pop();
}
new ActionMap(%obj);
return %obj;
}
//*******************************************************//
//shutdownProject
//
// attempt to safely shutdown Game
//*******************************************************//
function shutdownProject()
{
print("Shutting Down Game");
setGamePaused(0);
pauseGame(getCurrentSceneGraph());
deactivatePackage(Game);
endGame();
quit();
}