Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // game objects
  2. var player,
  3. baddie,
  4. platforms,
  5. ladders,
  6. cursors,
  7. coins,
  8. //new variable to store whether or not hero is currently on a ladder
  9. onLadder = false;
  10.  
  11. var score = 0,
  12. scoreText;
  13.  
  14. var playerSpeed = 250,
  15. gravity = 300,
  16. assetRoot = "https://tomoliverharrison.co.uk/phaser/";
  17.  
  18. //create our game object... remember it's
  19. //width, height, rendering context (canvas, webgl or auto), DOM ID, object of essential phaser functions
  20. var game = new Phaser.Game(600, 400, Phaser.AUTO, 'game', { preload: preload, create: create, update: update });
  21.  
  22. //function to preload all our game sprites
  23. function preload() {
  24.   //this is for codepen to allow images hosted on my website
  25.   game.load.crossOrigin = "anonymous";
  26.  
  27.   game.load.image('ground', assetRoot + 'assets/platform.png');
  28.   game.load.image('coin', assetRoot + 'assets/coin.png');
  29.   game.load.image('hero', assetRoot + 'assets/hero.png');
  30.   game.load.image('baddie', assetRoot + 'assets/baddie.png');
  31.   game.load.image('ladder', assetRoot + 'assets/ladder.png');
  32. }
  33.  
  34. //setup all the game objects, physics etc
  35. function create() {
  36.     //set background colour
  37.     game.stage.backgroundColor = "#243166";
  38.     //reset the score
  39.     score = 0;
  40.     //we're going to be using physics, so enable the Arcade Physics system
  41.     game.physics.startSystem(Phaser.Physics.ARCADE);
  42.  
  43.     //the platforms group will contain the ground and the 2 ledges we can jump on
  44.     platforms = game.add.group();
  45.  
  46.     //we will enable physics for any object that is created in this group
  47.     platforms.enableBody = true;
  48.  
  49.     //here we create the ground and pass it the ground sprite
  50.     var ground = platforms.create(0, game.world.height - 64, 'ground');
  51.  
  52.     //scale it to fit the width of the game (the original sprite is 400x32 in size)
  53.     ground.scale.setTo(2, 2);
  54.  
  55.     //this stops it from falling away when you jump on it
  56.     ground.body.immovable = true;
  57.  
  58.     //Now let's create a ledge
  59.     var ledge = platforms.create(300, 164, 'ground');
  60.     ledge.body.immovable = true;
  61.  
  62.     //and a ladder
  63.     //first we create a group, just in case we have move ladders later
  64.     ladders = game.add.group();
  65.     //enable all bodies in this group for physics
  66.     ladders.enableBody = true;
  67.     //then add out sprite to the group
  68.     var ladder = ladders.create(266, 164, 'ladder');
  69.     //make it sure this object doesn't move
  70.     ladder.body.immovable = true;
  71.  
  72.     //the player and its settings
  73.     player = game.add.sprite(32, game.world.height - 150, 'hero');
  74.  
  75.     //we need to enable physics on the player
  76.     game.physics.arcade.enable(player);
  77.  
  78.     //player physics properties. Give the little guy a slight bounce.
  79.     player.body.bounce.y = 0.2;
  80.     player.body.gravity.y = gravity;
  81.  
  82.     //confine our player to the world bounds so he can't fall out of the frame
  83.     player.body.collideWorldBounds = true;
  84.    
  85.     //set a flag for when player gets hit by baddie
  86.     player.dead = false;
  87.     //change rotation point so we can make player spin when hit.
  88.     player.anchor.setTo(.5, .5);
  89.     //add our baddy
  90.     baddie = game.add.sprite(320, game.world.height - 100, 'baddie');
  91.  
  92.     //enable physics on the baddie
  93.     game.physics.arcade.enable(baddie);
  94.  
  95.     //set physics properties. Give the little guy a slight bounce.
  96.     baddie.body.bounce.y = 0.2;
  97.     baddie.body.gravity.y = gravity;
  98.  
  99.     //confine our baddie to the world bounds so he can't fall out of the frame
  100.     baddie.body.collideWorldBounds = true;
  101.     baddie.body.velocity.x = -100;
  102.     baddie.maxDistance = 200;
  103.     baddie.previousX = baddie.x;
  104.     baddie.anchor.setTo(.5, .5);
  105.     //finally some coins to collect. We start by adding a group...
  106.     coins = game.add.group();
  107.  
  108.     //we will enable physics for any coin that is created in this group
  109.     coins.enableBody = true;
  110.  
  111.     //then we create 9 coins within the coins group
  112.     for (var i = 0; i < 9; i++)
  113.     {
  114.         //create a coin inside of the 'coins' group
  115.         //we are evenly spacing the coins every 70px
  116.         //the  + 16 is half the width of a 32px coin, it means the first coin is not half way off the screen
  117.         var coin = coins.create(i * 70 + 16, 16, 'coin');
  118.  
  119.         //add gravity to the coins group
  120.         coin.body.gravity.y = gravity;
  121.  
  122.         //this just gives each coin a slightly random bounce value
  123.         coin.body.bounce.y = 0.7 + Math.random() * 0.2;
  124.  
  125.         //set coins axis to the be the center, so it rotates nicely
  126.         coin.anchor.setTo(.5, .5);
  127.     }
  128.  
  129.     //Add score text to the screen
  130.     scoreText = game.add.text(16, 16, 'score: 0', { fontSize: '18px', fill: '#eee' });
  131.  
  132.     //Create controls object
  133.     cursors = game.input.keyboard.createCursorKeys();
  134. }
  135.  
  136. //the game loop. Game logic lives in here.
  137. //this is called every frame
  138. function update() {
  139.     //reset ladder status
  140.     onLadder = false;
  141.  
  142.     //reset player gravity
  143.     player.body.gravity.y = gravity;
  144.  
  145.     //Collide the player and the coins with the platforms
  146.     game.physics.arcade.collide(player, platforms);
  147.     game.physics.arcade.collide(coins, platforms);
  148.  
  149.     //collide baddie with platforms and player
  150.     game.physics.arcade.collide(baddie, platforms);
  151.    
  152.     //check how far baddie has travelled. If past maximum amount then switch direction
  153.     if (Math.abs(baddie.x - baddie.previousX) >= baddie.maxDistance) {
  154.         switchDirection(baddie);
  155.     }
  156.  
  157.     //we user overlap here instead of collide so baddie doesn't move
  158.     game.physics.arcade.overlap(player, baddie, hitBaddie);
  159.  
  160.     //check if player is on ladder
  161.     game.physics.arcade.overlap(player, ladders, isOnLadder);
  162.  
  163.     //Checks to see if the player overlaps with any of the coins, if he does call the collectcoin function
  164.     game.physics.arcade.overlap(player, coins, collectcoin, null, this);
  165.    
  166.     //Reset the players velocity (movement)
  167.     player.body.velocity.x = 0;
  168.  
  169.     //only take input from controls if we are still alive. No controlling a corpse.
  170.     if(!player.dead)
  171.     {
  172.         //if left key is down
  173.         if (cursors.left.isDown)
  174.         {
  175.             //Move to the left
  176.             player.body.velocity.x = -playerSpeed;
  177.         }
  178.         //if right key is down
  179.         else if (cursors.right.isDown)
  180.         {
  181.             //Move to the right
  182.             player.body.velocity.x = playerSpeed;
  183.         }
  184.  
  185.         //Allow the player to jump if they are touching the ground.
  186.         if (cursors.up.isDown && player.body.touching.down)
  187.         {
  188.             player.body.velocity.y = -playerSpeed;
  189.         }
  190.  
  191.         if(onLadder)
  192.         {
  193.             if(cursors.up.isDown)
  194.             {
  195.                 player.body.velocity.y = -playerSpeed/2;
  196.             }
  197.             if(cursors.down.isDown)
  198.             {
  199.                 player.body.velocity.y = playerSpeed/2;
  200.             }
  201.             if((!cursors.up.isDown && !cursors.down.isDown) && !game.input.pointer1.isDown)
  202.             {
  203.                 player.body.gravity.y = 0;
  204.                 player.body.velocity.y = 0;
  205.             }
  206.         }
  207.      
  208.         //touch controls
  209.         //check for two points of touch, pointer1 or pointer 2. This lets people use their thumbs in landscape mode and easily switch between the two
  210.         if (game.input.pointer1.isDown || game.input.pointer2.isDown)
  211.         {  
  212.             //If touch is to the left of the player move them left
  213.             if (game.input.x < player.body.x - player.body.width)
  214.             {      
  215.                 //Move to the left      
  216.                 player.body.velocity.x = -playerSpeed;        
  217.             }    
  218.             //If touch is to the right of the player move them right
  219.             if (game.input.x > player.body.x + player.body.width)
  220.             {      
  221.                 //Move to the right
  222.                 player.body.velocity.x = playerSpeed;
  223.             }
  224.             // jump
  225.             if(game.input.y < player.body.y - player.body.height && player.body.touching.down)
  226.             {
  227.                 player.body.velocity.y = -playerSpeed;
  228.             }
  229.  
  230.             if(onLadder)
  231.             {
  232.                 if(game.input.y > player.body.y + player.body.height)
  233.                 {
  234.                     player.body.velocity.y = playerSpeed/2;
  235.                 }
  236.                 if(game.input.y < player.body.y - player.body.height)
  237.                 {
  238.                     player.body.velocity.y = -playerSpeed/2;
  239.                 }
  240.             }
  241.         }
  242.     }
  243. }
  244.  
  245. //called from the main game loop whenever a hero collects a coin
  246. function collectcoin (player, coin) {
  247.    //check if we have already hit coin, and player is not dead
  248.    if(!coin.hit && !player.dead)
  249.    {
  250.         //if not then change hit flag to true
  251.         coin.hit = true;
  252.  
  253.         //make coin jump up slightly
  254.         coin.body.velocity.y = -100;
  255.  
  256.         //animate coin so it becomes invisible and spins. Store this tween in a variable
  257.         var tween = game.add.tween(coin).to( { alpha: 0, angle: 360 }, 500, "Linear", true);
  258.         //animate the scale property of our coin to make it halve in size
  259.         game.add.tween(coin.scale).to( { x: .5, y: .5 }, 500, "Linear", true);
  260.        
  261.         //when our fade tween is complete call the function killcoin
  262.         tween.onComplete.add(removeSprite);
  263.  
  264.         //update the score and write to screen
  265.         score += 10;
  266.         scoreText.text = 'Score: ' + score;
  267.    }
  268. }
  269.  
  270. function hitBaddie(player, baddie) {
  271.     //if baddie is hit on head and hasn't already been hit
  272.     if (baddie.body.touching.up && !baddie.hit)
  273.     {
  274.         // set baddie as being hit and remove physics
  275.         baddie.hit = true;
  276.         baddie.body.velocity.y = -100;
  277.         baddie.body.velocity.x = 0;
  278.  
  279.         //make our player bounce
  280.         player.body.velocity.y = -gravity;
  281.      
  282.         //create empty tweens
  283.         var baddieTween = game.add.tween(baddie),
  284.         baddieScaleTween = game.add.tween(baddie.scale);
  285.  
  286.         //assign tween values
  287.         baddieTween.to({ alpha: 0, angle: 360}, 1000, Phaser.Easing.Linear.None);
  288.         baddieScaleTween.to({ x: .5, y:.5 }, 1000, Phaser.Easing.Linear.None);
  289.  
  290.         //when tween is finished, remove sprite
  291.         baddieTween.onComplete.add(removeSprite);
  292.      
  293.         //start the tweens
  294.         baddieScaleTween.start();
  295.         baddieTween.start();
  296.     }
  297.     //otherwise you've hit baddie, but not on the head. This makes you die
  298.     else
  299.     {
  300.         //set player to dead
  301.         player.dead = true;
  302.         player.body.velocity.y =-playerSpeed;
  303.         player.body.velocity.x = 0;
  304.      
  305.         //create empty tweens
  306.         var playerTween = game.add.tween(player),
  307.         playerScaleTween = game.add.tween(player.scale);
  308.  
  309.         //assign tween values
  310.         playerTween.to({ alpha: 0,  angle: 360}, 500, Phaser.Easing.Linear.None);
  311.         playerScaleTween.to({ x: .5, y:.5 }, 500, Phaser.Easing.Linear.None);
  312.  
  313.         //when scale tween is finished, restart game
  314.         playerScaleTween.onComplete.add(restartGame);
  315.  
  316.         //start the tweens
  317.         playerScaleTween.start();
  318.         playerTween.start();
  319.     }
  320. }
  321.  
  322. //this replaced killcoin and is more generic
  323. function removeSprite(sprite) {
  324.     // Removes the sprite from the screen
  325.     sprite.kill();
  326. }
  327.  
  328. function switchDirection(baddie)
  329. {
  330.    //reverse velocity so baddie moves are same speed but in opposite direction
  331.    baddie.body.velocity.x *= -1;
  332.    //reset count
  333.    baddie.previousX = baddie.x;
  334. }
  335.  
  336. //when hero and ladder are touching, this is called
  337. function isOnLadder()
  338. {
  339.   onLadder = true;
  340. }
  341.  
  342. function restartGame()
  343. {
  344.   game.state.start(game.state.current);
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement