From 6405deb3383d68fef99562d0af77a36e4fcd9e36 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Mon, 18 Nov 2019 03:51:00 +0000 Subject: [PATCH] display scores --- public/bank.js | 37 +++++++++++++++++--- public/game.js | 90 ++++++++++++++++++++++++++++++++++++------------ public/host.js | 5 +-- public/money.js | 4 +++ public/player.js | 69 ++++++++++++++++++++++++++++++++----- public/store.js | 38 +++++++++++++++++--- 6 files changed, 199 insertions(+), 44 deletions(-) diff --git a/public/bank.js b/public/bank.js index d379d7a..6bf540f 100644 --- a/public/bank.js +++ b/public/bank.js @@ -1,15 +1,42 @@ 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); let purple = color(Colors.Eggplant); noFill(); stroke(purple); strokeWeight(2); - rect(x, y, w, h); - image(Images.bank, x, y, w, h); + rect(corner.x, corner.y, this.width, this.height); + 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; diff --git a/public/game.js b/public/game.js index 28cc445..ca35082 100644 --- a/public/game.js +++ b/public/game.js @@ -15,8 +15,18 @@ class Game { this.zombies = []; this.burgers = []; this.moneys = []; - this.store = new Store(); - this.bank = new Bank(); + this.store = new Store({ + 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) { @@ -37,9 +47,25 @@ class Game { update() { this.burgers.forEach(b => b.update()); this.zombies.forEach(z => z.update()); + for (let id in this.players) { let player = this.players[id]; 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) { @@ -57,6 +83,7 @@ class Game { this.zombies = this.zombies.filter(z => !z.isDead()); this.burgers = this.burgers.filter(b => !b.isDead()); + this.moneys = this.moneys.filter(m => m.alive); } draw() { @@ -70,6 +97,37 @@ class Game { this.zombies.forEach(z => z.draw()); this.burgers.forEach(b => b.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) { @@ -82,29 +140,17 @@ class Game { 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) { console.log(["game sees button input", [id, val]]); let player = this.players[id]; - if (player) { - console.log(["button input has player", player]); - player.buttonInput(val); + if (player && val) { + if (player.overlaps(this.store)) { + player.buyBurger(); + } else if (player.overlaps(this.bank)) { + player.makeDeposit(); + } else { + player.throwBurger(); + } } } diff --git a/public/host.js b/public/host.js index f0f6843..2368792 100644 --- a/public/host.js +++ b/public/host.js @@ -23,7 +23,7 @@ const local = false; // true if running locally, false // Global variables here. ----> const velScale = 10; -const debug = true; +const debug = false; let game; // <---- @@ -53,9 +53,6 @@ function draw () { if(isHostConnected(display=true)) { // Host/Game draw here. ---> - // Display player IDs in top left corner - game.printPlayerIds(5, 20); - // Update and draw game objects game.draw(); diff --git a/public/money.js b/public/money.js index cf36cd4..09a741d 100644 --- a/public/money.js +++ b/public/money.js @@ -25,6 +25,10 @@ class Money { rect(this.position.x - half.width, this.position.y - half.height, this.width, this.height); } } + + kill() { + this.alive = false; + } } Money.prototype.bounds = bounds; diff --git a/public/player.js b/public/player.js index f86aa6f..e844ed3 100644 --- a/public/player.js +++ b/public/player.js @@ -7,6 +7,10 @@ class Player { this.width = options.width || 64; this.height = options.height || 64; this.burgers = 5; + this.maxBurgers = 12; + this.moneys = 0; + this.maxMoneys = 20; + this.score = 0; this.position = {x: options.x, y: options.y}; this.velocity = {x: 0, y: 0}; } @@ -22,8 +26,16 @@ class Player { 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.Purple); image(this.image, corner.x, corner.y, this.width, this.height); + + this.drawBurgerMeter(); + this.drawMoneyMeter(); + if (debug) { strokeWeight(4); 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) { this.velocity.x = x * 200; this.velocity.y = y * -200; } - buttonInput(val) { - console.log(["player sees button input val", val]); - if (val) { - if (this.burgers > 0) { - this.burgers--; - this.game.addBurger(this.id); - } else { + buyBurger() { + if (this.moneys > 0 && (this.burgers+2) <= this.maxBurgers) { + this.moneys--; + this.burgers+=2; + } + } + + makeDeposit() { + if (this.moneys > 0) { + this.moneys--; + this.score++; + } + } + + throwBurger() { + if (this.burgers > 0) { + this.burgers--; + this.game.addBurger(this.id); + } else { - } } } } diff --git a/public/store.js b/public/store.js index 830e408..d48ab0a 100644 --- a/public/store.js +++ b/public/store.js @@ -1,15 +1,43 @@ 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); let purple = color(Colors.Eggplant); noFill(); + stroke(purple); strokeWeight(2); - rect(x, y, w, h); - image(Images.restaurant, x, y, w, h); + rect(corner.x, corner.y, this.width, this.height); + 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;