Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add slime blocks #445

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions server/block/hash.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/block/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func init() {
world.RegisterBlock(Sand{})
world.RegisterBlock(SeaLantern{})
world.RegisterBlock(Shroomlight{})
world.RegisterBlock(SlimeBlock{})
world.RegisterBlock(Snow{})
world.RegisterBlock(SoulSand{})
world.RegisterBlock(SoulSoil{})
Expand Down
61 changes: 61 additions & 0 deletions server/block/slime_block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package block

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

// SlimeBlock is a block that slows down entities and bounces them up if they drop onto it.
type SlimeBlock struct {
solid
transparent
}

// velocityEntity is an entity that has velocity.
type velocityEntity interface {
// Velocity returns the current velocity of the entity.
Velocity() mgl64.Vec3
// SetVelocity sets the velocity of the entity.
SetVelocity(v mgl64.Vec3)
}

// sneakingEntity is an entity that can sneak. This is typically just players.
type sneakingEntity interface {
// Sneaking returns whether the entity is sneaking.
Sneaking() bool
}

// EntityLand ...
func (SlimeBlock) EntityLand(_ cube.Pos, _ *world.World, e world.Entity) {
if e, ok := e.(fallDistanceEntity); ok {
HashimTheArab marked this conversation as resolved.
Show resolved Hide resolved
if s, ok := e.(sneakingEntity); ok && s.Sneaking() {
HashimTheArab marked this conversation as resolved.
Show resolved Hide resolved
e.ResetFallDistance()
}
}
if e, ok := e.(velocityEntity); ok {
v := e.Velocity()
v[1] *= -1
e.SetVelocity(v)
}
}

// BreakInfo ...
func (s SlimeBlock) BreakInfo() BreakInfo {
return newBreakInfo(0, alwaysHarvestable, nothingEffective, oneOf(s))
}

// Friction ...
func (SlimeBlock) Friction() float64 {
return 0.8
}

// EncodeItem ...
func (SlimeBlock) EncodeItem() (name string, meta int16) {
return "minecraft:slime", 0
}

// EncodeBlock ...
func (SlimeBlock) EncodeBlock() (string, map[string]interface{}) {
return "minecraft:slime", nil
}
16 changes: 16 additions & 0 deletions server/entity/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ func (it *Item) SetPickupDelay(d time.Duration) {
it.pickupDelay = ticks
}

// entityLander represents a block that reacts to an entity landing on it after falling.
type entityLander interface {
// EntityLand is called when an entity lands on the block.
EntityLand(pos cube.Pos, w *world.World, e world.Entity)
}

// Tick ticks the entity, performing movement.
func (it *Item) Tick(w *world.World, current int64) {
it.mu.Lock()
Expand All @@ -78,6 +84,16 @@ func (it *Item) Tick(w *world.World, current int64) {

m.Send()

pos := cube.PosFromVec3(m.pos)
b := w.Block(pos)
if len(b.Model().BBox(pos, w)) == 0 {
pos = pos.Sub(cube.Pos{0, 1})
b = w.Block(pos)
}
if h, ok := b.(entityLander); ok {
h.EntityLand(pos, w, it)
}

if m.pos[1] < float64(w.Range()[0]) && current%10 == 0 {
_ = it.Close()
return
Expand Down