github.com/df-mc/dragonfly@v0.9.13/server/block/model/fence.go (about)

     1  package model
     2  
     3  import (
     4  	"github.com/df-mc/dragonfly/server/block/cube"
     5  	"github.com/df-mc/dragonfly/server/world"
     6  )
     7  
     8  // Fence is a model used by fences of any type. It can attach to blocks with solid faces and other fences of the same
     9  // type and has a model height just slightly over 1.
    10  type Fence struct {
    11  	// Wood specifies if the Fence is made from wood. This field is used to check if two fences are able to attach to
    12  	// each other.
    13  	Wood bool
    14  }
    15  
    16  // BBox returns multiple physics.BBox depending on how many connections it has with the surrounding blocks.
    17  func (f Fence) BBox(pos cube.Pos, w *world.World) []cube.BBox {
    18  	const offset = 0.375
    19  
    20  	boxes := make([]cube.BBox, 0, 5)
    21  	mainBox := cube.Box(offset, 0, offset, 1-offset, 1.5, 1-offset)
    22  
    23  	for i := cube.Face(2); i < 6; i++ {
    24  		pos := pos.Side(i)
    25  		block := w.Block(pos)
    26  
    27  		if fence, ok := block.Model().(Fence); (ok && fence.Wood == f.Wood) || block.Model().FaceSolid(pos, i, w) {
    28  			boxes = append(boxes, mainBox.ExtendTowards(i, offset))
    29  		} else if _, ok := block.Model().(FenceGate); ok {
    30  			boxes = append(boxes, mainBox.ExtendTowards(i, offset))
    31  		}
    32  	}
    33  	return append(boxes, mainBox)
    34  }
    35  
    36  // FaceSolid returns true if the face is cube.FaceDown or cube.FaceUp.
    37  func (f Fence) FaceSolid(_ cube.Pos, face cube.Face, _ *world.World) bool {
    38  	return face == cube.FaceDown || face == cube.FaceUp
    39  }