Recurring cleanup round over the code since round 6 (056a622), 105
commits. Behaviour is unchanged. Duplicated code in src went from 29
clones with 229 lines (0.32 %) to 14 clones with 109 lines (0.15 %),
measured with jscpd and the same settings as round 6.
- BehaviorSystem.ts shrinks from 941 to 589 lines. Collecting the
pulses (keys, timers, collisions, animations) moved to
behaviorPulses.ts. The NodeContext the nodes receive moved to
behaviorContext.ts. Imports from outside stay the same, TimeScaled
is still exported from BehaviorSystem.ts.
- PointKindButton and drawPointGrip replace the "Point:" button with
its menu and the point and handle drawing that the curve and the
vector art tab each had on their own.
- lerpVec, cubicAt and splitCubic in src/core/util/vec.ts replace four
copies of the cubic curve and two de Casteljau splits.
- mapNested in Properties.ts is the one walk over lists and structs
that liveObjects and objectRefs did separately. withFieldRenamed
and withoutField replace three copies of renaming a struct field
and two of removing one.
- jsonBody and staleRevision build the JSON body and turn a 409 into
StaleRevisionError for all stores. The auth store turns a 401 into
its error in one place.
- Smaller helpers: structAssetFrom for the two "Make struct type"
commands, shownObject in the inspector, selectedItem for the size
gizmo and the polygon corners, solidTiles in the tilemap collision,
requireVariablesAsset and requireBehaviorVariable.
- Left as is on purpose: the plain forwarding from the collision and
physics systems to Solids, the pointer wiring of the canvases and
the identical last lines of the two make commands.
- Removed unused code: tilemapTileIds and tilemapsUsing were never
called since #128 and #131. EditorState.runtimeProject became unused
with GameRun in #266. The assetId getter of the make commands was
read only by a test.
- 60 values used only in their own file lost their export. The shader
and particle node catalogues stay exported as in round 6.
- Five comments that told history now describe the current state. The
doc comment of BehaviorSystem sits on the class again.
- README lists the struct, clip area and curve assets and mentions the
game link from #266.
Findings:
- Deleting a tile that is painted on a tilemap leaves its cells in
place, and they draw nothing. The delete dialog does not mention the
tilemaps. That was probably the job of the unused tilemapsUsing.
Left as is, because fixing it changes behaviour.
- The solid filter for tile surfaces was untested. A bent filter
stayed green. A new test in tests/core/surface.test.ts now fails
against it.
Tests:
- ./check.sh: 195 files, 2815 tests, exit 0 (one test removed with
tilemapsUsing, one added for tile surfaces).
- Full e2e run: ALL PASSED, 1580 checks.
- Smokes jumprun, tetris, actor: 24, 19 and 21 checks, all passed.
The example project files were restored byte for byte afterwards.
- Counter-checks, 12, all fail as expected: 10 in unit tests (among
them mapNested, splitCubic, cubicAt, staleRevision, the stay pulse,
the late timer and the time scale), 2 in the browser (Point button
always enabled fails 47, selectedItem without a hit fails 29).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
112 lines
4.4 KiB
TypeScript
112 lines
4.4 KiB
TypeScript
/**
|
||
* The built-in eases as starting points for a Curve asset (#247). Each is traced by a few Bezier
|
||
* segments: the points sit on the ease, and each handle points along the ease's slope on its side, a
|
||
* third of the way to the neighbour (a cubic Hermite piece). A segment that strays more than
|
||
* `TOLERANCE` from the ease is halved, and only that one – so a quad keeps its single segment, and
|
||
* circ gets ever shorter ones only towards its upright end.
|
||
*/
|
||
import type { EasingDirection, VectorPoint } from './Asset';
|
||
import type { Vec2 } from './Properties';
|
||
import { EASING_DIRECTIONS, EASING_SHAPES, easedShare, type EasingShape, easingValue } from './animation';
|
||
import { curveValue, linearCurve } from './curve';
|
||
|
||
export interface CurvePreset {
|
||
label: string;
|
||
/** The ease it traces; presets of one shape share it, so a menu can group them. */
|
||
shape: EasingShape;
|
||
build(): VectorPoint[];
|
||
}
|
||
|
||
const TOLERANCE = 0.004;
|
||
/** Enough for a bounce; a curve with more points than this is no longer one to edit by hand. */
|
||
const MAX_POINTS = 64;
|
||
const MAX_ROUNDS = 16;
|
||
/** Where each segment is compared with the ease. */
|
||
const SAMPLES = 24;
|
||
/** The step of the one-sided slopes: small, and never onto the point itself, where expo jumps to 0. */
|
||
const H = 1e-6;
|
||
|
||
export function curvePresets(): CurvePreset[] {
|
||
const presets: CurvePreset[] = [{ label: 'Linear', shape: 'linear', build: linearCurve }];
|
||
for (const { value: shape, label } of EASING_SHAPES) {
|
||
if (shape === 'linear' || shape === 'step' || shape === 'curve') continue;
|
||
for (const direction of EASING_DIRECTIONS)
|
||
presets.push({
|
||
label: `${label} ${direction.label.toLowerCase()}`,
|
||
shape,
|
||
build: () => easeCurve(shape, direction.value),
|
||
});
|
||
}
|
||
return presets;
|
||
}
|
||
|
||
export function easeCurve(shape: EasingShape, direction: EasingDirection): VectorPoint[] {
|
||
const interpolation = easingValue(shape, direction);
|
||
return curveFromEase((t) => easedShare(interpolation, t));
|
||
}
|
||
|
||
function curveFromEase(ease: (t: number) => number): VectorPoint[] {
|
||
let at = [0, 1];
|
||
let points = traced(ease, at);
|
||
for (let round = 0; round < MAX_ROUNDS; round++) {
|
||
const next = [0];
|
||
for (let i = 1; i < at.length; i++) {
|
||
const strays = segmentGap(points[i - 1]!, points[i]!, ease) > TOLERANCE;
|
||
if (strays && next.length + at.length - i < MAX_POINTS) next.push((at[i - 1]! + at[i]!) / 2);
|
||
next.push(at[i]!);
|
||
}
|
||
if (next.length === at.length) break;
|
||
at = next;
|
||
points = traced(ease, at);
|
||
}
|
||
return points;
|
||
}
|
||
|
||
function traced(ease: (t: number) => number, at: readonly number[]): VectorPoint[] {
|
||
const value = (x: number) => clean(ease(x));
|
||
return at.map((x, i) => {
|
||
const point: VectorPoint = { x, y: value(x) };
|
||
const before = at[i - 1];
|
||
const after = at[i + 1];
|
||
const slopeIn = before === undefined ? null : (ease(x - H) - ease(x - 2 * H)) / H;
|
||
const slopeOut = after === undefined ? null : (ease(x + 2 * H) - ease(x + H)) / H;
|
||
if (before !== undefined && slopeIn !== null)
|
||
point.in = handle((before - x) / 3, slopeIn, point.y - value(before));
|
||
if (after !== undefined && slopeOut !== null)
|
||
point.out = handle((after - x) / 3, slopeOut, value(after) - point.y);
|
||
if (
|
||
slopeIn !== null &&
|
||
slopeOut !== null &&
|
||
Math.abs(slopeIn - slopeOut) < 1e-3 * Math.max(1, Math.abs(slopeOut))
|
||
)
|
||
point.mode = 'smooth';
|
||
return point;
|
||
});
|
||
}
|
||
|
||
/**
|
||
* A handle along `slope`, reaching `reach` in `t`. Where the ease stands upright – circ at its steep
|
||
* end – the slope is huge, so the handle is held to the rise of its segment plus one step, keeping
|
||
* its direction: nearly vertical, as the ease is there, instead of shooting off the graph.
|
||
*/
|
||
function handle(reach: number, slope: number, rise: number): Vec2 {
|
||
const y = slope * reach;
|
||
const most = Math.abs(rise) + Math.abs(reach) * 3;
|
||
const scale = Math.abs(y) > most ? most / Math.abs(y) : 1;
|
||
return { x: reach * scale, y: y * scale };
|
||
}
|
||
|
||
/** Rounding noise off, so an ease that starts at 0 starts at 0 and not at 2e-16. */
|
||
function clean(value: number): number {
|
||
return Math.round(value * 1e12) / 1e12;
|
||
}
|
||
|
||
function segmentGap(from: VectorPoint, to: VectorPoint, ease: (t: number) => number): number {
|
||
let gap = 0;
|
||
for (let i = 1; i < SAMPLES; i++) {
|
||
const t = from.x + ((to.x - from.x) * i) / SAMPLES;
|
||
gap = Math.max(gap, Math.abs(curveValue([from, to], t) - ease(t)));
|
||
}
|
||
return gap;
|
||
}
|