Small netcode improvement (#45)

* Removed unnecessary casting

* Using byte for small state sync

* Better Overfilled state sync on Pumpler
This commit is contained in:
Flowaria authored and Ozzatron committed 2024-07-21 22:22:50 -04:00
1 parent 3b06dae051
commit 924be3700a
4 files changed
+22 -16

No files matched your search

+3 -3
View File
@@ -16,7 +16,7 @@ namespace CalamityMod.Projectiles.Melee
public class BladecrestOathswordProj : BaseIdleHoldoutProjectile
{
public override LocalizedText DisplayName => CalamityUtils.GetItemName<BladecrestOathsword>();
public enum SwingState
public enum SwingState : byte
{
Default,
Swinging
@@ -57,14 +57,14 @@ namespace CalamityMod.Projectiles.Melee
{
writer.Write(Direction);
writer.Write(PostSwingRepositionDelay);
writer.Write((int)CurrentState);
writer.Write((byte)CurrentState);
}
public override void ReceiveExtraAI(BinaryReader reader)
{
Direction = reader.ReadInt32();
PostSwingRepositionDelay = reader.ReadSingle();
CurrentState = (SwingState)reader.ReadInt32();
CurrentState = (SwingState)reader.ReadByte();
}
public override void SafeAI()
+3 -3
View File
@@ -16,7 +16,7 @@ namespace CalamityMod.Projectiles.Melee
public class OldLordClaymoreProj : BaseIdleHoldoutProjectile
{
public override LocalizedText DisplayName => CalamityUtils.GetItemName<OldLordClaymore>();
public enum SwingState
public enum SwingState : byte
{
Default,
Swinging,
@@ -63,7 +63,7 @@ namespace CalamityMod.Projectiles.Melee
writer.Write(Direction);
writer.Write(PostSwingRepositionDelay);
writer.Write(ChargePower);
writer.Write((int)CurrentState);
writer.Write((byte)CurrentState);
}
public override void ReceiveExtraAI(BinaryReader reader)
@@ -71,7 +71,7 @@ namespace CalamityMod.Projectiles.Melee
Direction = reader.ReadInt32();
PostSwingRepositionDelay = reader.ReadSingle();
ChargePower = reader.ReadSingle();
CurrentState = (SwingState)reader.ReadInt32();
CurrentState = (SwingState)reader.ReadByte();
}
public override void SafeAI()
+14 -8
View File
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using CalamityMod.Items.Weapons.Ranged;
using CalamityMod.Particles;
using Microsoft.Xna.Framework;
@@ -28,8 +29,13 @@ namespace CalamityMod.Projectiles.Ranged
private ref float CurrentChargingFrames => ref Projectile.ai[0];
private ref float PumpkinsCharge => ref Projectile.ai[1];
private ref float FramesToLoadNextPumpkin => ref Projectile.localAI[0];
//This possibly could moved to ai[2] ¯\_(ツ)_/¯
private ref float Overfilled => ref Projectile.localAI[1]; //This functionally is a "IsPlayingShootAnim" variable
private bool IsOverfilled => (Overfilled >= 0.5f);
private float angularSpread = MathHelper.ToRadians(15);
public override void SetDefaults()
@@ -49,10 +55,10 @@ namespace CalamityMod.Projectiles.Ranged
Vector2 tipPosition = armPosition + Projectile.velocity * Projectile.width * 0.5f;
// Unloads all pumpkins if you can't shoot/stop holding out the projectile or if the gun is overfilled
if (Owner.CantUseHoldout() || Overfilled == 1f)
if (Owner.CantUseHoldout() || IsOverfilled)
{
if (PumpkinsCharge <= 0f && Overfilled == 0f) //If the projectile isnt playing its animation and if there arent any pumpkins loaded, kill it
if (PumpkinsCharge <= 0f && !IsOverfilled) //If the projectile isnt playing its animation and if there arent any pumpkins loaded, kill it
{
Projectile.Kill();
return;
@@ -68,7 +74,7 @@ namespace CalamityMod.Projectiles.Ranged
Projectile.frameCounter = 0;
}
// Shoot anim is done? Remove the shoot anim tag
if (Projectile.frame == 0 && Overfilled == 1f)
if (Projectile.frame == 0 && IsOverfilled)
Overfilled = 0f;
//Animation stuff end
@@ -145,7 +151,7 @@ namespace CalamityMod.Projectiles.Ranged
SmokeBurst(tipPosition);
FramesToLoadNextPumpkin = Owner.ActiveItem().useAnimation; //reset the reload time
PumpkinsCharge = 0; //Unload the barrel
if (!(Overfilled == 1f))
if (!IsOverfilled)
Overfilled = 1f; //Make the shoot anim play
Projectile.frame = 6; //Initialize the animation
}
@@ -213,7 +219,7 @@ namespace CalamityMod.Projectiles.Ranged
public override bool PreDraw(ref Color lightColor)
{
Main.spriteBatch.EnterShaderRegion();
if (PumpkinsCharge > 0 && Overfilled == 0f)
if (PumpkinsCharge > 0 && !IsOverfilled)
{
GameShaders.Misc["CalamityMod:BasicTint"].UseOpacity(MathHelper.Clamp(1f - 0.20f * CurrentChargingFrames - 0.1f * (5f - PumpkinsCharge), 0f, 1f));
//tint effect is visible if its charging. The more pumpkins are loaded, the more opacity
@@ -240,12 +246,12 @@ namespace CalamityMod.Projectiles.Ranged
//netcode hell
public override void SendExtraAI(BinaryWriter writer)
{
writer.Write(Overfilled);
writer.Write(IsOverfilled);
}
public override void ReceiveExtraAI(BinaryReader reader)
{
Overfilled = reader.ReadInt32();
Overfilled = reader.ReadBoolean() ? 1.0f : 0.0f;
}
}
}
@@ -76,7 +76,7 @@ namespace CalamityMod.Projectiles.Summon
writer.Write(recharging);
writer.Write(circling);
writer.Write(circlingPlayer);
writer.Write((double)floatyDistance);
writer.Write(floatyDistance);
writer.Write(target is null ? -1 : target.whoAmI);
}
public override void ReceiveExtraAI(BinaryReader reader)
@@ -84,7 +84,7 @@ namespace CalamityMod.Projectiles.Summon
recharging = reader.ReadInt32();
circling = reader.ReadBoolean();
circlingPlayer = reader.ReadBoolean();
floatyDistance = (float)reader.ReadDouble();
floatyDistance = reader.ReadSingle();
int targ = reader.ReadInt32();
target = targ == -1 ? null : Main.npc[targ];
}