mysticsymbolic.github.io/lib/pages/creature-page.tsx

275 wiersze
7.7 KiB
TypeScript
Czysty Zwykły widok Historia

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";
import { SymbolContextWidget } from "../symbol-context-widget";
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";
import {
createCreatureSymbolFactory,
extractCreatureSymbolFromElement,
} from "../creature-symbol-factory";
2021-02-27 13:50:06 +00:00
import {
CreatureContext,
CreatureContextType,
CreatureSymbol,
NestedCreatureSymbol,
2021-02-27 13:50:06 +00:00
} from "../creature-symbol";
import { HoverDebugHelper } from "../hover-debug-helper";
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.
*/
const SYMBOL_MAP = new Map(
SvgVocabulary.map((symbol) => [symbol.name, symbol])
);
/** Symbols that can be the "root" (i.e., main body) of a creature. */
const ROOT_SYMBOLS = SvgVocabulary.filter(
(data) => data.meta?.always_be_nested !== true
);
/** Symbols that can be attached to the main body of a creature. */
const ATTACHMENT_SYMBOLS = ROOT_SYMBOLS;
/** Symbols that can be nested within any part of a creature. */
const NESTED_SYMBOLS = SvgVocabulary.filter(
(data) => data.meta?.always_nest !== true
);
2021-02-27 14:21:11 +00:00
/**
* Returns the data for the given symbol, throwing an error
* if it doesn't exist.
*/
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
/**
* Given a parent symbol, return an array of random children to be nested within
* it.
*
* Can return an empty array e.g. if the parent symbol doesn't have
* any nesting areas.
*/
function getNestingChildren(
parent: SvgSymbolData,
rng: Random
): NestedCreatureSymbol[] {
const { meta, specs } = parent;
if (meta?.always_nest && specs?.nesting) {
const indices = range(specs.nesting.length);
const child = rng.choice(NESTED_SYMBOLS);
return [
{
data: child,
attachments: [],
nests: [],
indices,
},
];
}
return [];
}
2021-02-27 14:22:07 +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.
*/
function getSymbolWithAttachments(
numAttachmentKinds: number,
rng: Random
): CreatureSymbol {
const root = rng.choice(ROOT_SYMBOLS);
const result: CreatureSymbol = {
data: root,
attachments: [],
nests: getNestingChildren(root, rng),
};
2021-02-27 14:22:07 +00:00
if (root.specs) {
const attachmentKinds = rng.uniqueChoices(
Array.from(iterAttachmentPoints(root.specs))
.filter((point) => point.type !== "anchor")
.map((point) => point.type),
numAttachmentKinds
);
for (let kind of attachmentKinds) {
const attachment = rng.choice(ATTACHMENT_SYMBOLS);
2021-02-27 14:22:07 +00:00
const indices = range(root.specs[kind]?.length ?? 0);
result.attachments.push({
data: attachment,
attachTo: kind,
indices,
attachments: [],
nests: getNestingChildren(attachment, rng),
});
2021-02-27 14:22:07 +00:00
}
}
return result;
2021-02-27 14:22:07 +00:00
}
const symbol = createCreatureSymbolFactory(getSymbol);
const Eye = symbol("eye");
const Hand = symbol("hand");
const Arm = symbol("arm");
const Antler = symbol("antler");
const Crown = symbol("crown");
const Wing = symbol("wing");
2021-02-16 01:20:41 +00:00
const MuscleArm = symbol("muscle_arm");
2021-02-16 01:20:41 +00:00
const Leg = symbol("leg");
2021-02-16 03:51:35 +00:00
const Tail = symbol("tail");
2021-02-16 01:20:41 +00:00
const Lightning = symbol("lightning");
const EYE_CREATURE = (
<Eye>
<Lightning nestInside />
<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>
);
const EYE_CREATURE_SYMBOL = extractCreatureSymbolFromElement(EYE_CREATURE);
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.
*/
function randomlyReplaceParts<T extends CreatureSymbol>(
rng: Random,
creature: T
): T {
const result: T = {
...creature,
2021-02-16 22:42:19 +00:00
data: rng.choice(SvgVocabulary),
attachments: creature.attachments.map((a) => randomlyReplaceParts(rng, a)),
nests: creature.nests.map((n) => randomlyReplaceParts(rng, n)),
};
return result;
2021-02-16 22:42:19 +00:00
}
type CreatureGenerator = (rng: Random) => CreatureSymbol;
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.
*/
const COMPLEXITY_LEVEL_GENERATORS: CreatureGenerator[] = [
...range(5).map((i) => getSymbolWithAttachments.bind(null, i)),
(rng) => randomlyReplaceParts(rng, EYE_CREATURE_SYMBOL),
];
const MAX_COMPLEXITY_LEVEL = COMPLEXITY_LEVEL_GENERATORS.length - 1;
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<{}> = () => {
const svgRef = useRef<SVGSVGElement>(null);
const [bgColor, setBgColor] = useState(DEFAULT_BG_COLOR);
2021-02-16 22:42:19 +00:00
const [randomSeed, setRandomSeed] = useState<number | null>(null);
const [symbolCtx, setSymbolCtx] = useState(createSvgSymbolContext());
const [complexity, setComplexity] = useState(MAX_COMPLEXITY_LEVEL);
const defaultCtx = useContext(CreatureContext);
const newRandomSeed = () => setRandomSeed(Date.now());
const ctx: CreatureContextType = {
...defaultCtx,
...symbolCtx,
fill: symbolCtx.showSpecs ? "none" : symbolCtx.fill,
};
2021-02-16 22:42:19 +00:00
const creature =
randomSeed === null
? EYE_CREATURE_SYMBOL
: COMPLEXITY_LEVEL_GENERATORS[complexity](new Random(randomSeed));
const handleSvgExport = () =>
exportSvg(getDownloadFilename(randomSeed), svgRef);
2021-02-15 13:34:22 +00:00
return (
<>
<h1>Creature!</h1>
<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>
<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}>
<u>R</u>andomize!
</button>{" "}
<button onClick={() => window.location.reload()}>Reset</button>{" "}
<button onClick={handleSvgExport}>Export SVG</button>
2021-02-17 01:47:12 +00:00
</p>
<CreatureContext.Provider value={ctx}>
<HoverDebugHelper>
<AutoSizingSvg padding={20} ref={svgRef} bgColor={bgColor}>
<g transform="scale(0.5 0.5)">
<CreatureSymbol {...creature} />
</g>
</AutoSizingSvg>
</HoverDebugHelper>
</CreatureContext.Provider>
2021-02-15 13:34:22 +00:00
</>
);
};