Added Separate Packet type for Rotation sync
This commit is contained in:
1 parent
ec0fdd5bb4
commit
aa8feeb4b1
6 files changed
+67
-5
No files matched your search
@@ -1708,7 +1708,10 @@ namespace CalamityMod.CalPlayer
|
||||
public bool justChangedGravity = false;
|
||||
|
||||
public Vector2 mouseWorld => Player.MountedCenter + mouseWorldDeltaFromPlayer;
|
||||
public Vector2 mouseNormalFromPlayer => mouseRotationFromPlayer.ToRotationVector2();
|
||||
public Vector2 mouseWorldDeltaFromPlayer;
|
||||
public float mouseRotationFromPlayer;
|
||||
|
||||
private Vector2 oldMouseWorldDeltaFromPlayer;
|
||||
private int mouseWorldSyncSpamCounter = 0;
|
||||
private const int mouseWorldSyncMinTickInterval = 2;
|
||||
@@ -1720,16 +1723,19 @@ namespace CalamityMod.CalPlayer
|
||||
public bool rightClickListener = false;
|
||||
/// <summary>
|
||||
/// Set this to true if you need to receive updates on the position of the player's mouse and sync them in multiplayer.<br/>
|
||||
/// Automatically resets itself after sending an update.
|
||||
/// Automatically resets itself after sending an update.<br/>
|
||||
/// This also update the rotation.
|
||||
/// </summary>
|
||||
public bool mouseWorldListener = false;
|
||||
/// <summary>
|
||||
/// Set this to true if you need to receive updates on the rotation of the mouse to the player. This sends updates less frequently the tighter the tolerance of mouseWorldListener.<br/>
|
||||
/// Automatically resets itself after sending an update.
|
||||
/// Automatically resets itself after sending an update.<br/>
|
||||
/// This does NOT update the position.
|
||||
/// </summary>
|
||||
public bool mouseRotationListener = false;
|
||||
|
||||
public bool syncMousePosition = false;
|
||||
public bool syncMouseRotation = false;
|
||||
public bool syncMouseRightClick = false;
|
||||
#endregion
|
||||
|
||||
@@ -4291,6 +4297,7 @@ namespace CalamityMod.CalPlayer
|
||||
mouseRight = PlayerInput.Triggers.Current.MouseRight;
|
||||
var worldPos = LockOnHelper.Enabled ? LockOnHelper.PredictedPosition : Main.MouseWorld;
|
||||
mouseWorldDeltaFromPlayer = worldPos - Player.MountedCenter;
|
||||
mouseRotationFromPlayer = mouseWorldDeltaFromPlayer.ToRotation();
|
||||
|
||||
if (rightClickListener && mouseRight != oldMouseRight)
|
||||
{
|
||||
@@ -4309,11 +4316,11 @@ namespace CalamityMod.CalPlayer
|
||||
mouseWorldListener = false;
|
||||
}
|
||||
|
||||
if (mouseRotationListener && Math.Abs((mouseWorldDeltaFromPlayer).ToRotation() - (oldMouseWorldDeltaFromPlayer).ToRotation()) > 0.15f)
|
||||
if (!syncMousePosition && mouseRotationListener && Math.Abs((mouseWorldDeltaFromPlayer).ToRotation() - (oldMouseWorldDeltaFromPlayer).ToRotation()) > 0.15f)
|
||||
{
|
||||
mouseWorldSyncSpamCounter = 0;
|
||||
oldMouseWorldDeltaFromPlayer = mouseWorldDeltaFromPlayer;
|
||||
syncMousePosition = true;
|
||||
syncMouseRotation = true;
|
||||
mouseRotationListener = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,6 +187,12 @@ namespace CalamityMod.CalPlayer
|
||||
syncMousePosition = false;
|
||||
MousePositionSync();
|
||||
}
|
||||
|
||||
if (syncMouseRotation)
|
||||
{
|
||||
syncMouseRotation = false;
|
||||
MouseRotationSync();
|
||||
}
|
||||
}
|
||||
|
||||
if (Player.HeldItem.type != ModContent.ItemType<SaharaSlicers>())
|
||||
|
||||
@@ -30,6 +30,11 @@ namespace CalamityMod.CalPlayer
|
||||
MousePositionSyncPacket.Send(this);
|
||||
}
|
||||
|
||||
internal void MouseRotationSync()
|
||||
{
|
||||
MouseRotationSyncPacket.Send(this);
|
||||
}
|
||||
|
||||
internal void MouseRightClickSync()
|
||||
{
|
||||
RightClickSyncPacket.Send(this);
|
||||
|
||||
@@ -245,6 +245,7 @@ namespace CalamityMod
|
||||
|
||||
// Mouse Controls syncs
|
||||
RightClickSync,
|
||||
MouseRotationSync,
|
||||
MousePositionSync,
|
||||
|
||||
// World state sync
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.IO;
|
||||
using CalamityMod.CalPlayer;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Terraria;
|
||||
|
||||
namespace CalamityMod.Packets
|
||||
@@ -31,7 +32,9 @@ namespace CalamityMod.Packets
|
||||
if (player is null)
|
||||
return;
|
||||
|
||||
player.mouseWorldDeltaFromPlayer = new(deltaX, deltaY);
|
||||
var delta = new Vector2(deltaX, deltaY);
|
||||
player.mouseWorldDeltaFromPlayer = delta;
|
||||
player.mouseRotationFromPlayer = delta.ToRotation();
|
||||
|
||||
if (Main.dedServ)
|
||||
Send(player, ignoreClient: sender);
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using CalamityMod.CalPlayer;
|
||||
using Terraria;
|
||||
|
||||
namespace CalamityMod.Packets
|
||||
{
|
||||
public sealed class MouseRotationSyncPacket : CalamityPacket
|
||||
{
|
||||
public static MouseRotationSyncPacket Instance { get; private set; }
|
||||
|
||||
public override byte MessageType => (byte)CalamityModMessageType.MouseRotationSync;
|
||||
|
||||
public static void Send(CalamityPlayer player, int toClient = -1, int ignoreClient = -1)
|
||||
{
|
||||
if (player is null)
|
||||
return;
|
||||
|
||||
var packet = Instance.CreateBasePacket();
|
||||
packet.WriteWhoAmI(player);
|
||||
packet.Write((Half)player.mouseRotationFromPlayer);
|
||||
packet.Send(toClient, ignoreClient);
|
||||
}
|
||||
|
||||
public override void HandlePacket(in BinaryReader packet, int sender)
|
||||
{
|
||||
var player = packet.ReadCalamityPlayer();
|
||||
var rotation = (float)packet.ReadHalf();
|
||||
|
||||
if (player is null)
|
||||
return;
|
||||
|
||||
player.mouseRotationFromPlayer = rotation;
|
||||
player.mouseWorldDeltaFromPlayer = rotation.ToRotationVector2();
|
||||
|
||||
if (Main.dedServ)
|
||||
Send(player, ignoreClient: sender);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user