From 04e887f7062f4bb32a699536386562aa082c6218 Mon Sep 17 00:00:00 2001 From: Yewon Date: Sun, 18 May 2025 23:50:24 +0900 Subject: [PATCH] Editing Gun.js - match to UML --- src/Gun.js | 22 ++++++++++++++-------- src/ScoreDisplay.js | 4 ++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/Gun.js b/src/Gun.js index 729820f..ad86e0c 100644 --- a/src/Gun.js +++ b/src/Gun.js @@ -7,31 +7,29 @@ import bulletHole from '../data/bulletHole.png'; import shot from '../data/shot.mp3'; import empty from '../data/empty.mp3'; -/* -The Gun is the mouse. To draw it, hide the cursor with the noCursor function and then draw instead the image cursor.png at the location of the mouse. To load and play sounds with p5.js take a look at this example. The sound files are shot.mp3 and empty.mp3, depending on whether there are still shots available. Finally, when you shoot with the gun, add a random CURSOR_SIZE noise (or a similarly small amount) to alter the bullet's final hitting location. -*/ - class Gun extends Subject { constructor(totShots) { super(); - this.cursorImg = null; + this.cursor = null; this.shotSound = null; this.emptySound = null; + + this.totshots = totShots; this.remainingShots = totShots; this.bullets = []; // array of Bullet objects } setup() { - this.cursorImg = loadImage(cursor); + this.cursor = loadImage(cursor); this.shotSound = loadSound(shot); this.emptySound = loadSound(empty); } draw() { noCursor(); - if (this.cursorImg) { - image(this.cursorImg, mouseX - CURSOR_SIZE / 2, mouseY - CURSOR_SIZE / 2, CURSOR_SIZE, CURSOR_SIZE); + if (this.cursor) { + image(this.cursor, mouseX - CURSOR_SIZE / 2, mouseY - CURSOR_SIZE / 2, CURSOR_SIZE, CURSOR_SIZE); } // custom cursor for (let bullet of this.bullets) { @@ -58,6 +56,14 @@ class Gun extends Subject { if (this.emptySound) this.emptySound.play(); } } + + reload() { + this.remainingShots = this.totShots; + } + + getRemainingShots() { + return this.remainingShots; + } } diff --git a/src/ScoreDisplay.js b/src/ScoreDisplay.js index f057678..3eb416c 100644 --- a/src/ScoreDisplay.js +++ b/src/ScoreDisplay.js @@ -1,4 +1,4 @@ -import { BULLET_SIZE, FONT_SIZE, TOT_SHOTS } from './Constants.js'; +import { BULLET_SIZE, FONT_SIZE } from './Constants.js'; import bullet from '../data/bullet.png'; @@ -16,7 +16,7 @@ class ScoreDisplay { draw() { // score textFont('Arial'); - textSize(25); + textSize(FONT_SIZE); fill(255, 0, 0); textAlign(LEFT, TOP); text(`Score: ${this.score}`, 10, 10);