Advertisement
Guest User

Untitled

a guest
Jun 11th, 2015
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.90 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class ActionButton : MonoBehaviour
  7. {
  8.     public enum ButtonType { spellBook, actionBars, inventory };
  9.  
  10.     [Tooltip( "What category does this draggable item belong to?" )]
  11.     public ButtonType buttonType; // TODO create sub-classes for these probably?
  12.  
  13.     // References to the different components in the button prefab
  14.     public Image spellIcon;
  15.     public Image cooldownOverlay;
  16.     public Text cooldownText;
  17.     public Text hotkeyText;
  18.  
  19.     public CastableSpell attachedSpell;
  20.     //public Item attachedItem; TODO worry about items later
  21.  
  22.     public SpellDatabase spellDatabase;
  23.  
  24.     [HideInInspector]
  25.     public ActionBarManager actionBarGroup; // Which group this button belongs to (i.e., left, center, right)
  26.  
  27.     [HideInInspector]
  28.     public int slotID; // A unique slot ID (per action par group) that we can use to tell ActionBarManager if we unassigned a spell
  29.  
  30.     private int mediumCooldown = 60;
  31.     private int shortCooldown = 5;
  32.  
  33.     private static Color longCooldownColor = new Color( 204 / 255f, 204 / 255f, 204 / 255f );
  34.     private static Color mediumCooldownColor = Color.white;
  35.     private static Color shortCooldownColor = Color.yellow;
  36.  
  37.     private static int longCooldownSize = 12;
  38.     private static int mediumCooldownSize = 26;
  39.     private static int shortCooldownSize = 26;
  40.  
  41.     private void Awake( )
  42.     {
  43.         spellIcon.enabled = false;
  44.         cooldownOverlay.enabled = false;
  45.         cooldownText.enabled = false;
  46.     }
  47.  
  48.     public void assignSpell( int spellID )
  49.     {
  50.         assignSpell( (CastableSpell)spellDatabase.getByIndex( spellID ) );
  51.     }
  52.  
  53.     public void assignSpell( CastableSpell newSpell )
  54.     {
  55.         Debug.Log( "assign spell at " + Time.time );
  56.  
  57.         // 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
  58.         if ( attachedSpell != null )
  59.         {
  60.             spellIcon.enabled = false;
  61.             cooldownOverlay.enabled = false;
  62.             cooldownText.enabled = false;
  63.  
  64.             // Inform the action group we have a new spell assigned so it can save the null spell ID to its settings
  65.             actionBarGroup.newlyUnassigned( slotID );
  66.  
  67.             // Unregister this button for cooldown events
  68.             SpellCastSystem.s.GCDEvent -= receiveGCDEvent;
  69.             SpellCastSystem.s.spellCooldownEvent -= receiveSpellCooldownEvent;
  70.  
  71.             if ( activeCooldownRoutine != null )
  72.                 StopCoroutine( activeCooldownRoutine );
  73.         }
  74.  
  75.         attachedSpell = newSpell;
  76.  
  77.         if ( newSpell != null )
  78.         {
  79.             spellIcon.sprite = newSpell.icon;
  80.  
  81.             // Enable the icon (cooldown overlay and cooldown text will remain hidden until enabled in a coroutine)
  82.             spellIcon.enabled = true;
  83.  
  84.             // Inform the action group we have a new spell assigned so it can save the spell ID to its settings
  85.             actionBarGroup.newlyAssigned( slotID, newSpell.ID );
  86.  
  87.             // Register this button for cooldown events
  88.             SpellCastSystem.s.GCDEvent += receiveGCDEvent;
  89.             SpellCastSystem.s.spellCooldownEvent += receiveSpellCooldownEvent;
  90.  
  91.             spellCooldownOnUse = SpellCastSystem.s.getCooldownOnUse( newSpell );
  92.             GCDOnUse = SpellCastSystem.s.getGCDOnUse( );
  93.  
  94.             // Start a coroutine for the button, will be instantly cancelled if it's not needed
  95.             activeCooldownRoutine = cooldownRoutine( );
  96.             StartCoroutine( activeCooldownRoutine );
  97.         }
  98.     }
  99.  
  100.     // Needed in case the ActionBarManager class destroys this action button game object
  101.     private void OnDisable( )
  102.     {
  103.         if ( attachedSpell != null )
  104.         {
  105.             SpellCastSystem.s.GCDEvent -= receiveGCDEvent;
  106.             SpellCastSystem.s.spellCooldownEvent -= receiveSpellCooldownEvent;
  107.         }
  108.     }
  109.  
  110.     // Called on UI button press
  111.     public void castSpell( )
  112.     {
  113.         attachedSpell.castThis( );
  114.     }
  115.  
  116.     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  117.  
  118.     private float spellCooldownOnUse;
  119.     public void receiveSpellCooldownEvent( CastableSpell c, float cooldownTime )
  120.     {
  121.         if ( c.ID == attachedSpell.ID )
  122.         {
  123.             // 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
  124.             spellCooldownOnUse = cooldownTime;
  125.  
  126.             if ( activeCooldownRoutine == null )
  127.             {
  128.                 activeCooldownRoutine = cooldownRoutine( );
  129.                 StartCoroutine( activeCooldownRoutine );
  130.             }
  131.         }
  132.     }
  133.  
  134.     private float GCDOnUse;
  135.     public void receiveGCDEvent( float cooldownTime )
  136.     {
  137.         GCDOnUse = cooldownTime;
  138.  
  139.         if ( activeCooldownRoutine == null )
  140.         {
  141.             activeCooldownRoutine = cooldownRoutine( );
  142.             StartCoroutine( activeCooldownRoutine );
  143.         }
  144.     }
  145.  
  146.     private IEnumerator activeCooldownRoutine;
  147.     private IEnumerator cooldownRoutine( )
  148.     {
  149.         float spellTime; // The time REMAINING on attached spell's cooldown
  150.         float GCDTime; // The time REMAINING on the GCD
  151.  
  152.         // 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)
  153.         while ( ( spellTime = SpellCastSystem.s.cooldownTimeRemaining( attachedSpell ) ) + ( GCDTime = SpellCastSystem.s.GCDTimeRemaining( ) ) > 0 )
  154.         {
  155.             if ( spellTime > 0 )
  156.             {
  157.                 cooldownText.text = Mathf.CeilToInt( spellTime ).ToString( );
  158.                 cooldownText.fontSize = ( spellTime <= shortCooldown ) ? shortCooldownSize : ( ( spellTime <= mediumCooldown ) ? mediumCooldownSize : longCooldownSize );
  159.                 cooldownText.color = ( spellTime <= shortCooldown ) ? shortCooldownColor : ( ( spellTime <= mediumCooldown ) ? mediumCooldownColor : longCooldownColor );
  160.                 cooldownText.enabled = true;
  161.             }
  162.             else
  163.                 cooldownText.enabled = false;
  164.  
  165.             cooldownOverlay.fillAmount = ( spellTime > GCDTime ) ? spellTime / spellCooldownOnUse : GCDTime / GCDOnUse;
  166.             cooldownOverlay.enabled = true;
  167.  
  168.             yield return null;
  169.         }
  170.  
  171.         Debug.Log( "end routine at " + Time.time );
  172.  
  173.         cooldownText.enabled = false;
  174.         cooldownOverlay.enabled = false;
  175.  
  176.         activeCooldownRoutine = null;
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement