From 831796ae05d58e766f6677010ba85a3be76fbd76 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 16 May 2024 00:46:42 +0800 Subject: [PATCH] Change InputManager To InputSystem Remove cursor image from MouseHandler, use unity's cursor supports Add vscode to gitignore --- .gitignore | 1 + Assets/Scenes/VoxelWorld.unity | 217 ++++- Assets/Scripts/Character/CameraController.cs | 19 +- .../Interaction/PlayerInteraction.cs | 23 +- Assets/Scripts/Character/PlayerController.cs | 85 +- Assets/Scripts/Character/VoxelPlacer.cs | 75 +- Assets/Scripts/Inputs.meta | 8 + .../Scripts/Inputs/InputSchemes.inputactions | 774 ++++++++++++++++++ .../Inputs/InputSchemes.inputactions.meta | 14 + .../ItemSys/Containers/ContainerManager.cs | 69 +- Assets/Scripts/SaveSys/PauseMenu.cs | 25 +- Assets/Scripts/Utils/MouseHandler.cs | 7 - Packages/manifest.json | 1 + Packages/packages-lock.json | 9 + 14 files changed, 1219 insertions(+), 108 deletions(-) create mode 100644 Assets/Scripts/Inputs.meta create mode 100644 Assets/Scripts/Inputs/InputSchemes.inputactions create mode 100644 Assets/Scripts/Inputs/InputSchemes.inputactions.meta diff --git a/.gitignore b/.gitignore index 58cbc82..e0f9560 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,7 @@ # Visual Studio cache directory .vs/ +.vscode/ # Gradle cache directory .gradle/ diff --git a/Assets/Scenes/VoxelWorld.unity b/Assets/Scenes/VoxelWorld.unity index 296efee..5ca5519 100644 --- a/Assets/Scenes/VoxelWorld.unity +++ b/Assets/Scenes/VoxelWorld.unity @@ -38,7 +38,7 @@ RenderSettings: m_ReflectionIntensity: 1 m_CustomReflection: {fileID: 0} m_Sun: {fileID: 0} - m_IndirectSpecularColor: {r: 0.37311953, g: 0.38074014, b: 0.3587274, a: 1} + m_IndirectSpecularColor: {r: 0.37311924, g: 0.38073963, b: 0.3587269, a: 1} m_UseRadianceAmbientProbe: 0 --- !u!157 &3 LightmapSettings: @@ -1010,7 +1010,7 @@ GameObject: m_Component: - component: {fileID: 200926058} - component: {fileID: 200926057} - - component: {fileID: 200926056} + - component: {fileID: 200926059} m_Layer: 0 m_Name: EventSystem m_TagString: Untagged @@ -1018,26 +1018,6 @@ GameObject: m_NavMeshLayer: 0 m_StaticEditorFlags: 0 m_IsActive: 1 ---- !u!114 &200926056 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 200926055} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 4f231c4fb786f3946a6b90b886c48677, type: 3} - m_Name: - m_EditorClassIdentifier: - m_SendPointerHoverToParent: 1 - m_HorizontalAxis: Horizontal - m_VerticalAxis: Vertical - m_SubmitButton: Submit - m_CancelButton: Cancel - m_InputActionsPerSecond: 10 - m_RepeatDelay: 0.5 - m_ForceModuleActive: 0 --- !u!114 &200926057 MonoBehaviour: m_ObjectHideFlags: 0 @@ -1068,6 +1048,47 @@ Transform: m_Children: [] m_Father: {fileID: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &200926059 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 200926055} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 01614664b831546d2ae94a42149d80ac, type: 3} + m_Name: + m_EditorClassIdentifier: + m_SendPointerHoverToParent: 1 + m_MoveRepeatDelay: 0.5 + m_MoveRepeatRate: 0.1 + m_XRTrackingOrigin: {fileID: 0} + m_ActionsAsset: {fileID: -944628639613478452, guid: a5c205126ac6346e5973f81c7f8f3cf4, + type: 3} + m_PointAction: {fileID: -9218119538062942064, guid: a5c205126ac6346e5973f81c7f8f3cf4, + type: 3} + m_MoveAction: {fileID: 7458188335781670867, guid: a5c205126ac6346e5973f81c7f8f3cf4, + type: 3} + m_SubmitAction: {fileID: -4670405060361892720, guid: a5c205126ac6346e5973f81c7f8f3cf4, + type: 3} + m_CancelAction: {fileID: -2859285527465700143, guid: a5c205126ac6346e5973f81c7f8f3cf4, + type: 3} + m_LeftClickAction: {fileID: 6167826794487314878, guid: a5c205126ac6346e5973f81c7f8f3cf4, + type: 3} + m_MiddleClickAction: {fileID: 774827817733501298, guid: a5c205126ac6346e5973f81c7f8f3cf4, + type: 3} + m_RightClickAction: {fileID: 8095314263638481154, guid: a5c205126ac6346e5973f81c7f8f3cf4, + type: 3} + m_ScrollWheelAction: {fileID: -5782915516651152657, guid: a5c205126ac6346e5973f81c7f8f3cf4, + type: 3} + m_TrackedDevicePositionAction: {fileID: -7634329024969114857, guid: a5c205126ac6346e5973f81c7f8f3cf4, + type: 3} + m_TrackedDeviceOrientationAction: {fileID: 7558417754318978033, guid: a5c205126ac6346e5973f81c7f8f3cf4, + type: 3} + m_DeselectOnBackgroundClick: 1 + m_PointerBehavior: 0 + m_CursorLockBehavior: 0 --- !u!1 &214045265 GameObject: m_ObjectHideFlags: 0 @@ -1252,6 +1273,7 @@ GameObject: - component: {fileID: 243511003} - component: {fileID: 243511002} - component: {fileID: 243511006} + - component: {fileID: 243511007} m_Layer: 5 m_Name: PauseMenu m_TagString: Untagged @@ -1363,6 +1385,37 @@ MonoBehaviour: - {fileID: 1662739551} - {fileID: 994200429} - {fileID: 562251371} +--- !u!114 &243511007 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 243511001} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 62899f850307741f2a39c98a8b639597, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Actions: {fileID: -944628639613478452, guid: a5c205126ac6346e5973f81c7f8f3cf4, + type: 3} + m_NotificationBehavior: 3 + m_UIInputModule: {fileID: 0} + m_DeviceLostEvent: + m_PersistentCalls: + m_Calls: [] + m_DeviceRegainedEvent: + m_PersistentCalls: + m_Calls: [] + m_ControlsChangedEvent: + m_PersistentCalls: + m_Calls: [] + m_ActionEvents: [] + m_NeverAutoSwitchControlSchemes: 0 + m_DefaultControlScheme: + m_DefaultActionMap: UI + m_SplitScreenIndex: -1 + m_Camera: {fileID: 0} --- !u!1 &287464678 GameObject: m_ObjectHideFlags: 0 @@ -3560,6 +3613,7 @@ GameObject: - component: {fileID: 587170009} - component: {fileID: 587170012} - component: {fileID: 587170013} + - component: {fileID: 587170014} m_Layer: 3 m_Name: Main Camera m_TagString: MainCamera @@ -3654,7 +3708,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: target: {fileID: 2134748502} - sensitivity: 3 + sensitivity: 0.4 height: 0.45 --- !u!114 &587170013 MonoBehaviour: @@ -3716,6 +3770,37 @@ MonoBehaviour: m_BeforeTransparentBundles: [] m_BeforeStackBundles: [] m_AfterStackBundles: [] +--- !u!114 &587170014 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 587170008} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 62899f850307741f2a39c98a8b639597, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Actions: {fileID: -944628639613478452, guid: a5c205126ac6346e5973f81c7f8f3cf4, + type: 3} + m_NotificationBehavior: 3 + m_UIInputModule: {fileID: 0} + m_DeviceLostEvent: + m_PersistentCalls: + m_Calls: [] + m_DeviceRegainedEvent: + m_PersistentCalls: + m_Calls: [] + m_ControlsChangedEvent: + m_PersistentCalls: + m_Calls: [] + m_ActionEvents: [] + m_NeverAutoSwitchControlSchemes: 0 + m_DefaultControlScheme: + m_DefaultActionMap: PlayerWorld + m_SplitScreenIndex: -1 + m_Camera: {fileID: 0} --- !u!1 &597739718 GameObject: m_ObjectHideFlags: 0 @@ -4114,7 +4199,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!224 &674595297 RectTransform: m_ObjectHideFlags: 0 @@ -12109,6 +12194,7 @@ GameObject: - component: {fileID: 2107211183} - component: {fileID: 2107211182} - component: {fileID: 2107211186} + - component: {fileID: 2107211187} m_Layer: 5 m_Name: ContainersUI m_TagString: Untagged @@ -12219,11 +12305,44 @@ MonoBehaviour: handSlot: {fileID: 2080997724} selectionIndicatorUI: {fileID: 1658847765} HUDCenter: {fileID: 1802441402} + m_ScrollSensitive: 0.1 + m_ScrollThreshold: 0.1 graphicRaycaster: {fileID: 2107211182} inventoryPanel: {fileID: 577491860} stashPanel: {fileID: 1724770033} tooltip: {fileID: 307063669} tooltipOffset: {x: 1, y: -1, z: 0} +--- !u!114 &2107211187 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2107211181} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 62899f850307741f2a39c98a8b639597, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Actions: {fileID: -944628639613478452, guid: a5c205126ac6346e5973f81c7f8f3cf4, + type: 3} + m_NotificationBehavior: 0 + m_UIInputModule: {fileID: 0} + m_DeviceLostEvent: + m_PersistentCalls: + m_Calls: [] + m_DeviceRegainedEvent: + m_PersistentCalls: + m_Calls: [] + m_ControlsChangedEvent: + m_PersistentCalls: + m_Calls: [] + m_ActionEvents: [] + m_NeverAutoSwitchControlSchemes: 0 + m_DefaultControlScheme: + m_DefaultActionMap: PlayerWorld + m_SplitScreenIndex: -1 + m_Camera: {fileID: 0} --- !u!1 &2114530368 GameObject: m_ObjectHideFlags: 0 @@ -12376,6 +12495,7 @@ GameObject: - component: {fileID: 2134748507} - component: {fileID: 2134748506} - component: {fileID: 2134748510} + - component: {fileID: 2134748511} m_Layer: 6 m_Name: PlayerCharacter m_TagString: Player @@ -12430,7 +12550,7 @@ MonoBehaviour: m_WalkSpeed: 6 m_RunSpeed: 11 m_LimitDiagonalSpeed: 1 - m_ToggleRun: 0 + m_ToggleRun: 1 m_JumpSpeed: 8 m_Gravity: 20 m_FallingThreshold: 10 @@ -12559,6 +12679,53 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 29a88bf0650fd1c40a5c7667a6502896, type: 3} m_Name: m_EditorClassIdentifier: +--- !u!114 &2134748511 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2134748501} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 62899f850307741f2a39c98a8b639597, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Actions: {fileID: -944628639613478452, guid: a5c205126ac6346e5973f81c7f8f3cf4, + type: 3} + m_NotificationBehavior: 3 + m_UIInputModule: {fileID: 0} + m_DeviceLostEvent: + m_PersistentCalls: + m_Calls: [] + m_DeviceRegainedEvent: + m_PersistentCalls: + m_Calls: [] + m_ControlsChangedEvent: + m_PersistentCalls: + m_Calls: [] + m_ActionEvents: + - m_PersistentCalls: + m_Calls: [] + m_ActionId: fbe75d9c-202d-4698-9cc2-85b7407bcfdf + m_ActionName: PlayerWorld/Movement[/Keyboard/w,/Keyboard/s,/Keyboard/a,/Keyboard/d] + - m_PersistentCalls: + m_Calls: [] + m_ActionId: 5a0d1040-0cd1-4797-8b50-7d6264e72345 + m_ActionName: PlayerWorld/Look[/Mouse/delta] + - m_PersistentCalls: + m_Calls: [] + m_ActionId: 89d058e3-7f94-4acf-94e9-effe7e7111da + m_ActionName: PlayerWorld/ScrollBar[/Mouse/scroll/y] + - m_PersistentCalls: + m_Calls: [] + m_ActionId: 40c3b9d9-8cff-482a-a37f-9158a540cd26 + m_ActionName: PlayerWorld/Jump[/Keyboard/space] + m_NeverAutoSwitchControlSchemes: 0 + m_DefaultControlScheme: + m_DefaultActionMap: PlayerWorld + m_SplitScreenIndex: -1 + m_Camera: {fileID: 0} --- !u!1001 &2162225582741634400 PrefabInstance: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Character/CameraController.cs b/Assets/Scripts/Character/CameraController.cs index 2d12496..45ca7fe 100644 --- a/Assets/Scripts/Character/CameraController.cs +++ b/Assets/Scripts/Character/CameraController.cs @@ -1,5 +1,8 @@ using UnityEngine; +using UnityEngine.InputSystem; + +[RequireComponent(typeof(PlayerInput))] public class CameraController : MonoBehaviour { [SerializeField] Transform target; @@ -10,9 +13,24 @@ public class CameraController : MonoBehaviour Vector2 mouseLook; Vector2 input; + private PlayerInput m_PlayerInput; + + void Awake() { cameraTransform = transform; + + m_PlayerInput = GetComponent(); + + var lookAction = m_PlayerInput.actions["Look"]; + lookAction.started += OnLookAxisChanged; + lookAction.performed += OnLookAxisChanged; + lookAction.canceled += OnLookAxisChanged; + } + + void OnLookAxisChanged(InputAction.CallbackContext context) + { + input = context.ReadValue(); } void Start() @@ -25,7 +43,6 @@ void LateUpdate() { if (!GameController.IsPaused) { - input = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")); input *= sensitivity; mouseLook += input; diff --git a/Assets/Scripts/Character/Interaction/PlayerInteraction.cs b/Assets/Scripts/Character/Interaction/PlayerInteraction.cs index b88278e..a93fa9f 100644 --- a/Assets/Scripts/Character/Interaction/PlayerInteraction.cs +++ b/Assets/Scripts/Character/Interaction/PlayerInteraction.cs @@ -1,5 +1,6 @@ using UnityEngine; using System.Collections; +using UnityEngine.InputSystem; public class PlayerInteraction : MonoBehaviour { @@ -7,20 +8,28 @@ public class PlayerInteraction : MonoBehaviour private ItemContainer inventory; private System.Random random = new System.Random(); - private void Start() + + private PlayerInput m_PlayerInput; + + void Awake() { cam = Camera.main; + inventory = GetComponent(); + + m_PlayerInput = GetComponent(); + var interactAction = m_PlayerInput.actions["Interact"]; + interactAction.performed += OnInteractAction; } - private void Update() + + void OnInteractAction(InputAction.CallbackContext context) { - if (Input.GetKeyDown(KeyCode.E)) - { - Interact(); - } + if (GameController.IsPaused) + return; + Interact(); } - public bool Interact() + bool Interact() { Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); RaycastHit hit; diff --git a/Assets/Scripts/Character/PlayerController.cs b/Assets/Scripts/Character/PlayerController.cs index 08481ae..308247f 100644 --- a/Assets/Scripts/Character/PlayerController.cs +++ b/Assets/Scripts/Character/PlayerController.cs @@ -1,11 +1,12 @@ using UnityEngine; +using UnityEngine.InputSystem; // FPSWalkerEnhanced // From Unify Community Wiki // https://wiki.unity3d.com/index.php/FPSWalkerEnhanced#FPSWalkerEnhanced.cs -[RequireComponent(typeof(CharacterController))] +[RequireComponent(typeof(CharacterController)), RequireComponent(typeof(PlayerInput))] public class PlayerController : MonoBehaviour { [Tooltip("How fast the player moves when walking (default move speed).")] @@ -83,9 +84,15 @@ public class PlayerController : MonoBehaviour private int m_JumpTimer; + private PlayerInput m_PlayerInput; + private Vector2 m_MoveAxis; + private bool m_jumpPressed; + + private void Awake() { // Saving component references to improve performance. + m_PlayerInput = GetComponent(); m_Transform = GetComponent(); m_Controller = GetComponent(); @@ -94,21 +101,69 @@ private void Awake() m_RayDistance = m_Controller.height * .5f + m_Controller.radius; m_SlideLimit = m_Controller.slopeLimit - .1f; m_JumpTimer = m_AntiBunnyHopFactor; + + SetupPlayerInput(); } +#region Input Events + void SetupPlayerInput() + { + var moveAction = m_PlayerInput.actions["Move"]; + moveAction.started += OnMovementAxisChanged; + moveAction.performed += OnMovementAxisChanged; + moveAction.canceled += OnMovementAxisChanged; - private void Update() + var jumpAction = m_PlayerInput.actions["Jump"]; + jumpAction.performed += OnJumpPerformed; + jumpAction.canceled += OnJumpCancel; + + + var runAction = m_PlayerInput.actions["Run"]; + runAction.performed += OnRunPerformed; + runAction.canceled += OnRunCancel; + } + + void OnMovementAxisChanged(InputAction.CallbackContext ctx) { - // If the run button is set to toggle, then switch between walk/run speed. (We use Update for this... - // FixedUpdate is a poor place to use GetButtonDown, since it doesn't necessarily run every frame and can miss the event) + m_MoveAxis = ctx.ReadValue(); + } + + void OnJumpPerformed(InputAction.CallbackContext ctx) + { + m_jumpPressed = true; + } - float inputX = Input.GetAxis("Horizontal"); - float inputY = Input.GetAxis("Vertical"); + void OnJumpCancel(InputAction.CallbackContext ctx) + { + m_jumpPressed = false; + } - if (m_ToggleRun && m_Grounded && Input.GetButtonDown("Run")) + void OnRunPerformed(InputAction.CallbackContext ctx) + { + if (m_ToggleRun) { m_Speed = (m_Speed == m_WalkSpeed ? m_RunSpeed : m_WalkSpeed); } + else + { + m_Speed = m_RunSpeed; + } + } + + void OnRunCancel(InputAction.CallbackContext ctx) + { + if (!m_ToggleRun) + { + m_Speed = m_WalkSpeed; + } + } +#endregion + + private void Update() + { + float inputX = m_MoveAxis.x; + float inputY = m_MoveAxis.y; + // If both horizontal and vertical are used simultaneously, limit speed (if allowed), so the total doesn't exceed normal move speed float inputModifyFactor = (inputX != 0.0f && inputY != 0.0f && m_LimitDiagonalSpeed) ? .7071f : 1.0f; @@ -129,10 +184,12 @@ private void Update() // So if the above raycast didn't catch anything, raycast down from the stored ControllerColliderHit point instead else { - Physics.Raycast(m_ContactPoint + Vector3.up, -Vector3.up, out m_Hit); - if (Vector3.Angle(m_Hit.normal, Vector3.up) > m_SlideLimit) + if (Physics.Raycast(m_ContactPoint + Vector3.up, -Vector3.up, out m_Hit)) { - sliding = true; + if (Vector3.Angle(m_Hit.normal, Vector3.up) > m_SlideLimit) + { + sliding = true; + } } } @@ -146,12 +203,6 @@ private void Update() } } - // If running isn't on a toggle, then use the appropriate speed depending on whether the run button is down - if (!m_ToggleRun) - { - m_Speed = Input.GetKey(KeyCode.LeftShift) ? m_RunSpeed : m_WalkSpeed; - } - // If sliding (and it's allowed), or if we're on an object tagged "Slide", get a vector pointing down the slope we're on if ((sliding && m_SlideWhenOverSlopeLimit) || (m_SlideOnTaggedObjects && m_Hit.collider.tag == "Slide")) { @@ -170,7 +221,7 @@ private void Update() } // Jump! But only if the jump button has been released and player has been grounded for a given number of frames - if (!Input.GetButton("Jump")) + if (!m_jumpPressed) { m_JumpTimer++; } diff --git a/Assets/Scripts/Character/VoxelPlacer.cs b/Assets/Scripts/Character/VoxelPlacer.cs index 4d04536..b4d21e0 100644 --- a/Assets/Scripts/Character/VoxelPlacer.cs +++ b/Assets/Scripts/Character/VoxelPlacer.cs @@ -1,5 +1,8 @@ using UnityEngine; +using UnityEngine.InputSystem; + +[RequireComponent(typeof(PlayerInput))] public class VoxelPlacer : MonoBehaviour { public GameObject blockSelection; @@ -10,42 +13,70 @@ public class VoxelPlacer : MonoBehaviour public bool canGrab = true; + private PlayerInput m_PlayerInput; + // private InputAction m_PlaceAction, m_GrabAction; + private bool m_LastVoxelPosHit = false; + private Vector3 m_LastVoxelPos, m_LastVoxelPos2; + + void Awake() + { + m_PlayerInput = GetComponent(); + var placeAction = m_PlayerInput.actions["Place"]; + placeAction.performed += OnPlaceAction; + var grabAction = m_PlayerInput.actions["Grab"]; + grabAction.performed += OnGrabAction; + } + + void OnPlaceAction(InputAction.CallbackContext context) + { + if (GameController.IsPaused) + return; + if (!canPlace) + return; + if (!m_LastVoxelPosHit) + return; + TerrainManager.Main.SetVoxel(m_LastVoxelPos, Voxel.Stone); + } + + void OnGrabAction(InputAction.CallbackContext context) + { + if (GameController.IsPaused) + return; + if (!canGrab) + return; + if (!m_LastVoxelPosHit) + return; + + Voxel voxel; + TerrainManager.Main.GetVoxel(m_LastVoxelPos2, out voxel); + if (voxel != Voxel.Air) + { + TerrainManager.Main.SetVoxel(m_LastVoxelPos2, Voxel.Air); + ItemStack itemStack = new ItemStack(ItemDatabase.Main.GetCopy(voxel.ToString())); + ContainerManager.Main.TryAlterContainer(itemStack); + } + } + void Update() { // Place selector on voxel player is currently looking Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); if (Physics.Raycast(ray, out RaycastHit hit, Consts.InteractionDistance, Consts.VoxelMask)) { + m_LastVoxelPosHit = true; + m_LastVoxelPos = hit.point - ray.direction * 0.01f; + m_LastVoxelPos2 = hit.point + ray.direction * 0.01f; + if (highlightVoxel && blockSelection) { - PlacementData posRot = ItemUtil.Snap(ray.direction, hit.point + ray.direction * 0.01f, Vector3Int.one, ItemPivot.MidCenter, SnapType.Center); + PlacementData posRot = ItemUtil.Snap(ray.direction, m_LastVoxelPos2, Vector3Int.one, ItemPivot.MidCenter, SnapType.Center); blockSelection.transform.position = posRot.position; blockSelection.SetActive(true); } - - // Set voxel if necessary - if (!GameController.IsPaused) - { - if (Input.GetMouseButtonDown(0) && canPlace) - { - TerrainManager.Main.SetVoxel(hit.point - ray.direction * 0.01f, Voxel.Stone); - } - - if (Input.GetMouseButtonDown(1) && canGrab) - { - Voxel voxel; - TerrainManager.Main.GetVoxel(hit.point + ray.direction * 0.01f, out voxel); - if (voxel != Voxel.Air) - { - TerrainManager.Main.SetVoxel(hit.point + ray.direction * 0.01f, Voxel.Air); - ItemStack itemStack = new ItemStack(ItemDatabase.Main.GetCopy(voxel.ToString())); - ContainerManager.Main.TryAlterContainer(itemStack); - } - } - } } else { + m_LastVoxelPosHit = false; blockSelection?.SetActive(false); } } diff --git a/Assets/Scripts/Inputs.meta b/Assets/Scripts/Inputs.meta new file mode 100644 index 0000000..cd74cd8 --- /dev/null +++ b/Assets/Scripts/Inputs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e7228eb82b21c4f958fe4374f6343bfd +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Inputs/InputSchemes.inputactions b/Assets/Scripts/Inputs/InputSchemes.inputactions new file mode 100644 index 0000000..89cbe8e --- /dev/null +++ b/Assets/Scripts/Inputs/InputSchemes.inputactions @@ -0,0 +1,774 @@ +{ + "name": "InputSchemes", + "maps": [ + { + "name": "PlayerWorld", + "id": "ae0324be-6611-443e-918d-c35d339c2b4f", + "actions": [ + { + "name": "Move", + "type": "Value", + "id": "fbe75d9c-202d-4698-9cc2-85b7407bcfdf", + "expectedControlType": "Vector2", + "processors": "", + "interactions": "", + "initialStateCheck": true + }, + { + "name": "Look", + "type": "PassThrough", + "id": "5a0d1040-0cd1-4797-8b50-7d6264e72345", + "expectedControlType": "Vector2", + "processors": "", + "interactions": "", + "initialStateCheck": false + }, + { + "name": "ScrollBar", + "type": "Value", + "id": "89d058e3-7f94-4acf-94e9-effe7e7111da", + "expectedControlType": "Axis", + "processors": "", + "interactions": "", + "initialStateCheck": true + }, + { + "name": "Jump", + "type": "Button", + "id": "40c3b9d9-8cff-482a-a37f-9158a540cd26", + "expectedControlType": "Button", + "processors": "", + "interactions": "", + "initialStateCheck": false + }, + { + "name": "Run", + "type": "Button", + "id": "4812662e-db40-4ef6-bde1-6d5868e92001", + "expectedControlType": "Button", + "processors": "", + "interactions": "", + "initialStateCheck": false + }, + { + "name": "Grab", + "type": "Button", + "id": "fb6097ec-4de1-4137-b7b7-8d7ae8588beb", + "expectedControlType": "Button", + "processors": "", + "interactions": "", + "initialStateCheck": false + }, + { + "name": "Place", + "type": "Button", + "id": "61f80822-ca32-4cdb-9a0e-df25a1d600ca", + "expectedControlType": "Button", + "processors": "", + "interactions": "", + "initialStateCheck": false + }, + { + "name": "Interact", + "type": "Button", + "id": "4b4e3835-0a6b-408d-9cdf-21cf3787a334", + "expectedControlType": "Button", + "processors": "", + "interactions": "", + "initialStateCheck": false + }, + { + "name": "Inv", + "type": "Button", + "id": "2531df7b-9b47-4d43-810f-447f4f38fde7", + "expectedControlType": "Button", + "processors": "", + "interactions": "", + "initialStateCheck": false + } + ], + "bindings": [ + { + "name": "", + "id": "be22aab1-17c8-4417-991a-ef733cf25de4", + "path": "/delta", + "interactions": "", + "processors": "", + "groups": "", + "action": "Look", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "WSAD", + "id": "e1c8372b-c44f-4ea9-a85a-9ba661340341", + "path": "2DVector", + "interactions": "", + "processors": "", + "groups": "", + "action": "Move", + "isComposite": true, + "isPartOfComposite": false + }, + { + "name": "up", + "id": "cf1e737a-a2d2-4257-bed5-dcc0a3cc1519", + "path": "/w", + "interactions": "", + "processors": "", + "groups": "", + "action": "Move", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "down", + "id": "938c0f5a-856e-4f00-b46b-e50373a724e7", + "path": "/s", + "interactions": "", + "processors": "", + "groups": "", + "action": "Move", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "left", + "id": "7372fd84-1940-4297-b291-f4785639b84f", + "path": "/a", + "interactions": "", + "processors": "", + "groups": "", + "action": "Move", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "right", + "id": "64674fef-68cf-4114-8651-ed4fc86156e2", + "path": "/d", + "interactions": "", + "processors": "", + "groups": "", + "action": "Move", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "", + "id": "a5ca676f-36e8-4e18-b46b-3604cca407ae", + "path": "/scroll/y", + "interactions": "", + "processors": "", + "groups": "", + "action": "ScrollBar", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "6c423f1c-1807-4d3b-a4d5-bf4fa3bf7f20", + "path": "/space", + "interactions": "", + "processors": "", + "groups": "", + "action": "Jump", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "44dd9a4a-dc4d-4a97-9fde-fede4e674d74", + "path": "/leftShift", + "interactions": "", + "processors": "", + "groups": "", + "action": "Run", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "ef2c835a-3b7a-4e44-9810-0d5878d9022c", + "path": "/leftButton", + "interactions": "", + "processors": "", + "groups": "", + "action": "Place", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "a9d42d6e-cc86-4fba-aeb9-0fc987d59034", + "path": "/rightButton", + "interactions": "", + "processors": "", + "groups": "", + "action": "Grab", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "b46d6dc0-b82b-44d2-9e58-a475ef0ee87d", + "path": "/e", + "interactions": "", + "processors": "", + "groups": "", + "action": "Interact", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "5162ef71-de4b-43dd-91f3-0e2abdc324ce", + "path": "/i", + "interactions": "", + "processors": "", + "groups": "", + "action": "Inv", + "isComposite": false, + "isPartOfComposite": false + } + ] + }, + { + "name": "UI", + "id": "7d6aa88c-d6be-499d-8123-ca2016fac1e4", + "actions": [ + { + "name": "Navigate", + "type": "PassThrough", + "id": "3c294d8d-5b67-46a7-b150-db2bc8df2873", + "expectedControlType": "Vector2", + "processors": "", + "interactions": "", + "initialStateCheck": false + }, + { + "name": "Submit", + "type": "Button", + "id": "3597bd9f-ce54-4df9-8b10-5ea1c653f8e7", + "expectedControlType": "Button", + "processors": "", + "interactions": "", + "initialStateCheck": false + }, + { + "name": "Cancel", + "type": "Button", + "id": "142f01a2-234b-43c2-bfd9-870cff91aad7", + "expectedControlType": "Button", + "processors": "", + "interactions": "", + "initialStateCheck": false + }, + { + "name": "Point", + "type": "PassThrough", + "id": "3ff94162-1402-43c4-ae10-0a72d09da3b3", + "expectedControlType": "Vector2", + "processors": "", + "interactions": "", + "initialStateCheck": true + }, + { + "name": "Click", + "type": "PassThrough", + "id": "f00a8890-56b1-4e7b-bb2a-5a579b5c87c5", + "expectedControlType": "Button", + "processors": "", + "interactions": "", + "initialStateCheck": true + }, + { + "name": "ScrollWheel", + "type": "PassThrough", + "id": "941d9757-98d0-4d0b-b276-03b0939afba7", + "expectedControlType": "Vector2", + "processors": "", + "interactions": "", + "initialStateCheck": false + }, + { + "name": "MiddleClick", + "type": "PassThrough", + "id": "65042559-79de-4798-b9dc-69d5015b201a", + "expectedControlType": "Button", + "processors": "", + "interactions": "", + "initialStateCheck": false + }, + { + "name": "RightClick", + "type": "PassThrough", + "id": "f9debe2b-1947-479c-b102-83ad3c359051", + "expectedControlType": "Button", + "processors": "", + "interactions": "", + "initialStateCheck": false + }, + { + "name": "TrackedDevicePosition", + "type": "PassThrough", + "id": "bd2891ee-353e-4f57-9633-1c8b5dea77b5", + "expectedControlType": "Vector3", + "processors": "", + "interactions": "", + "initialStateCheck": false + }, + { + "name": "TrackedDeviceOrientation", + "type": "PassThrough", + "id": "d4c7b99c-327a-441d-8579-ce282c92e317", + "expectedControlType": "Quaternion", + "processors": "", + "interactions": "", + "initialStateCheck": false + }, + { + "name": "Pause", + "type": "Button", + "id": "cf2643a4-f918-4023-9b1b-0a81c0412404", + "expectedControlType": "Button", + "processors": "", + "interactions": "", + "initialStateCheck": false + } + ], + "bindings": [ + { + "name": "Gamepad", + "id": "8836312a-7592-466b-8281-e06ef3fd8ae4", + "path": "2DVector", + "interactions": "", + "processors": "", + "groups": "", + "action": "Navigate", + "isComposite": true, + "isPartOfComposite": false + }, + { + "name": "up", + "id": "efab9323-e68f-4288-ba4d-03c2e3c1f18e", + "path": "/leftStick/up", + "interactions": "", + "processors": "", + "groups": ";Gamepad", + "action": "Navigate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "up", + "id": "a7aed0e7-2f9a-4b34-a2fd-6a068d89ffca", + "path": "/rightStick/up", + "interactions": "", + "processors": "", + "groups": ";Gamepad", + "action": "Navigate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "down", + "id": "dc0a8740-748c-457b-8198-4bb898982e01", + "path": "/leftStick/down", + "interactions": "", + "processors": "", + "groups": ";Gamepad", + "action": "Navigate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "down", + "id": "531df7ba-b888-4c4c-aecd-b43dd7beac79", + "path": "/rightStick/down", + "interactions": "", + "processors": "", + "groups": ";Gamepad", + "action": "Navigate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "left", + "id": "e86edf60-615d-41eb-ba46-450f083ea11c", + "path": "/leftStick/left", + "interactions": "", + "processors": "", + "groups": ";Gamepad", + "action": "Navigate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "left", + "id": "db68ec3d-20a7-4500-9238-825b0e47ff4c", + "path": "/rightStick/left", + "interactions": "", + "processors": "", + "groups": ";Gamepad", + "action": "Navigate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "right", + "id": "af7ef879-fead-44ab-8ea5-5217eb649244", + "path": "/leftStick/right", + "interactions": "", + "processors": "", + "groups": ";Gamepad", + "action": "Navigate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "right", + "id": "f9d2b6fb-8f42-44e7-acae-2cab283682ea", + "path": "/rightStick/right", + "interactions": "", + "processors": "", + "groups": ";Gamepad", + "action": "Navigate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "", + "id": "92107164-3c22-4200-b1c7-ae239bd56938", + "path": "/dpad", + "interactions": "", + "processors": "", + "groups": ";Gamepad", + "action": "Navigate", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "Joystick", + "id": "1bf5431a-6c9b-4445-a29c-3d02dba5f1e7", + "path": "2DVector", + "interactions": "", + "processors": "", + "groups": "", + "action": "Navigate", + "isComposite": true, + "isPartOfComposite": false + }, + { + "name": "up", + "id": "4fdab59e-c56a-46d7-ae48-1f3e034fe9db", + "path": "/stick/up", + "interactions": "", + "processors": "", + "groups": "Joystick", + "action": "Navigate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "down", + "id": "ca4992f2-6662-4653-a7ef-6f5a01016bbc", + "path": "/stick/down", + "interactions": "", + "processors": "", + "groups": "Joystick", + "action": "Navigate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "left", + "id": "df42d898-d392-4791-9d07-896b13ccc469", + "path": "/stick/left", + "interactions": "", + "processors": "", + "groups": "Joystick", + "action": "Navigate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "right", + "id": "43817ae1-7b52-419a-ad70-41d624b0095d", + "path": "/stick/right", + "interactions": "", + "processors": "", + "groups": "Joystick", + "action": "Navigate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "Keyboard", + "id": "77949965-e2f2-4007-8a95-29c260dcb1ef", + "path": "2DVector", + "interactions": "", + "processors": "", + "groups": "", + "action": "Navigate", + "isComposite": true, + "isPartOfComposite": false + }, + { + "name": "up", + "id": "408af0dd-f4b3-4999-8b00-7d23dc8703e9", + "path": "/w", + "interactions": "", + "processors": "", + "groups": "Keyboard&Mouse", + "action": "Navigate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "up", + "id": "bed25b47-f91d-4bdb-b61b-26e826cc4e69", + "path": "/upArrow", + "interactions": "", + "processors": "", + "groups": "Keyboard&Mouse", + "action": "Navigate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "down", + "id": "2444d25f-aadd-4c11-8cd3-e19e0a9b1ff3", + "path": "/s", + "interactions": "", + "processors": "", + "groups": "Keyboard&Mouse", + "action": "Navigate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "down", + "id": "c3229b10-ec55-4b08-b885-4ebae95d1cd3", + "path": "/downArrow", + "interactions": "", + "processors": "", + "groups": "Keyboard&Mouse", + "action": "Navigate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "left", + "id": "de634a16-78e7-4455-ae62-3664b65dcdb0", + "path": "/a", + "interactions": "", + "processors": "", + "groups": "Keyboard&Mouse", + "action": "Navigate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "left", + "id": "a43dbe18-29ff-46d6-9fb8-73524cb95747", + "path": "/leftArrow", + "interactions": "", + "processors": "", + "groups": "Keyboard&Mouse", + "action": "Navigate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "right", + "id": "f0cf82ea-3b21-4698-8247-7c22b67d381f", + "path": "/d", + "interactions": "", + "processors": "", + "groups": "Keyboard&Mouse", + "action": "Navigate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "right", + "id": "dd9c1caa-f8c9-42f6-b247-1163f3e91b33", + "path": "/rightArrow", + "interactions": "", + "processors": "", + "groups": "Keyboard&Mouse", + "action": "Navigate", + "isComposite": false, + "isPartOfComposite": true + }, + { + "name": "", + "id": "0e812016-8b63-4379-8d11-93c5c0e51232", + "path": "*/{Submit}", + "interactions": "", + "processors": "", + "groups": "Keyboard&Mouse;Gamepad;Touch;Joystick;XR", + "action": "Submit", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "da03491c-7d40-41d7-9b5c-0a1c5ac3252b", + "path": "*/{Cancel}", + "interactions": "", + "processors": "", + "groups": "Keyboard&Mouse;Gamepad;Touch;Joystick;XR", + "action": "Cancel", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "6d7e3bb4-d281-4e8d-937c-ef891217fd1a", + "path": "/position", + "interactions": "", + "processors": "", + "groups": "Keyboard&Mouse", + "action": "Point", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "559da672-69dd-4ae3-96ac-27dbf610de75", + "path": "/position", + "interactions": "", + "processors": "", + "groups": "Keyboard&Mouse", + "action": "Point", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "f2508b38-d040-46ae-8e1c-722df7095c1c", + "path": "/touch*/position", + "interactions": "", + "processors": "", + "groups": "Touch", + "action": "Point", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "0ca846b5-57a3-4993-bada-5ab194812caf", + "path": "/leftButton", + "interactions": "", + "processors": "", + "groups": ";Keyboard&Mouse", + "action": "Click", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "3faab35e-b975-4248-8112-aae77e0543d0", + "path": "/tip", + "interactions": "", + "processors": "", + "groups": ";Keyboard&Mouse", + "action": "Click", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "8992c6fe-1632-4bc0-94c7-3c685e066cd3", + "path": "/touch*/press", + "interactions": "", + "processors": "", + "groups": "Touch", + "action": "Click", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "20034128-e32a-43ec-9bf9-f944d47a48ec", + "path": "/trigger", + "interactions": "", + "processors": "", + "groups": "XR", + "action": "Click", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "3af6ec2e-7cb3-4c4b-b76a-1ba977edd38f", + "path": "/scroll", + "interactions": "", + "processors": "", + "groups": ";Keyboard&Mouse", + "action": "ScrollWheel", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "09a0d8ba-5ae0-4fef-a07f-24c1f7f379cf", + "path": "/middleButton", + "interactions": "", + "processors": "", + "groups": ";Keyboard&Mouse", + "action": "MiddleClick", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "bcc2c6b6-a2b6-48d6-9b8a-25e9dd51e2c2", + "path": "/rightButton", + "interactions": "", + "processors": "", + "groups": ";Keyboard&Mouse", + "action": "RightClick", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "52d7b00d-2a88-431c-a53f-e4da34152a60", + "path": "/devicePosition", + "interactions": "", + "processors": "", + "groups": "XR", + "action": "TrackedDevicePosition", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "3bdeeebf-1570-45d4-8980-6cd04bb7aecc", + "path": "/deviceRotation", + "interactions": "", + "processors": "", + "groups": "XR", + "action": "TrackedDeviceOrientation", + "isComposite": false, + "isPartOfComposite": false + }, + { + "name": "", + "id": "851a1566-5f29-44b6-a3db-1ed451dbd05b", + "path": "/escape", + "interactions": "", + "processors": "", + "groups": "", + "action": "Pause", + "isComposite": false, + "isPartOfComposite": false + } + ] + } + ], + "controlSchemes": [] +} \ No newline at end of file diff --git a/Assets/Scripts/Inputs/InputSchemes.inputactions.meta b/Assets/Scripts/Inputs/InputSchemes.inputactions.meta new file mode 100644 index 0000000..67afca1 --- /dev/null +++ b/Assets/Scripts/Inputs/InputSchemes.inputactions.meta @@ -0,0 +1,14 @@ +fileFormatVersion: 2 +guid: a5c205126ac6346e5973f81c7f8f3cf4 +ScriptedImporter: + internalIDToNameTable: [] + externalObjects: {} + serializedVersion: 2 + userData: + assetBundleName: + assetBundleVariant: + script: {fileID: 11500000, guid: 8404be70184654265930450def6a9037, type: 3} + generateWrapperCode: 0 + wrapperCodePath: + wrapperClassName: + wrapperCodeNamespace: diff --git a/Assets/Scripts/ItemSys/Containers/ContainerManager.cs b/Assets/Scripts/ItemSys/Containers/ContainerManager.cs index 400a665..2e21ad9 100644 --- a/Assets/Scripts/ItemSys/Containers/ContainerManager.cs +++ b/Assets/Scripts/ItemSys/Containers/ContainerManager.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using UnityEngine.UI; using TMPro; +using UnityEngine.InputSystem; // Slot handling is a mess, and it starts in this class // It works but it should really be handled in a better way. @@ -22,6 +23,27 @@ public class ContainerManager : Singleton public GameObject HUDCenter; + private PlayerInput m_PlayerInput; + [SerializeField] + private float m_ScrollSensitive = 0.5f; + [SerializeField] + private float m_ScrollThreshold = 0.5f; + + void Awake() + { + m_PlayerInput = GetComponent(); + var inventoryAction = m_PlayerInput.actions["Interact"]; + inventoryAction.performed += OnInventoryPerfomed; + + var scrollBarAction = m_PlayerInput.actions["ScrollBar"]; + scrollBarAction.started += OnScrollBarChanged; + scrollBarAction.performed += OnScrollBarChanged; + scrollBarAction.canceled += OnScrollBarChanged; + + var mouseAction = m_PlayerInput.actions["Place"]; + mouseAction.performed += OnMouseClickPerformed; + } + private void Start() { // Define slots @@ -55,35 +77,42 @@ private void Start() RefreshInventory(); // FreshPrince™ } - bool isVisible = false; - private void Update() + void OnInventoryPerfomed(InputAction.CallbackContext context) { - // TO DO: USE NEW INPUT SYSTEM!!! - if (Input.GetKeyDown(KeyCode.I)) - { - if (isVisible) - Hide(); - else - Show(); - } - CheckChangeSelection(); - if (Input.GetKeyDown(KeyCode.Mouse0) && !GameController.IsPaused) - { - UseHotbarItem(selectedHotbarSlotIndex); - } + if (GameController.IsPaused) + return; + + if (isVisible) + Hide(); + else + Show(); } - public void CheckChangeSelection() + void OnScrollBarChanged(InputAction.CallbackContext context) { - float wheelInput = Input.GetAxis("Mouse ScrollWheel"); - if (!(wheelInput.Equals(0f))) + float value = context.ReadValue() * m_ScrollSensitive; + if (value > m_ScrollThreshold) + selectedHotbarSlotIndex = (selectedHotbarSlotIndex + 1) % hotbarSlotIDs.Length; + else if (value < -m_ScrollThreshold) { - selectedHotbarSlotIndex = (selectedHotbarSlotIndex + (int)Mathf.Sign(wheelInput)) % hotbarSlotIDs.Length; + selectedHotbarSlotIndex = selectedHotbarSlotIndex - 1; if (selectedHotbarSlotIndex < 0) selectedHotbarSlotIndex += hotbarSlotIDs.Length; - selectionIndicatorUI.anchoredPosition = new Vector3(15f + selectedHotbarSlotIndex * 17f, .25f, 0f); } + selectionIndicatorUI.anchoredPosition = new Vector3(15f + selectedHotbarSlotIndex * 17f, .25f, 0f); } + void OnMouseClickPerformed(InputAction.CallbackContext context) + { + if (GameController.IsPaused) + return; + + UseHotbarItem(selectedHotbarSlotIndex); + } + + + bool isVisible = false; + + public void UseHotbarItem(int slotIndex) { UseItem(hotbarSlotIDs[slotIndex]); diff --git a/Assets/Scripts/SaveSys/PauseMenu.cs b/Assets/Scripts/SaveSys/PauseMenu.cs index 8e6cf34..4ca9400 100644 --- a/Assets/Scripts/SaveSys/PauseMenu.cs +++ b/Assets/Scripts/SaveSys/PauseMenu.cs @@ -1,6 +1,7 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; +using UnityEngine.InputSystem; public class PauseMenu : MonoBehaviour { @@ -10,23 +11,29 @@ public class PauseMenu : MonoBehaviour public GameObject HUDCenter; public GameObject[] otherPanels; + private PlayerInput m_PlayerInput; + + void Awake() + { + m_PlayerInput = GetComponent(); + var pauseAction = m_PlayerInput.actions["Pause"]; + pauseAction.performed += OnPauseAction; + } + private void Start() { Hide(); } - private void Update() + void OnPauseAction(InputAction.CallbackContext context) { - // TO DO: USE NEW INPUT SYSTEM!!! - if (Input.GetKeyDown(KeyCode.Escape)) - { - if (isVisible) - Hide(); - else - Show(); - } + if (isVisible) + Hide(); + else + Show(); } + public void Show() { pausePanel.SetActive(true); diff --git a/Assets/Scripts/Utils/MouseHandler.cs b/Assets/Scripts/Utils/MouseHandler.cs index 9b04ee8..8352ef2 100644 --- a/Assets/Scripts/Utils/MouseHandler.cs +++ b/Assets/Scripts/Utils/MouseHandler.cs @@ -6,7 +6,6 @@ public class MouseHandler : MonoBehaviour { public Transform cursorImageTransform; - public Image cursorImage; public bool disableOnStart; @@ -24,14 +23,12 @@ private void Start() public void UnlockMouse() { Cursor.lockState = CursorLockMode.None; - cursorImage.enabled = true; lastPauseState = GameController.IsPaused; } public void LockMouse() { Cursor.lockState = CursorLockMode.Locked; - cursorImage.enabled = false; lastPauseState = GameController.IsPaused; } @@ -44,9 +41,5 @@ private void Update() else LockMouse(); } - if (GameController.isPaused) - { - cursorImageTransform.position = Input.mousePosition; - } } } diff --git a/Packages/manifest.json b/Packages/manifest.json index e618dd4..9d8d863 100644 --- a/Packages/manifest.json +++ b/Packages/manifest.json @@ -6,6 +6,7 @@ "com.unity.feature.2d": "2.0.0", "com.unity.ide.visualstudio": "2.0.20", "com.unity.ide.vscode": "1.2.5", + "com.unity.inputsystem": "1.7.0", "com.unity.jobs": "0.70.0-preview.7", "com.unity.postprocessing": "3.2.2", "com.unity.test-framework": "1.1.33", diff --git a/Packages/packages-lock.json b/Packages/packages-lock.json index b39682f..7a853de 100644 --- a/Packages/packages-lock.json +++ b/Packages/packages-lock.json @@ -161,6 +161,15 @@ "dependencies": {}, "url": "https://packages.unity.com" }, + "com.unity.inputsystem": { + "version": "1.7.0", + "depth": 0, + "source": "registry", + "dependencies": { + "com.unity.modules.uielements": "1.0.0" + }, + "url": "https://packages.unity.com" + }, "com.unity.jobs": { "version": "0.70.0-preview.7", "depth": 0,