Log inGet started
World

Bastion Works: Towers, Turrets and Targeting Kit

README.md

turretsKit.aim

How far and how fast a turret can turn, as pure functions. Angles are in degrees, relative to the turret's home facing (the way its entity's +Z points). Yaw turns left (+) and right (-) about the vertical; pitch tilts the barrel up (+) and down (-).

A turret with limits never turns past them: step clamps the aim, and turretsKit.targeting.inArc skips anything outside the arc. Used by the turretsKit.Turret component every frame and by targeting.

The ctx table

{ origin = { x, y, z } (pivot), homeYaw (radians), yawMin, yawMax, pitchMin, pitchMax, yawSpeed, pitchSpeed (degrees, degrees per second), aimHeight }. yawMin = -180, yawMax = 180 means all round.

Functions

  • wrap(a: number): number: wraps degrees to (-180, 180].
  • fullCircle(ctx: any): boolean: whether the yaw limits cover the full circle.
  • anglesTo(ctx: any, pos: { number }): (number, number): relative yaw and pitch from ctx.origin toward pos (plus ctx.aimHeight).
  • yawAllowed(ctx: any, yaw: number): boolean: yaw within the left/right limits.
  • pitchAllowed(ctx: any, pitch: number): boolean: pitch within the up/down limits.
  • clamp(ctx: any, yaw: number, pitch: number): (number, number): clamps into the limits.
  • step(ctx, yaw, pitch, wantYaw, wantPitch, dt): (number, number): turns toward the wanted aim for dt seconds at the turret's speeds, never leaving its limits; a full-circle turret takes the short way round.
  • error(yaw, pitch, wantYaw, wantPitch): number: degrees off (the larger of the yaw and pitch errors); the Turret fires when this is within the type's fireCone.
  • turnTime(ctx, yaw, pitch, wantYaw, wantPitch): number: seconds needed to turn from one aim to another (ignores limits).