Add some doc comments.
rodzic
5a5030ceb6
commit
3ad6dd290f
|
@ -17,10 +17,17 @@ import {
|
||||||
|
|
||||||
const DEFAULT_BG_COLOR = "#858585";
|
const DEFAULT_BG_COLOR = "#858585";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mapping from symbol names to symbol data, for quick and easy access.
|
||||||
|
*/
|
||||||
const SYMBOL_MAP = new Map(
|
const SYMBOL_MAP = new Map(
|
||||||
SvgVocabulary.map((symbol) => [symbol.name, symbol])
|
SvgVocabulary.map((symbol) => [symbol.name, symbol])
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the data for the given symbol, throwing an error
|
||||||
|
* if it doesn't exist.
|
||||||
|
*/
|
||||||
function getSymbol(name: string): SvgSymbolData {
|
function getSymbol(name: string): SvgSymbolData {
|
||||||
const symbol = SYMBOL_MAP.get(name);
|
const symbol = SYMBOL_MAP.get(name);
|
||||||
if (!symbol) {
|
if (!symbol) {
|
||||||
|
@ -29,10 +36,19 @@ function getSymbol(name: string): SvgSymbolData {
|
||||||
return symbol;
|
return symbol;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
type CreatureSymbolWithDefaultProps = Omit<CreatureSymbolProps, "data"> & {
|
type CreatureSymbolWithDefaultProps = Omit<CreatureSymbolProps, "data"> & {
|
||||||
data?: SvgSymbolData;
|
data?: SvgSymbolData;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a React component that renders a `<CreatureSymbol>`, using the symbol
|
||||||
|
* with the given name as its default data.
|
||||||
|
*/
|
||||||
function createCreatureSymbol(
|
function createCreatureSymbol(
|
||||||
name: string
|
name: string
|
||||||
): React.FC<CreatureSymbolWithDefaultProps> {
|
): React.FC<CreatureSymbolWithDefaultProps> {
|
||||||
|
@ -60,6 +76,11 @@ const Tail = createCreatureSymbol("tail");
|
||||||
|
|
||||||
const Lightning = createCreatureSymbol("lightning");
|
const Lightning = createCreatureSymbol("lightning");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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(
|
function getSymbolWithAttachments(
|
||||||
numAttachmentKinds: number,
|
numAttachmentKinds: number,
|
||||||
rng: Random
|
rng: Random
|
||||||
|
@ -109,6 +130,12 @@ const EYE_CREATURE = (
|
||||||
</Eye>
|
</Eye>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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(rng: Random, creature: JSX.Element): JSX.Element {
|
function randomlyReplaceParts(rng: Random, creature: JSX.Element): JSX.Element {
|
||||||
return React.cloneElement<CreatureSymbolWithDefaultProps>(creature, {
|
return React.cloneElement<CreatureSymbolWithDefaultProps>(creature, {
|
||||||
data: rng.choice(SvgVocabulary),
|
data: rng.choice(SvgVocabulary),
|
||||||
|
@ -120,6 +147,13 @@ function randomlyReplaceParts(rng: Random, creature: JSX.Element): JSX.Element {
|
||||||
|
|
||||||
type CreatureGenerator = (rng: Random) => JSX.Element;
|
type CreatureGenerator = (rng: Random) => JSX.Element;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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[] = [
|
const COMPLEXITY_LEVEL_GENERATORS: CreatureGenerator[] = [
|
||||||
...range(5).map((i) => getSymbolWithAttachments.bind(null, i)),
|
...range(5).map((i) => getSymbolWithAttachments.bind(null, i)),
|
||||||
(rng) => randomlyReplaceParts(rng, EYE_CREATURE),
|
(rng) => randomlyReplaceParts(rng, EYE_CREATURE),
|
||||||
|
|
Ładowanie…
Reference in New Issue