----------------- Part 6 - Section 1 ----------------- Code Sample 6.1.1 " function startGame(%level) { exec("./player.cs"); exec("./fishfood.cs"); exec("./fish.cs"); // Set The GUI. Canvas.setContent(mainScreenGui); Canvas.setCursor(DefaultCursor); moveMap.push(); if( isFile( %level ) || isFile( %level @ ".dso")) sceneWindow2D.loadLevel(%level); } " Code Sample 6.1.2 " function Fish::getSpeed(%this) { return getRandom(%this.minSpeed, %this.maxSpeed); } " Code Sample 6.1.3 " function Fish::reposition(%this) { %this.setPositionY(getRandom(-45, 30)); } " Code Sample 6.1.4 " function Fish::onLevelLoaded(%this, %scenegraph) { %this.setLinearVelocityX(-%this.getSpeed()); } " Code Sample 6.1.5 " function Fish::onWorldLimit(%this, %mode, %limit) { switch$(%limit) { case "left": %this.setFlipX(true); %this.setLinearVelocityX(%this.getSpeed()); %this.reposition(); case "right": %this.setFlipX(false); %this.setLinearVelocityX(-%this.getSpeed()); %this.reposition(); } } " Code Sample 6.1.6 " function FishFood::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts) { if(%dstObj.class $= "PlayerFish" || %dstObj.class $= "Fish") { %srcObj.spawn(); } } " ----------------- Part 6 - Section 3 ----------------- Code Sample 6.3.1 " function PlayerFish::modifyLife(%this, %dmg) { %this.life += %dmg; if(%this.life > 100) { %this.life = 100; } else if (%this.life < 0) { %this.life = 0; } if(%this.life <= 30) { %this.dead(); } else { %this.updateLifeSize(); } } " Code Sample 6.3.2 " function PlayerFish::updateLifeSize(%this) { %lifeMultiplier = %this.life / 100; %newWidth = %this.maxWidth * %lifeMultiplier; %newHeight = %this.maxHeight * %lifeMultiplier; %this.setSize(%newWidth, %newHeight); } " Code Sample 6.3.3 " function PlayerFish::dead(%this) { %this.setFlipY(true); %this.setLinearVelocityY(-10); %this.dead = true; } " Code Sample 6.3.4 " function PlayerFish::lowerLife(%this) { %this.modifyLife(%this.lifeDrain); if(!%this.dead) { %this.schedule(500, "lowerLife"); } } " Code Sample 6.3.5 " function PlayerFish::onLevelLoaded(%this, %scenegraph) { $FishPlayer = %this; moveMap.bindCmd(keyboard, "w", "fishPlayerUp();", "fishPlayerUpStop();"); moveMap.bindCmd(keyboard, "s", "fishPlayerDown();", "fishPlayerDownStop();"); moveMap.bindCmd(keyboard, "a", "fishPlayerLeft();", "fishPlayerLeftStop();"); moveMap.bindCmd(keyboard, "d", "fishPlayerRight();", "fishPlayerRightStop();"); moveMap.bindCmd(keyboard, "space", "fishPlayerBoost();", "fishPlayerBoostStop();"); %this.lowerLife(); } " Code Sample 6.3.6 " function PlayerFish::updateMovement(%this) { if(%this.dead) return; " Code Sample 6.3.7 " function fishPlayerBoost() { if(%this.dead) return; " Code Sample 6.3.8 " function FishFood::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts) { if(%dstObj.class $= "PlayerFish" || %dstObj.class $= "Fish") { %srcObj.spawn(); } } " Code Sample 6.3.9 " function FishFood::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts) { if(%dstObj.class $= "PlayerFish") { %srcObj.spawn(); %dstObj.modifyLife(%srcObj.lifeValue); } else if (%dstObj.class $= "Fish") { %srcObj.spawn(); } } "