Guest User

phaser3 shooter

a guest
Oct 6th, 2019
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class level6 extends Phaser.Scene {
  2.  
  3.     constructor ()
  4.     {
  5.         super({ key: 'level6' });
  6.         // Put global variable here
  7.         this.starCount = 0;
  8.         this.bullets;
  9.     }
  10.  
  11. preload() {
  12.  
  13.     // map made with Tiled in JSON format
  14.     this.load.tilemapTiledJSON('map2', 'assets/level2.json');
  15.     this.load.spritesheet('tiles', 'assets/tiles64x64.png', {frameWidth: 64, frameHeight: 64});
  16.     this.load.spritesheet('player','assets/dude2.png', { frameWidth: 64, frameHeight: 96} );
  17.  
  18.     this.load.image('star', 'assets/star.png');
  19.     this.load.image('coin', 'assets/goldCoin.png');
  20.     this.load.image('bomb', 'assets/bomb.png');
  21.    
  22.     // Load the bullet
  23.     this.load.image('bullet', 'assets/bullet7.png');
  24.  
  25.     // Sound preload
  26.     this.load.audio('ping', 'assets/ping.mp3');
  27.     this.load.audio('explode', 'assets/explosion.mp3');
  28.     this.load.audio('blaster', 'assets/blaster.mp3');
  29. }
  30.  
  31. create() {
  32.  
  33.     this.add.text(0,0, 'Level 6 - Shooter overlap', { font: '24px Courier', fill: '#000000' }).setScrollFactor(0);
  34.  
  35.     // Sound variable
  36.     this.explodeSnd = this.sound.add('explode');
  37.     this.pingSnd = this.sound.add('ping');
  38.     this.blasterSnd = this.sound.add('blaster');
  39.  
  40.     // Load the tilemap to map variable
  41.     this.map2 = this.make.tilemap({key: 'map2'});
  42.    
  43.     // Must match tileSets name inside Tiled
  44.     let Tiles = this.map2.addTilesetImage('tiles64x64','tiles');
  45.  
  46.     // create the ground & platform layer
  47.     this.groundLayer = this.map2.createDynamicLayer('groundLayer', Tiles, 0, 0);
  48.     this.platformLayer = this.map2.createDynamicLayer('platformLayer', Tiles, 0, 0);
  49.  
  50.     // Set starting and ending position using object names in Tiled
  51.     this.startPoint5 = this.map2.findObject("ObjectLayer", obj => obj.name === "startPoint");
  52.     this.endPoint5 = this.map2.findObject("ObjectLayer", obj => obj.name === "endPoint");
  53.  
  54.     // Place an image manually on the endPoint
  55.     this.add.image(this.endPoint5.x, this.endPoint5.y, 'coin').setOrigin(0, 0);
  56.  
  57.     // create the player sprite    
  58.     this.player = this.physics.add.sprite(200, 200, 'player');
  59.     this.player.setBounce(0.1); // our this.player will bounce from items
  60.    
  61.     // small fix to our this.player images, we resize the physics body object slightly
  62.     this.player.body.setSize(this.player.width, this.player.height);
  63.     this.player.setCollideWorldBounds(true); // don't go out of the map  
  64.  
  65.     // Set this.player to starting position
  66.     //this.player.setPosition(this.startPoint.x, this.startPoint.y);  
  67.     this.player.setPosition(0, 0);  
  68.     window.player = this.player;
  69.  
  70.     // set the boundaries of our game world
  71.     this.physics.world.bounds.width = this.groundLayer.width;
  72.     this.physics.world.bounds.height = this.groundLayer.height;
  73.  
  74.     // Setup collider for ground and platform layer
  75.     this.groundLayer.setCollisionByProperty({ collides: true });
  76.     this.platformLayer.setCollisionByProperty({ collides: true });
  77.    
  78.     // Collides ground and platform with player
  79.     this.physics.add.collider(this.groundLayer, this.player);
  80.     this.physics.add.collider(this.platformLayer, this.player);
  81.  
  82.     // Collide ground and platform with stars
  83.     this.stars = this.physics.add.group({defaultKey: 'star'})
  84.     window.stars = this.stars;
  85.  
  86.     this.physics.add.collider(this.platformLayer, this.stars);
  87.     this.physics.add.collider(this.groundLayer, this.stars);
  88.  
  89.     // Timed events, drop starts every 2 secs, clear stars every 10 secs
  90.     this.timedEvent = this.time.addEvent({ delay: 2000, callback: this.dropStars, callbackScope: this, loop: true });
  91.     //this.timedEvent2 = this.time.addEvent({ delay: 5000, callback: this.clearBullets, callbackScope: this, loop: true });
  92.    
  93.  
  94.     this.stars.createMultiple({
  95.         key: 'star',
  96.         repeat: 20,
  97.         setXY: { x: Phaser.Math.Between(300, 400), y: Phaser.Math.Between(0, 500), stepX: Phaser.Math.Between(0, 500) }
  98.     })
  99.  
  100.     // Display the stars collected
  101.     this.starText = this.add.text(20, 40, 'Stars ' + this.starCount, {
  102.         fontSize: '20px',
  103.         fill: '#ffffff'
  104.     });
  105.     // fix the text to the camera
  106.     this.starText.setScrollFactor(0);
  107.     this.starText.visible = true;
  108.  
  109.     ///////////////////////////////
  110.     // Create animation for player
  111.     //////////////////////////////
  112.     this.anims.create({
  113.         key: 'left',
  114.         frames: this.anims.generateFrameNumbers('player', {
  115.             start: 0,
  116.             end: 3
  117.         }),
  118.         frameRate: 10,
  119.         repeat: -1
  120.     });
  121.  
  122.     this.anims.create({
  123.         key: 'idle',
  124.         frames: [{
  125.             key: 'player',
  126.             frame: 4
  127.         }],
  128.         frameRate: 20,
  129.         repeat: false
  130.     });
  131.  
  132.     this.anims.create({
  133.         key: 'right',
  134.         frames: this.anims.generateFrameNumbers('player', {
  135.             start: 5,
  136.             end: 8
  137.         }),
  138.         frameRate: 10,
  139.         repeat: -1
  140.     });
  141.  
  142.  
  143. ////////////////////////////
  144. // Setup cursors and cameras
  145. ////////////////////////////
  146.     this.cursors = this.input.keyboard.createCursorKeys();
  147.   // set bounds so the camera won't go outside the game world
  148.   this.cameras.main.setBounds(0, 0, this.map2.widthInPixels, this.map2.heightInPixels);
  149.   // make the camera follow the this.player
  150.   this.cameras.main.startFollow(this.player);
  151.   // set background color, so the sky is not black    
  152.   this.cameras.main.setBackgroundColor('#ccccff');
  153.  
  154.  
  155.  
  156. // Create the bullet object
  157. this.bullets = new Bullets(this);
  158.  
  159. // Define spacebar
  160. this.spaceDown = this.input.keyboard.addKey('SPACE');
  161. this.spaceDown.on('down', function(){
  162.     this.blasterSnd.play();
  163.     this.bullets.fireBullet(this.player.x, this.player.y+20);
  164.     //console.log(this.bullets);
  165.     }, this );
  166.  
  167. window.bullets = this.bullets;
  168.  
  169. // When bullets overlap with stars, run shootStar function
  170. this.physics.add.overlap(this.bullets, this.stars,this.shootStars, null, this );
  171.  
  172. }
  173. ///////////////////////// end of create() //////////////////////////
  174.  
  175. update() {
  176.  
  177.     if (this.cursors.left.isDown)
  178.     {
  179.         this.player.body.setVelocityX(-200);
  180.         this.player.anims.play('left', true);
  181.     }
  182.     else if (this.cursors.right.isDown)
  183.     {
  184.         this.player.body.setVelocityX(200);
  185.         this.player.anims.play('right', true);
  186.     } else {
  187.         this.player.body.setVelocityX(0);
  188.         this.player.anims.play('idle', true);
  189.     }
  190.     // jump
  191.     if (this.cursors.up.isDown && this.player.body.onFloor())
  192.     {
  193.         this.player.body.setVelocityY(-500);  
  194.     }
  195.    
  196. }
  197. /////////////////////////// end of update() /////////////////////////////////
  198.  
  199. shootStars(bullets, stars) {
  200.     // remove stars once collected
  201.     console.log('stars shot')
  202.     stars.disableBody(true, true);
  203.     bullets.disableBody(true, true);
  204.  
  205.     // Add counter
  206.     this.starCount += 1;
  207.     // Play sound
  208.     this.pingSnd.play();
  209.     console.log(this.starCount);
  210.  
  211.     // Update star counter
  212.     this.starText.setText('Stars ' + this.starCount); // set the text to show the current score
  213.  
  214.     return false;
  215. }
  216.  
  217. dropStars () {
  218.     // Add random stars
  219.     //console.log('Dropping stars');
  220.     this.stars.createMultiple({
  221.         key: 'star',
  222.         repeat: 20,
  223.         setXY: { x: Phaser.Math.Between(300, 400), y: Phaser.Math.Between(0, 500), stepX: Phaser.Math.Between(0, 500) }
  224.     })
  225.     this.cameras.main.shake(100);
  226. }
  227.  
  228. clearStars() {
  229.     console.log('Clearing stars');    
  230.     this.stars.clear(true,true);
  231.     //this.explodeSnd.play();
  232. }
  233.  
  234. clearBullets() {
  235. this.bullets.children.iterate( bullet => {
  236.     console.log('Clear faulty bullets');
  237.     if ( (bullet.body.prev.x === bullet.body.position.x ) &&  bullet.body.prev.x !== -6  ) {
  238.         console.log("prev ", bullet.body.prev.x);
  239.         console.log("pos  ", bullet.body.position.x);
  240.         bullet.setActive(false);
  241.         bullet.setVisible(false);
  242.     }
  243. });
  244. }
  245.  
  246.  
  247. }
  248.  
  249. // bullet class
  250. class Bullet extends Phaser.Physics.Arcade.Sprite {
  251.     constructor(scene, x, y) {
  252.         super(scene, x, y, 'bullet');
  253.     }
  254.  
  255.     fire(x, y) {
  256.         this.body.reset(x, y);
  257.         this.setVelocityX(400);
  258.  
  259.         this.body.setAllowGravity(false);
  260.         this.setActive(true);
  261.         this.setVisible(true);
  262.  
  263.         console.log('bullet (x,y) ', this.x, this.y);
  264.     }
  265.  
  266.     preUpdate(time, delta) {
  267.         super.preUpdate(time, delta);
  268.  
  269.         // Reset the bullets when it reaches end of screen
  270.         if (this.x > 2600) {
  271.             this.setActive(false);
  272.             this.setVisible(false);
  273.         }
  274.     }
  275. }
  276.  
  277. class Bullets extends Phaser.Physics.Arcade.Group {
  278.     constructor(scene) {
  279.         super(scene.physics.world, scene);
  280.  
  281.         this.createMultiple({
  282.             frameQuantity: 20,
  283.             key: 'bullet',
  284.             active: false,
  285.             visible: false,
  286.             classType: Bullet
  287.         });
  288.     }
  289.  
  290.     fireBullet(x, y) {
  291.         let bullet = this.getFirstDead(false);
  292.  
  293.         if (bullet) {
  294.             bullet.fire(x, y);
  295.         }
  296.     }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment