From 766cdff5ffa72b65d7f106658d1603f47739b2ba Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Fri, 27 Oct 2023 11:05:14 +0800 Subject: + init --- GameCode/LineOfSightTrigger.cs | 59 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 GameCode/LineOfSightTrigger.cs (limited to 'GameCode/LineOfSightTrigger.cs') diff --git a/GameCode/LineOfSightTrigger.cs b/GameCode/LineOfSightTrigger.cs new file mode 100644 index 0000000..99fca28 --- /dev/null +++ b/GameCode/LineOfSightTrigger.cs @@ -0,0 +1,59 @@ +using System; +using UnityEngine; +using UnityEngine.Events; + +public class LineOfSightTrigger : MonoBehaviour +{ + public UnityEvent turnOnEvent; + + public UnityEvent turnOffEvent; + + public UnityEvent switchTargetEvent; + + public Action turnOnAction; + + public Action switchTargetAction; + + public Action turnOffAction; + + private Player player; + + private bool isOn; + + private Player currentTarget; + + private void Start() + { + player = GetComponentInParent(); + } + + private void Update() + { + Player closestPlayerInTeam = PlayerManager.instance.GetClosestPlayerInTeam(base.transform.position, PlayerManager.instance.GetOtherTeam(player.teamID)); + if ((bool)closestPlayerInTeam) + { + if (currentTarget != player) + { + switchTargetEvent.Invoke(); + switchTargetAction?.Invoke(player); + currentTarget = closestPlayerInTeam; + } + if (!isOn) + { + isOn = true; + turnOnAction?.Invoke(player); + turnOnEvent.Invoke(); + } + } + else + { + if (isOn) + { + isOn = false; + turnOffAction?.Invoke(); + turnOffEvent.Invoke(); + } + currentTarget = null; + } + } +} -- cgit v1.1-26-g67d0