Editing ScoreDisplay.js - match to UML

This commit is contained in:
Yewon Kim 2025-05-18 23:36:18 +09:00
parent 9c9865f149
commit d27f84134e
3 changed files with 22 additions and 16 deletions

View File

@ -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);
}
}
}

View File

@ -23,5 +23,4 @@ class Subject {
}
}
export { Subject };

View File

@ -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);