Factor out CreatureAnimators.

pull/232/head
Atul Varma 2021-12-30 16:12:08 -05:00
rodzic c1722b1d53
commit 19791ac2e3
3 zmienionych plików z 22 dodań i 16 usunięć

Wyświetl plik

@ -60,22 +60,28 @@ const animateSpin: CreatureAnimate = (animPct, symbol) => {
return [svgTransformOrigin(origin, [svgRotate(animPct * 360)])]; return [svgTransformOrigin(origin, [svgRotate(animPct * 360)])];
}; };
export const hoverAnimator: CreatureAnimator = {
animate: animateHover,
getChildAnimator: () => hoverAnimator,
};
const spinAnimator: CreatureAnimator = { const spinAnimator: CreatureAnimator = {
animate: animateSpin, animate: animateSpin,
getChildAnimator: () => spinAnimator, getChildAnimator: () => spinAnimator,
}; };
export const hoverAndSpinAnimator: CreatureAnimator = { export const CREATURE_ANIMATOR_NAMES = ["none", "breathe", "spin"] as const;
animate: animateHover,
getChildAnimator: () => spinAnimator,
};
export const nullAnimator: CreatureAnimator = { type CreatureAnimatorName = typeof CREATURE_ANIMATOR_NAMES[number];
animate: () => [],
getChildAnimator: () => nullAnimator, export const CreatureAnimators: {
[k in CreatureAnimatorName]: CreatureAnimator;
} = {
none: {
animate: () => [],
getChildAnimator: () => CreatureAnimators.none,
},
breathe: {
animate: animateHover,
getChildAnimator: () => CreatureAnimators.breathe,
},
spin: {
animate: animateHover,
getChildAnimator: () => spinAnimator,
},
}; };

Wyświetl plik

@ -2,7 +2,7 @@ import React, { useContext, useMemo } from "react";
import { BBox, Point } from "../vendor/bezier-js"; import { BBox, Point } from "../vendor/bezier-js";
import { getAttachmentTransforms } from "./attach"; import { getAttachmentTransforms } from "./attach";
import { getBoundingBoxCenter, uniformlyScaleToFit } from "./bounding-box"; import { getBoundingBoxCenter, uniformlyScaleToFit } from "./bounding-box";
import { CreatureAnimator, nullAnimator } from "./creature-animator"; import { CreatureAnimator, CreatureAnimators } from "./creature-animator";
import { scalePointXY, subtractPoints } from "./point"; import { scalePointXY, subtractPoints } from "./point";
import { AttachmentPointType } from "./specs"; import { AttachmentPointType } from "./specs";
import { import {
@ -218,7 +218,7 @@ export const CreatureSymbol: React.FC<CreatureSymbolProps> = (props) => {
let ctx = useContext(CreatureContext); let ctx = useContext(CreatureContext);
const { data, attachments, nests } = props; const { data, attachments, nests } = props;
const attachmentCtx: CreatureContextType = { ...ctx, parent: data }; const attachmentCtx: CreatureContextType = { ...ctx, parent: data };
const animator = props.animator ?? nullAnimator; const animator = props.animator ?? CreatureAnimators.none;
const animPct = props.animPct ?? 0; const animPct = props.animPct ?? 0;
const svgTransforms = useMemo( const svgTransforms = useMemo(
() => animator.animate(animPct, data), () => animator.animate(animPct, data),

Wyświetl plik

@ -47,7 +47,7 @@ import { GalleryWidget } from "../../gallery-widget";
import { serializeCreatureDesign } from "./serialization"; import { serializeCreatureDesign } from "./serialization";
import { CreatureEditorWidget } from "./creature-editor"; import { CreatureEditorWidget } from "./creature-editor";
import { useAnimationPct } from "../../animation"; import { useAnimationPct } from "../../animation";
import { hoverAndSpinAnimator } from "../../creature-animator"; import { CreatureAnimators } from "../../creature-animator";
/** /**
* The minimum number of attachment points that any symbol used as the main body * The minimum number of attachment points that any symbol used as the main body
@ -384,7 +384,7 @@ function createCreatureAnimationRenderer(
<CreatureSymbol <CreatureSymbol
{...creature} {...creature}
animPct={animPct} animPct={animPct}
animator={hoverAndSpinAnimator} animator={CreatureAnimators.spin}
/> />
</CreatureContext.Provider> </CreatureContext.Provider>
</SvgTransform> </SvgTransform>