forked from MinaPecheux/UnityTutorials-RTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSkillManager.cs
More file actions
54 lines (48 loc) · 1.48 KB
/
Copy pathSkillManager.cs
File metadata and controls
54 lines (48 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class SkillManager : MonoBehaviour
{
public SkillData skill;
GameObject _source;
AudioSource _sourceContextualSource;
Button _button;
bool _ready;
public void Initialize(SkillData skill, GameObject source)
{
this.skill = skill;
_source = source;
// try to get the audio source from the source unit
UnitManager um = source.GetComponent<UnitManager>();
if (um != null)
_sourceContextualSource = um.contextualSource;
}
public void Trigger(GameObject target = null)
{
if (!_ready) return;
StartCoroutine(_WrappedTrigger(target));
}
public void SetButton(Button button)
{
_button = button;
_SetReady(true);
}
private IEnumerator _WrappedTrigger(GameObject target)
{
if (_sourceContextualSource != null && skill.onStartSound)
_sourceContextualSource.PlayOneShot(skill.onStartSound);
yield return new WaitForSeconds(skill.castTime);
if (_sourceContextualSource != null && skill.onEndSound)
_sourceContextualSource.PlayOneShot(skill.onEndSound);
skill.Trigger(_source, target);
_SetReady(false);
yield return new WaitForSeconds(skill.cooldown);
_SetReady(true);
}
private void _SetReady(bool ready)
{
_ready = ready;
if (_button != null)
_button.interactable = ready;
}
}