-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShieldThePlayer.cs
More file actions
57 lines (51 loc) · 2.45 KB
/
ShieldThePlayer.cs
File metadata and controls
57 lines (51 loc) · 2.45 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
55
56
57
using System.Collections;
using System.Threading.Tasks;
using UnityEngine;
public class ShieldThePlayer : MonoBehaviour {
[Tooltip("The state of the support spaceship abillity to shield the ally")] bool shield;
[Tooltip("The number of seconds that the shield remains active")] [SerializeField] float duration;
[SerializeField] shieldActivation playerOneShield;
[SerializeField] CircleAnim shieldTimer;
private void Start() {
this.shield = true;
}
private void OnTriggerEnter2D(Collider2D other) {
if (other.tag == "Player1" && shield) {
Debug.Log("Shield triggered by player");
var destroyComponent = other.GetComponent<DestroyOnTrigger2D>();
// var shieldAnimation = other.GetComponent<SpawnShield>();
// shieldAnimation.SetActive(false);
if (destroyComponent) {
destroyComponent.StartCoroutine(ShieldTemporarily(destroyComponent)); // co-routines
// NOTE: If you just call "StartCoroutine", then it will not work,
// since the present object is destroyed!
// ShieldTemporarily(destroyComponent); // async-await
// Destroy(gameObject); // Destroy the shield itself - prevent double-use
shield = false;
}
} else {
Debug.Log("Shield triggered by "+other.name);
}
}
private IEnumerator ShieldTemporarily(DestroyOnTrigger2D destroyComponent) { // co-routines
// private async void ShieldTemporarily(DestroyOnTrigger2D destroyComponent) { // async-await
destroyComponent.enabled = false;
playerOneShield.SetShield(true);
shieldTimer.resetCircle();
for (float i = duration; i > 0; i--) {
Debug.Log("Shield: " + i + " seconds remaining!");
yield return new WaitForSeconds(1); // co-routines
// await Task.Delay(1000); // async-await
}
Debug.Log("Shield gone!");
destroyComponent.enabled = true;
playerOneShield.SetShield(false);
for (float i = 10; i > 0; i--) {
Debug.Log("Shield abbility: " + i + " seconds remaining!");
yield return new WaitForSeconds(1); // co-routines
// await Task.Delay(1000); // async-await
}
shieldTimer.SetCircleColor(true);
this.shield = true;
}
}