Skip to content

Instantly share code, notes, and snippets.

@scope2229
Created July 4, 2018 13:57
Show Gist options
  • Save scope2229/f3f2f5c2edb4ec6fd1a154b79f4da5a4 to your computer and use it in GitHub Desktop.
Save scope2229/f3f2f5c2edb4ec6fd1a154b79f4da5a4 to your computer and use it in GitHub Desktop.
import phaser from 'phaser'
export class Lvl_1 extends phaser.Scene{
constructor(){
super('lvl_1')
this.score = 1;
this.playerHP = 0;
}
create(){
this.taiBombers = this.physics.add.group();
this.physics.world.setBoundsCollision(true, true, true, true)
this.launchTaiBomber(this)
}
update(){
this.taiBombers.children.each(function(tb){
let numberOfTicks = 0;
numberOfTicks++;
//tb.x = (tb.x * Math.sin(numberOfTicks * 0.8 * 3.14));
let positionXX = tb.x
tb.angle = 180 - Phaser.Math.RadToDeg(Math.atan2(tb.body.velocity.x, tb.body.velocity.y))
tb.x = positionXX + Math.sin((tb.y) / 8) * 2.5;
//let bank = Math.cos((tb.y + 50) / 8)
//tb.scaleX = 0.5 - Math.abs(bank) / 8;
//tb.angle = 180 - bank * 2;
if(tb.y > 650){
console.log(" tb.y " + tb.y);
tb.destroy();
//tb.body.x = tb.x + Math.sin((tb.y) / 70) * 60;
}
})
// this.taiBombers.children.each(function(tb){
// tb.body.setSize(tb.width * 3 / 4, tb.height * 3 / 4)
// console.log("taibomber children")
// tb.angle = 180 - Phaser.Math.RadToDeg(Math.atan2(tb.body.velocity.x, tb.body.velocity.y))
// tb.body.angle = 180 - Phaser.Math.RadToDeg(Math.atan2(tb.body.velocity.x, tb.body.velocity.y))
// if(tb.active){
// console.log("tb.active");
// }
// })
this.explosions.setVelocityY(300)
this.physics.add.collider(this.player, this.enemies, this.shipCollision, null, this)
this.physics.add.collider(this.bullets, this.enemies, this.bulletKillsEnemy, null, this)
this.physics.add.collider(this.bullets, this.taiBombers, this.bulletKillsEnemy, null, this)
}
launchTaiBomber(){
//group.createMultiple({ maxSize: 5 });
let launch = this.taiBombers.getFirstAlive();
this.startingX = Phaser.Math.Between(100, 700);
if(launch == 'false'){
console.log("lanuch is false now start emmiting enimes");
}
console.log(this.startingX);
console.log("taiBomb" + this);
let taibomber = this.taiBombers.getChildren();
let tbRow1 = this.taiBombers.create(this.startingX, 120, 'taiBomber');
tbRow1.body.velocity.y = 30;
let tbRow2 = this.taiBombers.create((this.startingX - 30), 70, 'taiBomber').setVelocityY(30);
let tbRow3 = this.taiBombers.create((this.startingX + 30), 70, 'taiBomber').setVelocityY(30);
console.log(tbRow1.y + ' first row location');
let launchEvent = this.time.addEvent({ delay: 800, callback: this.launchTaiBomber, callbackScope: this });
// for (var i =0; i < numEnemiesInWave; i++){
// while (i=1){
// let bomber = this.taiBombers.getFirstAlive(true);
// let bomberlast = this.taiBombers.getLast(false)
// if(bomber){
// bomber.x = startingX;
// bomber.setDepth(1000,10);
// bomber.setActive().body.reset(startingX, -verticalSpacing * i);
// bomber.body.velocity.y = verticalSpeed;
// }
// }
// }
// let timedEvent = this.time.addEvent({ delay: 7000, callback: this.launchTaiBomber, callbackScope: this })
// let bomberSpeed = 300;
// let tBomber = this.taiBombers.getFirstAlive(true)
// if (tBomber){
// tBomber.setActive().body.reset((Phaser.Math.Between(50,750)), -20);
// tBomber.setVisible(true);
// tBomber.body.velocity.x = 0;
// tBomber.body.velocity.y = bomberSpeed;
//
// }
// if(tBomber == this.taiBombers.getFirstAlive(false)){
// }
}
shipCollision(player, enemy, taiBomber){
console.log("explode")
let explosion = this.explosions.create(enemy.x, enemy.y, 'explosionTai')
enemy.destroy()
explosion.anims.play('explosionTai')
explosion.on('animationcomplete', () => { explosion.destroy()})
this.playerHP -= 10;
this.registry.set('playerHP', this.playerHP);
}
bulletKillsEnemy(bullet, enemy){
let explosion = this.explosions.create(enemy.x, enemy.y, 'explosionTai');
enemy.destroy();
bullet.destroy();
explosion.anims.play('explosionTai');
explosion.on('animationcomplete', () => { explosion.destroy()});
this.score += 10;
console.log(this.score);
this.registry.set('score', this.score);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment