|
| 1 | +import type { Vector2D } from '../types.js'; |
| 2 | + |
| 3 | +export interface TopDownState { |
| 4 | + position: Vector2D; |
| 5 | + velocity: Vector2D; |
| 6 | + facing: Vector2D; |
| 7 | +} |
| 8 | + |
| 9 | +export interface TopDownInput { |
| 10 | + x: number; |
| 11 | + y: number; |
| 12 | +} |
| 13 | + |
| 14 | +export interface TopDownUpdateOptions { |
| 15 | + delta: number; |
| 16 | + input: TopDownInput; |
| 17 | +} |
| 18 | + |
| 19 | +export interface TopDownMovementOptions { |
| 20 | + acceleration: number; |
| 21 | + deceleration: number; |
| 22 | + maxSpeed: number; |
| 23 | + drag?: number; |
| 24 | + normalizeDiagonal?: boolean; |
| 25 | +} |
| 26 | + |
| 27 | +export interface TopDownController { |
| 28 | + update(options: TopDownUpdateOptions): TopDownState; |
| 29 | + getState(): TopDownState; |
| 30 | + reset(state?: Partial<TopDownState>): void; |
| 31 | + setOptions(options: Partial<TopDownMovementOptions>): void; |
| 32 | +} |
| 33 | + |
| 34 | +interface InternalOptions { |
| 35 | + acceleration: number; |
| 36 | + deceleration: number; |
| 37 | + maxSpeed: number; |
| 38 | + drag: number; |
| 39 | + normalizeDiagonal: boolean; |
| 40 | +} |
| 41 | + |
| 42 | +function assertFinite(value: number, label: string): void { |
| 43 | + if (typeof value !== 'number' || Number.isNaN(value) || !Number.isFinite(value)) { |
| 44 | + throw new Error(`${label} must be a finite number.`); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +function normalizeOptions(options: TopDownMovementOptions): InternalOptions { |
| 49 | + assertFinite(options.acceleration, 'acceleration'); |
| 50 | + assertFinite(options.deceleration, 'deceleration'); |
| 51 | + assertFinite(options.maxSpeed, 'maxSpeed'); |
| 52 | + if (options.acceleration <= 0 || options.deceleration <= 0) { |
| 53 | + throw new Error('acceleration and deceleration must be greater than 0.'); |
| 54 | + } |
| 55 | + if (options.maxSpeed <= 0) { |
| 56 | + throw new Error('maxSpeed must be greater than 0.'); |
| 57 | + } |
| 58 | + const drag = options.drag ?? 0; |
| 59 | + assertFinite(drag, 'drag'); |
| 60 | + if (drag < 0) { |
| 61 | + throw new Error('drag must be >= 0.'); |
| 62 | + } |
| 63 | + const normalizeDiagonal = options.normalizeDiagonal ?? true; |
| 64 | + |
| 65 | + return { |
| 66 | + acceleration: options.acceleration, |
| 67 | + deceleration: options.deceleration, |
| 68 | + maxSpeed: options.maxSpeed, |
| 69 | + drag, |
| 70 | + normalizeDiagonal, |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +function cloneState(state: TopDownState): TopDownState { |
| 75 | + return { |
| 76 | + position: { x: state.position.x, y: state.position.y }, |
| 77 | + velocity: { x: state.velocity.x, y: state.velocity.y }, |
| 78 | + facing: { x: state.facing.x, y: state.facing.y }, |
| 79 | + }; |
| 80 | +} |
| 81 | + |
| 82 | +function magnitude(x: number, y: number): number { |
| 83 | + return Math.hypot(x, y); |
| 84 | +} |
| 85 | + |
| 86 | +function normalize(x: number, y: number): Vector2D { |
| 87 | + const length = magnitude(x, y); |
| 88 | + if (length === 0) { |
| 89 | + return { x: 0, y: 0 }; |
| 90 | + } |
| 91 | + return { x: x / length, y: y / length }; |
| 92 | +} |
| 93 | + |
| 94 | +/** |
| 95 | + * Creates a top-down movement controller with acceleration and directional damping. |
| 96 | + * Useful for: twin-stick or tile-based characters needing eight-direction movement. |
| 97 | + */ |
| 98 | +export function createTopDownController( |
| 99 | + options: TopDownMovementOptions, |
| 100 | + initialState: TopDownState = { |
| 101 | + position: { x: 0, y: 0 }, |
| 102 | + velocity: { x: 0, y: 0 }, |
| 103 | + facing: { x: 1, y: 0 }, |
| 104 | + } |
| 105 | +): TopDownController { |
| 106 | + let config = normalizeOptions(options); |
| 107 | + const baseline = cloneState(initialState); |
| 108 | + const state: TopDownState = cloneState(initialState); |
| 109 | + |
| 110 | + function setOptions(partial: Partial<TopDownMovementOptions>): void { |
| 111 | + config = normalizeOptions({ |
| 112 | + acceleration: partial.acceleration ?? config.acceleration, |
| 113 | + deceleration: partial.deceleration ?? config.deceleration, |
| 114 | + maxSpeed: partial.maxSpeed ?? config.maxSpeed, |
| 115 | + drag: partial.drag ?? config.drag, |
| 116 | + normalizeDiagonal: partial.normalizeDiagonal ?? config.normalizeDiagonal, |
| 117 | + }); |
| 118 | + } |
| 119 | + |
| 120 | + function update({ delta, input }: TopDownUpdateOptions): TopDownState { |
| 121 | + assertFinite(delta, 'delta'); |
| 122 | + if (delta < 0) { |
| 123 | + throw new Error('delta must be >= 0.'); |
| 124 | + } |
| 125 | + if (!input) { |
| 126 | + throw new Error('input is required.'); |
| 127 | + } |
| 128 | + |
| 129 | + let moveX = input.x ?? 0; |
| 130 | + let moveY = input.y ?? 0; |
| 131 | + if (!Number.isFinite(moveX) || !Number.isFinite(moveY)) { |
| 132 | + throw new Error('input values must be finite numbers.'); |
| 133 | + } |
| 134 | + |
| 135 | + if (config.normalizeDiagonal) { |
| 136 | + const magnitudeInput = magnitude(moveX, moveY); |
| 137 | + if (magnitudeInput > 1) { |
| 138 | + moveX /= magnitudeInput; |
| 139 | + moveY /= magnitudeInput; |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + const hasInput = Math.abs(moveX) > 1e-3 || Math.abs(moveY) > 1e-3; |
| 144 | + |
| 145 | + if (hasInput) { |
| 146 | + const direction = normalize(moveX, moveY); |
| 147 | + state.velocity.x += direction.x * config.acceleration * delta; |
| 148 | + state.velocity.y += direction.y * config.acceleration * delta; |
| 149 | + |
| 150 | + const speed = magnitude(state.velocity.x, state.velocity.y); |
| 151 | + if (speed > config.maxSpeed) { |
| 152 | + const normalized = normalize(state.velocity.x, state.velocity.y); |
| 153 | + state.velocity.x = normalized.x * config.maxSpeed; |
| 154 | + state.velocity.y = normalized.y * config.maxSpeed; |
| 155 | + } |
| 156 | + |
| 157 | + state.facing.x = direction.x; |
| 158 | + state.facing.y = direction.y; |
| 159 | + } else { |
| 160 | + const speed = magnitude(state.velocity.x, state.velocity.y); |
| 161 | + if (speed > 0) { |
| 162 | + const decelAmount = config.deceleration * delta; |
| 163 | + if (speed <= decelAmount) { |
| 164 | + state.velocity.x = 0; |
| 165 | + state.velocity.y = 0; |
| 166 | + } else { |
| 167 | + const normalized = normalize(state.velocity.x, state.velocity.y); |
| 168 | + const newSpeed = speed - decelAmount; |
| 169 | + state.velocity.x = normalized.x * newSpeed; |
| 170 | + state.velocity.y = normalized.y * newSpeed; |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + if (config.drag > 0) { |
| 176 | + const dragFactor = Math.max(0, 1 - config.drag * delta); |
| 177 | + state.velocity.x *= dragFactor; |
| 178 | + state.velocity.y *= dragFactor; |
| 179 | + } |
| 180 | + |
| 181 | + state.position.x += state.velocity.x * delta; |
| 182 | + state.position.y += state.velocity.y * delta; |
| 183 | + |
| 184 | + return cloneState(state); |
| 185 | + } |
| 186 | + |
| 187 | + function getState(): TopDownState { |
| 188 | + return cloneState(state); |
| 189 | + } |
| 190 | + |
| 191 | + function reset(partial: Partial<TopDownState> = {}): void { |
| 192 | + const position = partial.position ?? baseline.position; |
| 193 | + const velocity = partial.velocity ?? baseline.velocity; |
| 194 | + const facing = partial.facing ?? baseline.facing; |
| 195 | + |
| 196 | + state.position.x = position.x; |
| 197 | + state.position.y = position.y; |
| 198 | + state.velocity.x = velocity.x; |
| 199 | + state.velocity.y = velocity.y; |
| 200 | + state.facing.x = facing.x; |
| 201 | + state.facing.y = facing.y; |
| 202 | + } |
| 203 | + |
| 204 | + return { |
| 205 | + update, |
| 206 | + getState, |
| 207 | + reset, |
| 208 | + setOptions, |
| 209 | + }; |
| 210 | +} |
| 211 | + |
| 212 | +/** @internal */ |
| 213 | +export const __internals = { |
| 214 | + normalizeOptions, |
| 215 | + normalize, |
| 216 | + magnitude, |
| 217 | +}; |
0 commit comments