Editing Gun.js - match to UML

This commit is contained in:
Yewon Kim 2025-05-18 23:50:24 +09:00
parent f688f75ce6
commit 04e887f706
2 changed files with 16 additions and 10 deletions

View File

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

View File

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