using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System; using Terraria; using Terraria.Localization; using Terraria.Audio; using Terraria.Graphics.Shaders; using Terraria.ModLoader; namespace CalamityMod.Cooldowns { public class CooldownHandler { public static string ID => null; public CooldownInstance instance; #region Gameplay Behavior /// /// This method runs once every frame while the cooldown instance is active. /// public virtual void Tick() { } /// /// This method runs when the cooldown instance ends naturally.
/// It is not called if the cooldown instance is deleted because the player died. ///
public virtual void OnCompleted() { } /// /// Determines whether the cooldown instance can currently tick down.
/// For example, this is useful for cooldowns that don't tick down if there are any bosses alive.
/// You can also use it so that cooldowns which persist through death do not tick down while the player is dead. ///
public virtual bool CanTickDown => true; /// /// Set this to true to make this cooldown remain even when the player dies.
/// All cooldowns with PersistsThroughDeath set to false disappear immediately when the player dies. ///
public virtual bool PersistsThroughDeath => false; /// /// Set this to true to make this cooldown persist through saves and loads.
/// All cooldowns with SavedWithPlayer set to true are serialized into the modded player file. ///
public virtual bool SavedWithPlayer => true; /// /// When the cooldown instance ends, this sound is played. Leave at null for no sound. /// public virtual SoundStyle? EndSound => null; #endregion #region Display & Rendering /// /// The name of the cooldown instance, appears when the player hovers over the indicator /// public virtual LocalizedText DisplayName => LocalizedText.Empty; /// /// Whether or not this cooldown instance should appear in the cooldown rack UI. /// public virtual bool ShouldDisplay => true; /// /// The texture of the cooldown indicator.
/// These must be 20x20 pixels when at 2x2 scale. ///
public virtual string Texture => ""; /// /// This texture is overlaid atop the cooldown when the cooldown rack is rendering in compact mode. /// public virtual string OverlayTexture => $"{Texture}Overlay"; /// /// This outline texture is rendered around the base icon texture. /// public virtual string OutlineTexture => $"{Texture}Outline"; internal static string DefaultChargeBarTexture = "CalamityMod/Cooldowns/BarBase"; /// /// The texture of this cooldown's "charge bar", or the circle rendered by shaders.
/// By default, this is only used when the cooldown rack is rendering in expanded mode .
/// Leave it as the default to have the charge bar be displayed as a shader-rendered circle.
/// These must be 44x44 pixels when at 2x2 scale. ///
public virtual string ChargeBarTexture => DefaultChargeBarTexture; /// /// The background texture of this cooldown's "charge bar", or the circle rendered by shaders.
/// By default, this is only used when the cooldown rack is rendering in expanded mode.
/// Leave it as the default to have nothing render underneath the charge bar itself.
/// These must be 44x44 pixels when at 2x2 scale. ///
public virtual string ChargeBarBackTexture => DefaultChargeBarTexture; /// /// The color used for the icon outline when rendering in expanded mode.
/// In compact mode, this color is used for the overlay that goes above the icon. ///
public virtual Color OutlineColor => Color.White; /// /// The color used for the start of the circular cooldown timer rendered by shaders.
/// This color is only used when drawing in expanded mode and is ignored if a charge bar texture is provided. ///
public virtual Color CooldownStartColor => Color.Gray; /// /// The color used for the end of the circular cooldown timer rendered by shaders.
/// This color is only used when drawing in expanded mode and is ignored if a charge bar texture is provided. ///
public virtual Color CooldownEndColor => Color.White; /// /// This method is called to render the cooldown when the cooldown rack is in expanded mode. /// public virtual void DrawExpanded(SpriteBatch spriteBatch, Vector2 position, float opacity, float scale) { Texture2D sprite = ModContent.Request(Texture).Value; Texture2D outline = ModContent.Request(OutlineTexture).Value; Texture2D barBase = ModContent.Request(ChargeBarTexture).Value; //Draw the ring spriteBatch.End(); spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, Main.UIScaleMatrix); ApplyBarShaders(opacity); spriteBatch.Draw(barBase, position, null, Color.White * opacity, 0, barBase.Size() * 0.5f, scale, SpriteEffects.None, 0f); spriteBatch.End(); spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, Main.UIScaleMatrix); //Draw the outline spriteBatch.Draw(outline, position, null, OutlineColor * opacity, 0, outline.Size() * 0.5f, scale, SpriteEffects.None, 0f); //Draw the icon spriteBatch.Draw(sprite, position, null, Color.White * opacity, 0, sprite.Size() * 0.5f, scale, SpriteEffects.None, 0f); } /// /// This method is called to render the cooldown when the cooldown rack is in compact mode. /// public virtual void DrawCompact(SpriteBatch spriteBatch, Vector2 position, float opacity, float scale) { Texture2D sprite = ModContent.Request(Texture).Value; Texture2D outline = ModContent.Request(OutlineTexture).Value; Texture2D overlay = ModContent.Request(OverlayTexture).Value; Color outlineColor = OutlineColor; //Draw the outline spriteBatch.Draw(outline, position, null, outlineColor * opacity, 0, outline.Size() * 0.5f, scale, SpriteEffects.None, 0f); //Draw the icon spriteBatch.Draw(sprite, position, null, Color.White * opacity, 0, sprite.Size() * 0.5f, scale, SpriteEffects.None, 0f); //Draw the small overlay int lostHeight = (int)Math.Ceiling(overlay.Height * (1 - instance.Completion)); Rectangle crop = new Rectangle(0, lostHeight, overlay.Width, overlay.Height - lostHeight); spriteBatch.Draw(overlay, position + Vector2.UnitY * lostHeight * scale, crop, outlineColor * opacity * 0.9f, 0, sprite.Size() * 0.5f, scale, SpriteEffects.None, 0f); } /// /// Renders the circular cooldown timer with a radial shader.
/// If the charge bar texture is defined, it is used. Otherwise it renders a flat ring which slides from the start color to the end color. ///
public virtual void ApplyBarShaders(float opacity) { if (ChargeBarTexture == DefaultChargeBarTexture) { GameShaders.Misc["CalamityMod:CircularBarShader"].UseOpacity(opacity); GameShaders.Misc["CalamityMod:CircularBarShader"].UseSaturation(1 - instance.Completion); GameShaders.Misc["CalamityMod:CircularBarShader"].UseColor(CooldownStartColor); GameShaders.Misc["CalamityMod:CircularBarShader"].UseSecondaryColor(CooldownEndColor); GameShaders.Misc["CalamityMod:CircularBarShader"].Apply(); } else { GameShaders.Misc["CalamityMod:CircularBarSpriteShader"].SetShaderTexture(ModContent.Request(ChargeBarBackTexture)); GameShaders.Misc["CalamityMod:CircularBarSpriteShader"].UseOpacity(opacity); GameShaders.Misc["CalamityMod:CircularBarSpriteShader"].UseSaturation(1 - instance.Completion); GameShaders.Misc["CalamityMod:CircularBarSpriteShader"].Apply(); } } #endregion } }