diff --git a/Assets/Prefabs/Player.prefab b/Assets/Prefabs/Player.prefab index a73bdf5..d5b574a 100644 --- a/Assets/Prefabs/Player.prefab +++ b/Assets/Prefabs/Player.prefab @@ -348,7 +348,7 @@ MonoBehaviour: m_ActionId: 10927b2d-68ce-430a-bce1-c5a1f1d3daf0 m_ActionName: Player/Back[/Keyboard/escape,/Mouse/backButton,/Keyboard/tab] m_NeverAutoSwitchControlSchemes: 0 - m_DefaultControlScheme: Joystick + m_DefaultControlScheme: m_DefaultActionMap: Player m_SplitScreenIndex: -1 m_Camera: {fileID: 0} diff --git a/Assets/Scenes/MainScene.unity b/Assets/Scenes/MainScene.unity index cd77683..a19c480 100644 --- a/Assets/Scenes/MainScene.unity +++ b/Assets/Scenes/MainScene.unity @@ -663,8 +663,8 @@ MonoBehaviour: - {fileID: 4026393600238520495, guid: 4f847d9983c9b47469670e627d94626e, type: 3} enemySpawnSettings: isEnemySpawnEnabled: 1 - spawnBatchSize: 5 - spawnInterval: 3 + spawnBatchSize: 2 + spawnInterval: 10 enemyPrefabs: - {fileID: 2422363816341469345, guid: 878f06b5ada303f47b84bdd84f0d4917, type: 3} isNavMeshBaked: 0 @@ -778,10 +778,18 @@ PrefabInstance: m_Modification: m_TransformParent: {fileID: 0} m_Modifications: + - target: {fileID: -3976935303735240655, guid: 5d4e7d89f2ca88444a90ee28fd289237, type: 3} + propertyPath: m_DefaultControlScheme + value: Keyboard&Mouse + objectReference: {fileID: 0} - target: {fileID: -3976935303735240655, guid: 5d4e7d89f2ca88444a90ee28fd289237, type: 3} propertyPath: m_ActionEvents.Array.size value: 14 objectReference: {fileID: 0} + - target: {fileID: -3976935303735240655, guid: 5d4e7d89f2ca88444a90ee28fd289237, type: 3} + propertyPath: m_NeverAutoSwitchControlSchemes + value: 0 + objectReference: {fileID: 0} - target: {fileID: -3976935303735240655, guid: 5d4e7d89f2ca88444a90ee28fd289237, type: 3} propertyPath: m_ActionEvents.Array.data[13].m_ActionId value: 10927b2d-68ce-430a-bce1-c5a1f1d3daf0 diff --git a/Assets/Scripts/GameManagerScript.cs b/Assets/Scripts/GameManagerScript.cs index fd82843..28f3eb6 100644 --- a/Assets/Scripts/GameManagerScript.cs +++ b/Assets/Scripts/GameManagerScript.cs @@ -13,20 +13,30 @@ public class GameManagerScript : MonoBehaviour public bool isNavMeshBaked = false; - List enemies = new List(); + // Referência. GameObject? gamepadGroup; GameObject? menuPanel; GameObject? geometry; + List enemies = new List(); RoomNetwork roomNetwork = new RoomNetwork(); + // State. MenuState menuState = MenuState.closed; + ControlState controlState = ControlState.gamepad; public static GameManagerScript instance = default!; + // Getters de state. public MenuState GetMenuState => menuState; + public ControlState GetControlState => controlState; + + // Getters de tipo primitivo. + public string GetTargetControlScheme => controlState == ControlState.keyboard + ? ControlSchemes.keyboardMouse : ControlSchemes.gamepad; + + // Getters de referência. public GameObject GetGeometry => geometry ?? default!; public RoomNetwork GetRoomNetwork => roomNetwork; - public MapGenerationSettings GetMapGenerationSettings => mapGenerationSettings; public EnemySpawnSettings GetEnemySpawnSettings => enemySpawnSettings; @@ -47,6 +57,8 @@ public void DisableGamepad() gamepadGroup.SetActive(false); + controlState = ControlState.keyboard; + LocalPrefs.SetGamepadEnabled(false); } @@ -71,6 +83,8 @@ public void EnableGamepad() gamepadGroup.SetActive(true); + controlState = ControlState.gamepad; + LocalPrefs.SetGamepadEnabled(true); } diff --git a/Assets/Scripts/PlayerScript.cs b/Assets/Scripts/PlayerScript.cs index c742136..82b26f5 100644 --- a/Assets/Scripts/PlayerScript.cs +++ b/Assets/Scripts/PlayerScript.cs @@ -9,18 +9,16 @@ public class PlayerScript : MonoBehaviour PlayerInput? playerInput; Vector2 currentMoveInput; Vector2 processedMoveInput; - Vector2 lastMove; Vector3 characterVelocity; Animator? anima; GameObject? model; Transform? cameraTransform; Vector2 currentLookInput; - Vector2 lastLook; - float lastMoveTimestamp; int killCount; bool attackLock; - float lastLookTimestamp; + + public PlayerInput? GetPlayerInput => playerInput; System.Collections.IEnumerator AttackRoutine() { @@ -109,6 +107,16 @@ void HandleCameraTransforms(Transform cameraTransform) ); } + public void HandleCharacterControllerUpdate() + { + var tempCharacterVelocity = controller?.velocity ?? Vector3.zero; + + if (tempCharacterVelocity.magnitude > .1f) + { + characterVelocity = tempCharacterVelocity; + } + } + void HandleConfigInitialization() { GameManagerScript.instance.DisableMenu(); @@ -162,70 +170,37 @@ void HandleMovement(CharacterController controller) public void Look(InputAction.CallbackContext context) { - // TODO: Arrumar o flick do analógico var value = context.ReadValue(); - if (value.magnitude == 0) - { - if (lastLook.magnitude == 0) - { - currentLookInput = Vector2.zero; - } - } - else + if ( + playerInput?.currentControlScheme != + GameManagerScript.instance.GetTargetControlScheme + ) { - currentLookInput = value; - - if (currentLookInput.magnitude > .5f && !attackLock) - { - StartCoroutine(AttackRoutine()); - } + return; } - lastLook = value; - lastLookTimestamp = Time.timeSinceLevelLoad; - } - - public void Move(InputAction.CallbackContext context) - { - var value = context.ReadValue(); + currentLookInput = value; - if (value.magnitude == 0) + if (currentLookInput.magnitude > .5f && !attackLock) { - if (lastMove.magnitude == 0) - { - currentMoveInput = Vector2.zero; - } - } - else - { - currentMoveInput = value; + StartCoroutine(AttackRoutine()); } - - lastMove = value; - lastMoveTimestamp = Time.timeSinceLevelLoad; } - public void MoveCheck() + public void Move(InputAction.CallbackContext context) { - // TODO: Arrumar o flick do analógico - var value = playerInput?.actions["Move"].ReadValue(); + var value = context.ReadValue(); if ( - value?.magnitude == 0 && - lastMove.magnitude == 0 && - Time.timeSinceLevelLoad - lastMoveTimestamp > .01f + playerInput?.currentControlScheme != + GameManagerScript.instance.GetTargetControlScheme ) { - currentMoveInput = Vector2.zero; + return; } - var tempCharacterVelocity = controller?.velocity ?? Vector3.zero; - - if (tempCharacterVelocity.magnitude > .1f) - { - characterVelocity = tempCharacterVelocity; - } + currentMoveInput = value; } public void OnMenuClick() @@ -261,14 +236,15 @@ void Start() void Update() { - switch (GameManagerScript.instance.GetMenuState) + var manager = GameManagerScript.instance; + + switch (manager.GetMenuState) { case MenuState.closed: - MoveCheck(); // TODO: Mover - if (Input.mousePresent) + if (manager.GetControlState == ControlState.keyboard) { var mousePosition = Mouse.current.position; @@ -284,6 +260,8 @@ void Update() break; } + HandleCharacterControllerUpdate(); + if (controller != null) { HandleMovement(controller: controller); diff --git a/Assets/Scripts/States/ControlState.cs b/Assets/Scripts/States/ControlState.cs new file mode 100644 index 0000000..cbfa327 --- /dev/null +++ b/Assets/Scripts/States/ControlState.cs @@ -0,0 +1,5 @@ +public enum ControlState +{ + keyboard, + gamepad, +} diff --git a/Assets/Scripts/States/ControlState.cs.meta b/Assets/Scripts/States/ControlState.cs.meta new file mode 100644 index 0000000..337ee84 --- /dev/null +++ b/Assets/Scripts/States/ControlState.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 616ba7b344397e840ab3aedaf49d1b58 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Utils/ControlSchemes.cs b/Assets/Scripts/Utils/ControlSchemes.cs new file mode 100644 index 0000000..1d5ffe5 --- /dev/null +++ b/Assets/Scripts/Utils/ControlSchemes.cs @@ -0,0 +1,8 @@ +public class ControlSchemes +{ + public static readonly string keyboardMouse = "Keyboard&Mouse"; + public static readonly string gamepad = "Gamepad"; + public static readonly string touch = "Touch"; + public static readonly string joystick = "Joystick"; + public static readonly string xr = "XR"; +} \ No newline at end of file diff --git a/Assets/Scripts/Utils/ControlSchemes.cs.meta b/Assets/Scripts/Utils/ControlSchemes.cs.meta new file mode 100644 index 0000000..2f08da0 --- /dev/null +++ b/Assets/Scripts/Utils/ControlSchemes.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9b9c61a444eb22b44816e4efeaf70c1d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Utils/Layers.cs b/Assets/Scripts/Utils/Layers.cs index a40ea59..858e10f 100644 --- a/Assets/Scripts/Utils/Layers.cs +++ b/Assets/Scripts/Utils/Layers.cs @@ -1,12 +1,12 @@ using UnityEngine; -class Layers +public class Layers { - public static int defaultLayer = 0; - public static int uiLayer = 5; - public static int enemyLayer = 6; - public static int geometryLayer = 7; + public static readonly int defaultLayer = 0; + public static readonly int uiLayer = 5; + public static readonly int enemyLayer = 6; + public static readonly int geometryLayer = 7; - public static int enemyMask = ((int)Mathf.Pow(2, enemyLayer)); - public static int geometryMask = ((int)Mathf.Pow(2, geometryLayer)); + public static readonly int enemyMask = ((int)Mathf.Pow(2, enemyLayer)); + public static readonly int geometryMask = ((int)Mathf.Pow(2, geometryLayer)); } \ No newline at end of file