display scores

master
Jordan Orelli 5 years ago
parent 42165639f2
commit 6405deb338

@ -1,15 +1,42 @@
class Bank { class Bank {
constructor() { constructor(options) {
options = options || {};
this.position = {
x: options.x,
y: options.y
};
this.width = options.width;
this.height = options.height;
} }
draw(x, y, w, h) { draw() {
let corner = {
x: this.position.x - this.width*0.5,
y: this.position.y - this.height*0.5
};
let half = {
width: this.width * 0.5,
height: this.height * 0.5,
};
tint(Colors.Eggplant); tint(Colors.Eggplant);
let purple = color(Colors.Eggplant); let purple = color(Colors.Eggplant);
noFill(); noFill();
stroke(purple); stroke(purple);
strokeWeight(2); strokeWeight(2);
rect(x, y, w, h); rect(corner.x, corner.y, this.width, this.height);
image(Images.bank, x, y, w, h); image(Images.bank, corner.x, corner.y, this.width, this.height);
if (debug) {
strokeWeight(4);
stroke('#FF0000');
point(this.position.x, this.position.y);
strokeWeight(1);
noFill();
rect(corner.x, corner.y, this.width, this.height);
}
} }
} }
Bank.prototype.bounds = bounds;
Bank.prototype.outOfBounds = outOfBounds;
Bank.prototype.overlaps = overlaps;

@ -15,8 +15,18 @@ class Game {
this.zombies = []; this.zombies = [];
this.burgers = []; this.burgers = [];
this.moneys = []; this.moneys = [];
this.store = new Store(); this.store = new Store({
this.bank = new Bank(); x: width * 0.25,
y: height - 128,
width: 128,
height: 128,
});
this.bank = new Bank({
x: width * 0.75,
y: height - 128,
width: 128,
height: 128,
});
} }
add (id, x, y, w, h) { add (id, x, y, w, h) {
@ -37,9 +47,25 @@ class Game {
update() { update() {
this.burgers.forEach(b => b.update()); this.burgers.forEach(b => b.update());
this.zombies.forEach(z => z.update()); this.zombies.forEach(z => z.update());
for (let id in this.players) { for (let id in this.players) {
let player = this.players[id]; let player = this.players[id];
player.update(); player.update();
for (let zombie of this.zombies) {
if (player.overlaps(zombie)) {
zombie.kill();
player.kill();
}
}
for (let money of this.moneys) {
if (player.moneys < player.maxMoneys && player.overlaps(money)) {
money.kill();
player.moneys++;
console.log(["player picked up money", player]);
}
}
} }
for (let burger of this.burgers) { for (let burger of this.burgers) {
@ -57,6 +83,7 @@ class Game {
this.zombies = this.zombies.filter(z => !z.isDead()); this.zombies = this.zombies.filter(z => !z.isDead());
this.burgers = this.burgers.filter(b => !b.isDead()); this.burgers = this.burgers.filter(b => !b.isDead());
this.moneys = this.moneys.filter(m => m.alive);
} }
draw() { draw() {
@ -70,6 +97,37 @@ class Game {
this.zombies.forEach(z => z.draw()); this.zombies.forEach(z => z.draw());
this.burgers.forEach(b => b.draw()); this.burgers.forEach(b => b.draw());
this.moneys.forEach(m => m.draw()); this.moneys.forEach(m => m.draw());
this.drawScores();
}
drawScores() {
if (this.numPlayers == 0) {
return;
}
let x = 32;
let y = 90;
let rowHeight = 32;
let rowWidth = 128;
let row = 0;
for (let id in this.players) {
let player = this.players[id];
tint(Colors.Purple);
image(player.image, x, y + row * rowHeight, 32, 32);
textSize(24);
fill(Colors.Purple);
noStroke();
textAlign(LEFT, CENTER);
text(player.score, x + 32 + 10, y + rowHeight*0.5);
row++;
}
noFill();
stroke(Colors.Purple);
strokeWeight(2);
rect(x, y, rowWidth, rowHeight * row, 4, 4, 4, 4);
} }
remove (id) { remove (id) {
@ -82,29 +140,17 @@ class Game {
else { return false; } else { return false; }
} }
printPlayerIds (x, y) {
push();
noStroke();
fill(Colors.Eggplant);
textSize(16);
text("# players: " + this.numPlayers, x, y);
y = y + 16;
fill(Colors.Eggplant);
for (let id in this.players) {
text(this.players[id].id, x, y);
y += 16;
}
pop();
}
buttonInput(id, val) { buttonInput(id, val) {
console.log(["game sees button input", [id, val]]); console.log(["game sees button input", [id, val]]);
let player = this.players[id]; let player = this.players[id];
if (player) { if (player && val) {
console.log(["button input has player", player]); if (player.overlaps(this.store)) {
player.buttonInput(val); player.buyBurger();
} else if (player.overlaps(this.bank)) {
player.makeDeposit();
} else {
player.throwBurger();
}
} }
} }

@ -23,7 +23,7 @@ const local = false; // true if running locally, false
// Global variables here. ----> // Global variables here. ---->
const velScale = 10; const velScale = 10;
const debug = true; const debug = false;
let game; let game;
// <---- // <----
@ -53,9 +53,6 @@ function draw () {
if(isHostConnected(display=true)) { if(isHostConnected(display=true)) {
// Host/Game draw here. ---> // Host/Game draw here. --->
// Display player IDs in top left corner
game.printPlayerIds(5, 20);
// Update and draw game objects // Update and draw game objects
game.draw(); game.draw();

@ -25,6 +25,10 @@ class Money {
rect(this.position.x - half.width, this.position.y - half.height, this.width, this.height); rect(this.position.x - half.width, this.position.y - half.height, this.width, this.height);
} }
} }
kill() {
this.alive = false;
}
} }
Money.prototype.bounds = bounds; Money.prototype.bounds = bounds;

@ -7,6 +7,10 @@ class Player {
this.width = options.width || 64; this.width = options.width || 64;
this.height = options.height || 64; this.height = options.height || 64;
this.burgers = 5; this.burgers = 5;
this.maxBurgers = 12;
this.moneys = 0;
this.maxMoneys = 20;
this.score = 0;
this.position = {x: options.x, y: options.y}; this.position = {x: options.x, y: options.y};
this.velocity = {x: 0, y: 0}; this.velocity = {x: 0, y: 0};
} }
@ -22,8 +26,16 @@ class Player {
x: this.position.x - this.width*0.5, x: this.position.x - this.width*0.5,
y: this.position.y - this.height*0.5 y: this.position.y - this.height*0.5
}; };
let half = {
width: this.width * 0.5,
height: this.height * 0.5,
};
tint(Colors.Purple); tint(Colors.Purple);
image(this.image, corner.x, corner.y, this.width, this.height); image(this.image, corner.x, corner.y, this.width, this.height);
this.drawBurgerMeter();
this.drawMoneyMeter();
if (debug) { if (debug) {
strokeWeight(4); strokeWeight(4);
stroke('#FF0000'); stroke('#FF0000');
@ -35,20 +47,61 @@ class Player {
} }
} }
drawBurgerMeter() {
noStroke();
fill(Colors.Purple);
let meterWidth = this.width;
let meterHeight = this.height * 0.2;
let cellWidth = meterWidth / this.maxBurgers;
for (let i = 0; i < this.burgers; i++) {
let x = this.position.x - this.width * 0.5 + i * cellWidth;
let y = this.position.y + this.height + 10;
rect(x + cellWidth * 0.1, y, cellWidth*0.8, 10);
}
}
drawMoneyMeter() {
noStroke();
fill(Colors.DarkGreen);
let meterWidth = this.width;
let meterHeight = this.height * 0.2;
let cellWidth = meterWidth / this.maxMoneys;
for (let i = 0; i < this.moneys; i++) {
let x = this.position.x - this.width * 0.5 + i * cellWidth;
let y = this.position.y + this.height + 30;
rect(x + cellWidth * 0.1, y, cellWidth*0.8, 10);
}
}
kill() {
// something here
}
joystickInput(x, y) { joystickInput(x, y) {
this.velocity.x = x * 200; this.velocity.x = x * 200;
this.velocity.y = y * -200; this.velocity.y = y * -200;
} }
buttonInput(val) { buyBurger() {
console.log(["player sees button input val", val]); if (this.moneys > 0 && (this.burgers+2) <= this.maxBurgers) {
if (val) { this.moneys--;
if (this.burgers > 0) { this.burgers+=2;
this.burgers--; }
this.game.addBurger(this.id); }
} else {
makeDeposit() {
if (this.moneys > 0) {
this.moneys--;
this.score++;
}
}
throwBurger() {
if (this.burgers > 0) {
this.burgers--;
this.game.addBurger(this.id);
} else {
}
} }
} }
} }

@ -1,15 +1,43 @@
class Store { class Store {
constructor() { constructor(options) {
options = options || {};
this.position = {
x: options.x,
y: options.y
};
this.width = options.width;
this.height = options.height;
} }
draw(x, y, w, h) { draw() {
let corner = {
x: this.position.x - this.width*0.5,
y: this.position.y - this.height*0.5
};
let half = {
width: this.width * 0.5,
height: this.height * 0.5,
};
tint(Colors.Eggplant); tint(Colors.Eggplant);
let purple = color(Colors.Eggplant); let purple = color(Colors.Eggplant);
noFill(); noFill();
stroke(purple); stroke(purple);
strokeWeight(2); strokeWeight(2);
rect(x, y, w, h); rect(corner.x, corner.y, this.width, this.height);
image(Images.restaurant, x, y, w, h); image(Images.restaurant, corner.x, corner.y, this.width, this.height);
if (debug) {
strokeWeight(4);
stroke('#FF0000');
point(this.position.x, this.position.y);
strokeWeight(1);
noFill();
rect(corner.x, corner.y, this.width, this.height);
}
} }
} }
Store.prototype.bounds = bounds;
Store.prototype.outOfBounds = outOfBounds;
Store.prototype.overlaps = overlaps;

Loading…
Cancel
Save