Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //this game will have only 1 state
  2. var GameState = {
  3.  
  4.   //initiate game settings
  5.   init: function() {
  6.     //adapt to screen size, fit all the game
  7.     this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
  8.     this.scale.pageAlignHorizontally = true;
  9.     this.scale.pageAlignVertically = true;
  10.  
  11.     this.game.physics.startSystem(Phaser.Physics.ARCADE);
  12.     this.game.physics.arcade.gravity.y = 1000
  13.  
  14.     this.cursors = this.game.input.keyboard.createCursorKeys();
  15.  
  16.     this.game.world.setBounds(0,0,360,700);
  17.  
  18.     this.RUNNING_SPEED = 400;
  19.     this.JUMPING_SPEED = 550;
  20.  
  21.   },
  22.  
  23.   //load the game assets before the game starts
  24.   preload: function() {
  25.     this.load.image('ground', 'assets/images/ground.png');    
  26.     this.load.image('platform', 'assets/images/platform.png');    
  27.     this.load.image('goal', 'assets/images/gorilla3.png');    
  28.     this.load.image('arrowButton', 'assets/images/arrowButton.png');    
  29.     this.load.image('actionButton', 'assets/images/actionButton.png');    
  30.     this.load.image('barrel', 'assets/images/barrel.png');    
  31.  
  32.     this.load.spritesheet('player', 'assets/images/player_spritesheet.png', 28, 30, 5, 1, 1);    
  33.     this.load.spritesheet('fire', 'assets/images/fire_spritesheet.png', 20, 21, 2, 1, 1);      
  34.     this.load.image('coin', 'assets/images/coin.png', 32, 32);
  35.     this.load.text('level', 'assets/data/level.json');
  36.   },
  37.    
  38.    
  39.   //executed after everything is loaded
  40.   create: function() {    
  41.  
  42.     this.ground = this.add.sprite(0, 638, 'ground');
  43.     this.game.physics.arcade.enable(this.ground);
  44.     this.ground.body.allowGravity = false;
  45.     this.ground.body.immovable = true;
  46.  
  47.     //parse the file
  48.     this.levelData = JSON.parse(this.game.cache.getText('level'));
  49.  
  50.     this.platforms = this.add.group();
  51.     this.platforms.enableBody = true;
  52.  
  53.     this.levelData.platformData.forEach(function(element){
  54.       this.platforms.create(element.x, element.y, 'platform');
  55.     }, this);
  56.  
  57.     this.platforms.setAll('body.immovable', true);
  58.     this.platforms.setAll('body.allowGravity', false);
  59.  
  60.     //coins=====================
  61.     //  Here we create our coins group
  62.       var coin
  63.     coins = this.add.group();
  64.    
  65.     //  Now let's add 50 coins into it
  66.     for (var i = 0; i < 20; i++)
  67.     {
  68.         coin = this.add.sprite(this.world.randomX, this.world.randomY, 'coin', 0);
  69.         coins.enableBody = true;
  70.        
  71.         coins.add(coin);
  72.        
  73.     }
  74. //=================================
  75.     //  Now using the power of callAll we can add the same animation to all coins in the group:
  76.     //coins.callAll('animations.add', 'animations', 'spin', [0, 1, 2, 3, 4, 5], 10, true);
  77.  
  78.     //  And play them
  79.     //coins.callAll('animations.play', 'animations', 'spin');
  80.  
  81.  
  82.    
  83.     //fires
  84.     this.fires = this.add.group();
  85.     this.fires.enableBody = true;
  86.  
  87.     var fire;
  88.     this.levelData.fireData.forEach(function(element){
  89.       fire = this.fires.create(element.x, element.y, 'fire');
  90.       fire.animations.add('fire', [0, 1], 4, true);
  91.       fire.play('fire');
  92.     }, this);
  93.  
  94.     this.fires.setAll('body.allowGravity', false);
  95.    
  96.     //goal
  97.     this.goal = this.add.sprite(this.levelData.goal.x, this.levelData.goal.y, 'goal');
  98.     this.game.physics.arcade.enable(this.goal);
  99.     this.goal.body.allowGravity = false;
  100.  
  101.     //create player
  102.     this.player = this.add.sprite(this.levelData.playerStart.x, this.levelData.playerStart.y, 'player', 3);
  103.     this.player.anchor.setTo(0.5);
  104.     this.player.animations.add('walking', [0, 1, 2, 1], 6, true);
  105.     this.game.physics.arcade.enable(this.player);
  106.     this.player.customParams = {};
  107.     this.player.body.collideWorldBounds = true;
  108.  
  109.     this.game.camera.follow(this.player);
  110.  
  111.     this.createOnscreenControls();
  112.  
  113.     this.barrels = this.add.group();
  114.     this.barrels.enableBody = true;
  115.  
  116.     this.createBarrel();
  117.     this.barrelCreator = this.game.time.events.loop(Phaser.Timer.SECOND * this.levelData.barrelFrequency, this.createBarrel, this)
  118.   },
  119.   update: function() {
  120.     this.game.physics.arcade.collide(this.player, this.ground);
  121.     this.game.physics.arcade.collide(this.player, this.platforms);
  122.  
  123.     this.game.physics.arcade.collide(this.barrels, this.ground);
  124.     this.game.physics.arcade.collide(this.barrels, this.platforms);
  125.  
  126.     this.game.physics.arcade.overlap(this.player, this.fires, this.killPlayer);
  127.     this.game.physics.arcade.overlap(this.player, this.barrels, this.killPlayer);
  128.     this.game.physics.arcade.overlap(this.player, this.goal, this.win);
  129.  
  130.     //coin physics======================
  131.  
  132.     this.game.physics.arcade.collide(this.coin, this.player, this.platforms, this.ground, this.barrels);
  133.     this.game.physics.arcade.collide(this.coin, this.player, this.collectCoin);
  134.     this.game.physics.arcade.collide(this.coin, this.platforms);
  135.     this.game.physics.arcade.collide(this.coin, this.ground);
  136.      
  137.     function collectCoin(coin, player){
  138.       coin.kill();
  139.     }
  140. };
  141. //=========================================
  142.      
  143.     this.player.body.velocity.x = 0;
  144.  
  145.     if(this.cursors.left.isDown || this.player.customParams.isMovingLeft) {
  146.       this.player.body.velocity.x = -this.RUNNING_SPEED;
  147.       this.player.scale.setTo(1, 1);
  148.       this.player.play('walking');
  149.     }
  150.     else if(this.cursors.right.isDown || this.player.customParams.isMovingRight) {
  151.       this.player.body.velocity.x = this.RUNNING_SPEED;
  152.       this.player.scale.setTo(-1, 1);
  153.       this.player.play('walking');
  154.     }
  155.     else {
  156.       this.player.animations.stop();
  157.       this.player.frame = 3;
  158.  
  159.     }
  160.  
  161.     if((this.cursors.up.isDown || this.player.customParams.mustJump) && this.player.body.touching.down) {
  162.       this.player.body.velocity.y = -this.JUMPING_SPEED;
  163.       this.player.customParams.mustJump = false;
  164.     }
  165.  
  166.     this.barrels.forEach(function(element){
  167.       if(element.x < 10 && element.y > 600) {
  168.         element.kill();
  169.       }
  170.     }, this);
  171.   },
  172.   createOnscreenControls: function(){
  173.     this.leftArrow = this.add.button(20, 535, 'arrowButton');
  174.     this.rightArrow = this.add.button(110, 535, 'arrowButton');
  175.     this.actionButton = this.add.button(280, 535, 'actionButton');
  176.  
  177.     this.leftArrow.alpha = 0.5;
  178.     this.rightArrow.alpha = 0.5;
  179.     this.actionButton.alpha = 0.5;
  180.  
  181.     this.leftArrow.fixedToCamera = true;
  182.     this.rightArrow.fixedToCamera = true;
  183.     this.actionButton.fixedToCamera = true;
  184.  
  185.     this.actionButton.events.onInputDown.add(function(){
  186.       this.player.customParams.mustJump = true;
  187.     }, this);
  188.  
  189.     this.actionButton.events.onInputUp.add(function(){
  190.       this.player.customParams.mustJump = false;
  191.     }, this);
  192.  
  193.     //left
  194.     this.leftArrow.events.onInputDown.add(function(){
  195.       this.player.customParams.isMovingLeft = true;
  196.     }, this);
  197.  
  198.     this.leftArrow.events.onInputUp.add(function(){
  199.       this.player.customParams.isMovingLeft = false;
  200.     }, this);
  201.  
  202.     this.leftArrow.events.onInputOver.add(function(){
  203.       this.player.customParams.isMovingLeft = true;
  204.     }, this);
  205.  
  206.     this.leftArrow.events.onInputOut.add(function(){
  207.       this.player.customParams.isMovingLeft = false;
  208.     }, this);
  209.  
  210.     //right
  211.     this.rightArrow.events.onInputDown.add(function(){
  212.       this.player.customParams.isMovingRight = true;
  213.     }, this);
  214.  
  215.     this.rightArrow.events.onInputUp.add(function(){
  216.       this.player.customParams.isMovingRight = false;
  217.     }, this);
  218.  
  219.     this.rightArrow.events.onInputOver.add(function(){
  220.       this.player.customParams.isMovingRight = true;
  221.     }, this);
  222.  
  223.     this.rightArrow.events.onInputOut.add(function(){
  224.       this.player.customParams.isMovingRight = false;
  225.     }, this);
  226.   },
  227.   killPlayer: function(player, fire) {
  228.     console.log('I died!');
  229.     game.state.start('GameState');
  230.   },
  231.   win: function(player, goal) {
  232.     alert('You have passed the first stage!');
  233.     game.state.start('GameState');
  234.   },
  235.   createBarrel: function() {
  236.     //give me the first dead sprite
  237.     var barrel = this.barrels.getFirstExists(false);
  238.  
  239.     if(!barrel) {
  240.       barrel = this.barrels.create(0, 0, 'barrel');
  241.     }
  242.  
  243.     barrel.body.collideWorldBounds = true;
  244.     barrel.body.bounce.set(1, 0);
  245.  
  246.     barrel.reset(this.levelData.goal.x, this.levelData.goal.y);
  247.     barrel.body.velocity.x = this.levelData.barrelSpeed;
  248.   }
  249.  
  250. };
  251.  
  252.  
  253.  
  254. //initiate the Phaser framework
  255. var game = new Phaser.Game(360, 592, Phaser.AUTO);
  256.  
  257. game.state.add('GameState', GameState);
  258. game.state.start('GameState');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement