Advertisement
Guest User

Untitled

a guest
Aug 1st, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var config = {
  2.     type: Phaser.AUTO,
  3.     parent  : 'phaser-app',
  4.     width: 800,
  5.     height: 600,
  6.     physics: {
  7.         default: 'arcade',
  8.         arcade: {
  9.             gravity: {
  10.                 y: 300
  11.             },
  12.             debug: true
  13.         }
  14.     },
  15.     scene: {
  16.         preload: preload,
  17.         create: create,
  18.         update: update
  19.     }
  20. };
  21.  
  22. var ladder = false;
  23.  
  24. var game = new Phaser.Game(config);
  25.  
  26. function preload() {
  27.     this.load.tilemapTiledJSON('map', 'assets/Tiled-9.json');
  28.     this.load.spritesheet('tiles', 'assets/tiles64x64.png', {frameWidth: 64, frameHeight: 64});
  29.     this.load.atlas('player', 'assets/player.png', 'assets/player.json');
  30. }
  31.  
  32. function create() {
  33.     this.map = this.make.tilemap({key: 'map'});
  34.    
  35.     // Must match tileSets name above ( tiles64x64 )
  36.     this.Tiles = this.map.addTilesetImage('tiles64x64','tiles');
  37.  
  38.     // create the ground layer
  39.     this.groundLayer = this.map.createStaticLayer('groundLayer', this.Tiles, 0, 0);
  40.     this.platformLayer = this.map.createStaticLayer('platformLayer', this.Tiles, 0, 0);
  41.     this.ladderLayer = this.map.createDynamicLayer('ladderLayer', this.Tiles, 0, 0);
  42.  
  43.     // Climb ladder
  44.     this.ladderLayer.setTileIndexCallback(7, allowClimb, this);
  45.  
  46.     window.ladder = this.ladderLayer;
  47.    
  48.     // create the this.player sprite    
  49.     this.player = this.physics.add.sprite(0, 0, 'player');
  50.     this.player.setBounce(0.1); // our this.player will bounce from items
  51.     this.player.setOrigin(0.5, 0);
  52.     this.player.setCollideWorldBounds(true); // don't go out of the map  
  53.     this.player.setPosition(0, 0);  
  54.  
  55.     window.player = this.player;
  56.    
  57.     // set the boundaries of our game world
  58.     this.physics.world.bounds.width = this.groundLayer.width;
  59.     this.physics.world.bounds.height = this.groundLayer.height;
  60.  
  61.     // the this.player will collide with this layer
  62.     this.groundLayer.setCollisionByProperty({ collides: true });
  63.     this.platformLayer.setCollisionByProperty({ collides: true });
  64.    
  65.     // Collides with platform and ground
  66.     this.physics.add.collider(this.groundLayer, this.player);
  67.     this.physics.add.collider(this.platformLayer, this.player);
  68.  
  69.     this.physics.add.overlap(this.ladderLayer,this.player );
  70.  
  71.     // this.player walk animation
  72.     this.anims.create({
  73.         key: 'walk',
  74.         frames: this.anims.generateFrameNames('player', {prefix: 'p1_walk', start: 1, end: 11, zeroPad: 2}),
  75.         frameRate: 10,
  76.         repeat: -1
  77.     });
  78.     // idle with only one frame, so repeat is not neaded
  79.     this.anims.create({
  80.         key: 'idle',
  81.         frames: [{key: 'player', frame: 'p1_stand'}],
  82.         frameRate: 10,
  83.     });
  84.  
  85.     cursors = this.input.keyboard.createCursorKeys();
  86.  
  87.   // set bounds so the camera won't go outside the game world
  88.   this.cameras.main.setBounds(0, 0, this.map.widthInPixels, this.map.heightInPixels);
  89.   // make the camera follow the this.player
  90.   this.cameras.main.startFollow(this.player);
  91.  
  92.   // set background color, so the sky is not black    
  93.   this.cameras.main.setBackgroundColor('#ccccff');
  94.  
  95. }
  96.  
  97. function allowClimb(sprite, tile) {
  98.     console.log('Allow Climb');
  99.     let distance = Math.abs(this.player.x - (tile.pixelX + tile.width / 2)); // I haven't found how to reassign tile's anchor so I just added half of it's width to it's x coordinate
  100.     console.log(this.player.x, tile.pixelX, distance);
  101.     if (this.cursorKeys.up.isDown && distance <= 3) {
  102.         this.player.x = tile.pixelX + tile.width / 2;
  103.         //this.player.anims.play('playerClimb', true);
  104.         //this.player.setVelocityY(-gameSettings.playerSpeed);
  105.     } else if (this.cursorKeys.down.isDown) {
  106.         //this.player.anims.play('playerClimb', true);
  107.         //this.player.setVelocityY(gameSettings.playerSpeed);
  108.     } else {
  109.         this.player.setVelocityY(0);
  110.     }
  111. }
  112.  
  113. function update() {
  114.  
  115.  
  116.     if (cursors.left.isDown)
  117.     {
  118.             this.player.body.setVelocityX(-200);
  119.             this.player.anims.play('walk', true); // walk left
  120.             this.player.flipX = true;    
  121.     }
  122.     else if (cursors.right.isDown)
  123.     {
  124.         this.player.body.setVelocityX(200);
  125.         this.player.anims.play('walk', true);
  126.         this.player.flipX = false;
  127.     } else {
  128.             this.player.body.setVelocityX(0);
  129.             this.player.anims.play('idle', true);
  130.     }
  131.    
  132.     if (cursors.up.isDown )
  133.     {
  134.             this.player.body.setVelocityY(-300);      
  135.     }
  136.    
  137. }
  138.  
  139. function resizeApp ()
  140. {
  141.     // Width-height-ratio of game resolution
  142.     // Replace 360 with your game width, and replace 640 with your game height
  143.     let game_ratio = 360 / 640;
  144.    
  145.     // Make div full height of browser and keep the ratio of game resolution
  146.     let div = document.getElementById('phaser-app');
  147.     div.style.width = (window.innerHeight * game_ratio) + 'px';
  148.     div.style.height = window.innerHeight + 'px';
  149.    
  150.     // Check if device DPI messes up the width-height-ratio
  151.     let canvas  = document.getElementsByTagName('canvas')[0];
  152.    
  153.     let dpi_w   = parseInt(div.style.width) / canvas.width;
  154.     let dpi_h   = parseInt(div.style.height) / canvas.height;      
  155.    
  156.     let height  = window.innerHeight * (dpi_w / dpi_h);
  157.     let width   = height * game_ratio;
  158.    
  159.     // Scale canvas
  160.     canvas.style.width  = width + 'px';
  161.     canvas.style.height = height + 'px';
  162. }
  163.  
  164. window.addEventListener('resize', resizeApp);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement