API 參考取自原始碼中的 Go 文件註解,以英文撰寫。
package spawn
import "github.com/nsgo-dev/game/pkg/spawn"概覽
Package spawn is experimental: its API and documented formats may change in a minor release. See the API stability policy. schedules deterministic enemy spawn events from versioned YAML.
索引
- 常數
- func Validate(d *Document) error
- type Director
- func NewDirector(doc *Document, stream *random.Stream) *Director
- func (d *Director) AppendStep(dst []Spawn, dt float64, view Rect, target Vec2) []Spawn
- func (d *Director) Live() int
- func (d *Director) MaxPerStep() int
- func (d *Director) SetLive(n int)
- func (d *Director) SetMaxPerStep(n int)
- func (d *Director) SetState(s State)
- func (d *Director) SkipDue() int
- func (d *Director) State() State
- func (d *Director) Step(dt float64, view Rect, target Vec2) []Spawn
- type Document
- type Group
- type Rect
- type Spawn
- type State
- type Vec2
- type Wave
常數
函式
func Validate(d *Document) errorValidate reports a wrong version, a maxLive below 1, and a wave or group value that is negative, NaN or infinite, a group without an enemy, or a group count below 1.
型別
type Director struct {
// contains filtered or unexported fields
}Director advances waves and emits events without creating game entities.
func NewDirector(doc *Document, stream *random.Stream) *DirectorNewDirector returns a deterministic director for a copy of doc and stream. It panics if doc or stream is nil.
func (d *Director) AppendStep(dst []Spawn, dt float64, view Rect, target Vec2) []SpawnAppendStep is Step writing into dst instead of a new slice, and returns the extended slice, so a game can reuse one buffer and allocate nothing while it emits spawns. Pass nil to start a new slice, as Step does.
func (d *Director) Live() intLive returns the number of live entities the cap counts: the last SetLive value plus every spawn Step has emitted since.
func (d *Director) MaxPerStep() intMaxPerStep returns the per-step spawn cap, 0 when one step may emit as many spawns as the live cap allows.
func (d *Director) SetLive(n int)SetLive sets the live-entity count used by the cap, such as after enemies die. Negative values count as 0.
func (d *Director) SetMaxPerStep(n int)SetMaxPerStep caps how many spawns one step emits, so a refill of the live cap spreads over several steps instead of arriving as one burst on the same ring at the same instant; 0 or less removes the cap. The spawns a step holds back stay due, so later steps emit them as room opens. The cap is runtime state, like the director's other settings, and is not part of State.
func (d *Director) SetState(s State)SetState restores a director snapshot taken from a director of the same document.
func (d *Director) SkipDue() intSkipDue marks every spawn that is due at the current elapsed time as done, including the spawns the cap or the per-step limit held back, so a director that kept stepping while the game wanted no spawns does not release its whole backlog later. It returns how many spawns it dropped. Spawns that come due afterwards are emitted as usual.
func (d *Director) Step(dt float64, view Rect, target Vec2) []SpawnStep advances elapsed time by dt seconds and emits the spawns that are due, earlier waves first, placed around target outside view. It never lets Live exceed maxLive, and emits at most MaxPerStep per step when that is set: it counts each emitted spawn in Live and holds back the spawns the cap or the per-step limit blocks until a later step. A dt that is not positive and finite emits nothing. A game that steps every frame should call AppendStep with a reused buffer instead.
type Document struct {
Version int `yaml:"version"`
MaxLive int `yaml:"maxLive"`
Waves []Wave `yaml:"waves"`
}Document defines waves and the live-spawn cap.
func Parse(data []byte) (*Document, error)Parse decodes and validates one versioned spawn document. It refuses unknown fields and a second YAML document, and returns a *loaderr.Error.
func (d *Document) Encode() ([]byte, error)Encode validates and writes a deterministic spawn document.
type Group struct {
Enemy string `yaml:"enemy"`
Count int `yaml:"count"`
Interval float64 `yaml:"interval,omitempty"`
RingRadius float64 `yaml:"ringRadius,omitempty"`
}Group describes repeated enemy spawns: Count spawns of Enemy, the first when its wave starts and one more every Interval seconds, or all at once when Interval is 0, each placed RingRadius beyond the view.
type Rect struct{ Min, Max Vec2 }Rect is an axis-aligned view rectangle.
type Spawn struct {
// Enemy is the group's enemy name.
Enemy string
// Position is where to place the enemy, from RingPosition.
Position Vec2
// Wave is the index of the group's wave in Document.Waves as written.
Wave int
}Spawn is one emitted enemy event.
type State struct {
Elapsed float64 `yaml:"elapsed"`
Next []int `yaml:"next"`
Live int `yaml:"live"`
Random random.State `yaml:"random"`
}State is a serializable director snapshot.
type Vec2 struct{ X, Y float64 }Vec2 is a 2D position.
func RingPosition(s *random.Stream, target Vec2, view Rect, margin float64) Vec2RingPosition chooses a deterministic point on a circle around target that lies outside view: its radius is the distance from target to view's farthest corner plus margin, so a margin above 0 keeps the point off view's edge. A negative margin counts as 0. It draws one value from s.