-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoomManager.cs
60 lines (48 loc) · 1.52 KB
/
RoomManager.cs
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
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public sealed class RoomManager : MonoBehaviour
{
[SerializeField] private List<GameObject> doors;
[SerializeField] private List<GameObject> enemies;
[Header("Unity events")]
[SerializeField] private UnityEvent onRoomCleared = new UnityEvent();
[SerializeField] private UnityEvent onRoomEntered = new UnityEvent();
private UIStateMachineBehavior _uiStatemachineBehavior;
private CreditsMenuState _winningState;
public bool IsCleared { get; private set; }
private int _enemiesDeathAmount;
private void OnTriggerEnter2D(Collider2D col)
{
if(!col.gameObject.HasTag("Player")) return;
onRoomEntered?.Invoke();
}
public void SetDoorsStates(bool targetValue)
{
foreach (var door in doors)
{
door.SetActive(targetValue);
}
}
public void EnemyHasDied()
{
_enemiesDeathAmount++;
EveryEnemiesIsDead();
}
private void EveryEnemiesIsDead()
{
if(_enemiesDeathAmount != enemies.Count) return;
IsCleared = true;
onRoomCleared?.Invoke();
SetDoorsStates(false);
}
private void Start()
{
_uiStatemachineBehavior = FindObjectOfType<UIStateMachineBehavior>();
_winningState = FindFirstObjectByType<CreditsMenuState>(FindObjectsInactive.Include);
}
public void GoToCredits()
{
_uiStatemachineBehavior.ChangeState(_winningState);
}
}