Skip to content

Commit

Permalink
Added the project code.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredyork committed Feb 10, 2019
0 parents commit 321a972
Show file tree
Hide file tree
Showing 27 changed files with 183,726 additions and 0 deletions.
Binary file added content/sndBtnDown.wav
Binary file not shown.
Binary file added content/sndBtnOver.wav
Binary file not shown.
Binary file added content/sndExplode0.wav
Binary file not shown.
Binary file added content/sndExplode1.wav
Binary file not shown.
Binary file added content/sndLaser.wav
Binary file not shown.
Binary file added content/sprBg0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/sprBg1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/sprBtnPlay.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/sprBtnPlayDown.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/sprBtnPlayHover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/sprBtnRestart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/sprBtnRestartDown.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/sprBtnRestartHover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/sprEnemy0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/sprEnemy1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/sprEnemy2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/sprExplosion.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/sprLaserEnemy0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/sprLaserPlayer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added content/sprPlayer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta lang="en-us">
<title>Space Shooter</title>
<script src="js/phaser.js"></script> <!-- the file name should be the same as the Phaser script you added. -->
</head>
<body>
<script src="js/Entities.js"></script>
<script src="js/SceneMainMenu.js"></script>
<script src="js/SceneMain.js"></script>
<script src="js/SceneGameOver.js"></script>
<script src="js/game.js"></script>
</body>
</html>
232 changes: 232 additions & 0 deletions js/Entities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
class Entity extends Phaser.GameObjects.Sprite {
constructor(scene, x, y, key, type) {
super(scene, x, y, key);

this.scene = scene;
this.scene.add.existing(this);
this.scene.physics.world.enableBody(this, 0);
this.setData("type", type);
this.setData("isDead", false);
}

explode(canDestroy) {
if (!this.getData("isDead")) {
// Set the texture to the explosion image, then play the animation
this.setTexture("sprExplosion"); // this refers to the same animation key we used when we added this.anims.create previously
this.play("sprExplosion"); // play the animation
// pick a random explosion sound within the array we defined in this.sfx in SceneMain
this.scene.sfx.explosions[Phaser.Math.Between(0, this.scene.sfx.explosions.length - 1)].play();
if (this.shootTimer !== undefined) {
if (this.shootTimer) {
this.shootTimer.remove(false);
}
}
this.setAngle(0);
this.body.setVelocity(0, 0);
this.on('animationcomplete', function() {
if (canDestroy) {
this.destroy();
}
else {
this.setVisible(false);
}
}, this);
this.setData("isDead", true);
}
}
}

class Player extends Entity {
constructor(scene, x, y, key) {
super(scene, x, y, key, "Player");

this.setData("speed", 200);

this.setData("isShooting", false);
this.setData("timerShootDelay", 10);
this.setData("timerShootTick", this.getData("timerShootDelay") - 1);
}

moveUp() {
this.body.velocity.y = -this.getData("speed");
}
moveDown() {
this.body.velocity.y = this.getData("speed");
}
moveLeft() {
this.body.velocity.x = -this.getData("speed");
}
moveRight() {
this.body.velocity.x = this.getData("speed");
}

onDestroy() {
this.scene.time.addEvent({ // go to game over scene
delay: 1000,
callback: function() {
this.scene.scene.start("SceneGameOver");
},
callbackScope: this,
loop: false
});
}

update() {
this.body.setVelocity(0, 0);
this.x = Phaser.Math.Clamp(this.x, 0, this.scene.game.config.width);
this.y = Phaser.Math.Clamp(this.y, 0, this.scene.game.config.height);

if (this.getData("isShooting")) {
if (this.getData("timerShootTick") < this.getData("timerShootDelay")) {
this.setData("timerShootTick", this.getData("timerShootTick") + 1); // every game update, increase timerShootTick by one until we reach the value of timerShootDelay
}
else { // when the "manual timer" is triggered:
var laser = new PlayerLaser(this.scene, this.x, this.y);
this.scene.playerLasers.add(laser);

this.scene.sfx.laser.play(); // play the laser sound effect
this.setData("timerShootTick", 0);
}
}
}
}

class PlayerLaser extends Entity {
constructor(scene, x, y) {
super(scene, x, y, "sprLaserPlayer");
this.body.velocity.y = -200;
}
}

class EnemyLaser extends Entity {
constructor(scene, x, y) {
super(scene, x, y, "sprLaserEnemy0");
this.body.velocity.y = 200;
}
}

class ChaserShip extends Entity {
constructor(scene, x, y) {
super(scene, x, y, "sprEnemy1", "ChaserShip");

this.body.velocity.y = Phaser.Math.Between(50, 100);

this.states = {
MOVE_DOWN: "MOVE_DOWN",
CHASE: "CHASE"
};
this.state = this.states.MOVE_DOWN;
}

update() {
if (!this.getData("isDead") && this.scene.player) {
if (Phaser.Math.Distance.Between(
this.x,
this.y,
this.scene.player.x,
this.scene.player.y
) < 320) {

this.state = this.states.CHASE;
}

if (this.state == this.states.CHASE) {
var dx = this.scene.player.x - this.x;
var dy = this.scene.player.y - this.y;

var angle = Math.atan2(dy, dx);

var speed = 100;
this.body.setVelocity(
Math.cos(angle) * speed,
Math.sin(angle) * speed
);

if (this.x < this.scene.player.x) {
this.angle -= 5;
}
else {
this.angle += 5;
}
}
}
}
}

class GunShip extends Entity {
constructor(scene, x, y) {
super(scene, x, y, "sprEnemy0", "GunShip");
this.play("sprEnemy0");

this.body.velocity.y = Phaser.Math.Between(50, 100);

this.shootTimer = this.scene.time.addEvent({
delay: 1000,
callback: function() {
var laser = new EnemyLaser(
this.scene,
this.x,
this.y
);
laser.setScale(this.scaleX);
this.scene.enemyLasers.add(laser);
},
callbackScope: this,
loop: true
});
}

onDestroy() {
if (this.shootTimer !== undefined) {
if (this.shootTimer) {
this.shootTimer.remove(false);
}
}
}
}

class CarrierShip extends Entity {
constructor(scene, x, y) {
super(scene, x, y, "sprEnemy2", "CarrierShip");
this.play("sprEnemy2");

this.body.velocity.y = Phaser.Math.Between(50, 100);
}
}

class ScrollingBackground {
constructor(scene, key, velocityY) {
this.scene = scene;
this.key = key;
this.velocityY = velocityY;

this.layers = this.scene.add.group();

this.createLayers();
}

createLayers() {
for (var i = 0; i < 2; i++) {
// creating two backgrounds will allow a continuous flow giving the illusion that they are moving.
var layer = this.scene.add.sprite(0, 0, this.key);
layer.y = (layer.displayHeight * i);
var flipX = Phaser.Math.Between(0, 10) >= 5 ? -1 : 1;
var flipY = Phaser.Math.Between(0, 10) >= 5 ? -1 : 1;
layer.setScale(flipX * 2, flipY * 2);
layer.setDepth(-5 - (i - 1));
this.scene.physics.world.enableBody(layer, 0);
layer.body.velocity.y = this.velocityY;

this.layers.add(layer);
}
}

update() {
if (this.layers.getChildren()[0].y > 0) {
for (var i = 0; i < this.layers.getChildren().length; i++) {
var layer = this.layers.getChildren()[i];
layer.y = (-layer.displayHeight) + (layer.displayHeight * i);
}
}
}
}
62 changes: 62 additions & 0 deletions js/SceneGameOver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
class SceneGameOver extends Phaser.Scene {
constructor() {
super({ key: "SceneGameOver" });
}
create() {

this.title = this.add.text(this.game.config.width * 0.5, 128, "GAME OVER", {
fontFamily: 'monospace',
fontSize: 48,
fontStyle: 'bold',
color: '#ffffff',
align: 'center'
});
this.title.setOrigin(0.5);

this.sfx = {
btnOver: this.sound.add("sndBtnOver"),
btnDown: this.sound.add("sndBtnDown")
};

this.btnRestart = this.add.sprite(
this.game.config.width * 0.5,
this.game.config.height * 0.5,
"sprBtnRestart"
);

this.btnRestart.setInteractive();

this.btnRestart.on("pointerover", function() {
this.btnRestart.setTexture("sprBtnRestartHover"); // set the button texture to sprBtnPlayHover
this.sfx.btnOver.play(); // play the button over sound
}, this);

this.btnRestart.on("pointerout", function() {
this.setTexture("sprBtnRestart");
});

this.btnRestart.on("pointerdown", function() {
this.btnRestart.setTexture("sprBtnRestartDown");
this.sfx.btnDown.play();
}, this);

this.btnRestart.on("pointerup", function() {
this.btnRestart.setTexture("sprBtnRestart");
this.scene.start("SceneMain");
}, this);

this.backgrounds = [];
for (var i = 0; i < 5; i++) {
var keys = ["sprBg0", "sprBg1"];
var key = keys[Phaser.Math.Between(0, keys.length - 1)];
var bg = new ScrollingBackground(this, key, i * 10);
this.backgrounds.push(bg);
}
}

update() {
for (var i = 0; i < this.backgrounds.length; i++) {
this.backgrounds[i].update();
}
}
}
Loading

0 comments on commit 321a972

Please sign in to comment.