GitLab system status is available here and here

Commit 82c24cd7 authored by wbtate's avatar wbtate
Browse files

Now go boom

parent bd64db67
......@@ -13,7 +13,8 @@
</head>
<body>
<script src ="./lib/phaser.js"></script>
<script src ="src/prefabs/Rocket.js"></script>
<script src ="./src/prefabs/Rocket.js"></script>
<script src="./src/prefabs/Spaceship.js"></script>
<script src ="./src/scenes/Menu.js"></script>
<script src ="./src/scenes/Play.js"></script>
<script src = "./src/main.js"></script>
......
......@@ -8,3 +8,5 @@ let config = {
let game = new Phaser.Game(config);
let borderUISize = game.config.height / 15;
let borderPadding = borderUISize / 3;
// reserve keyboard vars
let keyF, keyR, keyLEFT, keyRIGHT;
\ No newline at end of file
const { Phaser } = require("../../lib/phaser");
//const { Phaser } = require("../../lib/phaser");
// Rocket prefab
class Rocket extends Phaser.GameObjects.Sprite {
......@@ -30,4 +30,9 @@ class Rocket extends Phaser.GameObjects.Sprite {
this.y=game.config.height-borderUISize-borderPadding;
}
}
reset(){
this.isFiring=false;
this.y=game.config.height-borderUISize-borderPadding;
}
}
\ No newline at end of file
//const { Phaser } = require("../../lib/phaser");
class Spaceship extends Phaser.GameObjects.Sprite{
constructor(scene, x, y, texture, frame, pointValue){
super(scene,x,y,texture,frame);
scene.add.existing(this);
this.points=pointValue;
this.moveSpeed=3;
}
update(){
this.x-=this.moveSpeed;
if(this.x<=0-this.width){
this.reset();
}
}
reset(){
this.x=game.config.width;
}
}
\ No newline at end of file
......@@ -7,6 +7,8 @@ class Play extends Phaser.Scene {
this.load.image('rocket', './assets/rocket.png');
this.load.image('spaceship', './assets/spaceship.png');
this.load.image('starfield', './assets/starfield.png');
// load spritesheet
this.load.spritesheet('explosion', './assets/explosion.png', {frameWidth: 64, frameHeight: 32, startFrame: 0, endFrame: 9});
}
create() {
this.add.text(20, 20, "Rocket Patrol Play");
......@@ -19,8 +21,70 @@ class Play extends Phaser.Scene {
this.add.rectangle(0, game.config.height - borderUISize, game.config.width, borderUISize, 0xFFFFFF).setOrigin(0, 0);
this.add.rectangle(0, 0, borderUISize, game.config.height, 0xFFFFFF).setOrigin(0, 0);
this.add.rectangle(game.config.width - borderUISize, 0, borderUISize, game.config.height, 0xFFFFFF).setOrigin(0, 0);
// add rocket (p1)
this.p1Rocket = new Rocket(this, game.config.width/2, game.config.height - borderUISize - borderPadding, 'rocket').setOrigin(0.5, 0);
// add spaceships (x3)
this.ship01 = new Spaceship(this, game.config.width + borderUISize*6, borderUISize*4, 'spaceship', 0, 30).setOrigin(0, 0);
this.ship02 = new Spaceship(this, game.config.width + borderUISize*3, borderUISize*5 + borderPadding*2, 'spaceship', 0, 20).setOrigin(0,0);
this.ship03 = new Spaceship(this, game.config.width, borderUISize*6 + borderPadding*4, 'spaceship', 0, 10).setOrigin(0,0);
// define keys
keyF = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.F);
keyR = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.R);
keyLEFT = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.LEFT);
keyRIGHT = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.RIGHT);
// animation config
this.anims.create({
key: 'explode',
frames: this.anims.generateFrameNumbers('explosion', { start: 0, end: 9, first: 0}),
frameRate: 30
});
}
update() {
this.starfield.tilePositionX -= 4;
this.p1Rocket.update();
this.ship01.update(); // update spaceships (x3)
this.ship02.update();
this.ship03.update();
// check collisions
if(this.checkCollision(this.p1Rocket, this.ship03)) {
this.p1Rocket.reset();
this.shipExplode(this.ship03);
}
if (this.checkCollision(this.p1Rocket, this.ship02)) {
this.p1Rocket.reset();
this.shipExplode(this.ship02);
}
if (this.checkCollision(this.p1Rocket, this.ship01)) {
this.p1Rocket.reset();
this.shipExplode(this.ship01);
}
}
checkCollision(rocket, ship) {
// simple AABB checking
if (rocket.x < ship.x + ship.width &&
rocket.x + rocket.width > ship.x &&
rocket.y < ship.y + ship.height &&
rocket.height + rocket.y > ship. y) {
return true;
} else {
return false;
}
}
shipExplode(ship) {
// temporarily hide ship
ship.alpha = 0;
// create explosion sprite at ship's position
let boom = this.add.sprite(ship.x, ship.y, 'explosion').setOrigin(0, 0);
boom.anims.play('explode'); // play explode animation
boom.on('animationcomplete', () => { // callback after anim completes
ship.reset(); // reset ship position
ship.alpha = 1; // make ship visible again
boom.destroy(); // remove explosion sprite
});
}
}
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment