Created
June 29, 2018 14:03
-
-
Save scope2229/d430ca612ec223e3a62c7cc34dddbbee to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import phaser from 'phaser' | |
export class Lvl_1 extends phaser.Scene{ | |
constructor(){ | |
super('lvl_1') | |
} | |
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') | |
let magSize = 1 | |
// Our bullet group | |
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.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', | |
createMultiple: 5, | |
enableBody: true, | |
physicsBodyType: Phaser.Physics.ARCADE, | |
runChildUpdate: true | |
}) | |
this.enemies.createMultiple({ | |
active: false, | |
defaultKey: this.enemies.defaultKey, | |
runChildUpdate: this.enemies.runChildUpdate, | |
repeat: this.enemies.createMultiple - 1 | |
}) | |
this.physics.world.setBoundsCollision(true, true, true, true) | |
this.launchTaiFighter(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) | |
} | |
//firing weapon | |
// Fire bullet | |
if(this.fireButton.isDown || this.input.activePointer.isDown){ | |
console.log("space test") | |
this.fireBullet() | |
} | |
this.bullets.children.each(function(b) { | |
if (b.active) { | |
if (b.y < 0) { | |
b.setActive(false) | |
} | |
} | |
}.bind(this)) | |
this.enemies.children.each(function(e) { | |
e.angle = 180 - Phaser.Math.RadToDeg(Math.atan2(e.body.velocity.x, e.body.velocity.y)) | |
if (e.active) { | |
//e.angle = 180 - Phaser.Math.RadToDeg(Math.atan2(e.body.velocity.x, e.body.velocity.y)) | |
if (e.y < 0) { | |
e.setActive(false) | |
} | |
} | |
}.bind(this)) | |
this.physics.add.collider(this.player, this.enemies, this.shipExplodes, null, this) | |
}//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(true).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 | |
if (enemy.y > 600) { | |
console.log("KILL"); | |
enemy.kill(); | |
} | |
} | |
//this.time.addEvent(Phaser.Math.Between(MIN_ENEMY_SPACING, MAX_ENEMY_SPACING), this.launchTaiFighter) | |
let timedEvent = this.time.addEvent({ delay: 800, callback: this.launchTaiFighter, callbackScope: this }) | |
} | |
shipExplodes(player, enemy){ | |
console.log("explode") | |
enemy.setBounce(0.2) | |
let explosion = this.explosions.create(enemy.x, enemy.y, 'explosionTai') | |
enemy.setActive(false) | |
enemy.setVisible(false) | |
enemy.destroy() | |
explosion.anims.play('explosionTai') | |
// enemies.anims.play('explosionTai', true) | |
// this.enemies.setActive(false) | |
// this.enemies.setVisible(false) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment