+ Added proper multiplayer support to Dialogue Display System

This commit is contained in:
Big E authored and Cameron Ewell committed 2025-12-29 16:11:33 -05:00
1 parent ce57ad6073
commit b30acba14a
3 files changed
+217 -9

No files matched your search

+3 -1
View File
@@ -209,6 +209,8 @@ namespace CalamityMod
WantToRefundReforges,
// Player Draw Effect Parameters
SyncPlayerDrawParameter
SyncPlayerDrawParameter,
DialogueDisplayStart,
}
}
+125
View File
@@ -0,0 +1,125 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CalamityMod.Items.Weapons.Ranged;
using CalamityMod.Systems;
using CalamityMod.UI.DialogueDisplay.DisplayEffects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader.IO;
using Terraria.Net;
using static CalamityMod.UI.DialogueDisplay.DialogueDisplaySystem;
namespace CalamityMod.Packets
{
internal sealed class StartDialogueDisplayPacket : CalamityPacket
{
public static StartDialogueDisplayPacket Instance { get; private set; }
public override byte MessageType => (byte)CalamityModMessageType.DialogueDisplayStart;
public static void Send(string name, bool progressDialogue, Vector2 position, int index, int uptime, DisplayEffectID effect, float wrapWidth, int toClient = -1, int ignoreClient = -1)
{
// Only Server should send Reponse to Clients
if (!Main.dedServ)
return;
var packet = Instance.CreateBasePacket();
packet.Write(name);
packet.WriteFlags(progressDialogue, false);
packet.WritePackedVector2(position);
packet.Write(index);
packet.Write(uptime);
packet.Write((byte)effect);
packet.Write(wrapWidth);
packet.Send(toClient, ignoreClient);
}
public enum EntityType
{
NPC,
Player,
Projectile
}
public static void Send(string name, bool progressDialogue, EntityType type, int entity, int index, int uptime, DisplayEffectID effect, float wrapWidth, int toClient = -1, int ignoreClient = -1)
{
// Only Server should send Reponse to Clients
if (!Main.dedServ)
return;
var packet = Instance.CreateBasePacket();
packet.Write(name);
packet.WriteFlags(progressDialogue, true);
packet.WriteFlags(type == EntityType.NPC, type == EntityType.Player);
packet.Write(entity);
packet.Write(index);
packet.Write(uptime);
packet.Write((byte)effect);
packet.Write(wrapWidth);
packet.Send(toClient, ignoreClient);
}
public override void HandlePacket(in BinaryReader packet, int sender)
{
// Only receive info as clients
if (Main.netMode != NetmodeID.MultiplayerClient)
{
packet.ReadString();
packet.ReadFlags(out _, out bool n);
if (n)
{
packet.ReadFlags(out _, out _);
packet.ReadInt32();
}
else
packet.ReadPackedVector2();
packet.ReadInt32();
packet.ReadInt32();
packet.ReadByte();
packet.ReadSingle();
return;
}
string name = packet.ReadString();
packet.ReadFlags(out bool progressDialogue, out bool hasEntity);
int entity = -1;
Vector2 pos = Vector2.zeroVector;
bool isNpc = false;
bool isPlayer = false;
if (hasEntity)
{
packet.ReadFlags(out isNpc, out isPlayer);
entity = packet.ReadInt32();
}
else
pos = packet.ReadPackedVector2();
int index = packet.ReadInt32();
int uptime = packet.ReadInt32();
byte effect = packet.ReadByte();
float wrapWidth = packet.ReadSingle();
DisplayEffect de = GetEffect((DisplayEffectID)effect);
if (hasEntity)
StartDialogueOnClient(name, isNpc ? Main.npc[entity] : isPlayer ? Main.player[entity] : Main.projectile.FirstOrDefault(p => p.identity == entity), index, uptime, progressDialogue, de, wrapWidth);
else
StartDialogueOnClient(name, pos, index, uptime, progressDialogue, de, wrapWidth);
}
}
}
+89 -8
View File
@@ -3,8 +3,10 @@ using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text.Json;
using System.Text.Json.Serialization;
using CalamityMod.Packets;
using CalamityMod.UI.DialogueDisplay.DialogueEvents;
using CalamityMod.UI.DialogueDisplay.DisplayEffects;
using CalamityMod.UI.DialogueDisplay.TextEffects;
@@ -22,6 +24,7 @@ using Terraria.ModLoader;
using Terraria.UI;
using Terraria.UI.Chat;
using static System.Runtime.InteropServices.JavaScript.JSType;
using static CalamityMod.Packets.StartDialogueDisplayPacket;
using static ReLogic.Graphics.DynamicSpriteFont;
namespace CalamityMod.UI.DialogueDisplay
@@ -63,8 +66,13 @@ namespace CalamityMod.UI.DialogueDisplay
{
if (dialog.entity.active)
dialog.ui.Position = dialog.entity.Center;
else
else if (ui.DisplayEffects.DespawnWithAttachedNPC)
dialog.ui.ClosingDialogue = true;
else
{
dialog.ui.Position = dialog.entity.Center;
dialog.entity = null;
}
}
if (dialog.upTime != -1)
@@ -911,6 +919,43 @@ namespace CalamityMod.UI.DialogueDisplay
internal static UserInterface UI;
public enum DisplayEffectID
{
Invalid = -1,
None,
AlwaysOnScreen,
BossText,
Built,
WhisperingPearls
}
public static DisplayEffectID GetID(object obj)
{
if (obj is not DisplayEffect)
return DisplayEffectID.Invalid;
return obj switch
{
AlwaysOnScreen => DisplayEffectID.AlwaysOnScreen,
BossText => DisplayEffectID.BossText,
BuiltEffect => DisplayEffectID.Built,
WhisperingPearlEffects => DisplayEffectID.WhisperingPearls,
_ => DisplayEffectID.None
};
}
public static DisplayEffect GetEffect(DisplayEffectID id)
{
return id switch
{
DisplayEffectID.AlwaysOnScreen => new AlwaysOnScreen(),
DisplayEffectID.BossText => new BossText(),
DisplayEffectID.Built => new BuiltEffect(),
DisplayEffectID.WhisperingPearls => new WhisperingPearlEffects(),
_ => new DisplayEffect()
};
}
public override void Load()
{
if (!Main.dedServ)
@@ -1009,8 +1054,26 @@ namespace CalamityMod.UI.DialogueDisplay
/// </summary>
/// <param name="name">The name of the dialogue's localization key</param>
/// <param name="startPosition">The position of the text in the world</param>
public static int StartDialogue(string name, Vector2 startPosition, int startIndex = 0, int Uptime = -1, bool progressDialogue = true, DisplayEffect effects = null, float wrapWidth = -1)
public static int StartDialogue(string name, Vector2 startPosition, int startIndex = 0, int Uptime = -1, bool progressDialogue = true, DisplayEffect effects = null, float wrapWidth = -1, int toClient = -1, int ignoreClient = -1)
{
if (Main.dedServ)
{
StartDialogueDisplayPacket.Send(name, progressDialogue, startPosition, startIndex, Uptime, GetID(effects), wrapWidth, toClient, ignoreClient);
return -1;
}
else if (Main.netMode == NetmodeID.SinglePlayer)
{
return StartDialogueOnClient(name, startPosition, startIndex, Uptime, progressDialogue, effects, wrapWidth);
}
return -1;
}
public static int StartDialogueOnClient(string name, Vector2 startPosition, int startIndex = 0, int Uptime = -1, bool progressDialogue = true, DisplayEffect effects = null, float wrapWidth = -1)
{
if (Main.dedServ)
return -1;
UI ??= new();
State ??= new();
effects ??= new DisplayEffect();
@@ -1048,10 +1111,28 @@ namespace CalamityMod.UI.DialogueDisplay
/// <param name="name">The name of the dialogue's localization key</param>
/// <param name="entity">The entity this dialogue will appear with</param>
/// <param name="Uptime">The entity this dialogue will appear with</param>
public static int StartDialogue(string name, Entity entity, int startIndex = 0, int Uptime = -1, bool progressDialogue = true, DisplayEffect effects = null, float wrapWidth = -1)
public static int StartDialogue(string name, Entity entity, int startIndex = 0, int Uptime = -1, bool progressDialogue = true, DisplayEffect effects = null, float wrapWidth = -1, int toClient = -1, int ignoreClient = -1)
{
UI ??= new();
State ??= new();
if (Main.dedServ)
{
StartDialogueDisplayPacket.Send(name, progressDialogue, entity is NPC ? EntityType.NPC : entity is Player ? EntityType.Player : EntityType.Projectile, entity is Projectile p ? p.identity : entity.whoAmI, startIndex, Uptime, GetID(effects), wrapWidth, toClient, ignoreClient);
return -1;
}
else if (Main.netMode == NetmodeID.SinglePlayer)
{
return StartDialogueOnClient(name, entity, startIndex, Uptime, progressDialogue, effects, wrapWidth);
}
return -1;
}
public static int StartDialogueOnClient(string name, Entity entity, int startIndex = 0, int Uptime = -1, bool progressDialogue = true, DisplayEffect effects = null, float wrapWidth = -1)
{
if (Main.dedServ)
return -1;
UI ??= new ();
State ??= new ();
effects ??= new DisplayEffect();
DialogueTextData textData = Deserialize(name);
@@ -1066,9 +1147,9 @@ namespace CalamityMod.UI.DialogueDisplay
};
int slot;
for (slot = 0; slot <= DialogueDisplayUI.Dialogues.Count; slot++)
if (!DialogueDisplayUI.Dialogues.ContainsKey(slot))
break;
for (slot = 0; slot <= DialogueDisplayUI.Dialogues.Count; slot++)
if (!DialogueDisplayUI.Dialogues.ContainsKey(slot))
break;
DialogueDisplayUI.Dialogues.Add(slot, (name, display, textData, entity, Uptime));