Skip to content

Instantly share code, notes, and snippets.

@scope2229
Created June 28, 2018 18:54
Show Gist options
  • Save scope2229/82b54d6142af560a112eb54a548baabf to your computer and use it in GitHub Desktop.
Save scope2229/82b54d6142af560a112eb54a548baabf to your computer and use it in GitHub Desktop.
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
let value = Phaser.Math.Between(300, 600)
console.log(value);
this.enemies = this.physics.add.group({
defaultKey: 'tai_fighter1',
createMultiple: 5,
enableBody: true,
physicsBodyType: Phaser.Physics.ARCADE
})
this.enemies.createMultiple({
active: false,
defaultKey: this.enemies.defaultKey,
repeat: this.enemies.createMultiple - 1
})
this.physics.world.setBoundsCollision(true, true, true, true)
// scale enemies
Phaser.Actions.ScaleXY(this.enemies.getChildren(), -0.7, -0.7)
// set speeds
this.launchTaiFighter(this)
//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) {
if (e.active) {
e.angle = e.body.angle
if (e.y < 0) {
e.setActive(false)
}
}
}.bind(this))
//enemy updates
let X = Phaser.Math.Between(0,800)
// set speeds
// Phaser.Actions.Call(this.enemies.getChildren(), function(enemy) {
// enemy.speed = Math.random() * 2 + 1
// }, this)
// enemy movement
// let enemies = this.enemies.getChildren();
// let numEnemies = enemies.length;
//
// for (let i = 0; i < numEnemies; i++) {
// // move enemies
// enemies[i].y += enemies[i].speed;
//
// // reverse movement if reached the edges
// if (enemies[i].y >= this.enemyMaxY && enemies[i].speed > 0) {
// enemies[i].speed *= -1;
// } else if (enemies[i].y <= this.enemyMinY && enemies[i].speed < 0) {
// enemies[i].speed *= -1;
// }
// }
}//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
enemy.update = function(){
console.log("");
enemy.angle = 180 - Phaser.Math.RAD_TO_DEG(Math.atan2(enemy.body.velocity.x, enemy.body.velocity.y));
}
}
//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 })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment