using System; using System.IO; using Terraria; using Terraria.ModLoader.IO; using CalamityMod.UI; namespace CalamityMod.Cooldowns { public class CooldownInstance { private const string NetIDSaveKey = "netID"; private const string DurationSaveKey = "duration"; private const string TimeLeftSaveKey = "timeLeft"; public CooldownInstance(Player p, Cooldown cd, int dur) { netID = cd.netID; player = p; duration = dur; timeLeft = dur; handler = null; AssignHandler(cd); } public CooldownInstance(Player p, Cooldown cd, int dur, params object[] args) { netID = cd.netID; player = p; duration = dur; timeLeft = dur; handler = null; AssignHandler(cd, args); } internal CooldownInstance(Player p, string id, TagCompound tag) { netID = (ushort)tag.GetAsInt(NetIDSaveKey); // Correct for netID mismatch if for some reason this occurs. // The string ID of the cooldown overrides whatever the netID may have been saved as. Cooldown cd = CooldownRegistry.Get(id); ushort registeredNetID = cd.netID; if (netID != registeredNetID) { // Log when this occurs, even though it should be harmless. CalamityMod.Instance.Logger.Warn($"Cooldown \"{id}\" loaded from NBT with discrepant netID {netID}. This cooldown was registered with netID {registeredNetID}"); netID = registeredNetID; } player = p; duration = tag.GetAsInt(DurationSaveKey); timeLeft = tag.GetAsInt(TimeLeftSaveKey); AssignHandler(cd); } /// /// Creates a cooldown instance from serialized binary data, used in netcode. /// /// Reader of the binary stream. internal CooldownInstance(BinaryReader reader) { netID = reader.ReadUInt16(); byte playerIDByte = reader.ReadByte(); player = Main.player[playerIDByte]; duration = reader.ReadInt32(); timeLeft = reader.ReadInt32(); string id = CooldownRegistry.registry[netID].ID; AssignHandler(CooldownRegistry.Get(id)); } // These two functions make the blind reflection assumption that every Cooldown passed to them will actually be // Cooldown // (which they all will be, of course, because nobody instantiates Cooldown) internal void AssignHandler(Cooldown cd) { Type handlerT = cd.GetType().GenericTypeArguments[0]; handler = Activator.CreateInstance(handlerT) as CooldownHandler; handler.instance = this; } internal void AssignHandler(Cooldown cd, params object[] args) { Type handlerT = cd.GetType().GenericTypeArguments[0]; handler = Activator.CreateInstance(handlerT, args) as CooldownHandler; handler.instance = this; } /// /// The netID of the cooldown represented by this cooldown instance.
/// This is used to look up gameplay behavior and rendering behavior as the cooldown instance is used in the game engine. ///
internal ushort netID; /// /// The player that the cooldown is applying to /// public Player player; /// /// The total, original duration of the cooldown. This value is in frames, 60 per second. /// public int duration; /// /// The remaining duration of the cooldown. This value is in frames, 60 per second. /// public int timeLeft; /// /// A ratio of how "completed" an instance of a cooldown is. /// public float Completion => CooldownRackUI.DebugFullDisplay ? CooldownRackUI.DebugForceCompletion : (duration != 0 ? timeLeft / (float)duration : 0); /// /// The handler which implements the behavior of this cooldown instance. /// public CooldownHandler handler; /// /// Serializes this cooldown instance into a TagCompound for saving with the world. /// /// An NBT TagCompound which contains the essential variables of this cooldown instance. internal TagCompound Save() { return new TagCompound { { NetIDSaveKey, (int)netID }, { DurationSaveKey, duration }, { TimeLeftSaveKey, timeLeft } }; } /// /// Serializes this cooldown instance into binary data for netcode. /// /// Writer for an unspecified binary stream. internal void Write(BinaryWriter writer) { writer.Write(netID); byte playerIDByte = (byte)player.whoAmI; writer.Write(playerIDByte); writer.Write(duration); writer.Write(timeLeft); } } }