Diese Seite ist noch nicht auf Deutsch übersetzt und wird daher auf Englisch angezeigt.
WeitereSpawn director
spawn.Document and spawn.Director provide deterministic versioned wave schedules for shooters and survivors-like games. Director.Step emits Spawn values without creating entities, keeps the live count under maxLive, and uses RingPosition to place events outside the camera rectangle. Save Director.State with game replays or saves.
The wave file
version: 1
maxLive: 40
waves:
- at: 0
groups:
- {enemy: bat, count: 6, interval: 0.5, ringRadius: 32}
- at: 30
groups:
- {enemy: slime, count: 3}
at is the wave's start in seconds. A group spawns count of its enemy, the first when the wave starts and one more every interval seconds, or all at once when interval is 0 or missing; ringRadius puts them that far beyond the view. spawn.Parse refuses unknown fields, a second YAML document, a version other than 1, a maxLive below 1, a group without an enemy or with a count below 1, and a time, interval or radius that is negative, NaN or infinite, with a *loaderr.Error naming the field. Document.Encode validates and writes the fields in a fixed order, so a parsed and re-encoded file encodes to the same bytes every time.
Running it
Director.Step(dt, view, target) advances the schedule by dt seconds and returns the spawns that are due, earlier waves first. Each spawn is placed by RingPosition, on a circle around target whose radius is the distance to the view's farthest corner plus the group's ringRadius, so it is outside the view even when target is not its center. Spawn.Wave is the wave's index in the file.
Each emitted spawn counts toward Director.Live, and a step never lets it pass maxLive; spawns the cap holds back come out on a later step. Report deaths with Director.SetLive(n), the number of enemies still alive.
Director.SetMaxPerStep(n) caps how many spawns one step emits, so a cap that has been full for a while refills over several steps instead of releasing its whole due backlog on one ring in one frame; 0 removes the cap. Spawns a step holds back this way stay due, so later steps emit them as room opens. Director.SkipDue() marks every spawn that is due now as done, including the ones the cap or the per-step limit held back, and returns how many it dropped: call it when a director kept stepping while the game wanted no spawns, so it does not release them all at once later.
A game that steps every frame should call Director.AppendStep(dst, dt, view, target), which is Step writing into dst and returning the extended slice, so one reused buffer keeps the fixed step allocation-free; Step calls it with a nil buffer. MaxPerStep is runtime configuration, like the wave document, so it is not part of Director.State.