Path: blob/main/public/games/files/gopher-kart/js/stateMain.js
1036 views
var StateMain = {12preload: function () {3//Music4game.load.audio("title", "assets/music/racingMain-compressed.m4a");5//Sound FX6game.load.audio("coinBeep", "assets/music/sfx/coin.wav");7game.load.audio("npc_explosion", "assets/music/sfx/npc_explosion.wav");8game.load.audio("drive", "assets/music/sfx/drive.wav");9game.load.audio("accelerate", "assets/music/sfx/accelerate.wav");10game.load.audio("countdownBeep", "assets/music/sfx/countdown.wav");1112game.stage.backgroundColor = 0xe9fffe;1314//Gopher sprites15game.load.spritesheet("blue", "assets/gopher-blue-updated.png", 64, 60, 15);16game.load.spritesheet("pink", "assets/gopher-pink.png", 64, 60, 15);17game.load.spritesheet("purple", "assets/gopher-purple.png", 64, 60, 15);18//Road19game.load.image("road", "assets/road-tile.png");20//Top rail21game.load.image("topRail", "assets/top-rail-long.png");22//Scene Extras23game.load.image("extras", "assets/signs.png");24//Light Posts25game.load.image("posts", "assets/light-posts.png");26//Truck27game.load.image("truck", "assets/truck.png");28//Bottom rail29game.load.image("bottomRail", "assets/bottom-rail-long.png");30//Add background31game.load.image("sky", "assets/clouds-re-colored.png");32game.load.image("city", "assets/city-re-colored.png");33game.load.image("mtn", "assets/mountains-recolored.png");34//Add coins35game.load.spritesheet("coin", "assets/coin-shadow.png", 16, 19, 6);36//NPCs37game.load.spritesheet("npc", "assets/other-gophers.png", 64, 60, 20);38//Explosion39game.load.spritesheet("explosion", "assets/explosion.png", 64, 60, 4);40//Add hearts41game.load.spritesheet("heart", "assets/heart-17x16.png", 17, 16, 6);42//Countdown Sprites43game.load.image("countDown3", "assets/three.png");44game.load.image("countDown2", "assets/two.png");45game.load.image("countDown1", "assets/one.png");46game.load.image("countDownGo", "assets/go.png");47game.load.image("background", "assets/bg-color.png");4849//Font50game.load.bitmapFont('pixelFont', 'assets/fonts/bitmapFonts/pixelFont.png', 'assets/fonts/bitmapFonts/pixelFont.xml');51var timeText;52},5354create: function () {5556game.world.setBounds(0, 0, 35000, this.game.height);5758//MUSIC59this.titleSong = game.add.audio("title");60this.titleSong.volume = 0.4;61this.titleSong.play('', 0, 1, true);6263//SFX64this.drivingSound = game.add.audio("drive");65this.accelerateSound = game.add.audio("accelerate");66this.countdownSound = game.add.audio("countdownBeep");67this.countdownSound.play('', 0, 1, false);68this.drivingSound.loopFull(1.3);6970var background = game.add.tileSprite(0, 0, this.world.width, 432, "background");7172//Prevents pausing of game when use clicks out of the game73game.stage.disableVisibilityChange = true;7475//Start Physics Engine76game.physics.startSystem(Phaser.Physics.ARCADE);7778//TIMER79setInterval(function(){80count += 1;81count.toString();82if(count < 10){83timeElapsed = "0" + count;84} else {85timeElapsed = count;86}87}, 1000);8889//Increase score every second90setInterval(function(){91score += 3;92},1000);939495this.timer = this.game.time.create(this.game);96this.timer.add(this.delay, this.readyForAction, this);97this.timer.start();9899//Set top and bottom boundaries for Gopher100//To prevent going past rails101this.top = game.height - 200;102this.bottom = game.height - 80;103104105//Random lane logic for NPC spawn106this.lane = function () {107return availLanes[Math.floor(Math.random()*availLanes.length)];108};109//Random NPC logic110this.pickNPC = function(){111return availNpcGophers[Math.floor(Math.random()*availNpcGophers.length)];112};113// this.npc = game.add.sprite(game.width, this.lane, this.pickNPC);114// console.log("NEW NPC ADDED: " + this.lane() + " , " + this.pickNPC());115116// BACKGROUND IMAGE TILES117sky = game.add.tileSprite(0, 6, this.world.width, 78, "sky");118mtn = game.add.tileSprite(0, 62, this.world.width, 133, "mtn");119city = game.add.tileSprite(0, 107, this.world.width, 90, "city");120truck = game.add.tileSprite(0, 84, this.world.width, 142, "truck");121road = game.add.tileSprite(0, 226, this.world.width, 159, "road");122posts = game.add.tileSprite(0, 15, this.world.width, 182, "posts");123extras = game.add.tileSprite(0, 120, this.world.width, 84, "extras");124topRail = game.add.tileSprite(0, 197, this.world.width, 29, "topRail");125bottomRail = game.add.tileSprite(0, 385, this.world.width, 47, "bottomRail");126127sky.fixedToCamera = true;128mtn.fixedToCamera = true;129city.fixedToCamera = true;130truck.fixedToCamera = true;131road.fixedToCamera = true;132posts.fixedToCamera = true;133extras.fixedToCamera = true;134topRail.fixedToCamera = true;135bottomRail.fixedToCamera = true;136137138//EMPTY LIVES139this.emptyHeart1 = game.add.sprite(10, game.world.centerY-205, "heart");140this.emptyHeart1.fixedToCamera = true;141this.emptyHeart1.frame = 5;142this.emptyHeart2 = game.add.sprite(30, game.world.centerY-205, "heart");143this.emptyHeart2.fixedToCamera = true;144this.emptyHeart2.frame = 5;145this.emptyHeart3 = game.add.sprite(50, game.world.centerY-205, "heart");146this.emptyHeart3.fixedToCamera = true;147this.emptyHeart3.frame = 5;148149150//LIVES151this.heart1 = game.add.sprite(10, game.world.centerY-205, "heart");152this.heart1.fixedToCamera = true;153this.heart2 = game.add.sprite(30, game.world.centerY-205, "heart");154this.heart2.fixedToCamera = true;155this.heart3 = game.add.sprite(50, game.world.centerY-205, "heart");156this.heart3.fixedToCamera = true;157158this.heartGroup = game.add.group();159this.heartGroup.add(this.heart1);160this.heartGroup.add(this.heart2);161this.heartGroup.add(this.heart3);162163//COINS164this.coins=game.add.group();165this.coins.createMultiple(40, 'coin');166this.coins.setAll('checkWorldBounds', true);167this.coins.setAll('outOfBoundsKill', true);168169// OTHER RACERS170this.npcRacers = game.add.group();171this.npcRacers.createMultiple(40, 'npc');172this.npcRacers.setAll('checkWorldBounds', true);173this.npcRacers.setAll('outOfBoundsKill', true);174175//Main racer176this.sprite = game.add.sprite(50, 289, character);177this.sprite.anchor.set(0.5, 0.5);178this.sprite.animations.add("crash", [2,3,4,5,6], 9, false);179this.sprite.animations.add("idle", [0, 1], 9, true);180this.sprite.animations.play("idle");181game.physics.arcade.enable([this.sprite, this.coins, this.npcRacers]);182game.camera.follow(this.sprite);183this.sprite.body.collideWorldBounds = true;184this.sprite.body.immovable = true;185this.sprite.body.width = 60;186this.sprite.body.height = 30;187this.sprite.body.offset.setTo(3, 30);188189//Add chosen racer to npcRacer group190//to allow for sorting and proper "z-index" effect191this.npcRacers.add(this.sprite);192193// console.log("You chose the " + character + " racer!");194195//Background image scroll speed196road.autoScroll(-390, 0);197topRail.autoScroll(-370, 0);198bottomRail.autoScroll(-380, 0);199sky.autoScroll(-5,0);200city.autoScroll(-30,0);201mtn.autoScroll(-15,0);202truck.autoScroll(-550, 0);203posts.autoScroll(-330, 0);204extras.autoScroll(-330, 0);205206//COUNTDOWN SPRITES207this.countDown1 = game.add.sprite(game.world.bounds.height - 135, game.world.centerY, 'countDown1');208this.countDown1.anchor.setTo(0.5, 0.5);209this.countDown1.alpha = 0;210211this.countDown2 = game.add.sprite(game.world.bounds.height - 135, game.world.centerY, 'countDown2');212this.countDown2.anchor.setTo(0.5, 0.5);213this.countDown2.alpha = 0;214215216this.countDown3 = game.add.sprite(game.world.bounds.height - 135, game.world.centerY, 'countDown3');217this.countDown3.anchor.setTo(0.5, 0.5);218this.countDown3.alpha = 0;219220221this.countDownGo = game.add.sprite(game.world.bounds.height - 80, game.world.centerY, 'countDownGo');222this.countDownGo.anchor.setTo(0.5, 0.5);223this.countDownGo.alpha = 0;224this.countDownGo.fixedToCamera = true;225226//COUNTDOWN GROUP227this.countGroup = game.add.group();228this.countGroup.add(this.countDown1);229this.countGroup.add(this.countDown2);230this.countGroup.add(this.countDown3);231this.countGroup.add(this.countDownGo);232233//TWEENS for 3..2..1..GO!234var tween1 = game.add.tween(this.countDown1).to({alpha: 1}, 500, Phaser.Easing.Linear.None, false,2350).to({alpha: 0}, 500, Phaser.Easing.Linear.None, false, 0);236var tween2 = game.add.tween(this.countDown2).to({alpha: 1}, 500, Phaser.Easing.Linear.None, false,2370).to({alpha: 0}, 500, Phaser.Easing.Linear.None, false, 0);238var tween3 = game.add.tween(this.countDown3).to({alpha: 1}, 500, Phaser.Easing.Linear.None, false,2390).to({alpha: 0}, 500, Phaser.Easing.Linear.None, false, 0);240var tweenGo = game.add.tween(this.countDownGo).to({alpha: 1}, 500, Phaser.Easing.Linear.None, false,2410).to({alpha: 0}, 500, Phaser.Easing.Linear.None, false, 0);242tween3.chain(tween2);243tween2.chain(tween1);244tween1.chain(tweenGo);245tween3.start();246247//Set cursors to accept input from the keyboard248cursors = game.input.keyboard.createCursorKeys();249250//Prevent user from moving until after "GO!"251game.input.enabled = false;252253setTimeout(function(){254//Turn input back on255game.input.enabled = true;256},3000);257258//TEXT259var scoreLabel = game.add.bitmapText(585, 7, 'pixelFont', 'SCORE', 21);260scoreLabel.anchor.set(1.0 , 0);261scoreText = game.add.bitmapText(570, 27, 'pixelFont', '0', 21);262scoreText.anchor.set(1.0 , 0);263var timeLabel;264timeLabel = game.add.bitmapText(275, 7, 'pixelFont', 'TIME', 21);265timeText = game.add.bitmapText(290, 27, 'pixelFont', timeElapsed, 21);266267//Fix game status elements (w/e you want to call these :D) to the camera268this.timer.fixedToCamera = scoreText.fixedToCamera = timeLabel.fixedToCamera =269timeText.fixedToCamera = scoreLabel.fixedToCamera = true;270271// game.debug.bodyInfo(this.npcRacers);272273this.setListeners();274},275276setListeners: function(){277//Spawn coins and NPCs278game.time.events.loop(Phaser.Timer.SECOND * coinSpawnRate, this.loadCoin, this);279game.time.events.loop(Phaser.Timer.SECOND * npcSpawnRate, this.loadNPC, this);280281},282283284//NPC SPAWN285loadNPC: function (){286var newNpc = this.npcRacers.getFirstDead();287//maintains that npc spawns just to right of screen288var xx = this.camera.view.right + 20;289var yy = this.lane();290// newNpc.key = this.pickNPC();291newNpc.anchor.set(0.5 , 0.5);292newNpc.reset(xx, yy);293newNpc.enabled = true;294newNpc.body.velocity.x = -200;295newNpc.animations.add("idle", this.pickNPC(), 12, true);296newNpc.animations.play("idle");297newNpc.body.immovable = false;298newNpc.body.checkCollision.up = false;299newNpc.body.checkCollision.down = false;300newNpc.body.width = 60;301newNpc.body.height = 30;302newNpc.body.offset.setTo(3, 30);303},304305//COIN SPAWN306loadCoin: function (){307var coin = this.coins.getFirstDead();308//y position309var yy = game.rnd.integerInRange(240, game.height-70);310//x position - maintains that coin spawns just to right of screen311var xx = this.camera.view.right + 20;312313314coin.reset(xx, yy);315coin.enabled = true;316coin.body.velocity.x = -200;317coin.animations.add("spin", [0, 1, 2, 3, 4, 5], 12, true);318coin.animations.play("spin");319},320321//COIN PICK UP322onPickUp: function (sprite, coin){323coin.kill();324score += 10;325// console.log("Your score is --> " + score);326this.coinBeep = game.add.audio("coinBeep");327this.coinBeep.play('', 0, 1, false);328this.coinBeep.volume = 0.3;329},330331332//COLLISION HANDLER333onCrash: function (sprite, npc){334sprite.animations.play("crash");335this.npc_explosion = game.add.audio("npc_explosion");336this.npc_explosion.play('', 0, 1, false);337this.npc_explosion.volume = 0.3;338sprite.events.onAnimationComplete.add(function(){339// console.log("Crash animation complete");340sprite.animations.play("idle");341}, this);342lives -= 1;343var heart = this.heartGroup.getFirstAlive();344345heart.animations.add("drain", [0,1,2,3,4,5],12, false);346heart.animations.play("drain");347348349//DECREASE SPEED OF GOPHER UPON COLLISON ... not working ??350sprite.body.velocity.x = -500;351setTimeout(function(){352heart.kill();353}, 1000);354355//EXPLOSION356explosion = this.game.add.sprite(npc.body.x,npc.body.y,"explosion");357explosion.anchor.setTo(0.1,0.5);358explosion.animations.add("explosion", [0, 1, 2, 3], 12, false);359explosion.animations.play("explosion", 12, false, true);360npc.kill();361console.log("You have " + lives + "lives left!");362363if(lives === 0){364this.titleSong.stop();365this.drivingSound.stop();366game.state.start("StateOver");367}368},369370update: function (){371this.sprite.body.maxVelocity.x= 500;372this.sprite.body.maxVelocity.y= 500;373//Allows for correct "z-index" of gopher374this.npcRacers.sort('y', Phaser.Group.SORT_ASCENDING);375376377//Collision checks378game.physics.arcade.collide(this.sprite, this.coins, null, this.onPickUp, this);379game.physics.arcade.collide(this.npcRacers, this.coins, null, this.NpcPickUp, this);380game.physics.arcade.collide(this.sprite, this.npcRacers, null, this.onCrash, this);381// timeText.text = '' + Math.round(game.time.now);382timeText.text = timeElapsed;383scoreText.text = score;384385//Cursors - Keyboard key check ⌨️386if (game.input.keyboard.downDuration(Phaser.Keyboard.RIGHT)) {387this.accelerateSound.play('', 0, 1.4, false, true);388this.drivingSound.volume = 1.6;389390if(this.drivingSound._sound) {391this.drivingSound._sound.playbackRate.value = 1.4;392}393}394395if(cursors.right.isDown) {396this.sprite.body.velocity.x = 80;397398sky.tilePosition.x -=0.5;399mtn.tilePosition.x -=0.7;400city.tilePosition.x -=0.9;401truck.tilePosition.x -=2;402road.tilePosition.x -=2;403bottomRail.tilePosition.x -= 2;404posts.tilePosition.x -=5;405extras.tilePosition.x -=5;406topRail.tilePosition.x -=2;407408}409410if(cursors.right.isUp) {411this.sprite.body.velocity.x = -150;412this.drivingSound.volume = 1.3;413414if(this.drivingSound._sound) {415this.drivingSound._sound.playbackRate.value = 1.0;416}417}418419if(cursors.up.isDown) {420this.sprite.body.velocity.y = -90;421}422423if(cursors.up.isUp) {424this.sprite.body.velocity.y = 0;425}426427if(cursors.down.isDown) {428this.sprite.body.velocity.y = 90;429}430431if(cursors.left.isDown) {432this.sprite.body.velocity.x = -250;433}434435if(this.sprite.y<this.top) {436this.sprite.y=this.top;437}438439if(this.sprite.y>this.bottom) {440this.sprite.y = this.bottom;441}442443},444445render: function () {446// game.debug.body(this.sprite);447// game.debug.text('Sprite z-depth: ' + this.sprite.z, 10, 20);448// console.log('NPCs z-depth: ' + this.npcRacers.z);449},450451};//END StateMain452453454