github.com/df-mc/dragonfly@v0.9.13/server/entity/tnt.go (about) 1 package entity 2 3 import ( 4 "github.com/df-mc/dragonfly/server/block" 5 "github.com/df-mc/dragonfly/server/block/cube" 6 "github.com/df-mc/dragonfly/server/internal/nbtconv" 7 "github.com/df-mc/dragonfly/server/world" 8 "github.com/go-gl/mathgl/mgl64" 9 "math" 10 "math/rand" 11 "time" 12 ) 13 14 // NewTNT creates a new primed TNT entity. 15 func NewTNT(pos mgl64.Vec3, fuse time.Duration) *Ent { 16 config := tntConf 17 config.ExistenceDuration = fuse 18 ent := Config{Behaviour: config.New()}.New(TNTType{}, pos) 19 20 angle := rand.Float64() * math.Pi * 2 21 ent.vel = mgl64.Vec3{-math.Sin(angle) * 0.02, 0.1, -math.Cos(angle) * 0.02} 22 return ent 23 } 24 25 var tntConf = PassiveBehaviourConfig{ 26 Gravity: 0.04, 27 Drag: 0.02, 28 Expire: explodeTNT, 29 } 30 31 // explodeTNT creates an explosion at the position of e. 32 func explodeTNT(e *Ent) { 33 var config block.ExplosionConfig 34 config.Explode(e.World(), e.Position()) 35 } 36 37 // TNTType is a world.EntityType implementation for TNT. 38 type TNTType struct{} 39 40 func (TNTType) EncodeEntity() string { return "minecraft:tnt" } 41 func (TNTType) NetworkOffset() float64 { return 0.49 } 42 func (TNTType) BBox(world.Entity) cube.BBox { 43 return cube.Box(-0.49, 0, -0.49, 0.49, 0.98, 0.49) 44 } 45 46 func (TNTType) DecodeNBT(m map[string]any) world.Entity { 47 tnt := NewTNT(nbtconv.Vec3(m, "Pos"), nbtconv.TickDuration[uint8](m, "Fuse")) 48 tnt.vel = nbtconv.Vec3(m, "Motion") 49 return tnt 50 } 51 52 func (TNTType) EncodeNBT(e world.Entity) map[string]any { 53 t := e.(*Ent) 54 return map[string]any{ 55 "Pos": nbtconv.Vec3ToFloat32Slice(t.Position()), 56 "Motion": nbtconv.Vec3ToFloat32Slice(t.Velocity()), 57 "Fuse": uint8(t.Behaviour().(*PassiveBehaviour).Fuse().Milliseconds() / 50), 58 } 59 }