smooth client side turret rotation

This commit is contained in:
Ozzatron committed 2020-09-15 21:48:55 -04:00
1 parent c375ccc044
commit a902090187
3 files changed
+23 -2

No files matched your search

+4
View File
@@ -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()
+15 -1
View File
@@ -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();
+4 -1
View File
@@ -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());