@inglorious/engine
Version:
A JavaScript game engine written with global state, immutability, and pure functions in mind. Have fun(ctional programming) with it!
40 lines (32 loc) • 1.03 kB
JavaScript
import { matchVelocity } from "@inglorious/engine/ai/movement/dynamic/match-velocity.js"
import { magnitude, setMagnitude } from "@inglorious/utils/math/vector.js"
import { subtract } from "@inglorious/utils/math/vectors.js"
export const DEFAULT_TARGET_RADIUS = 1
export const DEFAULT_SLOW_RADIUS = 100
export const DEFAULT_TIME_TO_TARGET = 0.1
const DEFAULT_MAX_SPEED = 0
export function arrive(
entity,
target,
dt,
{
targetRadius = DEFAULT_TARGET_RADIUS,
slowRadius = DEFAULT_SLOW_RADIUS,
timeToTarget = DEFAULT_TIME_TO_TARGET,
} = {},
) {
const maxSpeed = entity.maxSpeed ?? DEFAULT_MAX_SPEED
const direction = subtract(target.position, entity.position)
const distance = magnitude(direction)
if (distance < targetRadius) {
return entity
}
let speed
if (distance > slowRadius) {
speed = maxSpeed
} else {
speed = (distance * maxSpeed) / slowRadius
}
const velocity = setMagnitude(direction, speed)
return matchVelocity(entity, { velocity }, dt, { timeToTarget })
}