Input Refactoring

Polish
Sascha 2023-06-19 15:28:08 +07:00
parent fbfe70d87b
commit 70e59eeaae
20 changed files with 131 additions and 45 deletions

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 35e2ac7db1165da40aad559f01021327
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0.17838746, g: 0.22335976, b: 0.30537635, a: 1}
m_IndirectSpecularColor: {r: 0.17838776, g: 0.22336027, b: 0.30537742, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
@ -450,7 +450,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
moveSpeed: 10
rotationSpeed: 100
zoomAmount: 1
zoomIncreaseAmount: 1
zoomSpeed: 1
cinemachineVirtualCamera: {fileID: 598754948}
--- !u!1001 &323133488
@ -10683,6 +10683,50 @@ PrefabInstance:
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 1c577f9fe0c6219a992900cb942290f8, type: 3}
--- !u!1 &1835055189
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1835055191}
- component: {fileID: 1835055190}
m_Layer: 0
m_Name: InputManager
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1835055190
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1835055189}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 6509c009d33d26763b221dd193b4ed0b, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!4 &1835055191
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1835055189}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 1
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1848247809
GameObject:
m_ObjectHideFlags: 0
@ -12041,3 +12085,4 @@ SceneRoots:
- {fileID: 7995715121813558043}
- {fileID: 2118659100}
- {fileID: 361322554}
- {fileID: 1835055191}

@ -6,11 +6,12 @@ using UnityEngine.Serialization;
namespace Actions {
public class UnitActionSystem : MonoBehaviour {
[FormerlySerializedAs("UnitsLayerMask")] [SerializeField] private LayerMask unitsLayerMask;
[FormerlySerializedAs("UnitsLayerMask")] [SerializeField]
private LayerMask unitsLayerMask;
[SerializeField] private Unit selectedUnit;
private BaseAction selectedAction;
private bool isBusy;
private BaseAction selectedAction;
public static UnitActionSystem Instance { get; private set; }
@ -59,7 +60,7 @@ namespace Actions {
}
private void HandleSelectedAction() {
if (!Input.GetMouseButtonDown(0)) return;
if (!InputManager.Instance.IsMouseButtonDown(0)) return;
GridPosition mouseGridPosition = LevelGrid.Instance.GetGridPosition(MouseWorld.GetPosition());
if (!SelectedAction.IsValidActionGridPosition(mouseGridPosition)) return;
if (!selectedUnit.TrySpendActionPointsToTakeAction(SelectedAction)) return;
@ -78,12 +79,12 @@ namespace Actions {
public event EventHandler OnActionStarted;
private bool TryHandleUnitSelection() {
if (!Input.GetMouseButtonDown(0)) return false;
if (!Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out RaycastHit raycastHit, float.MaxValue, unitsLayerMask)) return false;
if (!InputManager.Instance.IsMouseButtonDown(0)) return false;
if (!Physics.Raycast(Camera.main.ScreenPointToRay(InputManager.Instance.GetMouseScreenPosition()), out RaycastHit raycastHit, float.MaxValue, unitsLayerMask)) return false;
if (!raycastHit.transform.TryGetComponent(out Unit unit) || unit is null) return false;
if (unit == selectedUnit) return false;
if (unit.IsEnemy) return false;
SetSelectedUnit(unit);
return true;
}

@ -5,11 +5,22 @@ using UnityEngine.Serialization;
public class CameraController : MonoBehaviour {
private const float minFollowYOffset = 2f;
private const float maxFollowYOffset = 12f;
[FormerlySerializedAs("MoveSpeed")] [SerializeField] private float moveSpeed = 10f;
[FormerlySerializedAs("RotationSpeed")] [SerializeField] private float rotationSpeed = 100f;
[FormerlySerializedAs("ZoomAmount")] [SerializeField] private float zoomAmount = 1f;
[FormerlySerializedAs("ZoomSpeed")] [SerializeField] private float zoomSpeed = 5f;
[FormerlySerializedAs("CinemachineVirtualCamera")] [SerializeField] private CinemachineVirtualCamera cinemachineVirtualCamera;
[FormerlySerializedAs("MoveSpeed")] [SerializeField]
private float moveSpeed = 10f;
[FormerlySerializedAs("RotationSpeed")] [SerializeField]
private float rotationSpeed = 100f;
[FormerlySerializedAs("zoomAmount")] [FormerlySerializedAs("ZoomAmount")] [SerializeField]
private float zoomIncreaseAmount = 1f;
[FormerlySerializedAs("ZoomSpeed")] [SerializeField]
private float zoomSpeed = 5f;
[FormerlySerializedAs("CinemachineVirtualCamera")] [SerializeField]
private CinemachineVirtualCamera cinemachineVirtualCamera;
private CinemachineTransposer cinemachineTransposer;
private Vector3 targetFollowOffset;
@ -24,31 +35,17 @@ public class CameraController : MonoBehaviour {
}
private void HandleMovement() {
Vector3 inputMoveDir = new();
if (Input.GetKey(KeyCode.W)) inputMoveDir.z = +1f;
if (Input.GetKey(KeyCode.S)) inputMoveDir.z = -1f;
if (Input.GetKey(KeyCode.A)) inputMoveDir.x = -1f;
if (Input.GetKey(KeyCode.D)) inputMoveDir.x = +1f;
transform.position += Time.deltaTime * moveSpeed * (transform.forward * inputMoveDir.z + transform.right * inputMoveDir.x);
Vector2 inputMoveDir = InputManager.Instance.GetCameraMoveVector();
transform.position += Time.deltaTime * moveSpeed * (transform.forward * inputMoveDir.y + transform.right * inputMoveDir.x);
}
private void HandleRotation() {
Vector3 rotationVector = new();
if (Input.GetKey(KeyCode.Q)) rotationVector.y = +1f;
if (Input.GetKey(KeyCode.E)) rotationVector.y = -1f;
Vector3 rotationVector = new() { y = InputManager.Instance.GetCameraRotateAmount() };
transform.eulerAngles += Time.deltaTime * rotationSpeed * rotationVector;
}
private void HandleZoom() {
switch (Input.mouseScrollDelta.y) {
case > 0:
targetFollowOffset.y -= zoomAmount;
break;
case < 0:
targetFollowOffset.y += zoomAmount;
break;
}
targetFollowOffset.y += InputManager.Instance.GetCameraZoomAmount() * zoomIncreaseAmount;
targetFollowOffset.y = Mathf.Clamp(targetFollowOffset.y, minFollowYOffset, maxFollowYOffset);
cinemachineTransposer.m_FollowOffset = Vector3.Lerp(cinemachineTransposer.m_FollowOffset, targetFollowOffset, zoomSpeed * Time.deltaTime);
}

@ -0,0 +1,41 @@
using UnityEngine;
public class InputManager : MonoBehaviour {
public static InputManager Instance { get; private set; }
private void Awake() {
if (Instance is not null) {
Debug.LogError($"There is more than one InputManager! {transform} - {Instance}");
Destroy(gameObject);
return;
}
Instance = this;
}
public Vector2 GetMouseScreenPosition() => Input.mousePosition;
public bool IsMouseButtonDown(int button) => Input.GetMouseButtonDown(button);
public Vector2 GetCameraMoveVector() {
Vector2 inputMoveDir = new();
if (Input.GetKey(KeyCode.W)) inputMoveDir.y = +1f;
if (Input.GetKey(KeyCode.S)) inputMoveDir.y = -1f;
if (Input.GetKey(KeyCode.A)) inputMoveDir.x = -1f;
if (Input.GetKey(KeyCode.D)) inputMoveDir.x = +1f;
return inputMoveDir;
}
public float GetCameraRotateAmount() {
float rotateAmount = 0f;
if (Input.GetKey(KeyCode.Q)) rotateAmount = +1f;
if (Input.GetKey(KeyCode.E)) rotateAmount = -1f;
return rotateAmount;
}
public float GetCameraZoomAmount() =>
Input.mouseScrollDelta.y switch {
> 0 => -1f,
< 0 => +1f,
_ => 0f
};
}

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 6509c009d33d26763b221dd193b4ed0b

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 3fff90bc61614efb8c72f30ea2a7dde0
timeCreated: 1687177480

@ -4,12 +4,13 @@ using UnityEngine.Serialization;
public class MouseWorld : MonoBehaviour {
private static MouseWorld instance;
[FormerlySerializedAs("MousePlaneLayerMask")] [SerializeField] private LayerMask mousePlaneLayerMask;
[FormerlySerializedAs("MousePlaneLayerMask")] [SerializeField]
private LayerMask mousePlaneLayerMask;
private void Awake() => instance = this;
public static Vector3 GetPosition() {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Ray ray = Camera.main.ScreenPointToRay(InputManager.Instance.GetMouseScreenPosition());
Physics.Raycast(ray, out RaycastHit raycastHit, float.MaxValue, instance.mousePlaneLayerMask);
return raycastHit.point;
}

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 60942cbd7232459a86ab359a808e2e92
timeCreated: 1687177514

@ -1,13 +1,11 @@
using System.Collections.Generic;
using Grid;
using UnityEngine;
using UnityEngine.Serialization;
public class Testing : MonoBehaviour {
[FormerlySerializedAs("Unit")] [SerializeField] private Unit unit;
[FormerlySerializedAs("Unit")] [SerializeField]
private Unit unit;
private void Update() {
if (Input.GetKeyDown(KeyCode.T)) {
}
if (Input.GetKeyDown(KeyCode.T)) { }
}
}