Created
June 15, 2018 22:13
-
-
Save unity3dcollege/7c34cf0808d87164e2e75e6b4aae517a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Corale.Colore.Core; | |
using Corale.Colore.Razer.Keyboard; | |
using System.Collections.Generic; | |
using System.Linq; | |
using UnityEngine; | |
public class ChromaCooldown : MonoBehaviour | |
{ | |
public const float COOLDOWN_TIME = 4f; | |
private Dictionary<Key, float> refreshTimers = new Dictionary<Key, float>(); | |
private void OnEnable() | |
{ | |
for (KeyCode keyCode = KeyCode.Alpha0; keyCode <= KeyCode.Alpha9; keyCode++) | |
{ | |
BeginRefresh(keyCode.ToKey()); | |
} | |
InitializeMoveKeys(); | |
} | |
private void InitializeMoveKeys() | |
{ | |
Keyboard.Instance[Key.W] = UnityEngine.Color.yellow.ToColore(); | |
Keyboard.Instance[Key.A] = UnityEngine.Color.yellow.ToColore(); | |
Keyboard.Instance[Key.S] = UnityEngine.Color.yellow.ToColore(); | |
Keyboard.Instance[Key.D] = UnityEngine.Color.yellow.ToColore(); | |
} | |
private void Update() | |
{ | |
for (KeyCode i = KeyCode.Alpha0; i <= KeyCode.Alpha9; i++) | |
{ | |
if (Input.GetKeyDown(i)) | |
BeginRefresh(i.ToKey()); | |
} | |
foreach (var key in refreshTimers.Keys.ToList()) | |
{ | |
if (refreshTimers[key] > 0) | |
{ | |
refreshTimers[key] -= Time.deltaTime; | |
float pctRefreshed = refreshTimers[key] / COOLDOWN_TIME; | |
UpdateKeyColor(key, pctRefreshed); | |
} | |
else | |
{ | |
UpdateKeyColor(key, 0f); | |
} | |
} | |
} | |
private void UpdateKeyColor(Key key, float pct) | |
{ | |
UnityEngine.Color color = UnityEngine.Color.Lerp(UnityEngine.Color.green, UnityEngine.Color.red, pct); | |
Keyboard.Instance[key] = color.ToColore(); | |
} | |
private void BeginRefresh(Key key) | |
{ | |
refreshTimers[key] = COOLDOWN_TIME; | |
} | |
} | |
public static class Extensions | |
{ | |
public static Corale.Colore.Core.Color ToColore(this UnityEngine.Color source) | |
{ | |
return new Corale.Colore.Core.Color(source.r, source.g, source.b); | |
} | |
public static Key ToKey(this KeyCode keyCode) | |
{ | |
switch(keyCode) | |
{ | |
case KeyCode.Alpha0: return Key.D0; | |
case KeyCode.Alpha1: return Key.D1; | |
case KeyCode.Alpha2: return Key.D2; | |
case KeyCode.Alpha3: return Key.D3; | |
case KeyCode.Alpha4: return Key.D4; | |
case KeyCode.Alpha5: return Key.D5; | |
case KeyCode.Alpha6: return Key.D6; | |
case KeyCode.Alpha7: return Key.D7; | |
case KeyCode.Alpha8: return Key.D8; | |
case KeyCode.Alpha9: return Key.D9; | |
} | |
return Key.Escape; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment