Translate camera instruction set

This commit is contained in:
AkmalFairuz
2025-06-20 02:11:25 +07:00
parent 10608d39cf
commit fa70365ddf
3 changed files with 100 additions and 28 deletions

View File

@@ -9,7 +9,7 @@ import (
// CameraInstruction gives a custom camera specific instructions to operate.
type CameraInstruction struct {
// Set is a camera instruction that sets the camera to a specified preset.
Set protocol.Optional[protocol.CameraInstructionSet]
Set protocol.Optional[proto.CameraInstructionSet]
// Clear can be set to true to clear all the current camera instructions.
Clear protocol.Optional[bool]
// Fade is a camera instruction that fades the screen to a specified colour.

View File

@@ -88,7 +88,6 @@ func (x *CameraPreset) FromLatest(cp protocol.CameraPreset) CameraPreset {
x.Radius = cp.Radius
x.AudioListener = cp.AudioListener
x.PlayerEffects = cp.PlayerEffects
x.AlignTargetAndCameraForward = cp.AlignTargetAndCameraForward
x.AimAssist = cp.AimAssist
x.ControlScheme = cp.ControlScheme
return *x
@@ -116,7 +115,6 @@ func (x *CameraPreset) ToLatest() protocol.CameraPreset {
MaxYawLimit: x.MaxYawLimit,
AudioListener: x.AudioListener,
PlayerEffects: x.PlayerEffects,
AlignTargetAndCameraForward: x.AlignTargetAndCameraForward,
AimAssist: x.AimAssist,
ControlScheme: x.ControlScheme,
}
@@ -154,7 +152,7 @@ func (x *CameraPreset) Marshal(r protocol.IO) {
protocol.OptionalFunc(r, &x.Radius, r.Float32)
protocol.OptionalFunc(r, &x.AudioListener, r.Uint8)
protocol.OptionalFunc(r, &x.PlayerEffects, r.Bool)
if IsProtoGTE(r, ID748) {
if IsProtoGTE(r, ID748) && IsProtoLT(r, ID818) {
protocol.OptionalFunc(r, &x.AlignTargetAndCameraForward, r.Bool)
}
if IsProtoGTE(r, ID766) {
@@ -164,3 +162,69 @@ func (x *CameraPreset) Marshal(r protocol.IO) {
protocol.OptionalFunc(r, &x.ControlScheme, r.Uint8)
}
}
// CameraInstructionSet represents a camera instruction that sets the camera to a specified preset and can be extended
// with easing functions and translations to the camera's position and rotation.
type CameraInstructionSet struct {
// Preset is the index of the preset in the CameraPresets packet sent to the player.
Preset uint32
// Ease represents the easing function that is used by the instruction.
Ease protocol.Optional[protocol.CameraEase]
// Position represents the position of the camera.
Position protocol.Optional[mgl32.Vec3]
// Rotation represents the rotation of the camera.
Rotation protocol.Optional[mgl32.Vec2]
// Facing is a vector that the camera will always face towards during the duration of the instruction.
Facing protocol.Optional[mgl32.Vec3]
// ViewOffset is an offset based on a pivot point to the player, causing the camera to be shifted in a
// certain direction.
ViewOffset protocol.Optional[mgl32.Vec2]
// EntityOffset is an offset from the entity that the camera should be rendered at.
EntityOffset protocol.Optional[mgl32.Vec3]
// Default determines whether the camera is a default camera or not.
Default protocol.Optional[bool]
// IgnoreStartingValuesComponent behavior is currently unknown.
IgnoreStartingValuesComponent bool
}
func (x *CameraInstructionSet) FromLatest(cis protocol.CameraInstructionSet) CameraInstructionSet {
x.Preset = cis.Preset
x.Ease = cis.Ease
x.Position = cis.Position
x.Rotation = cis.Rotation
x.Facing = cis.Facing
x.ViewOffset = cis.ViewOffset
x.EntityOffset = cis.EntityOffset
x.Default = cis.Default
x.IgnoreStartingValuesComponent = cis.IgnoreStartingValuesComponent
return *x
}
func (x *CameraInstructionSet) ToLatest() protocol.CameraInstructionSet {
return protocol.CameraInstructionSet{
Preset: x.Preset,
Ease: x.Ease,
Position: x.Position,
Rotation: x.Rotation,
Facing: x.Facing,
ViewOffset: x.ViewOffset,
EntityOffset: x.EntityOffset,
Default: x.Default,
IgnoreStartingValuesComponent: x.IgnoreStartingValuesComponent,
}
}
// Marshal encodes/decodes a CameraInstructionSet.
func (x *CameraInstructionSet) Marshal(r protocol.IO) {
r.Uint32(&x.Preset)
protocol.OptionalMarshaler(r, &x.Ease)
protocol.OptionalFunc(r, &x.Position, r.Vec3)
protocol.OptionalFunc(r, &x.Rotation, r.Vec2)
protocol.OptionalFunc(r, &x.Facing, r.Vec3)
protocol.OptionalFunc(r, &x.ViewOffset, r.Vec2)
protocol.OptionalFunc(r, &x.EntityOffset, r.Vec3)
protocol.OptionalFunc(r, &x.Default, r.Bool)
if IsProtoGTE(r, ID818) {
r.Bool(&x.IgnoreStartingValuesComponent)
}
}

View File

@@ -382,8 +382,12 @@ func (p *Protocol) downgradePackets(pks []packet.Packet, conn *minecraft.Conn) [
EntityLink: (&proto.EntityLink{}).FromLatest(pk.EntityLink),
}
case *packet.CameraInstruction:
var iSet protocol.Optional[proto.CameraInstructionSet]
if v, ok := pk.Set.Value(); ok {
iSet = protocol.Option((&proto.CameraInstructionSet{}).FromLatest(v))
}
pks[pkIndex] = &legacypacket.CameraInstruction{
Set: pk.Set,
Set: iSet,
Clear: pk.Clear,
Fade: pk.Fade,
Target: pk.Target,
@@ -952,8 +956,12 @@ func (p *Protocol) upgradePackets(pks []packet.Packet, conn *minecraft.Conn) []p
EntityLink: pk.EntityLink.ToLatest(),
}
case *legacypacket.CameraInstruction:
var iSet protocol.Optional[protocol.CameraInstructionSet]
if v, ok := pk.Set.Value(); ok {
iSet = protocol.Option(v.ToLatest())
}
pks[pkIndex] = &packet.CameraInstruction{
Set: pk.Set,
Set: iSet,
Clear: pk.Clear,
Fade: pk.Fade,
Target: pk.Target,