Die API-Referenz stammt aus den Go-Doc-Kommentaren im Quellcode und ist auf Englisch verfasst.
package bullets
import "github.com/nsgo-dev/game/pkg/bullets"Übersicht
Package bullets spawns, moves and draws the bullet patterns of shooters: rings, spirals and aimed bursts defined in versioned YAML.
Pattern Files
A pattern file holds named patterns. Parse decodes one, refusing unknown fields, unsupported versions and invalid values, and Encode writes it back as deterministic YAML that round-trips through Parse:
version: 1
patterns:
- name: flower
kind: spiral
frame: orb
count: 6
speed: 140
spin: 11
shots: 0
interval: 60ms
lifetime: 5s
radius: 4
Angles are in degrees, clockwise from +X on the screen; speeds are in points a second; durations are Go durations such as 60ms or 2s.
Fields and Emitters
A Field owns the live bullets: it takes them from a perf.Pool, moves them in Update, removes those that outlive their lifetime or leave its Bounds, and draws them through one sprite.Batch. An Emitter fires one pattern's shots into a field at the pattern's times, drawing any jitter from its own seeded random.Stream, so an emitter with the same pattern, seed and steps spawns the same bullets.
Each fixed step, update the field first and then its emitters: an emitter places the bullets of a shot fired partway through the step where they are at the step's end.
Index
- Konstanten
- func Encode(f *File) ([]byte, error)
- type Bullet
- type Emitter
- func NewEmitter(f *Field, p *Pattern, seed uint64) (*Emitter, error)
- func (e *Emitter) Done() bool
- func (e *Emitter) Elapsed() time.Duration
- func (e *Emitter) Pattern() Pattern
- func (e *Emitter) Reset()
- func (e *Emitter) Seed() uint64
- func (e *Emitter) Shots() int
- func (e *Emitter) Update(dt time.Duration) int
- type Field
- type File
- type Kind
- type Pattern
Konstanten
const FormatVersion = 1FormatVersion is the pattern file schema version that Parse accepts and Encode writes.
const MinInterval = time.MillisecondMinInterval is the shortest Interval a pattern may have, other than 0: shorter intervals would fire thousands of shots a frame.
Funktionen
func Encode(f *File) ([]byte, error)Encode validates f and writes it as YAML with 2-space indents, its fields in a fixed order and optional fields left out when zero, so encoding the same file always gives the same bytes and Parse reads them back unchanged.
Typen
type Bullet struct {
// X and Y are where the bullet is, in the field's coordinates.
X, Y float32
// Speed is how fast it moves, in points a second.
Speed float32
// Heading is the direction it moves, in radians clockwise from +X.
Heading float32
// Radius is its hit radius in points.
Radius float32
// Age is how long it has lived.
Age time.Duration
// Lifetime is how long it lives; 0 keeps it until it leaves the field's bounds or is removed.
Lifetime time.Duration
// Pattern is the name of the pattern that fired it.
Pattern string
// Shot is the index of the emitter's shot that fired it, from 0.
Shot int
// Sprite draws it; the field moves the sprite with the bullet.
Sprite sprite.Sprite
// contains filtered or unexported fields
}Bullet is one live bullet of a Field. A field owns its bullets: a game reads them, for hit tests, and removes them with Field.RemoveFunc, but does not keep a pointer past the next Update, RemoveFunc or Clear, as the field reuses a removed bullet for a later one.
func (b *Bullet) Circle() math2d.CircleCircle returns the bullet's hit circle.
func (b *Bullet) Velocity() math2d.Vec2Velocity returns the bullet's velocity in points a second.
type Emitter struct {
// X and Y are where its shots start, in the field's coordinates; move them with the thing that shoots.
X, Y float32
// TargetX and TargetY are what an Aimed pattern aims at, such as the player.
TargetX, TargetY float32
// Stopped, when true, pauses the emitter: Update neither fires nor runs its clock, and Done reports true. Once Stopped is false again, the emitter picks up its pattern where it paused, owing no shots for the time it was stopped.
Stopped bool
// contains filtered or unexported fields
}Emitter fires one pattern's shots into a field at the pattern's times. It draws the pattern's jitter from its own random stream, so two emitters with the same pattern and seed, moved and stepped alike, spawn the same bullets.
An Emitter is not safe for concurrent use.
func NewEmitter(f *Field, p *Pattern, seed uint64) (*Emitter, error)NewEmitter returns an emitter that fires a copy of p into f, drawing jitter from a stream seeded with seed. It returns an error when p is invalid, or names a frame that f's atlas does not have.
func (e *Emitter) Done() boolDone reports whether the emitter fires no more shots: it is stopped, or has fired all of a pattern's Shots.
func (e *Emitter) Elapsed() time.DurationElapsed returns how long the emitter has run since it was made or reset, not counting the time it was stopped.
func (e *Emitter) Pattern() PatternPattern returns the emitter's copy of its pattern.
func (e *Emitter) Reset()Reset starts the emitter over from its first shot, with its random stream rewound to its seed, and lets it fire again if it was stopped. It leaves bullets already fired in the field.
func (e *Emitter) Seed() uint64Seed returns the seed of the emitter's random stream.
func (e *Emitter) Shots() intShots returns how many shots the emitter has fired since it was made or reset.
func (e *Emitter) Update(dt time.Duration) intUpdate runs the emitter's clock on by dt and fires each shot due in that time into its field, returning the number of bullets it spawned. A shot due partway through dt spawns its bullets where they are at its end, so update the field before its emitters.
A shot past the field's Limit spawns only as many bullets as fit; it still counts as fired and draws the same random numbers, so the bullets that do spawn are the same as with no limit.
One call fires at most MaxCount bullets' worth of shots, and at least one shot. When more shots are due, as after a long dt from a hitch, it fires the latest of them and skips the earlier ones: they count as fired but spawn nothing and draw no random numbers. This bounds the work of a step whatever the pattern's Interval and dt.
type Field struct {
// Atlas holds the frames the bullets draw; emitters look their pattern's frame up in it.
Atlas *sprite.Atlas
// Bounds, when not empty, is the area bullets live in: Update removes a bullet once its hit circle lies wholly outside it.
Bounds math2d.Rect
// Limit, when above 0, is the most bullets the field holds: a shot spawns none past it.
Limit int
// contains filtered or unexported fields
}Field holds a shooter's live bullets: it takes them from a pool, moves them, removes the ones that expire or leave its bounds and draws them through one sprite batch.
A Field is not safe for concurrent use.
func NewField(atlas *sprite.Atlas, capacity int) *FieldNewField returns a field drawing from atlas that keeps capacity bullets ready, so that it spawns up to capacity bullets at once without allocating.
func (f *Field) Bullets() []*BulletBullets returns the live bullets, oldest first. The slice and the bullets are the field's: they are valid until the next Update, spawn, RemoveFunc or Clear.
func (f *Field) Clear()Clear removes every bullet, keeping the field's memory for the next ones.
func (f *Field) Draw(d *core.Drawing)Draw draws the bullets into d through the field's sprite batch, lowest Z first and oldest first for the same Z. It draws nothing when the field has no atlas.
func (f *Field) RemoveFunc(remove func(b *Bullet) bool) intRemoveFunc removes the bullets for which remove returns true, such as those that hit the player, keeping the others in order, and returns how many it removed.
func (f *Field) Update(dt time.Duration)Update moves every bullet on by dt, then removes the ones that have lived their lifetime or left the field's bounds, keeping the others in order.
type File struct {
// Version is the schema version; it must equal FormatVersion.
Version int `yaml:"version"`
// Patterns are the file's patterns, each with a unique name.
Patterns []Pattern `yaml:"patterns"`
}File is a pattern file: a schema version and its patterns, in the order written.
func Parse(data []byte) (*File, error)Parse decodes and validates a YAML pattern file. Unknown fields, trailing YAML documents and invalid patterns are refused.
func (f *File) Pattern(name string) (*Pattern, bool)Pattern returns the file's pattern named name, or nil and false when it has none.
func (f *File) Validate() errorValidate checks the file's version, that its pattern names are unique and that each pattern is valid.
type Kind stringKind is how a pattern places the bullets of a shot.
const (
// Ring fires Count bullets spaced evenly around a full turn, the first at Angle, turned by Spin more each shot.
Ring Kind = "ring"
// Spiral is a ring that turns by Spin (which must not be 0) each shot, so short intervals trace spiral arms: one arm per bullet of Count.
Spiral Kind = "spiral"
// Aimed fires Count bullets spread evenly across Spread degrees, centered on the direction from the emitter to its target, turned by Angle and by Spin each shot.
Aimed Kind = "aimed"
)type Pattern struct {
// Name identifies the pattern in its file; it must be unique and not empty.
Name string `yaml:"name"`
// Kind is how a shot places its bullets: Ring, Spiral or Aimed.
Kind Kind `yaml:"kind"`
// Frame is the name of the atlas frame that draws each bullet; an empty Frame spawns bullets that move and collide but draw nothing.
Frame string `yaml:"frame,omitempty"`
// Count is the number of bullets each shot fires, from 1 to MaxCount.
Count int `yaml:"count"`
// Speed is each bullet's starting speed in points a second.
Speed float32 `yaml:"speed"`
// SpeedJitter varies each bullet's starting speed by a random amount in [-SpeedJitter, SpeedJitter), never below 0.
SpeedJitter float32 `yaml:"speedJitter,omitempty"`
// Angle is the direction of a shot's first bullet in degrees, clockwise from +X; for Aimed, it turns the whole burst from the target's direction.
Angle float32 `yaml:"angle,omitempty"`
// Spread is the arc in degrees an Aimed burst covers, from its first bullet to its last; it applies to Aimed only, and must be above 0 when Count is above 1.
Spread float32 `yaml:"spread,omitempty"`
// AngleJitter turns each bullet by a random amount in [-AngleJitter, AngleJitter) degrees.
AngleJitter float32 `yaml:"angleJitter,omitempty"`
// Spin turns each shot by this many degrees more than the one before.
Spin float32 `yaml:"spin,omitempty"`
// Shots is the number of shots the pattern fires; 0 fires until the emitter is stopped.
Shots int `yaml:"shots"`
// Delay is the time before the first shot.
Delay time.Duration `yaml:"delay,omitempty"`
// Interval is the time between shots; it must be at least MinInterval, or 0 when Shots is 1.
Interval time.Duration `yaml:"interval,omitempty"`
// Lifetime is how long each bullet lives; 0 keeps it until it leaves the field's bounds or is removed.
Lifetime time.Duration `yaml:"lifetime,omitempty"`
// Accel changes each bullet's speed by this many points a second, each second; a bullet slowed to 0 stops.
Accel float32 `yaml:"accel,omitempty"`
// Turn curves each bullet's path by this many degrees a second, clockwise when positive.
Turn float32 `yaml:"turn,omitempty"`
// Radius is each bullet's hit radius in points, which the field also uses to tell when a bullet has left its bounds.
Radius float32 `yaml:"radius,omitempty"`
// FaceHeading turns each bullet's sprite to the direction it moves, for bullets whose picture has a front.
FaceHeading bool `yaml:"faceHeading,omitempty"`
// Z orders the bullets' sprites in the field's batch.
Z int `yaml:"z,omitempty"`
}Pattern is one bullet pattern: how its shots place, aim and time their bullets, and how those bullets move, live and look.
func (p *Pattern) Validate() errorValidate checks that p names itself, has a known kind, fires 1 to MaxCount bullets a shot, has finite numbers in range, and times its shots so that they end or repeat after an interval of at least MinInterval.