Skip to content

Commit

Permalink
Implemented torches (df-mc#74)
Browse files Browse the repository at this point in the history
* Implemented torches

* Embed transparent struct

* Wood Fence produce bass sounds w/ noteblocks
  • Loading branch information
DaPigGuy authored Oct 3, 2020
1 parent 062328d commit 1fd9649
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions dragonfly/block/hashes.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ const (
hashSponge
hashStainedTerracotta
hashStone
hashTorch
hashGranite
hashDiorite
hashAndesite
Expand Down
3 changes: 3 additions & 0 deletions dragonfly/block/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func init() {
world.RegisterBlock(BlueIce{})
world.RegisterBlock(GildedBlackstone{})
world.RegisterBlock(Shroomlight{})
world.RegisterBlock(allTorch()...)
world.RegisterBlock(allCake()...)
world.RegisterBlock(NetherWart{}, NetherWart{Age: 1}, NetherWart{Age: 2}, NetherWart{Age: 3})
world.RegisterBlock(InvisibleBedrock{})
Expand Down Expand Up @@ -304,6 +305,8 @@ func init() {
world.RegisterItem("minecraft:blue_ice", BlueIce{})
world.RegisterItem("minecraft:gilded_blackstone", GildedBlackstone{})
world.RegisterItem("minecraft:shroomlight", Shroomlight{})
world.RegisterItem("minecraft:torch", Torch{Type: fire.Normal()})
world.RegisterItem("minecraft:soul_torch", Torch{Type: fire.Soul()})
world.RegisterItem("minecraft:cake", Cake{})
world.RegisterItem("minecraft:nether_wart", NetherWart{})
world.RegisterItem("minecraft:invisibleBedrock", InvisibleBedrock{})
Expand Down
123 changes: 123 additions & 0 deletions dragonfly/block/torch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package block

import (
"github.com/df-mc/dragonfly/dragonfly/block/fire"
"github.com/df-mc/dragonfly/dragonfly/item"
"github.com/df-mc/dragonfly/dragonfly/world"
"github.com/go-gl/mathgl/mgl64"
)

// Torch are non-solid blocks that emit light.
type Torch struct {
noNBT
transparent
empty

// Facing is the direction from the torch to the block.
Facing world.Face
// Type is the type of fire lighting the torch.
Type fire.Fire
}

// LightEmissionLevel ...
func (t Torch) LightEmissionLevel() uint8 {
switch t.Type {
case fire.Normal():
return 14
default:
return t.Type.LightLevel
}
}

// UseOnBlock ...
func (t Torch) UseOnBlock(pos world.BlockPos, face world.Face, clickPos mgl64.Vec3, w *world.World, user item.User, ctx *item.UseContext) bool {
pos, face, used := firstReplaceable(w, pos, face, t)
if !used {
return false
}
if face == world.FaceDown {
return false
}
if _, ok := w.Block(pos).(world.Liquid); ok {
return false
}
if !w.Block(pos.Side(face.Opposite())).Model().FaceSolid(pos.Side(face.Opposite()), face, w) {
found := false
for _, i := range []world.Face{world.FaceSouth, world.FaceWest, world.FaceNorth, world.FaceEast, world.FaceDown} {
if w.Block(pos.Side(i)).Model().FaceSolid(pos.Side(i), i.Opposite(), w) {
found = true
face = i.Opposite()
break
}
}
if !found {
return false
}
}
t.Facing = face.Opposite()

place(w, pos, t, user, ctx)
return placed(ctx)
}

// NeighbourUpdateTick ...
func (t Torch) NeighbourUpdateTick(pos, _ world.BlockPos, w *world.World) {
if !w.Block(pos.Side(t.Facing)).Model().FaceSolid(pos.Side(t.Facing), t.Facing.Opposite(), w) {
w.BreakBlockWithoutParticles(pos)
}
}

// HasLiquidDrops ...
func (t Torch) HasLiquidDrops() bool {
return true
}

// EncodeItem ...
func (t Torch) EncodeItem() (id int32, meta int16) {
switch t.Type {
case fire.Normal():
return 50, 0
case fire.Soul():
return -268, 0
}
panic("invalid fire type")
}

// EncodeBlock ...
func (t Torch) EncodeBlock() (name string, properties map[string]interface{}) {
facing := "up"
switch t.Facing {
case world.FaceDown:
facing = "top"
case world.FaceNorth:
facing = "north"
case world.FaceEast:
facing = "east"
case world.FaceSouth:
facing = "south"
case world.FaceWest:
facing = "west"
}

switch t.Type {
case fire.Normal():
return "minecraft:torch", map[string]interface{}{"torch_facing_direction": facing}
case fire.Soul():
return "minecraft:soul_torch", map[string]interface{}{"torch_facing_direction": facing}
}
panic("invalid fire type")
}

// Hash ...
func (t Torch) Hash() uint64 {
return hashTorch | (uint64(t.Facing) << 32) | (uint64(t.Type.Uint8()) << 35)
}

// allTorch ...
func allTorch() (torch []world.Block) {
for i := world.Face(0); i < 6; i++ {
torch = append(torch, Torch{Type: fire.Normal(), Facing: i})
torch = append(torch, Torch{Type: fire.Soul(), Facing: i})
}
return
}
1 change: 1 addition & 0 deletions dragonfly/block/wood_fence.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type WoodFence struct {
noNBT
transparent
bass

// Wood is the type of wood of the fence. This field must have one of the values found in the wood
// package.
Expand Down

0 comments on commit 1fd9649

Please sign in to comment.