2021-02-27 13:35:34 +00:00
|
|
|
import React, { useContext, useRef, useState } from "react";
|
2021-02-15 13:34:22 +00:00
|
|
|
import { SvgVocabulary } from "../svg-vocabulary";
|
2021-02-27 13:50:06 +00:00
|
|
|
import { createSvgSymbolContext, SvgSymbolData } from "../svg-symbol";
|
|
|
|
import { iterAttachmentPoints } from "../specs";
|
2021-02-16 22:42:19 +00:00
|
|
|
import { Random } from "../random";
|
2021-02-17 13:07:04 +00:00
|
|
|
import { SymbolContextWidget } from "../symbol-context-widget";
|
2021-02-23 02:50:14 +00:00
|
|
|
import { range } from "../util";
|
2021-02-27 13:50:06 +00:00
|
|
|
|
2021-02-27 13:35:34 +00:00
|
|
|
import { AutoSizingSvg } from "../auto-sizing-svg";
|
2021-02-27 13:43:31 +00:00
|
|
|
import { exportSvg } from "../export-svg";
|
2021-02-27 13:50:06 +00:00
|
|
|
import {
|
|
|
|
CreatureContext,
|
|
|
|
CreatureContextType,
|
|
|
|
CreatureSymbol,
|
|
|
|
CreatureSymbolProps,
|
|
|
|
} from "../creature-symbol";
|
2021-02-15 14:56:02 +00:00
|
|
|
|
2021-02-26 00:40:18 +00:00
|
|
|
const DEFAULT_BG_COLOR = "#858585";
|
|
|
|
|
2021-02-27 14:21:11 +00:00
|
|
|
/**
|
|
|
|
* Mapping from symbol names to symbol data, for quick and easy access.
|
|
|
|
*/
|
2021-02-15 14:56:02 +00:00
|
|
|
const SYMBOL_MAP = new Map(
|
|
|
|
SvgVocabulary.map((symbol) => [symbol.name, symbol])
|
|
|
|
);
|
|
|
|
|
2021-02-27 14:21:11 +00:00
|
|
|
/**
|
|
|
|
* Returns the data for the given symbol, throwing an error
|
|
|
|
* if it doesn't exist.
|
|
|
|
*/
|
2021-02-15 14:56:02 +00:00
|
|
|
function getSymbol(name: string): SvgSymbolData {
|
|
|
|
const symbol = SYMBOL_MAP.get(name);
|
|
|
|
if (!symbol) {
|
|
|
|
throw new Error(`Unable to find the symbol "${name}"!`);
|
|
|
|
}
|
|
|
|
return symbol;
|
|
|
|
}
|
2021-02-15 13:34:22 +00:00
|
|
|
|
2021-02-27 14:21:11 +00:00
|
|
|
/**
|
|
|
|
* A creature symbol that comes with default (but overrideable) symbol data.
|
|
|
|
* This makes it easy to use the symbol in JSX, but also easy to dynamically
|
|
|
|
* replace the symbol with a different one.
|
|
|
|
*/
|
2021-02-16 22:42:19 +00:00
|
|
|
type CreatureSymbolWithDefaultProps = Omit<CreatureSymbolProps, "data"> & {
|
|
|
|
data?: SvgSymbolData;
|
|
|
|
};
|
|
|
|
|
2021-02-27 14:21:11 +00:00
|
|
|
/**
|
|
|
|
* Returns a React component that renders a `<CreatureSymbol>`, using the symbol
|
|
|
|
* with the given name as its default data.
|
|
|
|
*/
|
2021-02-15 22:19:07 +00:00
|
|
|
function createCreatureSymbol(
|
|
|
|
name: string
|
2021-02-16 22:42:19 +00:00
|
|
|
): React.FC<CreatureSymbolWithDefaultProps> {
|
2021-02-15 22:19:07 +00:00
|
|
|
const data = getSymbol(name);
|
2021-02-16 22:11:41 +00:00
|
|
|
return (props) => <CreatureSymbol data={props.data || data} {...props} />;
|
2021-02-15 22:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Eye = createCreatureSymbol("eye");
|
|
|
|
|
|
|
|
const Hand = createCreatureSymbol("hand");
|
|
|
|
|
2021-02-16 00:28:03 +00:00
|
|
|
const Arm = createCreatureSymbol("arm");
|
2021-02-15 22:19:07 +00:00
|
|
|
|
2021-02-16 01:12:14 +00:00
|
|
|
const Antler = createCreatureSymbol("antler");
|
|
|
|
|
|
|
|
const Crown = createCreatureSymbol("crown");
|
|
|
|
|
2021-02-16 01:20:41 +00:00
|
|
|
const Wing = createCreatureSymbol("wing");
|
|
|
|
|
2021-02-19 01:06:41 +00:00
|
|
|
const MuscleArm = createCreatureSymbol("muscle_arm");
|
2021-02-16 01:20:41 +00:00
|
|
|
|
2021-02-16 03:51:35 +00:00
|
|
|
const Leg = createCreatureSymbol("leg");
|
|
|
|
|
2021-02-16 01:20:41 +00:00
|
|
|
const Tail = createCreatureSymbol("tail");
|
|
|
|
|
2021-02-26 02:57:10 +00:00
|
|
|
const Lightning = createCreatureSymbol("lightning");
|
|
|
|
|
2021-02-27 14:21:11 +00:00
|
|
|
/**
|
|
|
|
* Randomly creates a symbol with the given number of
|
|
|
|
* types of attachments. The symbol itself, and where the
|
|
|
|
* attachments are attached, are chosen randomly.
|
|
|
|
*/
|
2021-02-23 02:50:14 +00:00
|
|
|
function getSymbolWithAttachments(
|
|
|
|
numAttachmentKinds: number,
|
|
|
|
rng: Random
|
|
|
|
): JSX.Element {
|
|
|
|
const children: JSX.Element[] = [];
|
|
|
|
const root = rng.choice(SvgVocabulary);
|
|
|
|
if (root.specs) {
|
|
|
|
const attachmentKinds = rng.uniqueChoices(
|
2021-02-23 16:46:00 +00:00
|
|
|
Array.from(iterAttachmentPoints(root.specs))
|
|
|
|
.filter((point) => point.type !== "anchor")
|
|
|
|
.map((point) => point.type),
|
2021-02-23 02:50:14 +00:00
|
|
|
numAttachmentKinds
|
|
|
|
);
|
|
|
|
for (let kind of attachmentKinds) {
|
|
|
|
const attachment = rng.choice(SvgVocabulary);
|
|
|
|
const indices = range(root.specs[kind]?.length ?? 0);
|
|
|
|
children.push(
|
|
|
|
<CreatureSymbol
|
|
|
|
data={attachment}
|
|
|
|
key={children.length}
|
|
|
|
attachTo={kind}
|
|
|
|
indices={indices}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return <CreatureSymbol data={root} children={children} />;
|
|
|
|
}
|
|
|
|
|
2021-02-16 22:11:41 +00:00
|
|
|
const EYE_CREATURE = (
|
|
|
|
<Eye>
|
2021-02-26 02:57:10 +00:00
|
|
|
<Lightning nestInside />
|
2021-02-16 22:11:41 +00:00
|
|
|
<Arm attachTo="arm" left>
|
|
|
|
<Wing attachTo="arm" left right />
|
|
|
|
</Arm>
|
|
|
|
<Arm attachTo="arm" right>
|
|
|
|
<MuscleArm attachTo="arm" left right />
|
|
|
|
</Arm>
|
|
|
|
<Antler attachTo="horn" left right />
|
|
|
|
<Crown attachTo="crown">
|
|
|
|
<Hand attachTo="horn" left right>
|
|
|
|
<Arm attachTo="arm" left />
|
|
|
|
</Hand>
|
|
|
|
</Crown>
|
|
|
|
<Leg attachTo="leg" left right />
|
|
|
|
<Tail attachTo="tail" />
|
|
|
|
</Eye>
|
|
|
|
);
|
|
|
|
|
2021-02-27 14:21:11 +00:00
|
|
|
/**
|
|
|
|
* Randomly replace all the parts of the given creature. Note that this
|
|
|
|
* might end up logging some console messages about not being able to find
|
|
|
|
* attachment/nesting indices, because it doesn't really check to make
|
|
|
|
* sure the final creature structure is fully valid.
|
|
|
|
*/
|
2021-02-16 22:42:19 +00:00
|
|
|
function randomlyReplaceParts(rng: Random, creature: JSX.Element): JSX.Element {
|
|
|
|
return React.cloneElement<CreatureSymbolWithDefaultProps>(creature, {
|
|
|
|
data: rng.choice(SvgVocabulary),
|
|
|
|
children: React.Children.map(creature.props.children, (child, i) => {
|
|
|
|
return randomlyReplaceParts(rng, child);
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-02-23 02:50:14 +00:00
|
|
|
type CreatureGenerator = (rng: Random) => JSX.Element;
|
|
|
|
|
2021-02-27 14:21:11 +00:00
|
|
|
/**
|
|
|
|
* Each index of this array represents the algorithm we use to
|
|
|
|
* randomly construct a creature with a particular "complexity level".
|
|
|
|
*
|
|
|
|
* For instance, the algorithm used to create a creature with
|
|
|
|
* complexity level 0 will be the first item of this array.
|
|
|
|
*/
|
2021-02-23 02:50:14 +00:00
|
|
|
const COMPLEXITY_LEVEL_GENERATORS: CreatureGenerator[] = [
|
|
|
|
...range(5).map((i) => getSymbolWithAttachments.bind(null, i)),
|
|
|
|
(rng) => randomlyReplaceParts(rng, EYE_CREATURE),
|
|
|
|
];
|
|
|
|
|
|
|
|
const MAX_COMPLEXITY_LEVEL = COMPLEXITY_LEVEL_GENERATORS.length - 1;
|
|
|
|
|
2021-02-17 14:14:06 +00:00
|
|
|
function getDownloadFilename(randomSeed: number | null) {
|
|
|
|
let downloadBasename = "mystic-symbolic-creature";
|
|
|
|
|
|
|
|
if (randomSeed !== null) {
|
|
|
|
downloadBasename += `-${randomSeed}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${downloadBasename}.svg`;
|
|
|
|
}
|
2021-02-17 01:54:05 +00:00
|
|
|
|
|
|
|
export const CreaturePage: React.FC<{}> = () => {
|
2021-02-17 14:14:06 +00:00
|
|
|
const svgRef = useRef<SVGSVGElement>(null);
|
2021-02-26 00:40:18 +00:00
|
|
|
const [bgColor, setBgColor] = useState(DEFAULT_BG_COLOR);
|
2021-02-16 22:42:19 +00:00
|
|
|
const [randomSeed, setRandomSeed] = useState<number | null>(null);
|
2021-02-17 13:07:04 +00:00
|
|
|
const [symbolCtx, setSymbolCtx] = useState(createSvgSymbolContext());
|
2021-02-23 02:50:14 +00:00
|
|
|
const [complexity, setComplexity] = useState(MAX_COMPLEXITY_LEVEL);
|
2021-02-16 03:36:18 +00:00
|
|
|
const defaultCtx = useContext(CreatureContext);
|
2021-02-23 02:50:14 +00:00
|
|
|
const newRandomSeed = () => setRandomSeed(Date.now());
|
2021-02-16 03:36:18 +00:00
|
|
|
const ctx: CreatureContextType = {
|
|
|
|
...defaultCtx,
|
2021-02-17 13:07:04 +00:00
|
|
|
...symbolCtx,
|
|
|
|
fill: symbolCtx.showSpecs ? "none" : symbolCtx.fill,
|
2021-02-16 03:36:18 +00:00
|
|
|
};
|
2021-02-16 22:42:19 +00:00
|
|
|
const creature =
|
|
|
|
randomSeed === null
|
|
|
|
? EYE_CREATURE
|
2021-02-23 02:50:14 +00:00
|
|
|
: COMPLEXITY_LEVEL_GENERATORS[complexity](new Random(randomSeed));
|
2021-02-17 14:14:06 +00:00
|
|
|
const handleSvgExport = () =>
|
|
|
|
exportSvg(getDownloadFilename(randomSeed), svgRef);
|
2021-02-16 03:36:18 +00:00
|
|
|
|
2021-02-15 13:34:22 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<h1>Creature!</h1>
|
2021-02-20 16:38:04 +00:00
|
|
|
<SymbolContextWidget ctx={symbolCtx} onChange={setSymbolCtx}>
|
|
|
|
<label htmlFor="bgColor">Background: </label>
|
|
|
|
<input
|
|
|
|
type="color"
|
|
|
|
value={bgColor}
|
|
|
|
onChange={(e) => setBgColor(e.target.value)}
|
|
|
|
/>{" "}
|
|
|
|
</SymbolContextWidget>
|
2021-02-17 01:47:12 +00:00
|
|
|
<p>
|
2021-02-23 02:50:14 +00:00
|
|
|
<label htmlFor="complexity">Random creature complexity: </label>
|
|
|
|
<input
|
|
|
|
type="range"
|
|
|
|
min={0}
|
|
|
|
max={MAX_COMPLEXITY_LEVEL}
|
|
|
|
step={1}
|
|
|
|
value={complexity}
|
|
|
|
onChange={(e) => {
|
|
|
|
setComplexity(parseInt(e.target.value));
|
|
|
|
newRandomSeed();
|
|
|
|
}}
|
|
|
|
/>{" "}
|
|
|
|
{complexity === MAX_COMPLEXITY_LEVEL ? "bonkers" : complexity}
|
|
|
|
</p>
|
|
|
|
<p>
|
|
|
|
<button accessKey="r" onClick={newRandomSeed}>
|
2021-02-20 16:07:57 +00:00
|
|
|
<u>R</u>andomize!
|
|
|
|
</button>{" "}
|
2021-02-17 14:14:06 +00:00
|
|
|
<button onClick={() => window.location.reload()}>Reset</button>{" "}
|
|
|
|
<button onClick={handleSvgExport}>Export SVG</button>
|
2021-02-17 01:47:12 +00:00
|
|
|
</p>
|
2021-02-16 03:36:18 +00:00
|
|
|
<CreatureContext.Provider value={ctx}>
|
2021-02-20 16:38:04 +00:00
|
|
|
<AutoSizingSvg padding={20} ref={svgRef} bgColor={bgColor}>
|
2021-02-17 13:46:30 +00:00
|
|
|
<g transform="scale(0.5 0.5)">{creature}</g>
|
2021-02-17 01:54:05 +00:00
|
|
|
</AutoSizingSvg>
|
2021-02-16 03:36:18 +00:00
|
|
|
</CreatureContext.Provider>
|
2021-02-15 13:34:22 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|