GitLab system status is available here and here

Commit fdb2a841 authored by Aaron Gonzales's avatar Aaron Gonzales
Browse files

Added left/right movement and firing

parent 6a5af172
......@@ -13,6 +13,7 @@
</head>
<body>
<script src="./lib/phaser.js"></script>
<script src="./src/prefabs/Rocket.js"></script>
<script src="./src/scenes/Menu.js"></script>
<script src="./src/scenes/Play.js"></script>
<script src="./src/main.js"></script>
......
......@@ -7,6 +7,9 @@ let config = {
let game = new Phaser.Game(config);
// reserve keyboard vars
let keyF, keyR, keyLEFT, keyRIGHT;
// Define global variables for UI size
let borderUISize = game.config.height / 15;
let borderPadding = borderUISize / 3;
\ No newline at end of file
// Rocket prefab
class Rocket extends Phaser.GameObjects.Sprite {
constructor(scene, x, y, texture, frame) {
super(scene, x, y, texture, frame);
// add object to existing scene
scene.add.existing(this);
this.isFiring = false;
this.moveSpeed = 2;
}
update() {
// Left/Right Movement
if (!this.isFiring) {
if (keyLEFT.isDown && this.x >= borderUISize + this.width) {
this.x -= this.moveSpeed;
} else if (keyRIGHT.isDown && this.x <= game.config.width - borderUISize - this.width) {
this.x += this.moveSpeed;
}
}
// Firing
if (Phaser.Input.Keyboard.JustDown(keyF)) {
this.isFiring = true;
}
// If fired, move sprite upward
if (this.isFiring && this.y >= borderUISize * 3 + borderPadding) {
this.y -= this.moveSpeed;
}
// If missed, reset position of sprite
if (this.y <= borderUISize * 3 + borderPadding) {
this.isFiring = false;
this.y = game.config.height - borderUISize - borderPadding;
}
}
}
\ No newline at end of file
......@@ -3,14 +3,38 @@ class Play extends Phaser.Scene {
super("playScene");
}
preload() {
// load images/tile sprites
this.load.image('rocket', './assets/rocket.png');
this.load.image('spaceship', './assets/spaceship.png');
this.load.image('starfield', './assets/starfield.png');
}
create() {
// place tile sprite
this.starfield = this.add.tileSprite(0, 0, 640, 480, 'starfield').setOrigin(0, 0);
// green UI background
this.add.rectangle(0, borderUISize + borderPadding, game.config.width, borderUISize * 2, 0x00FF00).setOrigin(0, 0);
// white borders
this.add.rectangle(0, 0, game.config.width, borderUISize, 0xFFFFFF).setOrigin(0, 0);
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);
// 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);
}
update() {
this.starfield.tilePositionX -= 4;
this.p1Rocket.update();
}
}
\ 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