-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCharacterManager.cs
More file actions
62 lines (53 loc) · 1.78 KB
/
Copy pathCharacterManager.cs
File metadata and controls
62 lines (53 loc) · 1.78 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
58
59
60
61
62
using System;
using System.Collections;
using UnityEngine;
using Random = UnityEngine.Random;
namespace FortniteSample {
/// <summary>
/// Sample Character Manager that just fire events randomly.
/// </summary>
public class CharacterManager : MonoBehaviour {
public event Action<float> OnHealthChanged;
public event Action<int> OnSlotIndexChanged;
float _health = 100f;
int _slotIndex = 0;
void Start() {
StartCoroutine(ChangeHealthCo());
StartCoroutine(ChangeSlotIndexCo());
}
public float GetHealth() {
return _health;
}
public int GetSlotIndex() {
return _slotIndex;
}
IEnumerator ChangeHealthCo() {
var waitTime = Random.Range(1f, 5f);
yield return new WaitForSeconds(waitTime);
ChangeHealth();
}
void ChangeHealth() {
var hp = _health;
while (Mathf.Abs(hp - _health) < 20) { // Roll a number that's different from the current value
hp = Random.Range(0, 100);
}
_health = hp;
OnHealthChanged?.Invoke(_health);
StartCoroutine(ChangeHealthCo());
}
IEnumerator ChangeSlotIndexCo() {
var waitTime = Random.Range(1f, 5f);
yield return new WaitForSeconds(waitTime);
ChangeSlotIndex();
}
void ChangeSlotIndex() {
var index = _slotIndex;
while (index == _slotIndex) { // Roll a number that's different from the current value
index = Random.Range(0, 3);
}
_slotIndex = index;
OnSlotIndexChanged?.Invoke(_slotIndex);
StartCoroutine(ChangeSlotIndexCo());
}
}
}