|
| 1 | +import React, { useEffect, useRef, useState } from 'react'; |
| 2 | + |
| 3 | +/** |
| 4 | + * The heart HUD for the pixel-monster defense game (bottom-right). |
| 5 | + * |
| 6 | + * ShaderGuide owns every game rule (raids, dart hits, pickups, the taunt |
| 7 | + * dance) and broadcasts 'mdflow:hearts' — this component only renders the |
| 8 | + * consequences. The row carries [data-hearts-anchor] so the raiders in the |
| 9 | + * shader know exactly where to charge, and it stays pointer-events-none so |
| 10 | + * slingshots fired from this corner still work. |
| 11 | + * |
| 12 | + * Hidden until the first alien materializes: readers who never meet a |
| 13 | + * monster never see a HUD. |
| 14 | + */ |
| 15 | + |
| 16 | +interface HeartsDetail { |
| 17 | + hearts: number; |
| 18 | + max: number; |
| 19 | + reason: 'show' | 'steal' | 'gain' | 'defeat' | 'reset'; |
| 20 | +} |
| 21 | + |
| 22 | +export const AlienDefense: React.FC = () => { |
| 23 | + const [state, setState] = useState<HeartsDetail | null>(null); |
| 24 | + const [fx, setFx] = useState<'steal' | 'gain' | null>(null); |
| 25 | + const [msg, setMsg] = useState<string | null>(null); |
| 26 | + const msgTimer = useRef(0); |
| 27 | + const fxTimer = useRef(0); |
| 28 | + const briefed = useRef(false); |
| 29 | + |
| 30 | + useEffect(() => { |
| 31 | + const say = (text: string, ms: number) => { |
| 32 | + window.clearTimeout(msgTimer.current); |
| 33 | + setMsg(text); |
| 34 | + msgTimer.current = window.setTimeout(() => setMsg(null), ms); |
| 35 | + }; |
| 36 | + const onHearts = (ev: Event) => { |
| 37 | + const d = (ev as CustomEvent<HeartsDetail>).detail; |
| 38 | + setState(d); |
| 39 | + if (d.reason === 'steal' || d.reason === 'gain') { |
| 40 | + window.clearTimeout(fxTimer.current); |
| 41 | + setFx(d.reason); |
| 42 | + fxTimer.current = window.setTimeout(() => setFx(null), 700); |
| 43 | + } |
| 44 | + if (d.reason === 'show' && !briefed.current) { |
| 45 | + briefed.current = true; |
| 46 | + say('👾 raiders inbound — sling darts at them before they reach your hearts', 9000); |
| 47 | + } else if (d.reason === 'steal') { |
| 48 | + say(d.hearts <= 1 ? '💔 last heart — stop them!' : '💔 heart stolen!', 4000); |
| 49 | + } else if (d.reason === 'gain') { |
| 50 | + say('❤️ heart restored', 3000); |
| 51 | + } else if (d.reason === 'defeat') { |
| 52 | + say('👾👾👾 the aliens win this round — watch them gloat', 9000); |
| 53 | + } else if (d.reason === 'reset') { |
| 54 | + say('❤️ hearts restored. round two.', 4000); |
| 55 | + } |
| 56 | + }; |
| 57 | + window.addEventListener('mdflow:hearts', onHearts); |
| 58 | + return () => { |
| 59 | + window.removeEventListener('mdflow:hearts', onHearts); |
| 60 | + window.clearTimeout(msgTimer.current); |
| 61 | + window.clearTimeout(fxTimer.current); |
| 62 | + }; |
| 63 | + }, []); |
| 64 | + |
| 65 | + if (!state) return null; |
| 66 | + return ( |
| 67 | + <> |
| 68 | + <style>{` |
| 69 | + @keyframes hud-heart-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } } |
| 70 | + @keyframes hud-steal-kf { |
| 71 | + 0%, 100% { transform: translateX(0); } |
| 72 | + 20% { transform: translateX(-5px); } |
| 73 | + 40% { transform: translateX(5px); } |
| 74 | + 60% { transform: translateX(-4px); } |
| 75 | + 80% { transform: translateX(3px); } |
| 76 | + } |
| 77 | + @keyframes hud-gain-kf { |
| 78 | + 0% { transform: scale(1); } |
| 79 | + 40% { transform: scale(1.25); } |
| 80 | + 100% { transform: scale(1); } |
| 81 | + } |
| 82 | + `}</style> |
| 83 | + <div |
| 84 | + className="fixed bottom-3 right-3 z-50 flex flex-col items-end gap-1.5 select-none pointer-events-none" |
| 85 | + style={{ animation: 'hud-heart-in 0.4s ease-out' }} |
| 86 | + > |
| 87 | + {msg && ( |
| 88 | + <div className="max-w-[240px] rounded-lg border border-zinc-700/80 bg-zinc-950/85 px-3 py-2 text-right font-mono text-[11px] leading-snug text-zinc-300 backdrop-blur-md"> |
| 89 | + {msg} |
| 90 | + </div> |
| 91 | + )} |
| 92 | + <div |
| 93 | + data-hearts-anchor |
| 94 | + aria-label={`${state.hearts} of ${state.max} hearts left`} |
| 95 | + className="flex items-center gap-1 rounded-full border border-white/10 bg-black/60 px-2.5 py-1 backdrop-blur-md" |
| 96 | + style={fx === 'steal' ? { animation: 'hud-steal-kf 0.5s ease-in-out' } : undefined} |
| 97 | + > |
| 98 | + {Array.from({ length: state.max }, (_, i) => { |
| 99 | + const filled = i < state.hearts; |
| 100 | + return ( |
| 101 | + <span |
| 102 | + key={i} |
| 103 | + className={filled |
| 104 | + ? 'text-sm leading-none text-rose-500 drop-shadow-[0_0_5px_rgba(244,63,94,0.9)]' |
| 105 | + : 'text-sm leading-none text-zinc-700'} |
| 106 | + style={fx === 'gain' && i === state.hearts - 1 |
| 107 | + ? { animation: 'hud-gain-kf 0.6s ease-out' } |
| 108 | + : undefined} |
| 109 | + > |
| 110 | + ♥ |
| 111 | + </span> |
| 112 | + ); |
| 113 | + })} |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + </> |
| 117 | + ); |
| 118 | +}; |
0 commit comments