diff --git a/Assets/Scripts/Controls.cs b/Assets/Scripts/Controls.cs index f844571..8317868 100644 --- a/Assets/Scripts/Controls.cs +++ b/Assets/Scripts/Controls.cs @@ -7,6 +7,7 @@ using UnityEngine.InputSystem; using UnityEngine.UI; using Assets.Scripts.Player; using Assets.Scripts.InteractableObjects; +using System; public class Controls : MonoBehaviour { @@ -55,34 +56,9 @@ public class Controls : MonoBehaviour { playerInput.SwitchCurrentActionMap("Menu"); } - if (!player.GetComponent().takeDamage(0)) + if (uihandler.canPlayerMove()) { - if (!uihandler.isPlayerInFight()) - { - if (uihandler.canPlayerRotate()) - { - lookInput = Mouse.current.delta.ReadValue(); - Vector2 targetRotation = lookInput * sensitivityMouse * Time.deltaTime; - currentRotation = Vector2.SmoothDamp( - currentRotation, - targetRotation, - ref rotationVelocity, - smoothTime - ); - - yaw += currentRotation.x; - pitch -= currentRotation.y; - pitch = Mathf.Clamp(pitch, minPitch, maxPitch); - // Apply rotations - transform.rotation = Quaternion.Euler(0f, yaw, 0f); - playerCam.GetComponent().LookAround(pitch); - player.GetComponent().rotate(yaw); - } - if (uihandler.canPlayerMove()) - { - player.GetComponent().move(input); - } - } + ControlEvents.Move(input); } } @@ -105,9 +81,30 @@ public class Controls : MonoBehaviour } } - public void OnLooking(InputValue value) + public void LateUpdate() { - //lookInput = value.Get(); + //Stop execution if player is not created yet or if player is dead + if (player.GetComponent().takeDamage(0)) return; + //Stop execution if player is in fight + if (uihandler.isPlayerInFight()) return; + + if (uihandler.canPlayerRotate()) + { + lookInput = Mouse.current.delta.ReadValue(); + Vector2 targetRotation = lookInput * sensitivityMouse * Time.deltaTime; + currentRotation = Vector2.SmoothDamp( + currentRotation, + targetRotation, + ref rotationVelocity, + smoothTime + ); + + yaw += currentRotation.x; + pitch -= currentRotation.y; + pitch = Mathf.Clamp(pitch, minPitch, maxPitch); + + ControlEvents.Look(pitch, yaw); + } } public void OnMovement(InputValue value) diff --git a/Assets/Scripts/Events.meta b/Assets/Scripts/Events.meta new file mode 100644 index 0000000..5902b06 --- /dev/null +++ b/Assets/Scripts/Events.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 200db5235a9d2ed2ebc3fe21cb9776bb +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Events/ControlEvents.cs b/Assets/Scripts/Events/ControlEvents.cs new file mode 100644 index 0000000..f540be0 --- /dev/null +++ b/Assets/Scripts/Events/ControlEvents.cs @@ -0,0 +1,17 @@ +using UnityEngine; + +public static class ControlEvents +{ + public static event System.Action OnLookingInput; + public static event System.Action OnMovingInput; + + public static void Look(float pitch, float yaw) + { + OnLookingInput?.Invoke(pitch, yaw); + } + + public static void Move(Vector3 force) + { + OnMovingInput?.Invoke(force); + } +} \ No newline at end of file diff --git a/Assets/Scripts/Events/ControlEvents.cs.meta b/Assets/Scripts/Events/ControlEvents.cs.meta new file mode 100644 index 0000000..c16bfa5 --- /dev/null +++ b/Assets/Scripts/Events/ControlEvents.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: f6f57f29eb0f62ddcbeda3faf4517459 \ No newline at end of file diff --git a/Assets/Scripts/Player/PlayerCamera.cs b/Assets/Scripts/Player/PlayerCamera.cs index 9510d33..f97c52b 100644 --- a/Assets/Scripts/Player/PlayerCamera.cs +++ b/Assets/Scripts/Player/PlayerCamera.cs @@ -11,6 +11,16 @@ namespace Assets.Scripts.Player UIHandler uihandler; GameObject interact; + private void OnEnable() + { + ControlEvents.OnLookingInput += LookAround; + } + + private void OnDisable() + { + ControlEvents.OnLookingInput -= LookAround; + } + // Start is called before the first frame update void Start() { @@ -43,7 +53,7 @@ namespace Assets.Scripts.Player return null; } - public void LookAround(float pitch) + public void LookAround(float pitch, float yaw) { transform.localRotation = Quaternion.Euler(pitch, 0f, 0f); } diff --git a/Assets/Scripts/Player/PlayerGameObject.cs b/Assets/Scripts/Player/PlayerGameObject.cs index 6398797..fa81d5b 100644 --- a/Assets/Scripts/Player/PlayerGameObject.cs +++ b/Assets/Scripts/Player/PlayerGameObject.cs @@ -38,6 +38,14 @@ namespace Assets.Scripts.Player #if UNITY_EDITOR SceneHandler.switchGameToMenu(); #endif + ControlEvents.OnLookingInput += rotate; + ControlEvents.OnMovingInput += move; + } + + private void OnDisable() + { + ControlEvents.OnLookingInput -= rotate; + ControlEvents.OnMovingInput -= move; } void Start() @@ -179,7 +187,7 @@ namespace Assets.Scripts.Player GameObject.Find("QuestLog").GetComponent().updateQuests("explore", gameObject, 1); } - public void rotate(float yaw){ + public void rotate(float pitch, float yaw){ transform.rotation = Quaternion.Euler(0f, yaw, 0f); } diff --git a/UserSettings/EditorUserSettings.asset b/UserSettings/EditorUserSettings.asset index ececa2f..5a71027 100644 --- a/UserSettings/EditorUserSettings.asset +++ b/UserSettings/EditorUserSettings.asset @@ -18,10 +18,10 @@ EditorUserSettings: value: 065551555651080c54570d2741715e1541154a79752925322f2c4965b7b0646d flags: 0 RecentlyUsedSceneGuid-4: - value: 5a5757560101590a5d0c0e24427b5d44434e4c7a7b7a23677f2b4565b7b5353a + value: 56060d5e5301505e0f5a5a2344200944154e4128792a2763297b4a30b2e2363a flags: 0 RecentlyUsedSceneGuid-5: - value: 56060d5e5301505e0f5a5a2344200944154e4128792a2763297b4a30b2e2363a + value: 5a5757560101590a5d0c0e24427b5d44434e4c7a7b7a23677f2b4565b7b5353a flags: 0 RecentlyUsedScenePath-0: value: 22424703114646680e0b0227036c6c111b07142f1f2b233e2867083debf42d