Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using UnityEngine;
- using UnityEngine.UI;
- public class ActionButton : MonoBehaviour
- {
- public enum ButtonType { spellBook, actionBars, inventory };
- [Tooltip( "What category does this draggable item belong to?" )]
- public ButtonType buttonType; // TODO create sub-classes for these probably?
- // References to the different components in the button prefab
- public Image spellIcon;
- public Image cooldownOverlay;
- public Text cooldownText;
- public Text hotkeyText;
- public CastableSpell attachedSpell;
- //public Item attachedItem; TODO worry about items later
- public SpellDatabase spellDatabase;
- [HideInInspector]
- public ActionBarManager actionBarGroup; // Which group this button belongs to (i.e., left, center, right)
- [HideInInspector]
- public int slotID; // A unique slot ID (per action par group) that we can use to tell ActionBarManager if we unassigned a spell
- private int mediumCooldown = 60;
- private int shortCooldown = 5;
- private static Color longCooldownColor = new Color( 204 / 255f, 204 / 255f, 204 / 255f );
- private static Color mediumCooldownColor = Color.white;
- private static Color shortCooldownColor = Color.yellow;
- private static int longCooldownSize = 12;
- private static int mediumCooldownSize = 26;
- private static int shortCooldownSize = 26;
- private void Awake( )
- {
- spellIcon.enabled = false;
- cooldownOverlay.enabled = false;
- cooldownText.enabled = false;
- }
- public void assignSpell( int spellID )
- {
- assignSpell( (CastableSpell)spellDatabase.getByIndex( spellID ) );
- }
- public void assignSpell( CastableSpell newSpell )
- {
- Debug.Log( "assign spell at " + Time.time );
- // If we're being assigned a spell and we already have one (whether new one is null or not), first we need to unregister events and stop all coroutines
- if ( attachedSpell != null )
- {
- spellIcon.enabled = false;
- cooldownOverlay.enabled = false;
- cooldownText.enabled = false;
- // Inform the action group we have a new spell assigned so it can save the null spell ID to its settings
- actionBarGroup.newlyUnassigned( slotID );
- // Unregister this button for cooldown events
- SpellCastSystem.s.GCDEvent -= receiveGCDEvent;
- SpellCastSystem.s.spellCooldownEvent -= receiveSpellCooldownEvent;
- if ( activeCooldownRoutine != null )
- StopCoroutine( activeCooldownRoutine );
- }
- attachedSpell = newSpell;
- if ( newSpell != null )
- {
- spellIcon.sprite = newSpell.icon;
- // Enable the icon (cooldown overlay and cooldown text will remain hidden until enabled in a coroutine)
- spellIcon.enabled = true;
- // Inform the action group we have a new spell assigned so it can save the spell ID to its settings
- actionBarGroup.newlyAssigned( slotID, newSpell.ID );
- // Register this button for cooldown events
- SpellCastSystem.s.GCDEvent += receiveGCDEvent;
- SpellCastSystem.s.spellCooldownEvent += receiveSpellCooldownEvent;
- spellCooldownOnUse = SpellCastSystem.s.getCooldownOnUse( newSpell );
- GCDOnUse = SpellCastSystem.s.getGCDOnUse( );
- // Start a coroutine for the button, will be instantly cancelled if it's not needed
- activeCooldownRoutine = cooldownRoutine( );
- StartCoroutine( activeCooldownRoutine );
- }
- }
- // Needed in case the ActionBarManager class destroys this action button game object
- private void OnDisable( )
- {
- if ( attachedSpell != null )
- {
- SpellCastSystem.s.GCDEvent -= receiveGCDEvent;
- SpellCastSystem.s.spellCooldownEvent -= receiveSpellCooldownEvent;
- }
- }
- // Called on UI button press
- public void castSpell( )
- {
- attachedSpell.castThis( );
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- private float spellCooldownOnUse;
- public void receiveSpellCooldownEvent( CastableSpell c, float cooldownTime )
- {
- if ( c.ID == attachedSpell.ID )
- {
- // Note: cooldowns may change (be reduced) after they were triggered, but we want to use the time on trigger, so we save it at the start
- spellCooldownOnUse = cooldownTime;
- if ( activeCooldownRoutine == null )
- {
- activeCooldownRoutine = cooldownRoutine( );
- StartCoroutine( activeCooldownRoutine );
- }
- }
- }
- private float GCDOnUse;
- public void receiveGCDEvent( float cooldownTime )
- {
- GCDOnUse = cooldownTime;
- if ( activeCooldownRoutine == null )
- {
- activeCooldownRoutine = cooldownRoutine( );
- StartCoroutine( activeCooldownRoutine );
- }
- }
- private IEnumerator activeCooldownRoutine;
- private IEnumerator cooldownRoutine( )
- {
- float spellTime; // The time REMAINING on attached spell's cooldown
- float GCDTime; // The time REMAINING on the GCD
- // The current time remaining may be reduced mid-cooldown as well, and that we DO want to update on the fly (e.g., spells that remove all remaining cooldown times by 10s, for example)
- while ( ( spellTime = SpellCastSystem.s.cooldownTimeRemaining( attachedSpell ) ) + ( GCDTime = SpellCastSystem.s.GCDTimeRemaining( ) ) > 0 )
- {
- if ( spellTime > 0 )
- {
- cooldownText.text = Mathf.CeilToInt( spellTime ).ToString( );
- cooldownText.fontSize = ( spellTime <= shortCooldown ) ? shortCooldownSize : ( ( spellTime <= mediumCooldown ) ? mediumCooldownSize : longCooldownSize );
- cooldownText.color = ( spellTime <= shortCooldown ) ? shortCooldownColor : ( ( spellTime <= mediumCooldown ) ? mediumCooldownColor : longCooldownColor );
- cooldownText.enabled = true;
- }
- else
- cooldownText.enabled = false;
- cooldownOverlay.fillAmount = ( spellTime > GCDTime ) ? spellTime / spellCooldownOnUse : GCDTime / GCDOnUse;
- cooldownOverlay.enabled = true;
- yield return null;
- }
- Debug.Log( "end routine at " + Time.time );
- cooldownText.enabled = false;
- cooldownOverlay.enabled = false;
- activeCooldownRoutine = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement