package { import flash.display.*; import flash.events.*; import flash.text.*; import flash.display.LoaderInfo; import flash.display.Loader; import flash.net.URLRequest; import flash.system.Security; /** * ... * @author tom fraser (curiousGaming) */ public class GITD20 extends MovieClip { private var playerInput : PlayerInput; private var mxv : Number; private var myv : Number; private var starArray : Array; private var miniStar : Array; private var score : int; private var timeLeft : int; private var introClip : MovieClip; private var day : int; private var dayScore : int; private var starCount : int; private var allowedTime : int; private var speedPower : int; private var silverCount : int; private var gravity : int; private var brake : int; private var victoryAchieved : Boolean; private var startButton : SimpleButton; private var continueButton : SimpleButton; private var dadMeeple : MovieClip; private var meeple : MovieClip; public var kongregate : *; public function GITD20() : void { stage.showDefaultContextMenu = false; stage.focus = stage; stage.stageFocusRect = false; loadKong(); } public function loadKong() : void { // Pull the API path from the FlashVars var paramObj:Object = LoaderInfo(root.loaderInfo).parameters; // The API path. The "shadow" API will load if testing locally. var apiPath:String = paramObj.kongregate_api_path || "http://www.kongregate.com/flash/API_AS3_Local.swf"; // Allow the API access to this SWF Security.allowDomain(apiPath); // Load the API var request:URLRequest = new URLRequest(apiPath); var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete); loader.load(request); this.addChild(loader); } function loadComplete(event:Event):void { // Save Kongregate API reference kongregate = event.target.content; // Connect to the back-end kongregate.services.connect(); } public function begin() : void { if (this.root.loaderInfo.url.indexOf("kongregate.com") != -1) //change == to != for site locking code { starArray = []; miniStar = []; intro(); playerInput = new PlayerInput(stage); meeple = new Meeple(); addChild(meeple); } else { var siteLockBox : MovieClip = new SiteLockBox(); addChild(siteLockBox); siteLockBox.x = (stage.stageWidth) / 2 - (siteLockBox.width / 2); siteLockBox.y = (stage.stageHeight) / 2 - (siteLockBox.height / 2); } } private function intro() : void { SoundManager.musicForbidden = true; introClip = new IntroClip(); addChild(introClip); introClip.gotoAndStop(1); introClip.addEventListener(MouseEvent.CLICK, nextPage); stage.addEventListener(KeyboardEvent.KEY_DOWN, nextPage); } private function nextPage(e : Event) : void { if (introClip.currentFrame == 3) { introClip.removeEventListener(MouseEvent.CLICK, nextPage); stage.removeEventListener(KeyboardEvent.KEY_DOWN, nextPage); introClip.parent.removeChild(introClip); stage.focus = stage; initialiseGame(); SoundManager.musicForbidden = false; SoundManager.bootMusic(); } else { introClip.gotoAndStop(introClip.currentFrame+1); } } private function initialiseGame() : void { score = 1; day = 0; scoreText.text = score.toString(); starCount = 20; allowedTime = 20; speedPower = 2; silverCount = 0; gravity = 0; brake = 0; victoryAchieved = false; nextLevel(); } private function clearStarsAway() : void { for each (var st : Star in starArray) { st.dispose(); } starArray = []; } private function nextLevel() : void { clearStarsAway(); timeLeft = allowedTime * 50; //50fps * seconds meeple.x = 250; meeple.y = 362; dayScore = 0; mxv = 0; myv = 0; meeple.gotoAndStop(1); meeple.scaleX = 1.0; var i : int; var s : Star; for (i = 0; i < starCount; i++) { s = new Star(stage, 1); s.clip.y -= (Math.random() * 525); starArray.push(s); } for (i = 0; i < silverCount; i++) { s = new Star(stage, 2); s.clip.y -= (Math.random() * 525); starArray.push(s); } addEventListener(Event.ENTER_FRAME, eachFrame); stage.focus = stage; } private function endOfDay() : void { kongregate.stats.submit ("Most stars in one day", dayScore); kongregate.stats.submit ("Total stars grabbed", dayScore); clearMiniStars(); removeEventListener(Event.ENTER_FRAME, eachFrame); if (score >= 1000 && !victoryAchieved) { victory(); } else { showUpgrader(); } } private function showUpgrader() : void { introClip = new UpgraderClip(); addChild(introClip); day++; introClip.dayText.text = "Day " + day.toString(); introClip.startButton.addEventListener(MouseEvent.CLICK, nextDay); resetUpgraderText(); introClip.box1.addEventListener(MouseEvent.CLICK, buyMoreStars); introClip.box2.addEventListener(MouseEvent.CLICK, buyMoreTime); introClip.box3.addEventListener(MouseEvent.CLICK, buyMoreSpeed); introClip.box4.addEventListener(MouseEvent.CLICK, buyMoreSilver); introClip.box5.addEventListener(MouseEvent.CLICK, buyMoreGravity); introClip.box6.addEventListener(MouseEvent.CLICK, buyBrake); } private function resetUpgraderText() : void { scoreText.text = score.toString(); introClip.scoreText.text = score.toString(); introClip.box1.titleText.text = "More Stars"; introClip.box1.boxPic.gotoAndStop(1); introClip.box1.currentText.text = "Current: " + starCount; if (starCost() == 0) { introClip.box1.nextText.text = ""; introClip.box1.costText.text = "Fully upgraded"; } else { introClip.box1.nextText.text = "Next: " + (starCount+5); introClip.box1.costText.text = "Cost: " + starCost() + " stars"; } introClip.box2.titleText.text = "Extra Time"; introClip.box2.boxPic.gotoAndStop(2); introClip.box2.currentText.text = "Current: " + allowedTime; if (timeCost() == 0) { introClip.box2.nextText.text = ""; introClip.box2.costText.text = "Fully upgraded"; } else { introClip.box2.nextText.text = "Next: " + (allowedTime+10); introClip.box2.costText.text = "Cost: " + timeCost() + " stars"; } introClip.box3.titleText.text = "Run Faster"; introClip.box3.boxPic.gotoAndStop(3); introClip.box3.currentText.text = "Current: " + speedPower; if (speedCost() == 0) { introClip.box3.nextText.text = ""; introClip.box3.costText.text = "Fully upgraded"; } else { introClip.box3.nextText.text = "Next: " + (speedPower+1); introClip.box3.costText.text = "Cost: " + speedCost() + " stars"; } introClip.box4.titleText.text = "Silver Stars"; introClip.box4.boxPic.gotoAndStop(4); introClip.box4.currentText.text = "Current: " + silverCount; if (silverCost() == 0) { introClip.box4.nextText.text = ""; introClip.box4.costText.text = "Fully upgraded"; } else { introClip.box4.nextText.text = "Next: " + (silverCount+1); introClip.box4.costText.text = "Cost: " + silverCost() + " stars"; } introClip.box5.titleText.text = "Star Attraction"; introClip.box5.boxPic.gotoAndStop(5); introClip.box5.currentText.text = "Current: " + gravity; if (gravityCost() == 0) { introClip.box5.nextText.text = ""; introClip.box5.costText.text = "Fully upgraded"; } else { introClip.box5.nextText.text = "Next: " + (gravity+1); introClip.box5.costText.text = "Cost: " + gravityCost() + " stars"; } introClip.box6.titleText.text = "Control"; introClip.box6.boxPic.gotoAndStop(6); if (brakeCost() == 0) { introClip.box6.currentText.text = "Current: Grippy"; introClip.box6.nextText.text = ""; introClip.box6.costText.text = "Fully upgraded"; } else { introClip.box6.currentText.text = "Current: Slippy"; introClip.box6.nextText.text = "Next: Grippy"; introClip.box6.costText.text = "Cost: " + brakeCost() + " stars"; } } private function brakeCost() : int { if (brake >= 1) { return 0; } else { return 50; } } private function gravityCost() : int { if (gravity >= 5) { return 0; } else { return 15 + (gravity + 1) * (gravity + 1) * 5; } } private function silverCost() : int { if (silverCount >= 5) { return 0; } else { return (silverCount + 1) * (silverCount + 1) * 5; } } private function speedCost() : int { if (speedPower >= 8) { return 0; } else { return 10 * speedPower; } } private function timeCost() : int { if (allowedTime >= 90) { return 0; } else { return allowedTime + 30; } } private function starCost() : int { if (starCount >= 50) { return 0; } else { return (2*(starCount - 10)); } } private function buyMoreGravity(e : Event) : void { if ((score >= gravityCost()) && (gravityCost() != 0)) { score -= gravityCost(); gravity += 1; resetUpgraderText(); } } private function buyMoreStars(e : Event) : void { if ((score >= starCost()) && (starCost() != 0)) { score -= starCost(); starCount += 5; resetUpgraderText(); } } private function buyMoreTime(e : Event) : void { if ((score >= timeCost()) && (timeCost() != 0)) { score -= timeCost(); allowedTime += 10; resetUpgraderText(); } } private function buyMoreSpeed(e : Event) : void { if ((score >= speedCost()) && (speedCost() != 0)) { score -= speedCost(); speedPower += 1; resetUpgraderText(); } } private function buyMoreSilver(e : Event) : void { if ((score >= silverCost()) && (silverCost() != 0)) { score -= silverCost(); silverCount += 1; resetUpgraderText(); } } private function buyBrake(e : Event) : void { if ((score >= brakeCost()) && (brakeCost() != 0)) { score -= brakeCost(); brake += 1; resetUpgraderText(); } } private function nextDay(e : Event) : void { introClip.startButton.removeEventListener(MouseEvent.CLICK, nextDay); introClip.box1.removeEventListener(MouseEvent.CLICK, buyMoreStars); introClip.box2.removeEventListener(MouseEvent.CLICK, buyMoreTime); introClip.box3.removeEventListener(MouseEvent.CLICK, buyMoreSpeed); introClip.box4.removeEventListener(MouseEvent.CLICK, buyMoreSilver); introClip.box5.removeEventListener(MouseEvent.CLICK, buyMoreGravity); introClip.box6.removeEventListener(MouseEvent.CLICK, buyBrake); removeChild(introClip); nextLevel(); } private function testCollision(s : Star) : void { if (s.clip.y > 250) { var dx : int = meeple.x - s.clip.x; var dy : int = meeple.y - s.clip.y; var dist : int = (dx * dx) + (dy * dy); if (dist < 400) { starBurst(s.clip); s.clip.y = 600; score++; dayScore++; if (s.clip.currentFrame == 2) { score += 4; dayScore += 4; SoundManager.addSound(gotStar); } else { SoundManager.addSound(gotGold); } scoreText.text = score.toString(); } else if (dist < (2500 * gravity)) { pullStar(s, dist/1000); } } } private function pullStar(s : Star, dist : Number) : void { if (meeple.x < s.clip.x) { s.clip.x -= gravity / dist; if (meeple.x > s.clip.x) { s.clip.x = meeple.x; } } else if (meeple.x > s.clip.x) { s.clip.x += gravity / dist; if (meeple.x < s.clip.x) { s.clip.x = meeple.x; } } } private function eachFrame(e : Event) : void { SoundManager.eachFrame(); moveMiniStars(); for each (var s : Star in starArray) { s.eachFrame(timeLeft); testCollision(s); } if (playerInput.leftKey()) { if ((brake > 0) && (mxv > 0)) { mxv = 0; } meeple.scaleX = -1.0; mxv -= speedPower / 10; } if (playerInput.rightKey()) { if ((brake > 0) && (mxv < 0)) { mxv = 0; } meeple.scaleX = 1.0; mxv += speedPower / 10; } if (playerInput.upKey() && (meeple.y >= 362)) { myv = -9; //SoundManager.addSound(jump); } if (!playerInput.rightKey() && !playerInput.leftKey() && brake > 0) { mxv *= 0.92; //extra friction } meeple.x += mxv; meeple.y += myv; if (meeple.y > 362) { meeple.y = 362; myv = 0; } mxv *= 0.95; //friction myv += 0.8; if (meeple.x > 495) { meeple.x = 495; mxv = 0; } if (meeple.x < 5) { meeple.x = 5; mxv = 0; } var desiredFrame : int = (meeple.currentFrame + Math.round(Math.abs(mxv))) % 32; meeple.gotoAndStop(desiredFrame); updateTime(); if (allGone()) { endOfDay(); } } private function allGone() : Boolean { var starsInPlay : int = 0; for each (var s : Star in starArray) { if (s.clip.y < 600) { starsInPlay++; } } return (starsInPlay == 0); } private function updateTime() : void { timeLeft--; if (timeLeft < 0) { timeLeft = 0; } var minutesLeft : int = Math.floor(timeLeft / 3000); var secondsLeft : int = Math.floor(timeLeft / 50) - (minutesLeft * 60); var secondsText : String = secondsLeft.toString(); if (secondsLeft < 10) { secondsText = "0" + secondsText; } timeText.text = minutesLeft.toString() + ":" + secondsText; } private function victory() : void { SoundManager.musicForbidden = true; SoundManager.stopMusic(); timeLeft = 50; kongregate.stats.submit ("Fastest Win", (day+1)); victoryAchieved = true; introClip = new ExitClip(); addChild(introClip); introClip.gotoAndStop(1); introClip.addEventListener(MouseEvent.CLICK, nextExitPage); stage.addEventListener(KeyboardEvent.KEY_DOWN, nextExitPage); startRunners(); } private function nextExitPage(e : Event) : void { if (timeLeft < 0) { introClip.gotoAndStop(introClip.currentFrame+1); timeLeft = 50; if (introClip.currentFrame == 3) { introClip.removeEventListener(MouseEvent.CLICK, nextExitPage); stage.removeEventListener(KeyboardEvent.KEY_DOWN, nextExitPage); startButton = new RestartButton(); introClip.addChild(startButton); startButton.x = 90; startButton.y = 360; startButton.addEventListener(MouseEvent.CLICK, reboot); continueButton = new ContinueButton(); introClip.addChild(continueButton); continueButton.x = 410; continueButton.y = 360; continueButton.addEventListener(MouseEvent.CLICK, continueGame); } } } private function continueGame(e : Event) : void { clearUp(); showUpgrader(); } private function reboot(e : Event) : void { clearUp(); initialiseGame(); } private function clearUp() : void { SoundManager.musicForbidden = false; SoundManager.bootMusic(); startButton.removeEventListener(MouseEvent.CLICK, reboot); continueButton.removeEventListener(MouseEvent.CLICK, continueGame); introClip.removeChild(continueButton); introClip.removeChild(startButton); removeChild(dadMeeple); removeChild(introClip); removeEventListener(Event.ENTER_FRAME, zombieRun); } private function startRunners() : void { SoundManager.addSound(zombie); dadMeeple = new MeepleDad(); dadMeeple.scaleX = 1.4; dadMeeple.scaleY = 1.4; dadMeeple.y = 240; dadMeeple.gotoAndStop(1); meeple.y = 240; meeple.x = -250; meeple.scaleX = 1.0; mxv = 5.0; addChild(dadMeeple); addChild(meeple); addEventListener(Event.ENTER_FRAME, zombieRun); } private function zombieRun(e : Event) : void { SoundManager.eachFrame(); moveMiniStars(); timeLeft--; meeple.x += mxv; dadMeeple.x = meeple.x - (mxv * 15); meeple.gotoAndStop((meeple.currentFrame + 7)%32); dadMeeple.gotoAndStop((dadMeeple.currentFrame + 5)%32); if (meeple.x > 600) { mxv = -5.0; dadMeeple.scaleX = -1.4; meeple.scaleX = -1; } if (meeple.x < -100) { mxv = 5.0; dadMeeple.scaleX = 1.4; meeple.scaleX = 1; } } private function starBurst(dadClip : MovieClip) : void { for (var i : int = 0; i < 20; i++) { var m : MiniStar = new MiniStar(stage, dadClip); miniStar.push(m); } } private function moveMiniStars() : void { for each (var m : MiniStar in miniStar) { m.eachFrame(); } for (var i : int = miniStar.length-1; i >= 0; i--) { if (miniStar[i].fadeCount >= 40) { miniStar[i].dispose(); miniStar.splice(i, 1); } } } private function clearMiniStars() : void { for (var i : int = miniStar.length-1; i >= 0; i--) { miniStar[i].dispose(); miniStar.splice(i, 1); } miniStar = []; } } //end class }