|
| 1 | +/** |
| 2 | + * Combat visual effects and hex flash rendering. |
| 3 | + * Pure Canvas drawing functions extracted from Renderer. |
| 4 | + */ |
| 5 | + |
| 6 | +import type { PixelCoord } from '../shared/hex'; |
| 7 | + |
| 8 | +export interface CombatEffect { |
| 9 | + type: 'beam' | 'explosion' | 'gameOverExplosion'; |
| 10 | + from: PixelCoord; |
| 11 | + to: PixelCoord; |
| 12 | + startTime: number; |
| 13 | + duration: number; |
| 14 | + color: string; |
| 15 | +} |
| 16 | + |
| 17 | +export interface HexFlash { |
| 18 | + position: PixelCoord; |
| 19 | + startTime: number; |
| 20 | + duration: number; |
| 21 | + color: string; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Render and prune active combat visual effects (beams, explosions, game-over blasts). |
| 26 | + * Returns the filtered array with expired effects removed. |
| 27 | + */ |
| 28 | +export function drawCombatEffects( |
| 29 | + ctx: CanvasRenderingContext2D, |
| 30 | + effects: CombatEffect[], |
| 31 | + now: number, |
| 32 | +): CombatEffect[] { |
| 33 | + const live = effects.filter(e => now < e.startTime + e.duration); |
| 34 | + |
| 35 | + for (const effect of live) { |
| 36 | + if (now < effect.startTime) continue; |
| 37 | + const progress = (now - effect.startTime) / effect.duration; |
| 38 | + |
| 39 | + if (effect.type === 'beam') { |
| 40 | + drawBeamEffect(ctx, effect, progress); |
| 41 | + } else if (effect.type === 'explosion') { |
| 42 | + drawExplosionEffect(ctx, effect, progress); |
| 43 | + } else if (effect.type === 'gameOverExplosion') { |
| 44 | + drawGameOverExplosionEffect(ctx, effect, progress); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return live; |
| 49 | +} |
| 50 | + |
| 51 | +function drawBeamEffect(ctx: CanvasRenderingContext2D, effect: CombatEffect, progress: number): void { |
| 52 | + const beamAlpha = 1 - progress; |
| 53 | + const beamProgress = Math.min(progress * 3, 1); |
| 54 | + |
| 55 | + ctx.strokeStyle = effect.color; |
| 56 | + ctx.globalAlpha = beamAlpha * 0.8; |
| 57 | + ctx.lineWidth = 2 * (1 - progress); |
| 58 | + ctx.beginPath(); |
| 59 | + ctx.moveTo(effect.from.x, effect.from.y); |
| 60 | + ctx.lineTo( |
| 61 | + effect.from.x + (effect.to.x - effect.from.x) * beamProgress, |
| 62 | + effect.from.y + (effect.to.y - effect.from.y) * beamProgress, |
| 63 | + ); |
| 64 | + ctx.stroke(); |
| 65 | + |
| 66 | + // Glow line |
| 67 | + ctx.globalAlpha = beamAlpha * 0.3; |
| 68 | + ctx.lineWidth = 6 * (1 - progress); |
| 69 | + ctx.beginPath(); |
| 70 | + ctx.moveTo(effect.from.x, effect.from.y); |
| 71 | + ctx.lineTo( |
| 72 | + effect.from.x + (effect.to.x - effect.from.x) * beamProgress, |
| 73 | + effect.from.y + (effect.to.y - effect.from.y) * beamProgress, |
| 74 | + ); |
| 75 | + ctx.stroke(); |
| 76 | + ctx.globalAlpha = 1; |
| 77 | +} |
| 78 | + |
| 79 | +function drawExplosionEffect(ctx: CanvasRenderingContext2D, effect: CombatEffect, progress: number): void { |
| 80 | + const maxRadius = 20; |
| 81 | + const radius = maxRadius * progress; |
| 82 | + const alpha = 1 - progress; |
| 83 | + |
| 84 | + ctx.strokeStyle = effect.color; |
| 85 | + ctx.lineWidth = 3 * (1 - progress); |
| 86 | + ctx.globalAlpha = alpha * 0.8; |
| 87 | + ctx.beginPath(); |
| 88 | + ctx.arc(effect.from.x, effect.from.y, radius, 0, Math.PI * 2); |
| 89 | + ctx.stroke(); |
| 90 | + |
| 91 | + if (progress < 0.3) { |
| 92 | + ctx.fillStyle = effect.color; |
| 93 | + ctx.globalAlpha = (1 - progress / 0.3) * 0.6; |
| 94 | + ctx.beginPath(); |
| 95 | + ctx.arc(effect.from.x, effect.from.y, radius * 0.5, 0, Math.PI * 2); |
| 96 | + ctx.fill(); |
| 97 | + } |
| 98 | + ctx.globalAlpha = 1; |
| 99 | +} |
| 100 | + |
| 101 | +function drawGameOverExplosionEffect(ctx: CanvasRenderingContext2D, effect: CombatEffect, progress: number): void { |
| 102 | + const maxRadius = 50; |
| 103 | + const alpha = 1 - progress; |
| 104 | + |
| 105 | + // Outer expanding ring |
| 106 | + const outerRadius = maxRadius * progress; |
| 107 | + ctx.strokeStyle = effect.color; |
| 108 | + ctx.lineWidth = 4 * (1 - progress); |
| 109 | + ctx.globalAlpha = alpha * 0.7; |
| 110 | + ctx.beginPath(); |
| 111 | + ctx.arc(effect.from.x, effect.from.y, outerRadius, 0, Math.PI * 2); |
| 112 | + ctx.stroke(); |
| 113 | + |
| 114 | + // Second ring (slightly behind) |
| 115 | + if (progress > 0.1) { |
| 116 | + const innerProgress = (progress - 0.1) / 0.9; |
| 117 | + const innerRadius = maxRadius * 0.7 * innerProgress; |
| 118 | + ctx.lineWidth = 3 * (1 - innerProgress); |
| 119 | + ctx.globalAlpha = (1 - innerProgress) * 0.5; |
| 120 | + ctx.beginPath(); |
| 121 | + ctx.arc(effect.from.x, effect.from.y, innerRadius, 0, Math.PI * 2); |
| 122 | + ctx.stroke(); |
| 123 | + } |
| 124 | + |
| 125 | + // Bright core flash |
| 126 | + if (progress < 0.4) { |
| 127 | + const coreAlpha = (1 - progress / 0.4); |
| 128 | + const coreRadius = 15 * (1 - progress * 0.5); |
| 129 | + ctx.fillStyle = '#ffffff'; |
| 130 | + ctx.globalAlpha = coreAlpha * 0.8; |
| 131 | + ctx.beginPath(); |
| 132 | + ctx.arc(effect.from.x, effect.from.y, coreRadius, 0, Math.PI * 2); |
| 133 | + ctx.fill(); |
| 134 | + |
| 135 | + ctx.fillStyle = effect.color; |
| 136 | + ctx.globalAlpha = coreAlpha * 0.4; |
| 137 | + ctx.beginPath(); |
| 138 | + ctx.arc(effect.from.x, effect.from.y, coreRadius * 2, 0, Math.PI * 2); |
| 139 | + ctx.fill(); |
| 140 | + } |
| 141 | + |
| 142 | + // Debris lines radiating outward |
| 143 | + if (progress > 0.05 && progress < 0.8) { |
| 144 | + const debrisAlpha = progress < 0.4 ? 1 : (0.8 - progress) / 0.4; |
| 145 | + ctx.strokeStyle = effect.color; |
| 146 | + ctx.globalAlpha = debrisAlpha * 0.6; |
| 147 | + ctx.lineWidth = 1.5; |
| 148 | + const seed = (effect.from.x * 7 + effect.from.y * 13) | 0; |
| 149 | + for (let d = 0; d < 8; d++) { |
| 150 | + const angle = (seed + d * 0.785) % (Math.PI * 2); |
| 151 | + const innerR = maxRadius * progress * 0.3; |
| 152 | + const outerR = maxRadius * progress * 0.7; |
| 153 | + ctx.beginPath(); |
| 154 | + ctx.moveTo( |
| 155 | + effect.from.x + Math.cos(angle) * innerR, |
| 156 | + effect.from.y + Math.sin(angle) * innerR, |
| 157 | + ); |
| 158 | + ctx.lineTo( |
| 159 | + effect.from.x + Math.cos(angle) * outerR, |
| 160 | + effect.from.y + Math.sin(angle) * outerR, |
| 161 | + ); |
| 162 | + ctx.stroke(); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + ctx.globalAlpha = 1; |
| 167 | +} |
| 168 | + |
| 169 | +/** |
| 170 | + * Render and prune hex flash highlights. |
| 171 | + * Returns the filtered array with expired flashes removed. |
| 172 | + */ |
| 173 | +export function drawHexFlashes( |
| 174 | + ctx: CanvasRenderingContext2D, |
| 175 | + flashes: HexFlash[], |
| 176 | + now: number, |
| 177 | + hexSize: number, |
| 178 | +): HexFlash[] { |
| 179 | + const live = flashes.filter(f => now < f.startTime + f.duration); |
| 180 | + |
| 181 | + for (const flash of live) { |
| 182 | + if (now < flash.startTime) continue; |
| 183 | + const progress = (now - flash.startTime) / flash.duration; |
| 184 | + const alpha = (1 - progress) * 0.6; |
| 185 | + const radius = hexSize * (0.5 + progress * 0.5); |
| 186 | + |
| 187 | + ctx.beginPath(); |
| 188 | + ctx.arc(flash.position.x, flash.position.y, radius, 0, Math.PI * 2); |
| 189 | + ctx.fillStyle = flash.color; |
| 190 | + ctx.globalAlpha = alpha * 0.3; |
| 191 | + ctx.fill(); |
| 192 | + ctx.strokeStyle = flash.color; |
| 193 | + ctx.lineWidth = 2 * (1 - progress); |
| 194 | + ctx.globalAlpha = alpha; |
| 195 | + ctx.stroke(); |
| 196 | + ctx.globalAlpha = 1; |
| 197 | + } |
| 198 | + |
| 199 | + return live; |
| 200 | +} |
0 commit comments