From a9020901877ca84bf8aa21bccdb60fbc70d03d24 Mon Sep 17 00:00:00 2001 From: Ozzatron Date: Mon, 14 Sep 2020 17:09:40 -0400 Subject: [PATCH] smooth client side turret rotation --- TileEntities/TEBaseTurret.cs | 4 ++++ TileEntities/TEDraedonLabTurret.cs | 16 +++++++++++++++- TileEntities/TileEntityTimeHandler.cs | 5 ++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/TileEntities/TEBaseTurret.cs b/TileEntities/TEBaseTurret.cs index ee36d9e2d4..e6df4ba569 100644 --- a/TileEntities/TEBaseTurret.cs +++ b/TileEntities/TEBaseTurret.cs @@ -92,6 +92,10 @@ namespace CalamityMod.TileEntities UpdateAngle(); } + // This function does nothing by default, but turrets can use it if they need to update themselves manually on multiplayer clients. + // Calamity calls this function exactly once per frame. + public virtual void UpdateClient() { } + // This function runs on both client and server side. Clients run it manually using Tile Entity Time Handler. // This ensures that turrets aim at players visually in multiplayer without causing a packet storm. public virtual void UpdateAngle() diff --git a/TileEntities/TEDraedonLabTurret.cs b/TileEntities/TEDraedonLabTurret.cs index 97e5aa7273..969bd6c5bf 100644 --- a/TileEntities/TEDraedonLabTurret.cs +++ b/TileEntities/TEDraedonLabTurret.cs @@ -24,7 +24,7 @@ namespace CalamityMod.TileEntities // Projectile spawn location variables public override Vector2 TurretCenterOffset => new Vector2(22f + 4f * Direction, -2f); - protected override float ShootForwardsOffset => 32f; + protected override float ShootForwardsOffset => 6f; // Targeting variables public override float MaxRange => 600f; @@ -97,6 +97,20 @@ namespace CalamityMod.TileEntities return ret; } + // Makes turrets track their targeted player correctly client side. + // This doesn't improve their actual accuracy, because they only fire server side. + // However, it makes them rotate much more smoothly. + // Any targeting and rotation data derived from this update will be overridden by the turret's next sync. + public override void UpdateClient() + { + if (PlayerTargetIndex != -1) + { + Player p = Main.player[PlayerTargetIndex]; + if (p.active && !p.dead) + TargetPos = p.Center; + } + } + // This type of turret syncs extra data: its player target index. protected override void WriteExtraData(BinaryWriter writer) => writer.Write(_playerTargetIndex); protected override void ReadExtraData(Mod mod, BinaryReader reader) => _playerTargetIndex = reader.ReadInt32(); diff --git a/TileEntities/TileEntityTimeHandler.cs b/TileEntities/TileEntityTimeHandler.cs index f363b3fb11..eadc860b7d 100644 --- a/TileEntities/TileEntityTimeHandler.cs +++ b/TileEntities/TileEntityTimeHandler.cs @@ -49,8 +49,11 @@ namespace CalamityMod.TileEntities } else if (te is TEBaseTurret turret) { + // Perform any client-specific update tasks for this turret. + turret.UpdateClient(); + // Specifically on multiplayer clients, manually update the turret's rotation every frame. This is exactly the same code run server side. - // This makes sure they visually track players in multiplayer. It will NOT fire projectiles; that code can only run server side. + // This makes sure they visually track targets in multiplayer. It will NOT fire projectiles; that code can only run server side. turret.UpdateAngle(); } } while (enumerator.MoveNext());