Documentation
¶
Overview ¶
Package time provides a Facility for managing time in an ecs.System.
Time in an ecs.System is counted as the number of Process()ing rounds that have happened (counting the current one!); so Time(0) will never be seen in practice.
NOTE while you can, and probably should, embed a single Facility in your toplevel ecs.System, there's nothing to stop you from having multiple levels of time. Such a setup might make sense if there's a non-trivial relationship between toplevel Process()ing and an inner system's Process()ing.
Example ¶
package main import ( "fmt" "github.com/borkshop/bork/internal/ecs" "github.com/borkshop/bork/internal/ecs/time" ) const ( wcTime ecs.ComponentType = 1 << iota wcFoo wcBar wcBaz ) type world struct { ecs.System time time.Facility } func (w *world) init() { w.time.Init(&w.Core, wcTime) w.Procs = append(w.Procs, &w.time, ecs.ProcFunc(w.dump), ) w.RegisterCreator(wcFoo, func(id ecs.EntityID, _ ecs.ComponentType) { fmt.Printf("[%v] +foo after 2 bar\n", id) w.time.After(w.Ref(id), 2, wcBar.ApplyTo) }) w.RegisterCreator(wcBar, func(id ecs.EntityID, _ ecs.ComponentType) { fmt.Printf("[%v] +bar after 3 none\n", id) w.time.After(w.Ref(id), 3, ecs.NoType.ApplyTo) }) w.RegisterCreator(wcBaz, func(id ecs.EntityID, _ ecs.ComponentType) { fmt.Printf("[%v] +baz after 4 bar\n", id) w.time.After(w.Ref(id), 4, wcBar.ApplyTo) }) w.RegisterDestroyer(ecs.NoType, func(id ecs.EntityID, _ ecs.ComponentType) { fmt.Printf("[%v] gone\n", id) }) } func (w *world) dump() { fmt.Printf("now=%v\n", w.time.Now()) for it := w.Iter(); it.Next(); { if it.Type() != ecs.NoType { fmt.Printf("- [%v]<%v>\n", it.ID(), it.Type()) } } fmt.Printf("\n") } func main() { var w world w.init() w.AddEntity(wcFoo) w.AddEntity(wcBar) w.AddEntity(wcBaz) w.dump() for i := 0; i < 1000 && !w.Empty(); i++ { w.Process() } }
Output: [1] +foo after 2 bar [2] +bar after 3 none [3] +baz after 4 bar now=t0 - [1]<0000000000000003> - [2]<0000000000000005> - [3]<0000000000000009> now=t1 - [1]<0000000000000003> - [2]<0000000000000005> - [3]<0000000000000009> [1] +bar after 3 none now=t2 - [1]<0000000000000005> - [2]<0000000000000005> - [3]<0000000000000009> [2] gone now=t3 - [1]<0000000000000005> - [3]<0000000000000009> [3] +bar after 3 none now=t4 - [1]<0000000000000005> - [3]<0000000000000005> [1] gone now=t5 - [3]<0000000000000005> now=t6 - [3]<0000000000000005> [3] gone now=t7
Index ¶
- type Duration
- type Facility
- func (fac *Facility) After(ent ecs.Entity, d Duration, callback func(ecs.Entity))
- func (fac *Facility) Cancel(ent ecs.Entity) bool
- func (fac *Facility) Every(ent ecs.Entity, d Duration, callback func(ecs.Entity))
- func (fac *Facility) Init(core *ecs.Core, t ecs.ComponentType)
- func (fac Facility) Now() Time
- func (fac *Facility) Process()
- type Time
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Duration ¶
type Duration int64
Duration represents ecs.System time spans; it counts a number of Process() ticks.
type Facility ¶
type Facility struct {
// contains filtered or unexported fields
}
Facility implements a timer facility attached to an ecs.Core.
func (*Facility) After ¶
After attaches a one-shot timer to the given entity that expires after d processing time has elapsed calling the given function with the attached entity.
Any prior timer (one-shot or periodic) attached to the entity is overwritten.
Panics if the entity does not belong to the Facility's core, or the duration is not positive.
func (*Facility) Cancel ¶
Cancel deletes any timer (one-shot or periodic )attached to the given entity, returning true only if there was such a timer to delete.
func (*Facility) Every ¶
Every attaches a periodic timer to the given entity that fires every d processing time has elapsed, calling the given function with the attached entity every time.
Any prior timer (one-shot or periodic) attached to the entity is overwritten.
Panics if the entity does not belong to the Facility's core, or the duration is not positive.
func (*Facility) Init ¶
func (fac *Facility) Init(core *ecs.Core, t ecs.ComponentType)
Init sets up the timer facility, attached to the given ecs.Core, and using the supplied ComponentType to indicate "has a timer". The given ComponentType MUST NOT be registered by another allocator.
func (*Facility) Process ¶
func (fac *Facility) Process()
Process calls any timers whose time has come.
Panics if The End Time has come (2^64 Process()ing ticks integer overflow).
Callback functions are called (in an ARBITRARY order) in one batch AFTER all expired timers have been processed. Therefore callbacks may re-set a one-shot, or cancel a periodic (their own timer, or another).
type Time ¶
type Time uint64
Time represents Proccess() time in an ecs.System; it counts number of processing ticks.
func (Time) Add ¶
Add a Duration, returning a Time; result is clamped to Time(0) and Time(math.MaxUint64).