algospaced-dsa / algospaced-ui /src /components /map /RealisticForest.tsx
iamfebin's picture
feat: initial commit for AlgoSpaced DSA Review
8800c81
Raw History Blame Contribute Delete
5.36 kB
import React, { useMemo } from 'react';
import type { ForestPatch, MapTheme } from '../../types/mapTypes';
import { RealisticTree } from './RealisticTree';
interface RealisticForestProps {
forest: ForestPatch;
theme: MapTheme;
showLabels: boolean;
}
export const RealisticForest: React.FC<RealisticForestProps> = ({
forest,
theme,
showLabels,
}) => {
const isDark = theme.id !== 'parchment';
const pencil = theme.pencilColor;
const shade = theme.pencilShade;
const { id, name, cx, cy, rx, ry, trees } = forest;
const sortedTrees = useMemo(() => {
return [...trees].sort((a, b) => a.y - b.y);
}, [trees]);
const understory = useMemo(() => {
const boulders: { x: number; y: number; rx: number; ry: number; angle: number }[] = [];
const fallenLogs: { x: number; y: number; length: number; angle: number }[] = [];
const fernClusters: { x: number; y: number }[] = [];
const seed = cx * 13 + cy * 19;
const count = 6;
for (let i = 0; i < count; i++) {
const angle = (i * 1.8 + seed * 0.1) % (Math.PI * 2);
const bx = cx + Math.cos(angle) * (rx * 0.7);
const by = cy + Math.sin(angle) * (ry * 0.7);
if (i % 2 === 0) {
boulders.push({
x: Math.round(bx),
y: Math.round(by),
rx: 5 + (i % 3) * 2,
ry: 3 + (i % 2) * 1.5,
angle: (i * 35) % 60 - 30,
});
} else {
fallenLogs.push({
x: Math.round(bx),
y: Math.round(by),
length: 12 + (i % 4) * 3,
angle: (i * 45) % 90 - 45,
});
}
fernClusters.push({
x: Math.round(cx + Math.cos(angle + 0.5) * (rx * 0.5)),
y: Math.round(cy + Math.sin(angle + 0.5) * (ry * 0.5)),
});
}
return { boulders, fallenLogs, fernClusters };
}, [cx, cy, rx, ry]);
const boundaryPath = useMemo(() => {
const points: { x: number; y: number }[] = [];
const steps = 16;
for (let i = 0; i < steps; i++) {
const angle = (i / steps) * Math.PI * 2;
const noise = 1 + Math.sin(angle * 4 + cx * 0.05) * 0.08 + Math.cos(angle * 3 + cy * 0.05) * 0.06;
const px = cx + Math.cos(angle) * (rx * noise);
const py = cy + Math.sin(angle) * (ry * noise);
points.push({ x: Math.round(px), y: Math.round(py) });
}
return points.map((p, idx) => (idx === 0 ? `M ${p.x},${p.y}` : `L ${p.x},${p.y}`)).join(' ') + ' Z';
}, [cx, cy, rx, ry]);
return (
<g id={`forest-zone-${id}`} className="realistic-forest-group select-none">
{/* 1. Forest Floor Shading */}
<path
d={boundaryPath}
fill={isDark ? 'rgba(32, 42, 26, 0.35)' : 'rgba(195, 208, 175, 0.22)'}
stroke={shade}
strokeWidth="0.85"
strokeDasharray="5 7 2 7"
opacity={0.8}
/>
{/* 2. Forest Understory */}
<g id={`boulders-${id}`} stroke={pencil} strokeWidth="1">
{understory.boulders.map((b, bIdx) => (
<g key={bIdx} transform={`translate(${b.x}, ${b.y}) rotate(${b.angle})`}>
<ellipse
cx="0"
cy="0"
rx={b.rx}
ry={b.ry}
fill={isDark ? '#2b241c' : '#c4b69c'}
/>
<path
d={`M 0,-${b.ry} Q ${b.rx * 0.6},0 0,${b.ry} Q ${b.rx * 0.2},0 0,-${b.ry}`}
fill={pencil}
opacity={0.3}
/>
</g>
))}
</g>
<g id={`logs-${id}`} stroke={pencil} strokeWidth="1.2">
{understory.fallenLogs.map((log, lIdx) => (
<g key={lIdx} transform={`translate(${log.x}, ${log.y}) rotate(${log.angle})`}>
<line
x1={-log.length / 2}
y1="0"
x2={log.length / 2}
y2="0"
strokeLinecap="round"
strokeWidth="2.2"
/>
<circle cx={-log.length / 2} cy="0" r="1.2" fill={pencil} />
<circle cx="2" cy="-1.5" r="0.8" fill={isDark ? '#d4bfa1' : '#f0e6d2'} />
</g>
))}
</g>
<g id={`ferns-${id}`} stroke={shade} strokeWidth="0.8" fill="none">
{understory.fernClusters.map((fern, fIdx) => (
<g key={fIdx} transform={`translate(${fern.x}, ${fern.y})`}>
<path d="M 0,0 Q -4,-5 -7,-4 M 0,0 Q -2,-7 -2,-10 M 0,0 Q 3,-6 6,-4" />
</g>
))}
</g>
{/* 3. Deep Y-Sorted Realistic Trees */}
<g id={`trees-${id}`}>
{sortedTrees.map((tree, tIdx) => (
<RealisticTree
key={tIdx}
tree={tree}
theme={theme}
/>
))}
</g>
{/* 4. Forest Banner Title */}
{showLabels && name && (
<g transform={`translate(${cx}, ${cy - ry - 12})`}>
<rect
x="-90"
y="-14"
width="180"
height="18"
rx="3"
fill={isDark ? '#1a1510' : '#f2e8d3'}
stroke={shade}
strokeWidth="0.75"
opacity={0.85}
/>
<text
x="0"
y="0"
textAnchor="middle"
fill={isDark ? '#d8c2a3' : '#453525'}
className="font-garamond italic font-bold text-[13px] tracking-widest select-none drop-shadow-sm"
>
❖ {name} ❖
</text>
</g>
)}
</g>
);
};