----------------- Part 3 ----------------- Code Sample 3.1 " function moleLevel::spawnMole(%this) { // if all spawn points are occupied don't spawn a new mole if( %this.spawnPointsOccupied == %this.respawnPointSet.getCount() ) return; // find a respawn point %pointFound = false; // search until we find a respawn point while ( !%pointFound ) { %respawnPointIndex = getRandom( %this.respawnPointSet.getCount()-1 ); %respawnPoint = %this.respawnPointSet.getObject( %respawnPointIndex ); if( %respawnPoint.isOccupied $= "" ) %pointFound = true; } // select a color for the mole %color = getRandom(2); if( %color == 0 ) %color = "Red"; if( %color == 1 ) %color = "Green"; if( %color == 2 ) %color = "Lilac"; // create a new Mole %mole = new t2dAnimatedSprite() { sceneGraph = %this; class = "Mole"; layer = 4; size = "16 16"; }; // play the right animation according to the color %mole.playAnimation("animMoleComeOut" @ %color); // save the selected color as dynamic field %mole.moleColor = %color; // set the position of the mole to the position of the respawn point %mole.setPosition( %respawnPoint.getPosition() ); // mark this respawn point as occupied %respawnPoint.isOccupied = true; // increment the occupied counter %this.spawnPointsOccupied += 1; // give the mole a reference to its respawn point %mole.respawnPoint = %respawnPoint; } " Code Sample 3.2 " function mole::onAnimationEnd(%this) { if( %this.getAnimationName() $= ("animMoleWhacked" @ %this.moleColor) ) { // free the respawn point for other moles %this.respawnPoint.isOccupied = ""; // decrement the occupied counter %this.sceneGraph.spawnPointsOccupied -= 1; %this.sceneGraph.schedule( 1500, "spawnMole"); %this.safeDelete(); } } " Code Sample 3.3 " function moleLevel::onLevelLoaded(%this) { %this.respawnPointSet = new SimSet(); // set the occupied counter to zero %this.spawnPointsOccupied = 0; } " Code Sample 3.4 " // values in milliseconds $MOLE_FACTORY::maxCreationInterval = 2000; $MOLE_FACTORY::minCreationInterval = 400; $MOLE_FACTORY::speedUp = 30; function moleLevel::startFactory(%this) { // spawn a new mole %this.spawnMole(); // calculate when to spawn the next mole %delay = %this.minCreationInterval + getRandom(%this.maxCreationInterval - %this.minCreationInterval); // call the function recursively but with the calculated delay // we save the event id so we can stop the function if we want %this.factoryEventId = %this.schedule( %delay, "startFactory" ); // calculate new creation intervals so the game gets faster %this.minCreationInterval -= %this.speedUp; if(%this.minCreationInterval < 1) %this.minCreationInterval = 1; %this.maxCreationInterval -= %this.speedUp; if(%this.maxCreationInterval < 1) %this.maxCreationInterval = 1; } function moleLevel::stopFactory(%this) { cancel( %this.factoryEventId ); } " Code Sample 3.5 " function moleLevel::onLevelLoaded(%this) { %this.respawnPointSet = new SimSet(); // set the occupied counter to zero %this.spawnPointsOccupied = 0; // load the start values %this.maxCreationInterval = $MOLE_FACTORY::maxCreationInterval; %this.minCreationInterval = $MOLE_FACTORY::minCreationInterval; %this.speedUp = $MOLE_FACTORY::speedUp; // start the mole creation %this.startFactory(); } function MoleLevel::onLevelEnded(%this) { // stop the mole creation %this.stopFactory(); %this.respawnPointSet.delete(); } " Code Sample 3.6 " //This function will let the mole dive into the earth again but only if it was not already whacked. function mole::diveIn(%this) { // only if not already whacked if( %this.getAnimationName() $= ("animMoleComeOut" @ %this.moleColor) ) %this.playAnimation("animMoleDiveIn" @ %this.moleColor); } function Mole::onAdd(%this) { %this.setUseMouseEvents( true ); // let them dive back into the earth after 1 second %this.diveInEventId = %this.schedule(1000,"diveIn"); } "