Adding to Target.js - Target class, Target subclasses, TargetFactory

This commit is contained in:
Yewon Kim 2025-05-18 22:46:18 +09:00
parent 687009f8fc
commit 71aeb0057a
2 changed files with 118 additions and 9 deletions

View File

@ -1,3 +1,4 @@
import { MAX_TARGETS, TARGET_WIDTH } from './Constants';
import { Subject } from './Subject';
import { Gun } from './Gun';
@ -5,17 +6,118 @@ import teddy from '../data/teddy.png';
import duck from '../data/duck.png';
import squirrel from '../data/squirrel.png';
class Target {
// TO DO
class Target extends Subject {
constructor(x, y, img) {
super();
this.x = x;
this.y = y;
this.img = img;
this.visible = true;
}
draw() {
if (this.visible && this.img) {
image(this.img, this.x, this.y, TARGET_WIDTH, TARGET_WIDTH);
}
}
getPoints() {
return 0; // to be overridden
}
hit() {
if (this.visible) {
this.visible = false;
this.notifySubscribers('target-hit', this.getPoints());
}
}
contains(px, py) {
return this.visible &&
px >= this.x &&
px <= this.x + TARGET_WIDTH &&
py >= this.y &&
py <= this.y + TARGET_WIDTH;
}
}
// TO DO
// class TeddyTarget ...
// class DuckTarget ...
// class SquirrelTarget ...
class TeddyTarget extends Target {
constructor(x, y) {
super(x, y, loadImage(teddy));
}
getPoints() {
return 1;
}
}
class DuckTarget extends Target {
constructor(x, y) {
super(x, y, loadImage(duck));
}
getPoints() {
return 3;
}
}
class SquirrelTarget extends Target {
constructor(x, y) {
super(x, y, loadImage(squirrel));
}
getPoints() {
return 5;
}
}
class TargetFactory {
// TO DO
static instance;
static getInstance() {
if (!TargetFactory.instance) {
TargetFactory.instance = new TargetFactory();
}
return TargetFactory.instance;
}
getTargetsByName(targetNames, targetWidth, y) {
const targets = [];
for (let i = 0; i < targetNames.length && i < MAX_TARGETS; i += 1) {
const name = targetNames[i];
const x = 100 + i * targetWidth;
switch (name) {
case 'teddy':
targets.push(new TeddyTarget(x, y));
break;
case 'duck':
targets.push(new DuckTarget(x, y));
break;
case 'squirrel':
targets.push(new SquirrelTarget(x, y));
break;
}
}
return targets;
}
getRandomTargets(numTargets, targetWidth, y) {
const names = ['teddy', 'duck', 'squirrel'];
const targetNames = [];
for (let i = 0; i < numTargets; i++) {
targetNames.push(random(names));
}
return this.getTargetsByName(targetNames, targetWidth, y);
} // create random targets
}
export { Target, TargetFactory };
export {
Target, TeddyTarget,
DuckTarget,
SquirrelTarget, TargetFactory
};

View File

@ -18,12 +18,16 @@ function setup() {
gun.setup();
score = new ScoreDisplay(TOT_SHOTS);
// 2. Init the targets
initTargets();
// 3. Subscribe gun
}
function draw() {
background('#eeeeee');
for (let target of targets) {
target.draw();
}
gun.draw();
score.draw();
// draw targets, gun, bullets, score
@ -47,6 +51,9 @@ function keyPressed() {
function initTargets() {
// Create new targets from the factory
// Remember to unsubscribe the previous targets and to subscribe the new ones
const factory = TargetFactory.getInstance();
targets = factory.getRandomTargets(MAX_TARGETS, TARGET_WIDTH, 200);
}
// Do not touch these