Skip to content

Commit

Permalink
block/oxidizable.go: Fix int division
Browse files Browse the repository at this point in the history
  • Loading branch information
TwistedAsylumMC committed Nov 18, 2024
1 parent 0637012 commit efaa386
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
63 changes: 63 additions & 0 deletions server/entity/animation/animation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package animation

// Animation represents an animation that may be played on an entity from an active resource pack on
// the client.
type Animation struct {
name string
nextState string
controller string
stopCondition string
}

// New returns a new animation that can be played on an entity. If no controller or stop condition is set,
// the animation will play for its full duration, including looping. Controllers can be set to manage
// multiple states of animations. It is also possible to use vanilla animations/controllers if they work
// for your entity, i.e. "animation.pig.baby_transform".
func New(name string) Animation {
return Animation{name: name}
}

// Name returns the name of the animation to be played.
func (a Animation) Name() string {
return a.name
}

// Controller returns the name of the controller to be used for the animation.
func (a Animation) Controller() string {
return a.controller
}

// WithController returns a copy of the Animation with the provided animation controller. An animation
// controller with the same name must be defined in a resource pack for it to work.
func (a Animation) WithController(controller string) Animation {
a.controller = controller
return a
}

// NextState returns the state to transition to after the animation has finished playing within the
// animation controller.
func (a Animation) NextState() string {
return a.nextState
}

// WithNextState returns a copy of the Animation with the provided state to transition to after the
// animation has finished playing within the animation controller.
func (a Animation) WithNextState(state string) Animation {
a.nextState = state
return a
}

// StopCondition returns the condition that must be met for the animation to stop playing. This is often
// a Molang expression that can be used to query various entity properties to determine when the animation
// should stop playing.
func (a Animation) StopCondition() string {
return a.stopCondition
}

// WithStopCondition returns a copy of the Animation with the provided stop condition. The stop condition
// is a Molang expression that can be used to query various entity properties to determine when the animation
// should stop playing.
func (a Animation) WithStopCondition(condition string) Animation {
a.stopCondition = condition
return a
}
8 changes: 6 additions & 2 deletions server/session/world.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package session

import (
"github.com/df-mc/dragonfly/server/entity/animation"
"github.com/df-mc/dragonfly/server/entity/effect"
"image/color"
"math/rand"
Expand Down Expand Up @@ -986,9 +987,12 @@ func (s *Session) ViewEntityState(e world.Entity) {
}

// ViewEntityAnimation ...
func (s *Session) ViewEntityAnimation(e world.Entity, animationName string) {
func (s *Session) ViewEntityAnimation(e world.Entity, a animation.Animation) {
s.writePacket(&packet.AnimateEntity{
Animation: animationName,
Animation: a.Name(),
NextState: a.NextState(),
StopCondition: a.StopCondition(),
Controller: a.Controller(),
EntityRuntimeIDs: []uint64{
s.entityRuntimeID(e),
},
Expand Down
7 changes: 4 additions & 3 deletions server/world/viewer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package world

import (
"github.com/df-mc/dragonfly/server/block/cube"
"github.com/df-mc/dragonfly/server/entity/animation"
"github.com/df-mc/dragonfly/server/world/chunk"
"github.com/go-gl/mathgl/mgl64"
"github.com/google/uuid"
Expand Down Expand Up @@ -49,8 +50,8 @@ type Viewer interface {
// ViewEntityState views the current state of an entity. It is called whenever an entity changes its
// physical appearance, for example when sprinting.
ViewEntityState(e Entity)
// ViewEntityAnimation starts viewing an animation performed by an entity. The animation has to be from a resource pack.
ViewEntityAnimation(e Entity, animationName string)
// ViewEntityAnimation starts viewing an animation performed by an entity.
ViewEntityAnimation(e Entity, a animation.Animation)
// ViewParticle views a particle spawned at a given position in the world. It is called when a particle,
// for example a block breaking particle, is spawned near the player.
ViewParticle(pos mgl64.Vec3, p Particle)
Expand Down Expand Up @@ -91,7 +92,7 @@ func (NopViewer) ViewEntityItems(Entity)
func (NopViewer) ViewEntityArmour(Entity) {}
func (NopViewer) ViewEntityAction(Entity, EntityAction) {}
func (NopViewer) ViewEntityState(Entity) {}
func (NopViewer) ViewEntityAnimation(Entity, string) {}
func (NopViewer) ViewEntityAnimation(Entity, animation.Animation) {}
func (NopViewer) ViewParticle(mgl64.Vec3, Particle) {}
func (NopViewer) ViewSound(mgl64.Vec3, Sound) {}
func (NopViewer) ViewBlockUpdate(cube.Pos, Block, int) {}
Expand Down
12 changes: 12 additions & 0 deletions server/world/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package world

import (
"errors"
"github.com/df-mc/dragonfly/server/entity/animation"
"math/rand"
"sync"
"time"
Expand Down Expand Up @@ -655,6 +656,17 @@ func (w *World) AddParticle(pos mgl64.Vec3, p Particle) {
}
}

// PlayEntityAnimation plays an animation on an entity in the world. The animation is played for all viewers
// of the entity.
func (w *World) PlayEntityAnimation(e Entity, a animation.Animation) {
if w == nil {
return
}
for _, viewer := range w.Viewers(e.Position()) {
viewer.ViewEntityAnimation(e, a)
}
}

// PlaySound plays a sound at a specific position in the world. Viewers of that position will be able to hear
// the sound if they're close enough.
func (w *World) PlaySound(pos mgl64.Vec3, s Sound) {
Expand Down

0 comments on commit efaa386

Please sign in to comment.