xxxxxxxxxx
189
// // // Globals
let menu;
let game;
// // Sprites
let shipSprite;
let shipBullet;
let enemySprite;
let enemyBullet;
// // // Audio
// let menu_bgm;
// let game_bgm;
// let win_bgm;
// let lose_bgm;
// let player_shoot;
// let player_hurt;
// let player_death;
// let player_win;
// // // Scene
function preload() {
shipSprite = loadImage('/img/spaceship.png');
shipBullet = loadImage('/img/bullet.png');
enemySprite = loadImage('/img/enemy.png'); // temp
}
function setup() {
createCanvas(500, 480);
imageMode(CENTER);
game = new Game();
}
function draw() {
background(20);
game.display();
}
// // // Classes
class Player {
constructor(x, y, sprite) {
// // Gameplay attributes
this.x = x;
this.y = y;
this.vx = 0;
this.vy = 0;
this.radius = 64;
this.sprite = sprite;
this.firerate = 1;
this.health = 5;
this.firing = false;
this.shielded = false;
this.bullets = [];
// // // Sounds // remove and use globals?
// this.shoot_sound = sound;
// this.hurt_sound = sound;
// this.death_sound = sound;
}
display() {
this.update();
image(this.sprite, this.x, this.y, 128, 128);
// circle(this.x, this.y, this.radius); // temp
for (let i = 0; i < this.bullets.length; i++) {
this.bullets[i].display();
}
}
distance() {}
collision() {}
update() {
// if (!game.gameover) {
// // Movement
if (keyIsDown(LEFT_ARROW) === true) {this.vx = -4}
else if (keyIsDown(RIGHT_ARROW) === true) {this.vx=4}
else {this.vx = 0}
if (keyIsDown(UP_ARROW) === true) {this.vy = -3}
else if (keyIsDown(DOWN_ARROW) === true) {this.vy=3}
else {this.vy = 0}
this.x += this.vx;
this.y += this.vy;
// // Combat behaviour
if (keyIsDown(32)) {this.firing = true}
else {this.firing = false}
if (this.firing && millis() % 10 == 0) {
append(this.bullets, new Bullet(this.x,
this.y - this.radius, true));
// console.log(this.bullets.length);
}
}
// }
}
class Enemy {
constructor(x, y) {
// // Gameplay attributes
this.x = x;
this.y = y;
this.radius = 64;
this.sprite = enemySprite;
this.firerate = 1;
this.health = 3;
}
display() {
this.update();
image(this.sprite, this.x, this.y, 64, 64);
// circle(this.x, this.y, this.radius); // temp
}
update() {
// this.y += 1;
}
}
class Bullet {
constructor(x, y, friendly=false) {
this.x = x;
this.y = y;
this.radius = 24;
if (friendly)
this.sprite = shipBullet;
else
this.sprite = enemyBullet;
this.spent = false;
}
display() {
this.update();
image(this.sprite, this.x, this.y, 24, 32);
// circle(this.x, this.y, this.radius); // temp
}
update() {
this.y += this.sprite == shipBullet ? -2 : 2;
// if (this.y < -100) {
// remove self from array
// }
// if (check_collision()) {this.spent = true}
}
check_collision() {}
}
class Game {
constructor() {
this.gameover = false;
this.score = 0;
this.time = 0;
this.start = millis();
this.enemies = [new Enemy(200, 100)];
this.enemy_bullets = [];
this.player = new Player(width/2,
height*0.75, shipSprite);
this.current_scene = 0;
}
display() {
this.update();
// // Gameplay
fill(255);
text("Score: " + this.score, width-80, 15);
text("Time: " + this.time, width-80, 30);
this.player.display();
for (let i = 0; i < this.enemies.length; i++) {
this.enemies[i].display();
}
// for (let i=0; i < this.enemy_bullets.length; i++){
// this.enemy_bullets[i].display();
// }
}
update() {
// // Check game state // temp
if (this.player.health <= 0 || this.player.y < -100) {
this.gameover = true;
}
if (this.gameover) {
// // Determine win/loss based on existing health
if (this.player.health > 0) {
console.log("Win");
}
else {
console.log("Lose");
}
return;
}
// // Game continues
this.time = Math.floor((millis() - this.start) / 1000);
}
}
// // // Helpers
function checkCollision(one, two) {
if (dist(one.x, one.y, two.x, two.y) <=
(one.radius + two.radius)) {return true}
return false;
}