using CalamityMod.DataStructures; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; using System.Collections.Generic; using System.Linq; using Terraria; using Terraria.Audio; using Terraria.Graphics.Shaders; using Terraria.ID; using CalamityMod.CalPlayer; using Terraria.ModLoader; using System.Runtime.Serialization; namespace CalamityMod.UI.CooldownIndicators { public class CooldownIndicator { public static Dictionary IDtoType; public static void Load() { Type baseCooldownType = typeof(CooldownIndicator); CalamityMod calamity = ModContent.GetInstance(); IDtoType = new Dictionary(); foreach (Type type in calamity.Code.GetTypes()) { if (type.IsSubclassOf(baseCooldownType) && !type.IsAbstract && type != baseCooldownType) { CooldownIndicator instance = (CooldownIndicator)FormatterServices.GetUninitializedObject(type); if (instance.SyncID != "") IDtoType[instance.SyncID] = type; } } } public static void Unload() { IDtoType = null; } /// /// The player that the cooldown is applying to /// public Player AfflictedPlayer; /// /// The ID of the cooldown for multiplayer syncing. Leave this string empty if you don't need multiplayer syncing /// public virtual string SyncID => ""; /// /// Should the cooldown be displayed in the cooldown rack. /// public virtual bool DisplayMe => false; /// /// The name of the cooldown, appears when the player hovers over the indicator /// public virtual string Name => ""; /// /// The texture of the cooldown indicator. For the sake of consistency, please keep the icons 20x20 (in 2x2) /// public virtual string Texture => ""; /// /// The overlay used above the base texture in compact mode /// public virtual string TextureOverlay => Texture + "Overlay"; /// /// The outline that goes around the icon /// public virtual string TextureOutline => Texture + "Outline"; /// /// The length of the cooldown /// public int Duration; /// /// Frames remaining before the cooldown ends /// public int TimeLeft; public float Completion => Duration != 0 ? TimeLeft / (float)Duration : 0; /// /// Set this to true to disable default drawing, thus calling CustomDraw()/CustomDrawCompact() instead. /// public virtual bool UseCustomDraw => false; /// /// Use this method if you want to handle the drawing yourself. Only called if UseCustomDraw is set to true. /// public virtual void CustomDraw(SpriteBatch spriteBatch, Vector2 position, float opacity, float scale) { } /// /// Use this method if you want to handle the drawing yourself. Only called if UseCustomDraw is set to true. /// public virtual void CustomDrawCompact(SpriteBatch spriteBatch, Vector2 position, float opacity, float scale) { } /// /// The texture of the charge bar. If set to a different texture path than the bar base, and if UseCustomDraw is set to false , it will be used as the ring around the cooldown icon /// Please keep this sprite 44x44 (In 2x2) /// public virtual string ChargeBarTexture => "CalamityMod/UI/CooldownIndicators/BarBase"; /// /// The texture under the charge bar. Will be used if ChargeBarTexture is set to an actual texture. Leave this as is to have nothing below the charge bar texture /// Please keep this sprite 44x44 (In 2x2) /// public virtual string ChargeBarBackTexture => "CalamityMod/UI/CooldownIndicators/BarBase"; /// /// Method used to determine the color of the outline around the icon, and the overlay that goes above the icon in compact mode /// public virtual Color OutlineColor => Color.White; /// /// The color of the bar as it starts going around the icon. Only used if no charge bar texture is provided /// public virtual Color CooldownColorStart => Color.Gray; /// /// The color of the bar as it finishes going around the icon. Only used if no charge bar texture is provided /// public virtual Color CooldownColorEnd => Color.White; /// /// Apply the shader that creates the bar. Only called if UseCustomDraw is set to false. By default just creates a flat ring with colors going from the start to the end color. /// public virtual void ApplyBarShaders(float opacity) { if (ChargeBarTexture == "CalamityMod/UI/CooldownIndicators/BarBase") { GameShaders.Misc["CalamityMod:CircularBarShader"].UseOpacity(opacity); GameShaders.Misc["CalamityMod:CircularBarShader"].UseSaturation(1 - Completion); GameShaders.Misc["CalamityMod:CircularBarShader"].UseColor(CooldownColorStart); GameShaders.Misc["CalamityMod:CircularBarShader"].UseSecondaryColor(CooldownColorEnd); GameShaders.Misc["CalamityMod:CircularBarShader"].Apply(); } else { GameShaders.Misc["CalamityMod:CircularBarSpriteShader"].SetShaderTexture(ModContent.GetTexture(ChargeBarBackTexture)); GameShaders.Misc["CalamityMod:CircularBarSpriteShader"].UseOpacity(opacity); GameShaders.Misc["CalamityMod:CircularBarSpriteShader"].UseSaturation(1 - Completion); GameShaders.Misc["CalamityMod:CircularBarSpriteShader"].Apply(); } } /// /// Determines if the cooldown can tick down. Useful for cooldowns that don't tick down when bosses are alive for example /// /// Wether or not the cooldown should tick down public virtual bool CanTickDown(Player player) => true; /// /// The sound played when the cooldown time is over /// public virtual LegacySoundStyle EndSound() => null; /// /// Wether or not the cooldown should get reset when the player dies /// public virtual bool ResetOnDeath => true; public CooldownIndicator(int duration, Player player) { TimeLeft = duration; Duration = duration; AfflictedPlayer = player; //Sync the cooldown if needed if (SyncID != "" && Main.netMode == NetmodeID.MultiplayerClient && player.whoAmI == Main.myPlayer) { player.Calamity().SyncCooldown(false, SyncID); } } } }