github.com/df-mc/dragonfly@v0.9.13/server/entity/stationary.go (about) 1 package entity 2 3 import ( 4 "github.com/df-mc/dragonfly/server/world" 5 "math" 6 "time" 7 ) 8 9 // StationaryBehaviourConfig holds settings that influence the way 10 // StationaryBehaviour operates. StationaryBehaviourConfig.New() may be called 11 // to create a new behaviour with this config. 12 type StationaryBehaviourConfig struct { 13 // ExistenceDuration is the duration that an entity with this behaviour 14 // should last. Once this time expires, the entity is closed. If 15 // ExistenceDuration is 0, the entity will never expire automatically. 16 ExistenceDuration time.Duration 17 // SpawnSounds is a slice of sounds to be played upon the spawning of the 18 // entity. 19 SpawnSounds []world.Sound 20 // Tick is a function called every world tick. It may be used to implement 21 // additional behaviour for stationary entities. 22 Tick func(e *Ent) 23 } 24 25 // New creates a StationaryBehaviour using the settings provided in conf. 26 func (conf StationaryBehaviourConfig) New() *StationaryBehaviour { 27 if conf.ExistenceDuration == 0 { 28 conf.ExistenceDuration = math.MaxInt64 29 } 30 return &StationaryBehaviour{conf: conf} 31 } 32 33 // StationaryBehaviour implements the behaviour of an entity that is unable to 34 // move, such as a text entity or an area effect cloud. Applying velocity to 35 // such entities will not move them. 36 type StationaryBehaviour struct { 37 conf StationaryBehaviourConfig 38 age time.Duration 39 close bool 40 } 41 42 // Tick checks if the entity should be closed and runs whatever additional 43 // behaviour the entity might require. 44 func (s *StationaryBehaviour) Tick(e *Ent) *Movement { 45 if s.close { 46 _ = e.Close() 47 return nil 48 } 49 50 if e.Age() == 0 { 51 for _, ss := range s.conf.SpawnSounds { 52 e.World().PlaySound(e.Position(), ss) 53 } 54 } 55 if s.conf.Tick != nil { 56 s.conf.Tick(e) 57 } 58 59 if e.Age() > s.conf.ExistenceDuration { 60 s.close = true 61 } 62 // Stationary entities never move. Always return nil here. 63 return nil 64 } 65 66 // Immobile always returns true. 67 func (s *StationaryBehaviour) Immobile() bool { 68 return true 69 }