From f688f75ce6b3c7eb93954af717c9e99b8007a6af Mon Sep 17 00:00:00 2001 From: Yewon Date: Sun, 18 May 2025 23:43:32 +0900 Subject: [PATCH] Editing Target.js - match to UML --- src/Target.js | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/Target.js b/src/Target.js index ac723e8..c26e359 100644 --- a/src/Target.js +++ b/src/Target.js @@ -1,23 +1,23 @@ import { MAX_TARGETS, TARGET_WIDTH } from './Constants'; import { Subject } from './Subject'; -import { Gun } from './Gun'; - import teddy from '../data/teddy.png'; import duck from '../data/duck.png'; import squirrel from '../data/squirrel.png'; class Target extends Subject { - constructor(x, y, img) { + constructor(x, y, width) { super(); this.x = x; this.y = y; - this.img = img; + this.width = width; + this.height = width; this.visible = true; + this.img = null; } draw() { if (this.visible && this.img) { - image(this.img, this.x, this.y, TARGET_WIDTH, TARGET_WIDTH); + image(this.img, this.x, this.y, this.width, this.height); } } @@ -25,34 +25,35 @@ class Target extends Subject { return 0; // to be overridden } - hit() { - if (this.visible) { + isHit(x, y) { + return ( + this.visible && + x >= this.x && + x <= this.x + this.width && + y >= this.y && + y <= this.y + this.height + ); + } + + shoot(x, y) { + if (this.isHit(x, y)) { 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; - } - update(source, ...others) { if (source === 'gun') { const [x, y] = others; - if (this.contains(x, y)) { - this.hit(); - } + this.shoot(x, y); } } } class TeddyTarget extends Target { constructor(x, y) { - super(x, y, loadImage(teddy)); + super(x, y, TARGET_WIDTH); + this.img = loadImage(teddy); } getPoints() { @@ -62,7 +63,8 @@ class TeddyTarget extends Target { class DuckTarget extends Target { constructor(x, y) { - super(x, y, loadImage(duck)); + super(x, y, TARGET_WIDTH); + this.img = loadImage(duck); } getPoints() { @@ -72,7 +74,8 @@ class DuckTarget extends Target { class SquirrelTarget extends Target { constructor(x, y) { - super(x, y, loadImage(squirrel)); + super(x, y, TARGET_WIDTH); + this.img = loadImage(squirrel); } getPoints() {