|
| 1 | +import type p5 from "p5"; |
| 2 | + |
| 3 | +export class FallingChar { |
| 4 | + x: number; |
| 5 | + y: number; |
| 6 | + char: string; |
| 7 | + speed: number; |
| 8 | + isBackground: boolean; |
| 9 | + color: string; |
| 10 | + size: number; |
| 11 | + opacity: number; |
| 12 | + toDelete: boolean; |
| 13 | + koreanPixelFont: p5.Font; |
| 14 | + isGrowing: boolean; |
| 15 | + growthStartTime: number; |
| 16 | + growthDuration: number; |
| 17 | + wasSuccessfullyHit: boolean; |
| 18 | + private p: p5; |
| 19 | + |
| 20 | + constructor( |
| 21 | + x: number, |
| 22 | + char: string, |
| 23 | + speed: number, |
| 24 | + koreanPixelFont: p5.Font, |
| 25 | + p: p5, |
| 26 | + isBackground = false, |
| 27 | + ) { |
| 28 | + this.x = x; |
| 29 | + this.y = -30; |
| 30 | + this.char = char; |
| 31 | + this.speed = speed; |
| 32 | + this.isBackground = isBackground; |
| 33 | + this.color = isBackground ? this.getRandomColor() : "#FFFFFF"; |
| 34 | + this.size = isBackground ? 24 : 32; |
| 35 | + this.opacity = isBackground ? 0.3 : 1; |
| 36 | + this.toDelete = false; |
| 37 | + this.koreanPixelFont = koreanPixelFont; |
| 38 | + this.isGrowing = false; |
| 39 | + this.growthStartTime = 0; |
| 40 | + this.growthDuration = 200; // Duration of growth animation in ms |
| 41 | + this.wasSuccessfullyHit = false; |
| 42 | + this.p = p; |
| 43 | + } |
| 44 | + |
| 45 | + getRandomColor(): string { |
| 46 | + const colors = [ |
| 47 | + "#00ff00", // Neon green |
| 48 | + "#ff0066", // Hot pink |
| 49 | + "#00ffff", // Cyan |
| 50 | + "#ffff00", // Yellow |
| 51 | + "#ff3399", // Pink |
| 52 | + "#33ccff", // Light blue |
| 53 | + ]; |
| 54 | + return colors[Math.floor(Math.random() * colors.length)]; |
| 55 | + } |
| 56 | + |
| 57 | + update(): void { |
| 58 | + this.y += this.speed; |
| 59 | + |
| 60 | + if (this.isGrowing) { |
| 61 | + const currentTime = performance.now(); |
| 62 | + const elapsed = currentTime - this.growthStartTime; |
| 63 | + const progress = Math.min(elapsed / this.growthDuration, 1); |
| 64 | + |
| 65 | + // Ease out cubic function for smooth growth |
| 66 | + const easedProgress = 1 - Math.pow(1 - progress, 3); |
| 67 | + this.size = 32 + easedProgress * 48; // Grow from 32 to 80 |
| 68 | + |
| 69 | + // Interpolate color to green |
| 70 | + const startColor = this.p.color(this.color); |
| 71 | + const endColor = this.p.color("#00ff00"); |
| 72 | + this.color = this.p.lerpColor(startColor, endColor, easedProgress).toString(); |
| 73 | + |
| 74 | + if (progress >= 1) { |
| 75 | + this.toDelete = true; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + startGrowth(): void { |
| 81 | + this.isGrowing = true; |
| 82 | + this.growthStartTime = performance.now(); |
| 83 | + } |
| 84 | + |
| 85 | + draw(p: p5): void { |
| 86 | + p.push(); |
| 87 | + p.textSize(this.size); |
| 88 | + p.textAlign(p.CENTER, p.CENTER); |
| 89 | + p.textStyle(p.BOLD); |
| 90 | + p.textFont(this.koreanPixelFont); |
| 91 | + |
| 92 | + // Draw outer glow |
| 93 | + if (!this.isBackground) { |
| 94 | + const glowColor = p.color(this.color); |
| 95 | + glowColor.setAlpha(this.opacity * 80); |
| 96 | + p.fill(glowColor); |
| 97 | + p.textSize(this.size + 4); |
| 98 | + p.text(this.char, this.x, this.y); |
| 99 | + |
| 100 | + // Draw middle glow |
| 101 | + glowColor.setAlpha(this.opacity * 120); |
| 102 | + p.fill(glowColor); |
| 103 | + p.textSize(this.size + 2); |
| 104 | + p.text(this.char, this.x, this.y); |
| 105 | + } |
| 106 | + |
| 107 | + // Draw main character |
| 108 | + const c = p.color(this.color); |
| 109 | + c.setAlpha(this.opacity * 255); |
| 110 | + p.fill(c); |
| 111 | + p.textSize(this.size); |
| 112 | + p.noStroke(); |
| 113 | + p.text(this.char, this.x, this.y); |
| 114 | + |
| 115 | + // Draw inner highlight |
| 116 | + if (!this.isBackground) { |
| 117 | + const highlightColor = p.color("#ffffff"); |
| 118 | + highlightColor.setAlpha(this.opacity * 180); |
| 119 | + p.fill(highlightColor); |
| 120 | + p.textSize(this.size * 0.9); |
| 121 | + p.text(this.char, this.x, this.y); |
| 122 | + } |
| 123 | + p.pop(); |
| 124 | + } |
| 125 | +} |
0 commit comments