----------------- Part 4 ----------------- Code Sample 4.1 " $MOLE_LEVEL::timePerGame = 30; // seconds function moleLevel::onLevelLoaded(%this) { %this.respawnPointSet = new SimSet(); // set the occupied counter to zero %this.spawnPointsOccupied = 0; %this.maxCreationInterval = $MOLE_FACTORY::maxCreationInterval; %this.minCreationInterval = $MOLE_FACTORY::minCreationInterval; %this.speedUp = $MOLE_FACTORY::speedUp; // Reset score %this.whackedCount = 0; %this.missedCount = 0; // Reset the GuiTextCtrls MoleGuiWhacked.setText("WHACKED:" SPC %this.whackedCount); MoleGuiMissed.setText("MISSED:" SPC %this.missedCount); // start the mole creation %this.startFactory(); } //Then we'll add functions to increase score and update the GuiTextCtrls. function moleLevel::incWhackedCount(%this) { %this.whackedCount++; MoleGuiWhacked.setText("WHACKED:" SPC %this.whackedCount); } function moleLevel::incMissedCount(%this) { %this.missedCount++; MoleGuiMissed.setText("MISSED:" SPC %this.missedCount); } " Code Sample 4.2 " function mole::onAnimationEnd(%this) { if( %this.getAnimationName() !$= ("animMoleComeOut" @ %this.moleColor) ) { // free the respawn point for other moles %this.respawnPoint.isOccupied = ""; // decrement the occupied counter %this.sceneGraph.spawnPointsOccupied -= 1; // update score if( %this.getAnimationName() $= ("animMoleWhacked" @ %this.moleColor) ) %this.scenegraph.incWhackedCount(); else %this.sceneGraph.incMissedCount(); %this.sceneGraph.schedule( 1500, "spawnMole"); %this.safeDelete(); } } " Code Sample 4.3 " function moleLevel::onUpdateScene(%this) { // Calculate how much time is left %timeLeft = $MOLE_LEVEL::timePerGame - %this.getSceneTime(); // Round it to full seconds %timeLeft = mFloor( %timeLeft + 0.5 ); // Update the GuiTextCtrl MoleGuiTimer.setText( %timeLeft ); } " Code Sample 4.4 " function restartGame() { Canvas.popDialog(MoleEndScreenGui); endGame(); startGame( $levelForRestart ); } " Code Sample 4.5 " function startGame(%level) { //exec game scripts exec("./mole.cs"); exec("./respawnPoint.cs"); exec("./moleLevel.cs"); //exec our gui files exec("~/gui/endScreen.gui"); // Set The GUI. Canvas.setContent(mainScreenGui); Canvas.setCursor(DefaultCursor); moveMap.push(); sceneWindow2D.setUseObjectMouseEvents( true ); if( isFile( %level ) || isFile( %level @ ".dso")) { sceneWindow2D.loadLevel(%level); $levelForRestart = %level; } } " Code Sample 4.6 " function moleLevel::onUpdateScene(%this) { // Calculate how much time is left %timeLeft = $MOLE_LEVEL::timePerGame - %this.getSceneTime(); // Time-check if( %timeLeft < 0 ) { %this.setScenePause( true ); Canvas.pushDialog( MoleEndScreenGui ); } // Round it to full seconds %timeLeft = mFloor( %timeLeft + 0.5 ); // Update the GuiTextCtrl MoleGuiTimer.setText( %timeLeft ); } "