{"modulePath":"github.com/weakpixel/engine","version":"v0.0.0-20261205071607-94fff0387040","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package camera\n\nPackage camera is a quick-start follow camera: mark an entity with a [Target](#Target), pick a [FollowMode](#FollowMode) in a [Rig](#Rig) and run the [System](#System) in the camera stage; it moves the render.Camera after the target every tick.\n\nA tick runs a small pipeline on the Rig's own (unclamped) follow position: the goal (the target's position plus Target.Offset) goes through the optional look-ahead and dead-zone stages, a motion (hard, smooth or spring) moves the follow position toward the result, and the camera's Pos becomes the follow position clamped to Rig.Bounds. Each FollowMode is a preset of that pipeline; Rig.DeadZone.On and Rig.LookAhead.On add a stage to any preset.\n\ngame.Engine runs the System first in its camera stage and owns the Rig (Config.CameraRig, Engine.CameraRig). Without a game.Engine, add the System to a schedule after a step that calls Camera.Snapshot; see doc/guide/engine/camera.md.\n\nThe package imports ecs, ecs/system, gfx, render and extract, never game.\n\n## Constants\n\n```go\nconst (\n\tDefaultSmoothTime      = 150 * time.Millisecond\n\tDefaultSpringFrequency = 2 // Hz\n\tDefaultSpringDamping   = 1 // critical\n\tDefaultLookDistance    = 48\n\tDefaultLookTime        = 300 * time.Millisecond\n\tDefaultLookMinSpeed    = 1 // world units per second\n)\n```\nDefault parameters, used where a Rig field is zero.\n\n```go\nconst (\n\tDefaultShakeFrequency = 20 // Hz\n\tDefaultShakeDecay     = 1  // trauma per second\n\tDefaultShakeExponent  = 2\n)\n```\nDefault shake parameters, used where a Shake field is zero.\n\n## Variables\n\n```go\nvar DefaultDeadZone = gfx.Vec2{X: 32, Y: 24}\n```\nDefaultDeadZone is the dead-zone half size used when DeadZone.HalfSize is zero, in world units.\n\n```go\nvar DefaultShakeOffset = gfx.Vec2{X: 6, Y: 6}\n```\nDefaultShakeOffset is the offset at full shake used when Shake.MaxOffset is zero, in world units.\n\n## Types\n\n```go\ntype DeadZone struct {\n\t// On adds the stage to modes other than deadzone.\n\tOn bool\n\t// HalfSize is half the window size in world units (it does not scale\n\t// with Zoom). Zero means DefaultDeadZone.\n\tHalfSize gfx.Vec2\n}\n```\nDeadZone configures the dead-zone stage: a window that moves only when the goal leaves it, by just enough to keep the goal on its edge.\n\n```go\ntype FollowMode string\n```\nFollowMode selects how the camera follows its target. Each mode is a preset of a motion and the goal stages it turns on (see the package documentation and doc/guide/engine/camera.md):\n\n\tmode       look-ahead  dead zone  motion\n\tdefault    -           -          hard\n\tsmooth     -           -          smooth\n\tdeadzone   -           on         hard\n\tspring     -           -          spring\n\tlookahead  on          -          smooth\n\nThe zero value \"\" means FollowDefault. Values are plain strings so they read well in config files, Lua and logs.\n\n```go\nconst (\n\t// FollowDefault puts the camera on the target every tick.\n\tFollowDefault FollowMode = \"default\"\n\t// FollowSmooth eases toward the target, independent of the tick rate\n\t// (Rig.Smooth).\n\tFollowSmooth FollowMode = \"smooth\"\n\t// FollowDeadZone leaves the camera still while the target moves inside\n\t// a rectangle around the view center, and pushes it along when the\n\t// target reaches an edge (Rig.DeadZone).\n\tFollowDeadZone FollowMode = \"deadzone\"\n\t// FollowSpring follows with a damped spring, which can overshoot when\n\t// under-damped (Rig.Spring).\n\tFollowSpring FollowMode = \"spring\"\n\t// FollowLookAhead eases toward a point ahead of the target in its\n\t// direction of motion (Rig.LookAhead, Rig.Smooth).\n\tFollowLookAhead FollowMode = \"lookahead\"\n)\n```\nThe follow modes.\n\n```go\nfunc (m FollowMode) Check()\n```\nCheck panics with \\`camera: unknown follow mode \"x\"\\` unless m is one of the FollowMode constants or the zero value.\n\n```go\nfunc (m FollowMode) String() string\n```\nString returns the mode name, \"default\" for the zero value.\n\n```go\ntype LookAhead struct {\n\t// On adds the stage to modes other than lookahead.\n\tOn bool\n\t// Distance is how far ahead, in world units. Zero means\n\t// DefaultLookDistance.\n\tDistance float32\n\t// Time is the easing time constant of the look offset, toward the\n\t// motion direction and back to zero. Zero means DefaultLookTime.\n\tTime time.Duration\n\t// MinSpeed is the target speed, in world units per second, below which\n\t// the look offset returns to zero. Zero means DefaultLookMinSpeed.\n\tMinSpeed float32\n}\n```\nLookAhead configures the look-ahead stage: the goal moves ahead of the target in its direction of motion.\n\n```go\ntype Rig struct {\n\t// Mode is the follow preset; \"\" means FollowDefault. An unknown mode\n\t// panics in System.Run.\n\tMode FollowMode\n\t// Smooth is the easing of the smooth motion (modes smooth, lookahead).\n\tSmooth Smooth\n\t// DeadZone configures the dead-zone stage (mode deadzone, or On).\n\tDeadZone DeadZone\n\t// Spring is the spring motion (mode spring).\n\tSpring Spring\n\t// LookAhead configures the look-ahead stage (mode lookahead, or On).\n\tLookAhead LookAhead\n\t// Bounds is the world rectangle the view must stay inside. The zero\n\t// Rect means unbounded. On an axis where Bounds is smaller than the\n\t// view, the camera is centered on Bounds. Rotation is ignored.\n\tBounds gfx.Rect\n\t// TeleportDistance snaps the camera (see Snap) when the goal is further\n\t// than this from the follow position, so a teleported target does not\n\t// make the camera sweep across the level. 0 turns it off.\n\tTeleportDistance float32\n\t// Shake configures the screen shake that AddTrauma starts.\n\tShake Shake\n\t// contains filtered or unexported fields\n}\n```\nRig configures the follow camera and holds its state between ticks. The System reads it as the world resource \\*Rig (game.Engine owns one, see Engine.CameraRig); change its fields at any time from any system or tool. Every zero field means its documented default, so the zero Rig is a hard follow without bounds.\n\nThe Rig keeps its own, unclamped follow position: Bounds only shapes the camera's Pos, and the next tick continues from the follow position, not from Camera.Pos. So offsets that later camera-stage systems add to Pos (a shake) do not feed back, and a system that overrides Pos does so for one tick only. The built-in shake (Shake, AddTrauma) works the same way.\n\n```go\nfunc (r *Rig) AddTrauma(t float32)\n```\nAddTrauma adds t to the shake trauma, which stays in \\[0, 1]; a negative t reduces it. Call it from any system: an explosion adds 0.6, a hit 0.3.\n\n```go\nfunc (r *Rig) Snap()\n```\nSnap makes the next tick with a target jump straight to it: the follow position and the dead-zone window move to the goal, the spring velocity and look offset reset, and Camera.Prev is cleared (Camera.Jump) so the frame does not sweep. Call it after a teleport or a level load. The System also snaps on its own the first tick it has a target after a tick without one, and when TeleportDistance is exceeded.\n\n```go\nfunc (r *Rig) StopShake()\n```\nStopShake sets the trauma to 0; the next System tick puts the camera back on its unshaken position.\n\n```go\nfunc (r *Rig) Trauma() float32\n```\nTrauma returns the current shake trauma in \\[0, 1].\n\n```go\ntype Shake struct {\n\t// MaxOffset is the offset at full shake, per axis, in world units.\n\t// Zero means DefaultShakeOffset.\n\tMaxOffset gfx.Vec2\n\t// MaxAngle is the rotation at full shake, in radians. 0 means no\n\t// rotation; a rotating shake turns off render.Camera.PixelSnap while\n\t// the view is rotated.\n\tMaxAngle float32\n\t// Frequency is the noise frequency in Hz: higher shakes faster. Zero\n\t// means DefaultShakeFrequency.\n\tFrequency float32\n\t// Decay is the trauma lost per second; 1 lets full trauma settle in a\n\t// second. Zero means DefaultShakeDecay.\n\tDecay float32\n\t// Exponent shapes the shake: the amplitude is trauma^Exponent. Zero\n\t// means DefaultShakeExponent.\n\tExponent float32\n\t// InBounds clamps the shaken view to Rig.Bounds, so the shake never\n\t// shows outside the level but flattens against its edges. By default\n\t// the shake is added after the clamp and stays visible at an edge. It\n\t// only matters with a Target and non-zero Bounds.\n\tInBounds bool\n\t// Off turns the shake off (an accessibility setting): AddTrauma still\n\t// counts and trauma still decays, but the camera does not move.\n\tOff bool\n}\n```\nShake configures the trauma-based screen shake: [Rig.AddTrauma](#Rig.AddTrauma) adds trauma in \\[0, 1], trauma falls by Decay per second, and the camera is offset by MaxOffset (and rotated by MaxAngle) times trauma^Exponent times smooth noise. Small hits barely move the view, big ones shake hard, and several hits add up. Every zero field means its documented default.\n\n```go\ntype Smooth struct {\n\t// Time is the time constant: after Time the camera has covered 63% of\n\t// a jump. Zero means DefaultSmoothTime.\n\tTime time.Duration\n}\n```\nSmooth configures exponential easing: each tick the follow position covers 1 - exp(-dt/Time) of the remaining distance, the same total motion at any tick rate.\n\n```go\ntype Spring struct {\n\t// Frequency is the natural frequency in Hz; higher is stiffer. Zero\n\t// means DefaultSpringFrequency.\n\tFrequency float32\n\t// Damping is the damping ratio: 1 is critical (fastest without\n\t// overshoot), below 1 overshoots, above 1 is sluggish. Zero means\n\t// DefaultSpringDamping.\n\tDamping float32\n}\n```\nSpring configures a damped spring, solved in closed form so it is stable at any tick rate.\n\n```go\ntype System struct {\n\t// contains filtered or unexported fields\n}\n```\nSystem moves the render.Camera after the [Target](#Target) entity, as configured by the [Rig](#Rig) resource. Run it in the camera stage, after the game moved its entities and after the camera's view was recorded for this tick (render.Camera.Snapshot; game.Engine does both and runs it first in StageCamera). It only assigns Camera.Pos, clears Camera.Prev when it snaps (Camera.Jump), and assigns Camera.Rotation while a rotating shake runs.\n\nEach tick it picks the target (highest Priority, then lowest entity ID), snaps when asked to (Rig.Snap, the first tick with a target after a tick without one, Rig.TeleportDistance), otherwise advances the Rig's pipeline by the tick's delta, and sets Camera.Pos to the follow position clamped to Rig.Bounds. It then adds the Rig's shake (see [Shake](#Shake)); the shake also rotates Camera.Rotation by its change since the last tick. Without a target it moves Camera.Pos only by the change of the shake offset, so a camera the game moves itself shakes too and ends where it started.\n\nIt needs the resources \\*render.Camera and \\*extract.Screen; Init creates a zero \\*Rig resource if there is none. Run does not allocate. It panics with \\`camera: unknown follow mode \"x\"\\` for an unknown Rig.Mode.\n\n```go\nfunc NewSystem(w *ecs.World) *System\n```\nNewSystem returns the system for w.\n\n```go\nfunc (s *System) Init(ctx *system.Context)\n```\nInit implements system.Initializer. It caches the resources, so set them (and replace the Rig, if at all) before the first Run.\n\n```go\nfunc (s *System) Run(ctx *system.Context)\n```\nRun implements system.Runner.\n\n```go\ntype Target struct {\n\t// Offset is added to the target's position: the camera aims at\n\t// Transform.Pos + Offset, for example to frame a character's head.\n\tOffset gfx.Vec2\n\t// Priority picks among several targets; higher wins.\n\tPriority int16\n}\n```\nTarget marks the entity the camera follows; the entity also needs a gfx.Transform. It is a runtime-only component, not part of any Game Object schema: add it with ecs.SetComp.\n\nWith several targets the highest Priority wins, and among equal priorities the entity with the lowest ecs.Entity ID (stable, unlike view order, which changes when entities move between archetypes). With none the System leaves the camera alone.\n\n","path":"github.com/weakpixel/engine/camera","name":"camera","synopsis":"Package camera is a quick-start follow camera: mark an entity with a Target, pick a FollowMode in a Rig and run the System in the camera stage; it moves the render.Camera after the target every tick.","isRedistributable":true}
