Guest User

Untitled

a guest
Jun 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. using Corale.Colore.Core;
  2. using Corale.Colore.Razer.Keyboard;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6.  
  7. public class ChromaCooldown : MonoBehaviour
  8. {
  9. public const float COOLDOWN_TIME = 4f;
  10.  
  11. private Dictionary<Key, float> refreshTimers = new Dictionary<Key, float>();
  12.  
  13. private void OnEnable()
  14. {
  15. for (KeyCode keyCode = KeyCode.Alpha0; keyCode <= KeyCode.Alpha9; keyCode++)
  16. {
  17. BeginRefresh(keyCode.ToKey());
  18. }
  19.  
  20. InitializeMoveKeys();
  21. }
  22.  
  23. private void InitializeMoveKeys()
  24. {
  25. Keyboard.Instance[Key.W] = UnityEngine.Color.yellow.ToColore();
  26. Keyboard.Instance[Key.A] = UnityEngine.Color.yellow.ToColore();
  27. Keyboard.Instance[Key.S] = UnityEngine.Color.yellow.ToColore();
  28. Keyboard.Instance[Key.D] = UnityEngine.Color.yellow.ToColore();
  29. }
  30.  
  31. private void Update()
  32. {
  33. for (KeyCode i = KeyCode.Alpha0; i <= KeyCode.Alpha9; i++)
  34. {
  35. if (Input.GetKeyDown(i))
  36. BeginRefresh(i.ToKey());
  37. }
  38.  
  39. foreach (var key in refreshTimers.Keys.ToList())
  40. {
  41. if (refreshTimers[key] > 0)
  42. {
  43. refreshTimers[key] -= Time.deltaTime;
  44. float pctRefreshed = refreshTimers[key] / COOLDOWN_TIME;
  45. UpdateKeyColor(key, pctRefreshed);
  46. }
  47. else
  48. {
  49. UpdateKeyColor(key, 0f);
  50. }
  51. }
  52. }
  53.  
  54. private void UpdateKeyColor(Key key, float pct)
  55. {
  56. UnityEngine.Color color = UnityEngine.Color.Lerp(UnityEngine.Color.green, UnityEngine.Color.red, pct);
  57. Keyboard.Instance[key] = color.ToColore();
  58. }
  59.  
  60. private void BeginRefresh(Key key)
  61. {
  62. refreshTimers[key] = COOLDOWN_TIME;
  63. }
  64. }
  65.  
  66. public static class Extensions
  67. {
  68. public static Corale.Colore.Core.Color ToColore(this UnityEngine.Color source)
  69. {
  70. return new Corale.Colore.Core.Color(source.r, source.g, source.b);
  71. }
  72.  
  73. public static Key ToKey(this KeyCode keyCode)
  74. {
  75. switch(keyCode)
  76. {
  77. case KeyCode.Alpha0: return Key.D0;
  78. case KeyCode.Alpha1: return Key.D1;
  79. case KeyCode.Alpha2: return Key.D2;
  80. case KeyCode.Alpha3: return Key.D3;
  81. case KeyCode.Alpha4: return Key.D4;
  82. case KeyCode.Alpha5: return Key.D5;
  83. case KeyCode.Alpha6: return Key.D6;
  84. case KeyCode.Alpha7: return Key.D7;
  85. case KeyCode.Alpha8: return Key.D8;
  86. case KeyCode.Alpha9: return Key.D9;
  87. }
  88. return Key.Escape;
  89. }
  90. }
Add Comment
Please, Sign In to add comment