particle

package
v3.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2022 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package particle provides options for generating renderable particle sources.

Package particle provides options for generating renderable particle sources.

Index

Constants

View Source
const (
	//IgnoreEnd refers to the life value given to particles that want to skip their source's end function.
	IgnoreEnd = -2000 / 2
)

Variables

View Source
var DefaultAllocator = NewAllocator()

DefaultAllocator is an allocator that starts running as soon as this package is imported.

View Source
var (
	// Inf represents Infinite duration
	Inf = intrange.NewInfinite()
)

Functions

func And

func And(as ...func(Generator)) func(Generator)

And chains together particle options into a single option for prebaking option sets

func Angle

func Angle(a floatrange.Range) func(Generator)

Angle sets the initial angle of a particle in degrees

func Color

func Color(start, startRand, end, endRand color.Color) func(Generator)

Color sets colors on a Colorable

func Color2

func Color2(start, startRand, end, endRand color.Color) func(Generator)

Color2 sets more colors on a Colorable2

func Duration

func Duration(i intrange.Range) func(Generator)

Duration sets how long a generator should produce particles for

func End

func End(ef func(Particle)) func(Generator)

End sets what function should happen when a particle dies

func EndSize

func EndSize(i intrange.Range) func(Generator)

EndSize sets the end size of a Sizeable

func Fragile

func Fragile(f bool) func(*CollisionGenerator)

Fragile sets whether the particles from this collisionGenerator are destroyed on contact

func Gravity

func Gravity(x, y float64) func(Generator)

Gravity sets how a particle should be shifted over time in either dimension

func HitMap

func HitMap(hm map[collision.Label]collision.OnHit) func(*CollisionGenerator)

HitMap sets functions to be called when particles from this generator collide with other spaces

func InfiniteLifeSpan

func InfiniteLifeSpan() func(Generator)

InfiniteLifeSpan will set particles to never die over time.

func Layer

func Layer(l func(physics.Vector) int) func(Generator)

Layer sets a function to determine what draw layer a particle should exist on

func LifeSpan

func LifeSpan(ls floatrange.Range) func(Generator)

LifeSpan sets how long a particle should last before dying

func Limit

func Limit(limit int) func(Generator)

Limit limits the total number of particles a particle generator can have active at once.

func NewPerFrame

func NewPerFrame(npf floatrange.Range) func(Generator)

NewPerFrame sets how many particles should be produced per frame

func Pos

func Pos(x, y float64) func(Generator)

Pos sets the initial position of spawned particles

func Progress

func Progress(pf func(x, y, w, h int) float64) func(Generator)

Progress sets a Progresses' Progress Function

func Rotation

func Rotation(a floatrange.Range) func(Generator)

Rotation rotates particles by a variable amount per frame

func Shape

func Shape(sf shape.Shape) func(Generator)

Shape is an option to set a generator's shape

func Size

func Size(i intrange.Range) func(Generator)

Size is an option to set a Sizeable size

func Speed

func Speed(s floatrange.Range) func(Generator)

Speed sets the initial speed of a particle

func SpeedDecay

func SpeedDecay(x, y float64) func(Generator)

SpeedDecay sets how the speed of a particle should decay

func Spread

func Spread(x, y float64) func(Generator)

Spread sets how far from a generator's position a particle can spawn

func Sprite

func Sprite(s *render.Sprite) func(Generator)

Sprite sets a Sprited's sprite

func SpriteRotation

func SpriteRotation(f floatrange.Range) func(Generator)

SpriteRotation sets a Sprited's rotation

Types

type Allocator

type Allocator struct {
	// contains filtered or unexported fields
}

An Allocator can allocate ids for particles

func NewAllocator

func NewAllocator() *Allocator

NewAllocator creates a new allocator

func (*Allocator) Allocate

func (a *Allocator) Allocate(id event.CID) int

Allocate requests a new block in the particle space for the given cid

func (*Allocator) Deallocate

func (a *Allocator) Deallocate(block int)

Deallocate requests that the given block be removed from the particle space

func (*Allocator) Lookup

func (a *Allocator) Lookup(id int) Particle

Lookup requests a specific particle in the particle space

func (*Allocator) LookupSource

func (a *Allocator) LookupSource(id int) *Source

LookupSource requests the source that generated a pid

func (*Allocator) Run

func (a *Allocator) Run()

Run spins up an allocator to accept allocation requests. It will run until Stop is called. This is a blocking call.

func (*Allocator) Stop

func (a *Allocator) Stop()

Stop stops the allocator's ongoing Run. Once stopped, allocator may not be reused. Stop must not be called more than once.

type BaseGenerator

type BaseGenerator struct {
	physics.Vector
	// This float is currently forced to an integer
	// at new particle rotation. This should be changed
	// to something along the lines of 'new per 30 frames',
	// or allow low fractional values to be meaningful,
	// so that more fine-tuned particle generation speeds are possible.
	NewPerFrame floatrange.Range
	// The number of frames each particle should persist
	// before being removed.
	LifeSpan floatrange.Range
	// 0 - between quadrant 1 and 4
	// 90 - between quadrant 2 and 1
	Angle  floatrange.Range
	Speed  floatrange.Range
	Spread physics.Vector
	// Duration in milliseconds for the particle source.
	// After this many milliseconds have passed, it will
	// stop sending out new particles. Old particles will
	// not be removed until their individual lifespans run
	// out.
	// A duration of -1 represents never stopping.
	Duration intrange.Range
	// Rotational acceleration, to change angle over time
	Rotation floatrange.Range
	// Gravity X() and Gravity Y() represent particle acceleration per frame.
	Gravity       physics.Vector
	SpeedDecay    physics.Vector
	EndFunc       func(Particle)
	LayerFunc     func(physics.Vector) int
	ParticleLimit int
}

A BaseGenerator fulfills all basic requirements to generate particles Modeled after Parcycle

func (*BaseGenerator) GetBaseGenerator

func (bg *BaseGenerator) GetBaseGenerator() *BaseGenerator

GetBaseGenerator returns this

func (*BaseGenerator) SetPos

func (bg *BaseGenerator) SetPos(x, y float64)

SetPos sets the position of a base generator

func (*BaseGenerator) ShiftX

func (bg *BaseGenerator) ShiftX(x float64)

ShiftX moves a base generator by an x value

func (*BaseGenerator) ShiftY

func (bg *BaseGenerator) ShiftY(y float64)

ShiftY moves a base generator by a y value

type CollisionGenerator

type CollisionGenerator struct {
	Generator
	Fragile bool
	HitMap  map[collision.Label]collision.OnHit
}

A CollisionGenerator generates collision particles

func (*CollisionGenerator) Generate

func (cg *CollisionGenerator) Generate(layer int) *Source

Generate creates a source using this generator

func (*CollisionGenerator) GenerateParticle

func (cg *CollisionGenerator) GenerateParticle(bp *baseParticle) Particle

GenerateParticle creates a particle from a generator

func (*CollisionGenerator) GetParticleSize

func (cg *CollisionGenerator) GetParticleSize() (w float64, h float64, perParticle bool)

GetParticleSize on a CollisionGenerator tells the caller that the particle size is per-particle specific

type CollisionParticle

type CollisionParticle struct {
	Particle
	// contains filtered or unexported fields
}

A CollisionParticle is a wrapper around other particles that also has a collision space and can functionally react with the environment on collision

func (*CollisionParticle) Cycle

func (cp *CollisionParticle) Cycle(generator Generator)

Cycle updates the collision particles variables once per rotation

func (*CollisionParticle) Draw

func (cp *CollisionParticle) Draw(buff draw.Image, xOff, yOff float64)

Draw redirects to DrawOffsetGen

func (*CollisionParticle) DrawOffsetGen

func (cp *CollisionParticle) DrawOffsetGen(generator Generator, buff draw.Image, xOff, yOff float64)

DrawOffsetGen draws a particle with it's generator's variables

func (*CollisionParticle) GetDims

func (cp *CollisionParticle) GetDims() (int, int)

GetDims returns the dimensions of the space of the particle

type ColorGenerator

type ColorGenerator struct {
	BaseGenerator
	StartColor, StartColorRand color.Color
	EndColor, EndColorRand     color.Color
	// The size, in pixel radius, of spawned particles
	Size    intrange.Range
	EndSize intrange.Range
	//
	// Some sort of particle type, for rendering triangles or squares or circles...
	Shape shape.Shape
}

A ColorGenerator generates ColorParticles

func (*ColorGenerator) Generate

func (cg *ColorGenerator) Generate(layer int) *Source

Generate creates a source using this generator

func (*ColorGenerator) GenerateParticle

func (cg *ColorGenerator) GenerateParticle(bp *baseParticle) Particle

GenerateParticle creates a particle from a generator

func (*ColorGenerator) GetParticleSize

func (cg *ColorGenerator) GetParticleSize() (w float64, h float64, perParticle bool)

GetParticleSize on a color generator returns that the particles are per-particle specificially sized

func (*ColorGenerator) SetEndColor

func (cg *ColorGenerator) SetEndColor(ec, ecr color.Color)

SetEndColor lets cg have its end color be set

func (*ColorGenerator) SetEndSize

func (cg *ColorGenerator) SetEndSize(i intrange.Range)

SetEndSize stasfies Sizeable

func (*ColorGenerator) SetShape

func (cg *ColorGenerator) SetShape(sf shape.Shape)

SetShape satisfies Shapeable

func (*ColorGenerator) SetSize

func (cg *ColorGenerator) SetSize(i intrange.Range)

SetSize satisfies Sizeable

func (*ColorGenerator) SetStartColor

func (cg *ColorGenerator) SetStartColor(sc, scr color.Color)

SetStartColor lets cg have its color be set

type ColorParticle

type ColorParticle struct {
	// contains filtered or unexported fields
}

A ColorParticle is a particle with a defined color and size

func (ColorParticle) Cycle

func (bp ColorParticle) Cycle(gen Generator)

func (*ColorParticle) Draw

func (cp *ColorParticle) Draw(buff draw.Image, xOff, yOff float64)

Draw redirects to DrawOffsetGen

func (*ColorParticle) DrawOffsetGen

func (cp *ColorParticle) DrawOffsetGen(generator Generator, buff draw.Image, xOff, yOff float64)

DrawOffsetGen draws a particle with it's generator's variables

func (ColorParticle) GetBaseParticle

func (bp ColorParticle) GetBaseParticle() *baseParticle

func (*ColorParticle) GetDims

func (cp *ColorParticle) GetDims() (int, int)

GetDims returns the color particle's size, twice

func (*ColorParticle) GetLayer

func (cp *ColorParticle) GetLayer() int

GetLayer returns baseParticle GetLayer. This is a safety check against auto-generated code which would not contain the nil check here

func (*ColorParticle) GetPos

func (cp *ColorParticle) GetPos() physics.Vector

GetPos returns the middle of a color particle

type Colorable

type Colorable interface {
	SetStartColor(color.Color, color.Color)
	SetEndColor(color.Color, color.Color)
}

A Colorable can have colors set on it.

type Colorable2

type Colorable2 interface {
	SetStartColor2(color.Color, color.Color)
	SetEndColor2(color.Color, color.Color)
}

A Colorable2 can have more colors set on it

type Generator

type Generator interface {
	GetBaseGenerator() *BaseGenerator
	GenerateParticle(*baseParticle) Particle
	Generate(int) *Source
	GetParticleSize() (float64, float64, bool)
	ShiftX(float64)
	ShiftY(float64)
	SetPos(float64, float64)
	GetPos() (float64, float64)
}

A Generator holds settings for generating particles

func NewCollisionGenerator

func NewCollisionGenerator(g Generator, options ...func(*CollisionGenerator)) Generator

NewCollisionGenerator creates a new collision generator

func NewColorGenerator

func NewColorGenerator(options ...func(Generator)) Generator

NewColorGenerator returns a new color generator with some applied options.

func NewGradientGenerator

func NewGradientGenerator(options ...func(Generator)) Generator

NewGradientGenerator returns a new GradientGenerator

func NewSpriteGenerator

func NewSpriteGenerator(options ...func(Generator)) Generator

NewSpriteGenerator creates a SpriteGenerator

type GradientGenerator

type GradientGenerator struct {
	ColorGenerator
	StartColor2, StartColor2Rand color.Color
	EndColor2, EndColor2Rand     color.Color
	ProgressFunction             func(x, y, w, h int) float64
}

A GradientGenerator is a ColorGenerator with a patterned gradient on its particles

func (*GradientGenerator) Generate

func (gg *GradientGenerator) Generate(layer int) *Source

Generate takes a generator and converts it into a source, drawing particles and binding functions for particle generation and rotation.

func (*GradientGenerator) GenerateParticle

func (gg *GradientGenerator) GenerateParticle(bp *baseParticle) Particle

GenerateParticle creates a particle from a generator

func (*GradientGenerator) SetEndColor2

func (gg *GradientGenerator) SetEndColor2(ec, ecr color.Color)

SetEndColor2 satisfies Colorable2

func (*GradientGenerator) SetProgress

func (gg *GradientGenerator) SetProgress(pf func(x, y, w, h int) float64)

SetProgress satisfies Progresses

func (*GradientGenerator) SetStartColor2

func (gg *GradientGenerator) SetStartColor2(sc, scr color.Color)

SetStartColor2 satisfies Colorable2

type GradientParticle

type GradientParticle struct {
	ColorParticle
	// contains filtered or unexported fields
}

A GradientParticle has a gradient from one color to another

func (GradientParticle) Cycle

func (bp GradientParticle) Cycle(gen Generator)

func (*GradientParticle) Draw

func (gp *GradientParticle) Draw(buff draw.Image, xOff, yOff float64)

Draw redirects to DrawOffsetGen

func (*GradientParticle) DrawOffsetGen

func (gp *GradientParticle) DrawOffsetGen(generator Generator, buff draw.Image, xOff, yOff float64)

DrawOffsetGen draws a particle with it's generator's variables

func (GradientParticle) GetBaseParticle

func (bp GradientParticle) GetBaseParticle() *baseParticle

type Particle

type Particle interface {
	render.Renderable
	GetBaseParticle() *baseParticle
	GetPos() physics.Vector
	DrawOffsetGen(gen Generator, buff draw.Image, xOff, yOff float64)
	Cycle(gen Generator)
	// contains filtered or unexported methods
}

A Particle is a renderable that is spawned by a generator, usually very fast, usually very small, for visual effects

type Progresses

type Progresses interface {
	SetProgress(func(x, y, w, h int) float64)
}

A Progresses has a SetProgress function where a progress function returns how far between two colors a given coordinate in a space is

type Shapeable

type Shapeable interface {
	SetShape(shape.Shape)
}

Shapeable generators can have the Shape option called on them

type Sizeable

type Sizeable interface {
	SetSize(i intrange.Range)
	SetEndSize(i intrange.Range)
}

A Sizeable is a generator that can have some size set to it

type Source

type Source struct {
	Generator Generator
	*Allocator

	CID event.CID

	EndFunc func()
	// contains filtered or unexported fields
}

A Source is used to store and control a set of particles.

func NewSource

func NewSource(g Generator, stackLevel int) *Source

NewSource creates a new source

func (*Source) Init

func (ps *Source) Init() event.CID

Init allows a source to be considered as an entity, and initializes it

func (*Source) IsPaused

func (ps *Source) IsPaused() bool

IsPaused checks for whether the source is currently in a paused state. It probably would have made more sense to export paused but this way if a lock is needed here in the future... Then it wont change the api.

func (*Source) Layer

func (ps *Source) Layer(v physics.Vector) int

Layer is shorthand for getting the base generator behind a source's layer

func (*Source) Pause

func (ps *Source) Pause()

Pause on a Source just stops the repetition of its rotation function, which moves, destroys, ages and generates particles. Existing particles will stay in place.

func (*Source) SetPos

func (ps *Source) SetPos(x, y float64)

SetPos sets a source's underlying generator

func (*Source) ShiftX

func (ps *Source) ShiftX(x float64)

ShiftX shift's a source's underlying generator

func (*Source) ShiftY

func (ps *Source) ShiftY(y float64)

ShiftY shift's a source's underlying generator

func (*Source) Stop

func (ps *Source) Stop()

Stop manually stops a Source, if its duration is infinite or if it should be stopped before expriring naturally.

func (*Source) UnPause

func (ps *Source) UnPause()

UnPause on a source a Source rebinds it's rotate function.

type SpriteGenerator

type SpriteGenerator struct {
	BaseGenerator
	SpriteRotation floatrange.Range
	Base           *render.Sprite
}

A SpriteGenerator generate SpriteParticles

func (*SpriteGenerator) Generate

func (sg *SpriteGenerator) Generate(layer int) *Source

Generate creates a source using this generator

func (*SpriteGenerator) GenerateParticle

func (sg *SpriteGenerator) GenerateParticle(bp *baseParticle) Particle

GenerateParticle creates a particle from a generator

func (*SpriteGenerator) GetParticleSize

func (sg *SpriteGenerator) GetParticleSize() (w float64, h float64, perParticle bool)

GetParticleSize returns the size of the sprite that the generator generates

func (*SpriteGenerator) SetSprite

func (sg *SpriteGenerator) SetSprite(s *render.Sprite)

SetSprite is the function on a sprite generator that satisfies Sprited

func (*SpriteGenerator) SetSpriteRotation

func (sg *SpriteGenerator) SetSpriteRotation(f floatrange.Range)

SetSpriteRotation satisfied Sprited for SpriteGenerators

type SpriteParticle

type SpriteParticle struct {
	// contains filtered or unexported fields
}

A SpriteParticle is a particle that has an amount of sprite rotation

func (SpriteParticle) Cycle

func (bp SpriteParticle) Cycle(gen Generator)

func (*SpriteParticle) Draw

func (sp *SpriteParticle) Draw(buff draw.Image, xOff, yOff float64)

Draw redirects to DrawOffsetGen

func (*SpriteParticle) DrawOffsetGen

func (sp *SpriteParticle) DrawOffsetGen(generator Generator, buff draw.Image, xOff, yOff float64)

DrawOffsetGen draws a particle with it's generator's variables

func (SpriteParticle) GetBaseParticle

func (bp SpriteParticle) GetBaseParticle() *baseParticle

func (SpriteParticle) GetDims

func (bp SpriteParticle) GetDims() (int, int)

func (SpriteParticle) GetLayer

func (bp SpriteParticle) GetLayer() int

func (SpriteParticle) GetPos

func (bp SpriteParticle) GetPos() physics.Vector

type Sprited

type Sprited interface {
	SetSprite(*render.Sprite)
	SetSpriteRotation(f floatrange.Range)
}

A Sprited can have a sprite set to it

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL