diff --git a/src/ScoreDisplay.js b/src/ScoreDisplay.js index 7cffb31..f057678 100644 --- a/src/ScoreDisplay.js +++ b/src/ScoreDisplay.js @@ -1,32 +1,28 @@ import { BULLET_SIZE, FONT_SIZE, TOT_SHOTS } from './Constants.js'; -import { Gun } from './Gun.js'; -import { Target } from './Target'; - import bullet from '../data/bullet.png'; class ScoreDisplay { constructor(initialBullets) { - this.bulletImg = null; + this.img = null; this.shotLeft = initialBullets; this.score = 0; - loadImage(bullet, (img) => { - this.bulletImg = img; + loadImage(bullet, (PImage) => { + this.img = PImage; }); } draw() { - // Draw score on top-left + // score textFont('Arial'); textSize(25); fill(255, 0, 0); textAlign(LEFT, TOP); text(`Score: ${this.score}`, 10, 10); - if (!this.bulletImg) return; - - // Draw remaining bullets on top-right + // bullets + if (!this.img) return; const bulletSpacing = 20; const marginRight = 10; const bulletsWidth = this.shotLeft * bulletSpacing; @@ -34,18 +30,30 @@ class ScoreDisplay { let y = 10; for (let i = 0; i < this.shotLeft; i++) { - image(this.bulletImg, startX + i * bulletSpacing, y, BULLET_SIZE, BULLET_SIZE); + image(this.img, startX + i * bulletSpacing, y, BULLET_SIZE, BULLET_SIZE); } } + resetScore() { + this.score = 0; + } + + addScore(scoreToAdd) { + this.score += scoreToAdd; + } + + setBullets(numBullets) { + this.shotLeft = numBullets; + } + update(source, ...others) { if (source === 'gun') { const [, , remainingShots] = others; - this.shotLeft = remainingShots; + this.setBullets(remainingShots); } else if (source === 'target-hit') { const [points] = others; - this.score += points; + this.addScore(points); } } } diff --git a/src/Subject.js b/src/Subject.js index 4defcce..ebd0bb0 100644 --- a/src/Subject.js +++ b/src/Subject.js @@ -23,5 +23,4 @@ class Subject { } } - export { Subject }; diff --git a/src/main.js b/src/main.js index 8170479..2a21ca0 100644 --- a/src/main.js +++ b/src/main.js @@ -27,7 +27,6 @@ function setup() { gun.subscribe(target); target.subscribe(score); } - } @@ -63,8 +62,8 @@ function keyPressed() { // init the targets function initTargets() { // Create new targets from the factory - // Remember to unsubscribe the previous targets and to subscribe the new ones const factory = TargetFactory.getInstance(); + // Remember to unsubscribe the previous targets and to subscribe the new ones gun.unsubscribeAll(); targets = factory.getRandomTargets(MAX_TARGETS, TARGET_WIDTH, 200);