From 035cd541a0c54d6bcf65642db777018f8de1ca6a Mon Sep 17 00:00:00 2001 From: Yewon Date: Sun, 18 May 2025 22:06:49 +0900 Subject: [PATCH] Adding Gun GUI - gun and bullet holes --- src/Gun.js | 77 +++++++++++++++++++++++++++++++++++++++++++---------- src/main.js | 6 ++++- 2 files changed, 68 insertions(+), 15 deletions(-) diff --git a/src/Gun.js b/src/Gun.js index e6d7d7a..49a693f 100644 --- a/src/Gun.js +++ b/src/Gun.js @@ -12,28 +12,77 @@ The Gun is the mouse. To draw it, hide the cursor with the noCursor function and */ -class Gun { - noCursor() { } - draw() { } - setup() { - shot = loadSound('data/shot.mp3'); - empty = loadSound('data/empty.mp3'); +class Gun extends Subject { + constructor(totalShots) { + super(); + this.cursorImg = null; + this.shotSound = null; + this.emptySound = null; + this.remainingShots = totalShots; + this.bullets = []; // array of Bullet objects } - // mousePressed() { - // if () { - // shot.play(); - // } else { // no bullets left - // empty.play(); - // } - // } + setup() { + this.cursorImg = loadImage(cursor); + this.shotSound = loadSound(shot); + this.emptySound = loadSound(empty); + } + draw() { + noCursor(); + + if (this.cursorImg) { + image(this.cursorImg, mouseX, mouseY, CURSOR_SIZE, CURSOR_SIZE); + } // custom cursor + + for (let bullet of this.bullets) { + bullet.draw(); + } // draw bullet holes + + this.bullets = this.bullets.filter(bullet => bullet.visible); // clean up bullet holes + } + + shoot() { + if (this.remainingShots > 0) { + this.remainingShots--; + + if (this.shotSound) this.shotSound.play(); + + const x = mouseX; + const y = mouseY; + + const bullet = new Bullet(x, y); + this.bullets.push(bullet); + + this.notifySubscribers('gun', x, y, this.remainingShots); + } else { + if (this.emptySound) this.emptySound.play(); + } + } } // Bullet class Bullet { + constructor(x, y) { + this.x = x; + this.y = y; + this.visible = true; - // TO DO + this.img = null; + loadImage(bulletHole, (img) => { + this.img = img; + }); + + setTimeout(() => { + this.visible = false; + }, BULLET_DURATION); + } + + draw() { + if (this.visible && this.img) { + image(this.img, this.x - BULLET_HOLE_SIZE / 2, this.y - BULLET_HOLE_SIZE / 2, BULLET_HOLE_SIZE, BULLET_HOLE_SIZE); + } + } } diff --git a/src/main.js b/src/main.js index f2bc57e..40b9076 100644 --- a/src/main.js +++ b/src/main.js @@ -14,6 +14,8 @@ function setup() { createCanvas(800, 600); // 1. Init gun and score display + gun = new Gun(TOT_SHOTS); + gun.setup(); score = new ScoreDisplay(TOT_SHOTS); // 2. Init the targets @@ -22,13 +24,15 @@ function setup() { function draw() { background('#eeeeee'); + gun.draw(); score.draw(); // draw targets, gun, bullets, score - + } // Shoot function mousePressed() { + gun.shoot(); // shoot }