| 1 | // Bread module - handles bread bit spawning and behavior (p5.js version) |
| 2 | |
| 3 | export class BreadBit { |
| 4 | constructor(p, x, y) { |
| 5 | this.p = p |
| 6 | this.x = x |
| 7 | this.y = y |
| 8 | this.size = p.random(4, 8) |
| 9 | this.eaten = false |
| 10 | this.bobOffset = p.random(1000) |
| 11 | this.driftX = p.random(-0.1, 0.1) |
| 12 | this.driftY = p.random(-0.05, 0.05) |
| 13 | this.fadeAlpha = 255 |
| 14 | } |
| 15 | |
| 16 | update() { |
| 17 | if (this.eaten) { |
| 18 | this.fadeAlpha -= 15 |
| 19 | return |
| 20 | } |
| 21 | this.x += this.driftX |
| 22 | this.y += this.driftY |
| 23 | } |
| 24 | |
| 25 | isDone() { |
| 26 | return this.eaten && this.fadeAlpha <= 0 |
| 27 | } |
| 28 | |
| 29 | draw() { |
| 30 | const p = this.p |
| 31 | const bob = Math.sin((p.frameCount + this.bobOffset) * 0.08) * 2 |
| 32 | |
| 33 | p.push() |
| 34 | p.translate(this.x, this.y + bob) |
| 35 | |
| 36 | const outlineAlpha = Math.min(this.fadeAlpha, 255) |
| 37 | |
| 38 | p.stroke(25, 20, 15, outlineAlpha) |
| 39 | p.strokeWeight(1.5) |
| 40 | p.fill(235, 195, 130, this.fadeAlpha) |
| 41 | p.ellipse(0, 0, this.size + 2, this.size * 0.75) |
| 42 | |
| 43 | p.noStroke() |
| 44 | p.fill(255, 230, 180, this.fadeAlpha * 0.9) |
| 45 | p.ellipse(-this.size * 0.15, -this.size * 0.15, this.size * 0.5, this.size * 0.35) |
| 46 | |
| 47 | p.pop() |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | export class BreadManager { |
| 52 | constructor(p) { |
| 53 | this.p = p |
| 54 | this.bits = [] |
| 55 | } |
| 56 | |
| 57 | spawnBread(x, y, count = null) { |
| 58 | const p = this.p |
| 59 | const numBits = count || Math.floor(p.random(3, 6)) |
| 60 | |
| 61 | for (let i = 0; i < numBits; i++) { |
| 62 | const offsetX = p.random(-15, 15) |
| 63 | const offsetY = p.random(-15, 15) |
| 64 | this.bits.push(new BreadBit(p, x + offsetX, y + offsetY)) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | update() { |
| 69 | for (let i = this.bits.length - 1; i >= 0; i--) { |
| 70 | this.bits[i].update() |
| 71 | if (this.bits[i].isDone()) { |
| 72 | this.bits.splice(i, 1) |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | draw() { |
| 78 | for (const bit of this.bits) { |
| 79 | bit.draw() |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | getActiveBits() { |
| 84 | return this.bits.filter(b => !b.eaten) |
| 85 | } |
| 86 | } |