using Animancer; using NodeCanvas.Framework; using NodeCanvas.StateMachines; using UnityEngine; namespace Ether { [RequireComponent(typeof(Rigidbody2D))] public class PlayerController : MonoBehaviour { private Rigidbody2D playerRigid; private FSMOwner fsmOwer; private Blackboard blackboard; private bool isInputEnabled = true; private void Start() { playerRigid = GetComponent(); fsmOwer = GetComponent(); blackboard = GetComponent(); DontDestroyOnLoad(gameObject); } private void OnEnable() { EventCenter.AddListener(EtherInputEvent.Up, OnUp); EventCenter.AddListener(EtherInputEvent.Down, OnDown); EventCenter.AddListener(EtherInputEvent.Left, OnLeft); EventCenter.AddListener(EtherInputEvent.Right, OnRight); } private void OnUp(bool isDown) { Vector2 move = blackboard.GetVariableValue("moveDirection"); move.y = isDown ? move.y + 1 : move.y - 1; OnMove(move); } private void OnDown(bool isDown) { Vector2 move = blackboard.GetVariableValue("moveDirection"); move.y = isDown ? move.y - 1 : move.y + 1; OnMove(move); } private void OnLeft(bool isDown) { Vector2 move = blackboard.GetVariableValue("moveDirection"); move.x = isDown ? move.x - 1 : move.x + 1; OnMove(move); } private void OnRight(bool isDown) { Vector2 move = blackboard.GetVariableValue("moveDirection"); move.x = isDown ? move.x + 1 : move.x - 1; OnMove(move); } private void OnMove(Vector2 move) { if (!isInputEnabled) { return; } if (move == Vector2.zero) { blackboard.SetVariableValue("moveDirection", move); } else { blackboard.SetVariableValue("moveDirection", move); } } public void SetInputOperation(bool isEnable) { Vector2 moveDirection = blackboard.GetVariableValue("moveDirection"); if (moveDirection != Vector2.zero) { blackboard.SetVariableValue("direction", moveDirection); blackboard.SetVariableValue("moveDirection", Vector2.zero); } isInputEnabled = isEnable; } private void OnDisable() { //EventCenter.RemoveListener(ReInputEvent.Move, OnMove); EventCenter.RemoveListener(EtherInputEvent.Up, OnUp); EventCenter.RemoveListener(EtherInputEvent.Down, OnDown); EventCenter.RemoveListener(EtherInputEvent.Left, OnLeft); EventCenter.RemoveListener(EtherInputEvent.Right, OnRight); } } }