Skip to content

Instantly share code, notes, and snippets.

@scope2229
Created July 3, 2018 10:40
Show Gist options
  • Save scope2229/5afcd09e537aadf08120c5b6294b5358 to your computer and use it in GitHub Desktop.
Save scope2229/5afcd09e537aadf08120c5b6294b5358 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(){
console.log('lvl_1');
//set background
this.starfield = this.add.tileSprite(400,300,800,4000, 'starfield')
this.juststars = this.add.tileSprite(400,300,800,4000, 'juststars')
this.nebula1 = this.add.tileSprite(400,300,800,4000, 'nebula1')
this.stars2 = this.add.tileSprite(400,300,800,4000, 'juststars')
this.nebula2 = this.add.tileSprite(400,300,2000,4000, 'nebula2')
this.registry.set('score', this.score);
this.score = 0;
this.registry.set('score', this.score);
this.registry.set('playerHP', this.playerHP);
this.playerHP = 100;
this.registry.set('playerHP', this.playerHP);
// Our bullet group
let magSize = 1
this.bullets = this.physics.add.group({
Key: 'laser1',
maxSize: magSize
})
this.anims.create({
key: 'fire',
frames: this.anims.generateFrameNumbers('laser1', { start: 0, end: 2 }),
frameRate: 10,
repeat: 0
})
this.anims.create({
key: 'fired',
frames: this.anims.generateFrameNumbers('laser1', { start: 3, end: 4 }),
frameRate: 25,
repeat: -1
})
//create the first player
this.player = this.physics.add.sprite(400, 500, 'ship')
this.player.setCollideWorldBounds(true)
this.player.setOrigin(0.5, 0.5)
this.player.body.setSize(this.player.width * 1 / 2, this.player.height * 4 / 4)
this.ACCELERATION = 600
let DRAG = 900
let MAXSPEED = 400
this.player.body.setMaxVelocity(MAXSPEED, MAXSPEED)
this.player.body.setDrag(DRAG, DRAG)
//Creating tai-fighter enemy
this.enemies = this.physics.add.group({
defaultKey: 'tai_fighter1'
})
//TaiBomber
this.taiBombers = this.physics.add.group({
defaultKey: 'taiBomber',
createIfNull: true
})
this.physics.world.setBoundsCollision(true, true, true, true)
this.launchTaiFighter(this)
this.time.addEvent({ delay: 1000, callback: this.launchTaiBomber, callbackScope: this })
// An explosion group
this.explosions = this.physics.add.group()
this.anims.create({
key: 'explosionTai',
frames: this.anims.generateFrameNumbers('explosionTai', { start: 0, end: 63 }),
frameRate: 35,
repeat: 0
})
//setup for controls keyboard mouse touchscreen
this.cursors = this.input.keyboard.createCursorKeys()
this.fireButton = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.CTRL)
}
update(){
this.starfield.tilePositionY -= 0.5
this.juststars.tilePositionY -= 0.2
this.nebula1.tilePositionY -= 0.8
this.stars2.tilePositionY -= 3.8
this.nebula2.tilePositionY -= 0.3
this.nebula2.tilePositionX -= 0.3
//Player1 movement
this.player.body.setAccelerationX(0)
if(this.cursors.left.isDown){
this.player.body.setAccelerationX(-this.ACCELERATION)
}else if (this.cursors.right.isDown){
this.player.body.setAccelerationX(this.ACCELERATION)
}else{
this.player.body.setAccelerationX(0)
}
if (this.cursors.up.isDown){
this.player.body.setAccelerationY(-this.ACCELERATION)
}else if(this.cursors.down.isDown){
this.player.body.setAccelerationY(this.ACCELERATION)
}else{
this.player.body.setAccelerationY(0)
}
// Fire bullet
if(this.fireButton.isDown || this.input.activePointer.isDown){
console.log("fire button down")
this.fireBullet()
}
this.bullets.children.each(function(b) {
b.setOrigin(1,+0.3)
b.body.setSize(b.width, b.height)
if (b.active) {
console.log("bullet active");
if (b.y < 0) {
b.setActive(false)
}
}
}.bind(this))
this.enemies.children.each(function(en){
en.body.setSize(en.width * 3 / 4, en.height * 3 / 4)
console.log("enemies children")
en.angle = 180 - Phaser.Math.RadToDeg(Math.atan2(en.body.velocity.x, en.body.velocity.y))
en.body.angle = 180 - Phaser.Math.RadToDeg(Math.atan2(en.body.velocity.x, en.body.velocity.y))
if(en.active){
console.log("en.active");
}
})
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.explosions.children.each(function(ex){
if(ex.active){
//console.log("ex.active");
}
})
this.physics.add.collider(this.player, this.enemies, this.shipCollision, null, this)
this.physics.add.collider(this.bullets, this.enemies, this.bulletKillsEnemy, null, this)
if(this.playerHP == 0){
this.gameOver();
}
}//end of update
render(){
}
fireBullet(){
console.log("FIREBULLET")
let laser1 = this.bullets.get(this.player.x, (this.player.y - 50))
if (laser1) {
laser1.setActive(true);
laser1.setVisible(true);
laser1.anims.play('fire', true);
laser1.body.velocity.y = -400;
setTimeout(function(){ laser1.anims.play('fired', true); }, 100)
}
}
launchTaiFighter() {
let ENEMY_SPEED = 300
let enemy = this.enemies.getFirstAlive(true)
if (enemy){
enemy.setActive().body.reset((Phaser.Math.Between(50,750)), -20)
enemy.setVisible(true)
enemy.body.velocity.x = Phaser.Math.Between(-300, 300)
enemy.body.velocity.y = ENEMY_SPEED
enemy.body.drag.x = 100
}
let timedEvent = this.time.addEvent({ delay: 800, callback: this.launchTaiFighter, callbackScope: this })
}
launchTaiBomber(){
//group.createMultiple({ maxSize: 5 });
let startingX = Phaser.Math.Between(100, 700);
let startingY = Phaser.Math.Between(-20, -100);
console.log(startingX);
let verticalSpeed = 80;
let spread = 60;
let frequency = 70;
let verticalSpacing = 70;
let numEnemiesInWave = 3;
console.log("taiBomb" + this);
for (var i =0; i < numEnemiesInWave; i++){
let bomber = this.taiBombers.getFirstAlive(true);
let bomberlast = this.taiBombers.getLast(false)
if(bomber){
bomber[i].x = startingX;
bomber[i].x = startingX + 50;
bomber[i].x = startingX - 50;
bomber.setActive().body.reset(800 / 2, -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)){
// let launchEvent = this.time.addEvent({ delay: 800, callback: this.launchTaiBomber, callbackScope: this });
// }
}
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);
}
gameOver(){
this.scene.start('gameOver');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment