export default class SceneMain extends Phaser.Scene { constructor() { super('SceneMain'); } preload(){ // Load images and sounds //area this.load.image('bg', 'assets/playfield/bg.png'); this.load.image('area', 'assets/playfield/area.png'); //player this.load.spritesheet('player-idle', 'assets/player/player-idle.png', {frameWidth:80,frameHeight:80}); this.load.spritesheet('player-run', 'assets/player/player-run.png', {frameWidth:80,frameHeight:80}); this.load.spritesheet('player-jump', 'assets/player/player-jump.png', {frameWidth:80,frameHeight:80}); this.load.spritesheet('player-shot', 'assets/player/player-shot.png', {frameWidth:80,frameHeight:80}); //bullet this.load.spritesheet('shot', 'assets/fx/shot.png', {frameWidth:6,frameHeight:4}); this.load.spritesheet('monster-idle', 'assets/monster/crab-idle.png', {frameWidth:48,frameHeight:32}); } create(){ // Define our objects this.add.image(0, 0, 'bg').setOrigin(0, 0); this.bg = this.add.image(0, 0, 'area').setOrigin(0, 0); this.player = this.physics.add.sprite(320, 300, 'player-idle'); this.player.setCollideWorldBounds(true); this.player.setGravityY(400); this.player.setSize(32,80); //bullet this.bullet = this.physics.add.group(); //enemy group this.monster //floor this.floor = this.physics.add.sprite(0, 480).setOrigin(0,0); this.floor.setCollideWorldBounds(true); this.floor.displayWidth = 640; this.floor.displayHeight = 60; this.floor.body.immovable = true; this.floor2 = this.physics.add.sprite(0, 480).setOrigin(0,0); this.floor2.setCollideWorldBounds(true); this.floor2.displayWidth = 110; this.floor2.displayHeight = 160; this.floor2.body.immovable = true; this.floor3 = this.physics.add.sprite(640, 480).setOrigin(0,0);
this.floor3.setCollideWorldBounds(true); this.floor3.displayWidth = 80; this.floor3.displayHeight = 160; this.floor3.body.immovable = true; //check shooting this.isShooting = false; //control this.cursors = this.input.keyboard.createCursorsKeys(); this.spaceKey = this.input.keyboard.addKey(Phaser.Input.keyboard.keyCodes.SPACE); //animation this.anims.create({ key: 'p-idle', frames: this.anims.generateFrameNumbers('player-idle',{start :0, end :3}), framerate: 10, repeat: -1, }); this.anims.create({ key: 'p-run', frames: this.anims.generateFrameNumbers('player-run',{start :0, end :9}), framerate: 10, repeat: -1, }); this.anims.create({ key: 'p-jump', frames: this.anims.generateFrameNumbers('player-jump',{start :0, end :5}), framerate: 10, repeat: -1, }); this.anims.create({ key: 'p-shot', frames: this.anims.generateFrameNumbers('player-shot',{start :0, end :9}), framerate: 10, repeat: -1, }); this.anims.create({ key: 'p-shoting', frames: this.anims.generateFrameNumbers('player-shot',{start :1, end :1}), repeat: 0, }); this.anims.create({ key: 'shoting', frames: this.anims.generateFrameNumbers('player-shot',{start :0, end :1}), framerate: 10, repeat: -1, }); //collider this.physics.add.collider(this.player, this.floor); this.physics.add.collider(this.player, this.floor2); this.physics.add.collider(this.player, this.floor3);
this.player.anims.play('p-run'); timeSpawn(timer){ this.timerToActive--; if (this.timerToActive < 0){ this.spawnEnemy(); this.timerToActive = timer; } } currentWave(wave){ switch (wave){ case 1 : return 5; case 2 : return 7; case 3 : return 10; } } spawnEnemy(){ console.log("Spawned | "+this.wave); if (this.spawn != this.currentWave(this.wave)){ var rand = Phaser.Math.Beetween(0,1); if (rand == 1){ var crabMonster = this.monsters.anims.play } } } } doShoot(arah){ if(this.isShooting == false){ if (arah == true){ var peluru = this.bullet.create(this.player.x-20, this.player.y+15,'shot'); peluru.anims.play('shoting'); peluru.body.setVelocityX(-450); }else{ var peluru = this.bullet.create(this.player.x+20, this.player.y+15,'shot'); peluru.anims.play('shoting'); peluru.body.setVelocityX(450); } this.isShooting = true; this.time.addEvent({ delay : 250, callback: () => { this.isShooting = false; } }); } } update(){ // Running loop if(!this.player.body.touching.down){ if (this.cursors.left.isDown){ this.player.flipX = true; this.player.setVelocityX(-160); }else if(this.cursors.right.isDown){ this.player.flipX= false; this.player.setVelocityX(160); }else {
this.player.setVelocityX(0); } }else if (this.spaceKey.isDown && this.player.body.touching.down){ if(this.cursors.left.isDown){ this.player.flipX = true; this.player.setVelocityX(-160); this.player.anims.play('p-shot', true); }else if (this.cursors.right.isDown){ this.player.flipX = false; this.player.setVelocityX(160); this.player.anims.play('p-shot', true); }else { this.player.setVelocityX(0); this.player.anims.play('p-shoting', true); } this.doShoot(this.player.flipX); }else { if(this.cursors.left.isDown){ this.player.flipX = true; this.player.setVelocityX(-160); this.player.anims.play('p-run', true); }else if (this.cursors.right.isDown){ this.player.flipX = false; this.player.setVelocityX(160); this.player.anims.play('p-run', true); }else { this.player.setVelocityX(0); this.player.anims.play('p-idle', true); } } //jump if (this.cursors.up.isDown && this.player.body.touching.down){ this.player.setVelocityY(-330); this.player.anims.play('p-jump', true); }
}