Editing ScoreDisplay.js - match to UML
This commit is contained in:
parent
9c9865f149
commit
d27f84134e
|
@ -1,32 +1,28 @@
|
||||||
import { BULLET_SIZE, FONT_SIZE, TOT_SHOTS } from './Constants.js';
|
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';
|
import bullet from '../data/bullet.png';
|
||||||
|
|
||||||
|
|
||||||
class ScoreDisplay {
|
class ScoreDisplay {
|
||||||
constructor(initialBullets) {
|
constructor(initialBullets) {
|
||||||
this.bulletImg = null;
|
this.img = null;
|
||||||
this.shotLeft = initialBullets;
|
this.shotLeft = initialBullets;
|
||||||
this.score = 0;
|
this.score = 0;
|
||||||
|
|
||||||
loadImage(bullet, (img) => {
|
loadImage(bullet, (PImage) => {
|
||||||
this.bulletImg = img;
|
this.img = PImage;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
draw() {
|
draw() {
|
||||||
// Draw score on top-left
|
// score
|
||||||
textFont('Arial');
|
textFont('Arial');
|
||||||
textSize(25);
|
textSize(25);
|
||||||
fill(255, 0, 0);
|
fill(255, 0, 0);
|
||||||
textAlign(LEFT, TOP);
|
textAlign(LEFT, TOP);
|
||||||
text(`Score: ${this.score}`, 10, 10);
|
text(`Score: ${this.score}`, 10, 10);
|
||||||
|
|
||||||
if (!this.bulletImg) return;
|
// bullets
|
||||||
|
if (!this.img) return;
|
||||||
// Draw remaining bullets on top-right
|
|
||||||
const bulletSpacing = 20;
|
const bulletSpacing = 20;
|
||||||
const marginRight = 10;
|
const marginRight = 10;
|
||||||
const bulletsWidth = this.shotLeft * bulletSpacing;
|
const bulletsWidth = this.shotLeft * bulletSpacing;
|
||||||
|
@ -34,18 +30,30 @@ class ScoreDisplay {
|
||||||
let y = 10;
|
let y = 10;
|
||||||
|
|
||||||
for (let i = 0; i < this.shotLeft; i++) {
|
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) {
|
update(source, ...others) {
|
||||||
if (source === 'gun') {
|
if (source === 'gun') {
|
||||||
const [, , remainingShots] = others;
|
const [, , remainingShots] = others;
|
||||||
this.shotLeft = remainingShots;
|
this.setBullets(remainingShots);
|
||||||
} else if (source === 'target-hit') {
|
} else if (source === 'target-hit') {
|
||||||
const [points] = others;
|
const [points] = others;
|
||||||
this.score += points;
|
this.addScore(points);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,5 +23,4 @@ class Subject {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export { Subject };
|
export { Subject };
|
||||||
|
|
|
@ -27,7 +27,6 @@ function setup() {
|
||||||
gun.subscribe(target);
|
gun.subscribe(target);
|
||||||
target.subscribe(score);
|
target.subscribe(score);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -63,8 +62,8 @@ function keyPressed() {
|
||||||
// init the targets
|
// init the targets
|
||||||
function initTargets() {
|
function initTargets() {
|
||||||
// Create new targets from the factory
|
// Create new targets from the factory
|
||||||
// Remember to unsubscribe the previous targets and to subscribe the new ones
|
|
||||||
const factory = TargetFactory.getInstance();
|
const factory = TargetFactory.getInstance();
|
||||||
|
// Remember to unsubscribe the previous targets and to subscribe the new ones
|
||||||
gun.unsubscribeAll();
|
gun.unsubscribeAll();
|
||||||
|
|
||||||
targets = factory.getRandomTargets(MAX_TARGETS, TARGET_WIDTH, 200);
|
targets = factory.getRandomTargets(MAX_TARGETS, TARGET_WIDTH, 200);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user