using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class MagicDoorController : MonoBehaviour { private EnemyController _guard1; private EnemyController _guard2; private void OnEnable() { GameManager.Instance.EventManager.OnResourceLoaded += GetGuardEvent; } private void OnDisable() { GameManager.Instance.EventManager.OnResourceLoaded -= GetGuardEvent; } public void TriggerClose() { GetComponent().SetTrigger("close"); } /// /// 获取守卫事件 /// private void GetGuardEvent() { _guard1 = null; _guard2 = null; // 从已使用物体列表中按位置获取物体 GameManager.Instance.PoolManager.UseList.ForEach(obj => { if ((Vector2)obj.transform.position == new Vector2(transform.position.x - 1, transform.position.y - 1)) { if (obj.GetComponent() != null) { _guard1 = obj.GetComponent(); _guard1.OnDeath += () => { _guard1 = null; DetectionOpen(); }; } } else if ((Vector2)obj.transform.position == new Vector2(transform.position.x + 1, transform.position.y - 1)) { if (obj.GetComponent() != null) { _guard2 = obj.GetComponent(); _guard2.OnDeath += () => { _guard2 = null; DetectionOpen(); }; } } }); } /// /// 检测门是否能打开 /// private void DetectionOpen() { if (_guard1 == null && _guard2 == null) GetComponent().Open(null); } }