@reicek/neataptic-ts 0.1.21 → 0.1.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/agents/boundary-mapper.agent.md +29 -0
- package/.github/agents/docs-scout.agent.md +29 -0
- package/.github/agents/plan-scout.agent.md +29 -0
- package/.github/agents/solid-split.agent.md +138 -0
- package/.github/copilot-instructions.md +103 -0
- package/package.json +6 -3
- package/plans/ES2023 migration +13 -8
- package/plans/Evolution_Training_Interoperability_Contracts.md +1 -1
- package/plans/Interactive_Examples_and_Learning_Path.md +10 -2
- package/plans/Memory_Optimization.md +3 -3
- package/plans/README.md +63 -0
- package/plans/Roadmap.md +15 -3
- package/plans/asciiMaze_SOLID_split.done.md +130 -0
- package/plans/flappy_bird_SOLID_split.done.md +67 -0
- package/scripts/assets/theme.css +221 -34
- package/scripts/copy-examples.mjs +9 -5
- package/scripts/export-onnx.mjs +3 -3
- package/scripts/generate-bench-tables.mjs +10 -10
- package/scripts/generate-bench-tables.ts +10 -10
- package/scripts/generate-docs.ts +1415 -449
- package/scripts/render-docs-html.ts +15 -8
- package/src/README.md +101 -223
- package/src/architecture/README.md +57 -185
- package/src/architecture/layer/README.md +38 -38
- package/src/architecture/network/README.md +33 -31
- package/src/architecture/network/activate/README.md +77 -77
- package/src/architecture/network/connect/README.md +15 -13
- package/src/architecture/network/deterministic/README.md +7 -7
- package/src/architecture/network/evolve/README.md +44 -44
- package/src/architecture/network/gating/README.md +20 -20
- package/src/architecture/network/genetic/README.md +51 -51
- package/src/architecture/network/mutate/README.md +97 -97
- package/src/architecture/network/onnx/README.md +264 -264
- package/src/architecture/network/prune/README.md +39 -39
- package/src/architecture/network/remove/README.md +26 -26
- package/src/architecture/network/serialize/README.md +56 -56
- package/src/architecture/network/slab/README.md +61 -61
- package/src/architecture/network/standalone/README.md +24 -24
- package/src/architecture/network/stats/README.md +9 -9
- package/src/architecture/network/topology/README.md +46 -46
- package/src/architecture/network/training/README.md +21 -21
- package/src/methods/README.md +9 -87
- package/src/multithreading/README.md +8 -77
- package/src/multithreading/workers/README.md +2 -2
- package/src/multithreading/workers/browser/README.md +0 -6
- package/src/multithreading/workers/node/README.md +0 -3
- package/src/neat/README.md +562 -568
- package/src/utils/README.md +18 -18
- package/test/examples/asciiMaze/README.md +59 -59
- package/test/examples/asciiMaze/asciiMaze.e2e.test.ts +14 -9
- package/test/examples/asciiMaze/browser-entry/README.md +196 -0
- package/test/examples/asciiMaze/browser-entry/browser-entry.abort.services.ts +95 -0
- package/test/examples/asciiMaze/browser-entry/browser-entry.constants.ts +23 -0
- package/test/examples/asciiMaze/browser-entry/browser-entry.curriculum.services.ts +115 -0
- package/test/examples/asciiMaze/browser-entry/browser-entry.globals.services.ts +106 -0
- package/test/examples/asciiMaze/browser-entry/browser-entry.host.services.ts +157 -0
- package/test/examples/asciiMaze/browser-entry/browser-entry.services.ts +14 -0
- package/test/examples/asciiMaze/browser-entry/browser-entry.ts +129 -0
- package/test/examples/asciiMaze/browser-entry/browser-entry.types.ts +120 -0
- package/test/examples/asciiMaze/browser-entry/browser-entry.utils.ts +98 -0
- package/test/examples/asciiMaze/browser-entry.ts +10 -576
- package/test/examples/asciiMaze/dashboardManager/README.md +276 -0
- package/test/examples/asciiMaze/dashboardManager/archive/README.md +16 -0
- package/test/examples/asciiMaze/dashboardManager/archive/dashboardManager.archive.services.ts +267 -0
- package/test/examples/asciiMaze/dashboardManager/dashboardManager.constants.ts +35 -0
- package/test/examples/asciiMaze/dashboardManager/dashboardManager.services.ts +103 -0
- package/test/examples/asciiMaze/dashboardManager/dashboardManager.ts +181 -0
- package/test/examples/asciiMaze/dashboardManager/dashboardManager.types.ts +267 -0
- package/test/examples/asciiMaze/dashboardManager/dashboardManager.utils.ts +254 -0
- package/test/examples/asciiMaze/dashboardManager/live/README.md +14 -0
- package/test/examples/asciiMaze/dashboardManager/live/dashboardManager.live.services.ts +264 -0
- package/test/examples/asciiMaze/dashboardManager/telemetry/README.md +47 -0
- package/test/examples/asciiMaze/dashboardManager/telemetry/dashboardManager.telemetry.services.ts +513 -0
- package/test/examples/asciiMaze/dashboardManager.ts +13 -2335
- package/test/examples/asciiMaze/evolutionEngine/README.md +1058 -0
- package/test/examples/asciiMaze/evolutionEngine/curriculumPhase.ts +90 -0
- package/test/examples/asciiMaze/evolutionEngine/engineState.constants.ts +36 -0
- package/test/examples/asciiMaze/evolutionEngine/engineState.ts +58 -513
- package/test/examples/asciiMaze/evolutionEngine/engineState.types.ts +212 -0
- package/test/examples/asciiMaze/evolutionEngine/engineState.utils.ts +301 -0
- package/test/examples/asciiMaze/evolutionEngine/evolutionEngine.types.ts +445 -0
- package/test/examples/asciiMaze/evolutionEngine/evolutionLoop.ts +81 -50
- package/test/examples/asciiMaze/evolutionEngine/optionsAndSetup.ts +2 -4
- package/test/examples/asciiMaze/evolutionEngine/populationDynamics.ts +17 -33
- package/test/examples/asciiMaze/evolutionEngine/populationPruning.ts +1 -1
- package/test/examples/asciiMaze/evolutionEngine/rngAndTiming.ts +1 -2
- package/test/examples/asciiMaze/evolutionEngine/sampling.ts +1 -1
- package/test/examples/asciiMaze/evolutionEngine/scratchPools.ts +2 -5
- package/test/examples/asciiMaze/evolutionEngine/setupHelpers.ts +30 -37
- package/test/examples/asciiMaze/evolutionEngine/telemetryMetrics.ts +16 -58
- package/test/examples/asciiMaze/evolutionEngine/trainingWarmStart.ts +2 -2
- package/test/examples/asciiMaze/evolutionEngine.ts +55 -55
- package/test/examples/asciiMaze/fitness.ts +2 -2
- package/test/examples/asciiMaze/fitness.types.ts +65 -0
- package/test/examples/asciiMaze/interfaces.ts +64 -1352
- package/test/examples/asciiMaze/mazeMovement/README.md +356 -0
- package/test/examples/asciiMaze/mazeMovement/finalization/README.md +49 -0
- package/test/examples/asciiMaze/mazeMovement/finalization/mazeMovement.finalization.ts +138 -0
- package/test/examples/asciiMaze/mazeMovement/mazeMovement.constants.ts +101 -0
- package/test/examples/asciiMaze/mazeMovement/mazeMovement.services.ts +230 -0
- package/test/examples/asciiMaze/mazeMovement/mazeMovement.ts +299 -0
- package/test/examples/asciiMaze/mazeMovement/mazeMovement.types.ts +185 -0
- package/test/examples/asciiMaze/mazeMovement/mazeMovement.utils.ts +153 -0
- package/test/examples/asciiMaze/mazeMovement/policy/README.md +91 -0
- package/test/examples/asciiMaze/mazeMovement/policy/mazeMovement.policy.ts +467 -0
- package/test/examples/asciiMaze/mazeMovement/runtime/README.md +95 -0
- package/test/examples/asciiMaze/mazeMovement/runtime/mazeMovement.runtime.ts +354 -0
- package/test/examples/asciiMaze/mazeMovement/shaping/README.md +124 -0
- package/test/examples/asciiMaze/mazeMovement/shaping/mazeMovement.shaping.ts +459 -0
- package/test/examples/asciiMaze/mazeMovement.ts +12 -2978
- package/test/examples/flappy_bird/Trace-20260309T191949.json +24124 -0
- package/test/examples/flappy_bird/browser-entry/README.md +1129 -0
- package/test/examples/flappy_bird/browser-entry/browser-entry.host.utils.ts +4 -324
- package/test/examples/flappy_bird/browser-entry/browser-entry.network-view.utils.ts +6 -399
- package/test/examples/flappy_bird/browser-entry/browser-entry.playback.utils.ts +1 -717
- package/test/examples/flappy_bird/browser-entry/browser-entry.spawn.utils.ts +11 -31
- package/test/examples/flappy_bird/browser-entry/browser-entry.visualization.utils.ts +15 -893
- package/test/examples/flappy_bird/browser-entry/host/README.md +307 -0
- package/test/examples/flappy_bird/browser-entry/host/host.resize.service.ts +1 -295
- package/test/examples/flappy_bird/browser-entry/host/host.ts +562 -6
- package/test/examples/flappy_bird/browser-entry/host/resize/README.md +274 -0
- package/test/examples/flappy_bird/browser-entry/host/resize/host.resize.service.constants.ts +31 -0
- package/test/examples/flappy_bird/browser-entry/host/resize/host.resize.service.services.ts +360 -0
- package/test/examples/flappy_bird/browser-entry/host/resize/host.resize.service.ts +117 -0
- package/test/examples/flappy_bird/browser-entry/host/resize/host.resize.service.types.ts +63 -0
- package/test/examples/flappy_bird/browser-entry/host/resize/host.resize.service.utils.ts +250 -0
- package/test/examples/flappy_bird/browser-entry/network-view/README.md +399 -0
- package/test/examples/flappy_bird/browser-entry/network-view/network-view.topology.utils.ts +255 -0
- package/test/examples/flappy_bird/browser-entry/network-view/network-view.ts +802 -7
- package/test/examples/flappy_bird/browser-entry/playback/README.md +684 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/README.md +277 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/README.md +770 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.cache.services.ts +178 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.constants.ts +107 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.geometry.utils.ts +518 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.math.utils.ts +117 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.pulse.utils.ts +233 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.services.ts +211 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.ts +48 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.types.ts +212 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/ground-grid/playback.background.ground-grid.utils.ts +81 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/playback.background.cache.services.ts +96 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/playback.background.constants.ts +62 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/playback.background.services.ts +244 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/playback.background.ts +53 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/playback.background.types.ts +68 -0
- package/test/examples/flappy_bird/browser-entry/playback/background/playback.background.utils.ts +100 -0
- package/test/examples/flappy_bird/browser-entry/playback/frame-render/README.md +310 -0
- package/test/examples/flappy_bird/browser-entry/playback/frame-render/playback.frame-render.service.ts +92 -0
- package/test/examples/flappy_bird/browser-entry/playback/frame-render/playback.frame-render.services.ts +272 -0
- package/test/examples/flappy_bird/browser-entry/playback/frame-render/playback.frame-render.types.ts +39 -0
- package/test/examples/flappy_bird/browser-entry/playback/frame-render/playback.frame-render.utils.ts +493 -0
- package/test/examples/flappy_bird/browser-entry/playback/playback.constants.ts +1 -1
- package/test/examples/flappy_bird/browser-entry/playback/playback.frame-render.service.ts +4 -0
- package/test/examples/flappy_bird/browser-entry/playback/playback.snapshot.utils.ts +44 -0
- package/test/examples/flappy_bird/browser-entry/playback/playback.starfield.service.ts +39 -122
- package/test/examples/flappy_bird/browser-entry/playback/playback.starfield.services.ts +272 -0
- package/test/examples/flappy_bird/browser-entry/playback/playback.starfield.types.ts +62 -0
- package/test/examples/flappy_bird/browser-entry/playback/playback.starfield.utils.ts +11 -4
- package/test/examples/flappy_bird/browser-entry/playback/playback.ts +409 -8
- package/test/examples/flappy_bird/browser-entry/playback/playback.types.ts +4 -12
- package/test/examples/flappy_bird/browser-entry/runtime/README.md +235 -0
- package/test/examples/flappy_bird/browser-entry/runtime/runtime.evolution-launch.service.ts +45 -0
- package/test/examples/flappy_bird/browser-entry/runtime/runtime.lifecycle.service.ts +81 -0
- package/test/examples/flappy_bird/browser-entry/runtime/runtime.startup.service.ts +74 -0
- package/test/examples/flappy_bird/browser-entry/runtime/runtime.ts +31 -121
- package/test/examples/flappy_bird/browser-entry/runtime/runtime.types.ts +36 -0
- package/test/examples/flappy_bird/browser-entry/visualization/README.md +557 -0
- package/test/examples/flappy_bird/browser-entry/visualization/visualization.constants.ts +110 -0
- package/test/examples/flappy_bird/browser-entry/visualization/visualization.draw.service.ts +957 -19
- package/test/examples/flappy_bird/browser-entry/visualization/visualization.legend.utils.ts +138 -3
- package/test/examples/flappy_bird/browser-entry/visualization/visualization.topology.utils.ts +3 -27
- package/test/examples/flappy_bird/browser-entry/visualization/visualization.ts +1 -23
- package/test/examples/flappy_bird/browser-entry/worker-channel/README.md +156 -0
- package/test/examples/flappy_bird/constants/README.md +1179 -0
- package/test/examples/flappy_bird/constants/constants.network-view.ts +24 -0
- package/test/examples/flappy_bird/constants/constants.palette.ts +7 -0
- package/test/examples/flappy_bird/constants/constants.starfield.ts +78 -3
- package/test/examples/flappy_bird/environment/README.md +143 -0
- package/test/examples/flappy_bird/environment/environment.observation.utils.ts +1 -19
- package/test/examples/flappy_bird/environment/environment.step.service.ts +3 -66
- package/test/examples/flappy_bird/evaluation/README.md +130 -0
- package/test/examples/flappy_bird/evaluation/evaluation.fitness.utils.ts +1 -1
- package/test/examples/flappy_bird/evaluation/evaluation.rollout.service.ts +5 -375
- package/test/examples/flappy_bird/evaluation/rollout/README.md +291 -0
- package/test/examples/flappy_bird/evaluation/rollout/evaluation.rollout.constants.ts +30 -0
- package/test/examples/flappy_bird/evaluation/rollout/evaluation.rollout.service.ts +58 -0
- package/test/examples/flappy_bird/evaluation/rollout/evaluation.rollout.services.ts +310 -0
- package/test/examples/flappy_bird/evaluation/rollout/evaluation.rollout.types.ts +56 -0
- package/test/examples/flappy_bird/evaluation/rollout/evaluation.rollout.utils.ts +368 -0
- package/test/examples/flappy_bird/flappy-evolution-worker/README.md +618 -0
- package/test/examples/flappy_bird/flappy-evolution-worker/flappy-evolution-worker.playback.service.ts +7 -7
- package/test/examples/flappy_bird/flappy-evolution-worker/flappy-evolution-worker.simulation.frame.service.ts +364 -0
- package/test/examples/flappy_bird/flappy-evolution-worker/flappy-evolution-worker.simulation.types.ts +14 -0
- package/test/examples/flappy_bird/flappy-evolution-worker/flappy-evolution-worker.simulation.utils.ts +4 -201
- package/test/examples/flappy_bird/flappy-evolution-worker/flappy-evolution-worker.ts +184 -345
- package/test/examples/flappy_bird/flappy-evolution-worker/flappy-evolution-worker.warm-start.service.ts +291 -0
- package/test/examples/flappy_bird/flappy.simulation.shared.utils.ts +5 -0
- package/test/examples/flappy_bird/simulation-shared/README.md +417 -0
- package/test/examples/flappy_bird/simulation-shared/observation/README.md +183 -0
- package/test/examples/flappy_bird/simulation-shared/observation/observation.features.utils.ts +301 -0
- package/test/examples/flappy_bird/simulation-shared/observation/observation.ts +9 -0
- package/test/examples/flappy_bird/simulation-shared/observation/observation.vector.utils.ts +59 -0
- package/test/examples/flappy_bird/simulation-shared/simulation-shared.observation.utils.ts +5 -403
- package/test/examples/flappy_bird/simulation-shared/simulation-shared.spawn.utils.ts +20 -6
- package/test/examples/flappy_bird/{evaluation/evaluation.statistics.utils.ts → simulation-shared/simulation-shared.statistics.utils.ts} +23 -8
- package/test/examples/flappy_bird/trainer/README.md +563 -0
- package/test/examples/flappy_bird/trainer/evaluation/README.md +199 -0
- package/test/examples/flappy_bird/trainer/evaluation/trainer.evaluation.service.constants.ts +9 -0
- package/test/examples/flappy_bird/trainer/evaluation/trainer.evaluation.service.services.ts +73 -0
- package/test/examples/flappy_bird/trainer/evaluation/trainer.evaluation.service.ts +165 -0
- package/test/examples/flappy_bird/trainer/evaluation/trainer.evaluation.service.types.ts +25 -0
- package/test/examples/flappy_bird/trainer/evaluation/trainer.evaluation.service.utils.ts +161 -0
- package/test/examples/flappy_bird/trainer/trainer.evaluation.service.ts +13 -0
- package/test/examples/flappy_bird/trainer/trainer.report.service.services.ts +181 -0
- package/test/examples/flappy_bird/trainer/trainer.report.service.ts +126 -0
- package/test/examples/flappy_bird/trainer/trainer.selection.utils.ts +89 -0
- package/test/examples/flappy_bird/trainer/trainer.ts +11 -553
- package/test/examples/flappy_bird/browser-entry/browser-entry.utils.ts +0 -12
- package/test/examples/flappy_bird/environment/environment.ts +0 -7
- package/test/examples/flappy_bird/evaluation/evaluation.ts +0 -7
- package/test/examples/flappy_bird/simulation-shared/simulation-shared.ts +0 -15
- package/test/examples/flappy_bird/trainer/trainer.statistics.utils.ts +0 -78
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import {
|
|
2
|
+
FLAPPY_BIRD_RADIUS_PX,
|
|
3
|
+
FLAPPY_BIRD_VIEWPORT_X_RATIO,
|
|
4
|
+
FLAPPY_BIRD_X_PX,
|
|
5
|
+
FLAPPY_NEON_PALETTE,
|
|
6
|
+
FLAPPY_PIPE_SPEED_PX_PER_FRAME,
|
|
7
|
+
FLAPPY_PIPE_WIDTH_PX,
|
|
8
|
+
} from '../../../constants/constants';
|
|
9
|
+
import type {
|
|
10
|
+
PopulationRenderState,
|
|
11
|
+
TrailPoint,
|
|
12
|
+
TrailState,
|
|
13
|
+
} from '../../browser-entry.types';
|
|
14
|
+
import { resolveWorldViewport } from '../../browser-entry.viewport.utils';
|
|
15
|
+
import { renderPlaybackBackground } from '../background/playback.background';
|
|
16
|
+
import { drawPipeNeonOutline } from '../playback.render.service';
|
|
17
|
+
import { resolveChampionBirdIndex } from '../playback.render.utils';
|
|
18
|
+
import type {
|
|
19
|
+
PlaybackFrameSceneContext,
|
|
20
|
+
PlaybackTrailRenderStyle,
|
|
21
|
+
} from './playback.frame-render.types';
|
|
22
|
+
|
|
23
|
+
type PlaybackBirdRenderer = (
|
|
24
|
+
context: CanvasRenderingContext2D,
|
|
25
|
+
birdYPx: number,
|
|
26
|
+
birdIndex: number,
|
|
27
|
+
championBirdIndex: number,
|
|
28
|
+
) => void;
|
|
29
|
+
|
|
30
|
+
type PlaybackTrailStyleResolver = (
|
|
31
|
+
birdIndex: number,
|
|
32
|
+
championBirdIndex: number,
|
|
33
|
+
) => PlaybackTrailRenderStyle;
|
|
34
|
+
|
|
35
|
+
type PlaybackTrailRenderer = (
|
|
36
|
+
context: CanvasRenderingContext2D,
|
|
37
|
+
trailPoints: TrailPoint[],
|
|
38
|
+
color: string,
|
|
39
|
+
anchorX: number,
|
|
40
|
+
baseOpacity: number,
|
|
41
|
+
edgeBounds: PlaybackFrameSceneContext['edgeBounds'],
|
|
42
|
+
) => void;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Resolves the shared scene contract used by one frame render pass.
|
|
46
|
+
*
|
|
47
|
+
* @param context - Canvas 2D drawing context.
|
|
48
|
+
* @param renderState - Mutable simulation state snapshot.
|
|
49
|
+
* @returns Viewport, camera, and edge-bounds state for the frame.
|
|
50
|
+
*/
|
|
51
|
+
export function resolvePlaybackFrameSceneContext(
|
|
52
|
+
context: CanvasRenderingContext2D,
|
|
53
|
+
renderState: PopulationRenderState,
|
|
54
|
+
): PlaybackFrameSceneContext {
|
|
55
|
+
// Step 1: Resolve viewport scaling and visible world bounds.
|
|
56
|
+
const viewport = resolveWorldViewport(context.canvas);
|
|
57
|
+
const visibleWorldWidthPx = Math.max(1, renderState.visibleWorldWidthPx);
|
|
58
|
+
const visibleWorldHeightPx = Math.max(1, renderState.visibleWorldHeightPx);
|
|
59
|
+
|
|
60
|
+
// Step 2: Resolve camera position and champion index for this frame.
|
|
61
|
+
const desiredBirdScreenXPx =
|
|
62
|
+
visibleWorldWidthPx * FLAPPY_BIRD_VIEWPORT_X_RATIO;
|
|
63
|
+
const cameraLeftPx = FLAPPY_BIRD_X_PX - desiredBirdScreenXPx;
|
|
64
|
+
const championBirdIndex = resolveChampionBirdIndex(renderState);
|
|
65
|
+
|
|
66
|
+
// Step 3: Return the shared frame scene context.
|
|
67
|
+
return {
|
|
68
|
+
viewport,
|
|
69
|
+
visibleWorldWidthPx,
|
|
70
|
+
visibleWorldHeightPx,
|
|
71
|
+
cameraLeftPx,
|
|
72
|
+
championBirdIndex,
|
|
73
|
+
edgeBounds: {
|
|
74
|
+
leftXPx: cameraLeftPx,
|
|
75
|
+
rightXPx: cameraLeftPx + visibleWorldWidthPx,
|
|
76
|
+
topYPx: 0,
|
|
77
|
+
bottomYPx: visibleWorldHeightPx,
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Resets the target canvas and base paint state before frame drawing begins.
|
|
84
|
+
*
|
|
85
|
+
* @param context - Canvas 2D drawing context.
|
|
86
|
+
* @returns Nothing.
|
|
87
|
+
*/
|
|
88
|
+
export function preparePlaybackFrameCanvas(
|
|
89
|
+
context: CanvasRenderingContext2D,
|
|
90
|
+
): void {
|
|
91
|
+
// Step 1: Reset base paint state to a predictable source-over draw pass.
|
|
92
|
+
context.globalAlpha = 1;
|
|
93
|
+
context.globalCompositeOperation = 'source-over';
|
|
94
|
+
context.shadowBlur = 0;
|
|
95
|
+
context.shadowColor = 'transparent';
|
|
96
|
+
context.shadowOffsetX = 0;
|
|
97
|
+
context.shadowOffsetY = 0;
|
|
98
|
+
|
|
99
|
+
// Step 2: Clear the full backing canvas before drawing the next frame.
|
|
100
|
+
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Applies the viewport transform used for world-space frame rendering.
|
|
105
|
+
*
|
|
106
|
+
* @param context - Canvas 2D drawing context.
|
|
107
|
+
* @param sceneContext - Shared scene geometry for the frame.
|
|
108
|
+
* @returns Nothing.
|
|
109
|
+
*/
|
|
110
|
+
export function beginPlaybackFrameViewportTransform(
|
|
111
|
+
context: CanvasRenderingContext2D,
|
|
112
|
+
sceneContext: PlaybackFrameSceneContext,
|
|
113
|
+
): void {
|
|
114
|
+
// Step 1: Save the caller state before applying viewport-space transforms.
|
|
115
|
+
context.save();
|
|
116
|
+
|
|
117
|
+
// Step 2: Translate and scale into the current world viewport.
|
|
118
|
+
context.translate(
|
|
119
|
+
sceneContext.viewport.offsetXPx,
|
|
120
|
+
sceneContext.viewport.offsetYPx,
|
|
121
|
+
);
|
|
122
|
+
context.scale(sceneContext.viewport.scale, sceneContext.viewport.scale);
|
|
123
|
+
context.translate(-sceneContext.cameraLeftPx, 0);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Draws the split playback background for the current world viewport.
|
|
128
|
+
*
|
|
129
|
+
* @param context - Canvas 2D drawing context.
|
|
130
|
+
* @param renderState - Mutable simulation state snapshot.
|
|
131
|
+
* @param sceneContext - Shared scene geometry for the frame.
|
|
132
|
+
* @returns Nothing.
|
|
133
|
+
*/
|
|
134
|
+
export function renderPlaybackFrameBackground(
|
|
135
|
+
context: CanvasRenderingContext2D,
|
|
136
|
+
renderState: PopulationRenderState,
|
|
137
|
+
sceneContext: PlaybackFrameSceneContext,
|
|
138
|
+
): void {
|
|
139
|
+
// Step 1: Render the world-space background behind all gameplay entities.
|
|
140
|
+
renderPlaybackBackground(context, {
|
|
141
|
+
viewportLeftXPx: sceneContext.cameraLeftPx,
|
|
142
|
+
visibleWorldWidthPx: sceneContext.visibleWorldWidthPx,
|
|
143
|
+
visibleWorldHeightPx: sceneContext.visibleWorldHeightPx,
|
|
144
|
+
frameIndex: renderState.frameIndex,
|
|
145
|
+
scrollBasePx: renderState.frameIndex * FLAPPY_PIPE_SPEED_PX_PER_FRAME,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Draws all visible pipe segments and their neon outlines for the frame.
|
|
151
|
+
*
|
|
152
|
+
* @param context - Canvas 2D drawing context.
|
|
153
|
+
* @param renderState - Mutable simulation state snapshot.
|
|
154
|
+
* @param sceneContext - Shared scene geometry for the frame.
|
|
155
|
+
* @returns Nothing.
|
|
156
|
+
*/
|
|
157
|
+
export function renderPlaybackFramePipes(
|
|
158
|
+
context: CanvasRenderingContext2D,
|
|
159
|
+
renderState: PopulationRenderState,
|
|
160
|
+
sceneContext: PlaybackFrameSceneContext,
|
|
161
|
+
): void {
|
|
162
|
+
// Step 1: Render each pipe pair against the current world height.
|
|
163
|
+
for (const pipe of renderState.pipes) {
|
|
164
|
+
const gapHalfPx = pipe.gapSizePx * 0.5;
|
|
165
|
+
const gapTopPx = pipe.gapCenterYPx - gapHalfPx;
|
|
166
|
+
const gapBottomPx = pipe.gapCenterYPx + gapHalfPx;
|
|
167
|
+
|
|
168
|
+
context.fillStyle = FLAPPY_NEON_PALETTE.pipeFill;
|
|
169
|
+
context.fillRect(pipe.xPx, 0, FLAPPY_PIPE_WIDTH_PX, gapTopPx);
|
|
170
|
+
drawPipeNeonOutline(context, pipe.xPx, 0, FLAPPY_PIPE_WIDTH_PX, gapTopPx);
|
|
171
|
+
|
|
172
|
+
context.fillRect(
|
|
173
|
+
pipe.xPx,
|
|
174
|
+
gapBottomPx,
|
|
175
|
+
FLAPPY_PIPE_WIDTH_PX,
|
|
176
|
+
sceneContext.visibleWorldHeightPx - gapBottomPx,
|
|
177
|
+
);
|
|
178
|
+
drawPipeNeonOutline(
|
|
179
|
+
context,
|
|
180
|
+
pipe.xPx,
|
|
181
|
+
gapBottomPx,
|
|
182
|
+
FLAPPY_PIPE_WIDTH_PX,
|
|
183
|
+
sceneContext.visibleWorldHeightPx - gapBottomPx,
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Draws all active birds for the current frame.
|
|
190
|
+
*
|
|
191
|
+
* @param context - Canvas 2D drawing context.
|
|
192
|
+
* @param renderState - Mutable simulation state snapshot.
|
|
193
|
+
* @param sceneContext - Shared scene geometry for the frame.
|
|
194
|
+
* @param renderBird - Bird body renderer owned by the detailed utility layer.
|
|
195
|
+
* @returns Nothing.
|
|
196
|
+
*/
|
|
197
|
+
export function renderPlaybackFrameBirds(
|
|
198
|
+
context: CanvasRenderingContext2D,
|
|
199
|
+
renderState: PopulationRenderState,
|
|
200
|
+
sceneContext: PlaybackFrameSceneContext,
|
|
201
|
+
renderBird: PlaybackBirdRenderer,
|
|
202
|
+
): void {
|
|
203
|
+
// Step 1: Render only currently active birds.
|
|
204
|
+
renderState.birds.forEach((bird, birdIndex) => {
|
|
205
|
+
if (bird.done) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
renderBird(context, bird.yPx, birdIndex, sceneContext.championBirdIndex);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Draws stepped trails for all active birds in the frame.
|
|
215
|
+
*
|
|
216
|
+
* @param context - Canvas 2D drawing context.
|
|
217
|
+
* @param renderState - Mutable simulation state snapshot.
|
|
218
|
+
* @param trailState - Leader trail render cache.
|
|
219
|
+
* @param sceneContext - Shared scene geometry for the frame.
|
|
220
|
+
* @param resolveTrailStyle - Trail style resolver owned by the detailed utility layer.
|
|
221
|
+
* @param renderTrail - Trail segment renderer owned by the detailed utility layer.
|
|
222
|
+
* @returns Nothing.
|
|
223
|
+
*/
|
|
224
|
+
export function renderPlaybackFrameTrails(
|
|
225
|
+
context: CanvasRenderingContext2D,
|
|
226
|
+
renderState: PopulationRenderState,
|
|
227
|
+
trailState: TrailState,
|
|
228
|
+
sceneContext: PlaybackFrameSceneContext,
|
|
229
|
+
resolveTrailStyle: PlaybackTrailStyleResolver,
|
|
230
|
+
renderTrail: PlaybackTrailRenderer,
|
|
231
|
+
): void {
|
|
232
|
+
// Step 1: Render only trails for active birds with available trail data.
|
|
233
|
+
renderState.birds.forEach((bird, birdIndex) => {
|
|
234
|
+
if (bird.done) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const birdTrailPoints = trailState.birdTrailsY[birdIndex];
|
|
239
|
+
if (!birdTrailPoints || birdTrailPoints.length === 0) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const { baseOpacity, trailColor } = resolveTrailStyle(
|
|
244
|
+
birdIndex,
|
|
245
|
+
sceneContext.championBirdIndex,
|
|
246
|
+
);
|
|
247
|
+
renderTrail(
|
|
248
|
+
context,
|
|
249
|
+
birdTrailPoints,
|
|
250
|
+
trailColor,
|
|
251
|
+
FLAPPY_BIRD_X_PX - FLAPPY_BIRD_RADIUS_PX,
|
|
252
|
+
baseOpacity,
|
|
253
|
+
sceneContext.edgeBounds,
|
|
254
|
+
);
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Restores the caller canvas state after viewport-space frame drawing.
|
|
260
|
+
*
|
|
261
|
+
* @param context - Canvas 2D drawing context.
|
|
262
|
+
* @returns Nothing.
|
|
263
|
+
*/
|
|
264
|
+
export function finalizePlaybackFrameCanvas(
|
|
265
|
+
context: CanvasRenderingContext2D,
|
|
266
|
+
): void {
|
|
267
|
+
// Step 1: Reset global alpha before restoring the caller transform state.
|
|
268
|
+
context.globalAlpha = 1;
|
|
269
|
+
|
|
270
|
+
// Step 2: Restore the caller state saved before viewport transforms.
|
|
271
|
+
context.restore();
|
|
272
|
+
}
|
package/test/examples/flappy_bird/browser-entry/playback/frame-render/playback.frame-render.types.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { resolveWorldViewport } from '../../browser-entry.viewport.utils';
|
|
2
|
+
import type { PlaybackEdgeBounds } from '../playback.types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Local type contracts for playback frame rendering.
|
|
6
|
+
*
|
|
7
|
+
* These types are extracted from the broader frame renderer so scene state,
|
|
8
|
+
* bird geometry, and trail styling can evolve behind a dedicated module
|
|
9
|
+
* boundary.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Shared scene contract used by one playback frame render pass.
|
|
14
|
+
*/
|
|
15
|
+
export type PlaybackFrameSceneContext = {
|
|
16
|
+
viewport: ReturnType<typeof resolveWorldViewport>;
|
|
17
|
+
visibleWorldWidthPx: number;
|
|
18
|
+
visibleWorldHeightPx: number;
|
|
19
|
+
cameraLeftPx: number;
|
|
20
|
+
championBirdIndex: number;
|
|
21
|
+
edgeBounds: PlaybackEdgeBounds;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Pixel-aligned square geometry used by bird paint helpers.
|
|
26
|
+
*/
|
|
27
|
+
export type PlaybackBirdGeometry = {
|
|
28
|
+
birdSideLengthPx: number;
|
|
29
|
+
birdLeftPx: number;
|
|
30
|
+
birdTopPx: number;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Resolved opacity and color for one bird trail render pass.
|
|
35
|
+
*/
|
|
36
|
+
export type PlaybackTrailRenderStyle = {
|
|
37
|
+
baseOpacity: number;
|
|
38
|
+
trailColor: string;
|
|
39
|
+
};
|