GitLab system status is available here and here

Commit 95edb3ab authored by Ankie Pon's avatar Ankie Pon
Browse files

got up to parsing keyboard input

parent 24264f7f
......@@ -5,9 +5,17 @@
<meta https-equiv="X-UA-Compatible" content="IE-edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rocket Patrol</title>
<style>
body {
background-color: beige;
}
</style>
</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>
</body>
</html>
......@@ -2,8 +2,17 @@
let config = {
type: Phaser.CANVAS,
width: 640,
height: 480
height: 480,
scene: [ Menu, Play ]
}
//game declaration
let game = new Phaser.Game(config);
\ No newline at end of file
let game = new Phaser.Game(config);
//reserve keyboard vars
let keyF, keyR, keyLEFT, keyRIGHT;
//set UI sizes
let borderUISize= game.config.height / 15;
let borderPadding = borderUISize / 3;
//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;
}
}
//fire button
if(Phaser.Input.Keyboard.JustDown(keyF)) {
this.isFiring = ture;
}
//if fired move up
if(this.isFiring && this.y >= borderUISize * 3 + borderPadding) {
this.y -= this.moveSpeed;
}
//reset on miss
if(this.y <= borderUISize * 3 + borderPadding) {
this.isFiring = false;
this.y = game.config.height - borderUISize - borderPadding;
}
}
}
\ No newline at end of file
class Menu extends Phaser.Scene {
constructor() {
super("menuScene");
}
create() {
this.add.text(20, 20, "Rocket Patrol Menu");
this.scene.start("playScene");
}
}
\ No newline at end of file
class Play extends Phaser.Scene {
constructor() {
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 rocekt (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