diff --git a/Assets/Scripts/PlayerMovement.cs b/Assets/Scripts/PlayerMovement.cs index 17c1026..3bd640a 100644 --- a/Assets/Scripts/PlayerMovement.cs +++ b/Assets/Scripts/PlayerMovement.cs @@ -6,14 +6,20 @@ using UnityEngine.InputSystem; [RequireComponent(typeof(PlayerInput))] public class PlayerMovement : MonoBehaviour { public PlayerData data; + public MapData mapData; + + public TextMeshProUGUI freeAngleText; + public TextMeshProUGUI coordsText; + public TextMeshProUGUI directionText; + + public bool tiledDirection = false; new Rigidbody rigidbody; Vector2 moveInput; Vector3 moveDirection; Transform camTransform; - public TextMeshProUGUI freeAngleText; - public TextMeshProUGUI roundedAngleText; + MapData.Direction lastDirection; void Awake() { rigidbody = GetComponent(); @@ -36,17 +42,43 @@ public class PlayerMovement : MonoBehaviour { camForward.Normalize(); //free moveDirection - moveDirection = camForward * moveInput.y + camTransform.right * moveInput.x; + Vector3 direction = camForward * moveInput.y + camTransform.right * moveInput.x; - float angle = Vector3.SignedAngle(Vector3.forward, moveDirection, Vector3.up); + float angle = Vector3.SignedAngle(Vector3.forward, direction, Vector3.up); freeAngleText.text = "FreeAngle: " + Mathf.RoundToInt(angle); + lastDirection = GetDirection(direction); + directionText.text = "Direction: " + lastDirection; + + var coords = mapData.WorldToAxial(transform.position); + coordsText.text = "Coords: " + coords + "\nNeighbor: " + MapData.GetNeighbor(coords, lastDirection); + + if (tiledDirection) { + Vector2 neighborCoords = MapData.GetNeighbor(coords, lastDirection); + Vector3 neighborWorld = mapData.AxialToWorld(neighborCoords) + Vector3.up * transform.position.y; + moveDirection = (neighborWorld - transform.position).normalized * direction.magnitude; + }else + moveDirection = direction; + } + + float GetRoundAngleOffset(Vector3 direction) { + float angle = Vector3.SignedAngle(Vector3.forward, moveDirection, Vector3.up); + float deltaAngle = Mathf.Sign(angle) * 29; deltaAngle -= (deltaAngle + angle) % 60; - roundedAngleText.text = "RoundedAngle: " + Mathf.RoundToInt(angle + deltaAngle); - var rotation = Quaternion.AngleAxis(deltaAngle, Vector3.up); - moveDirection = rotation * moveDirection; + return deltaAngle; + } + + Vector3 RoundedDirection(Vector3 direction) + => Quaternion.AngleAxis(GetRoundAngleOffset(direction), Vector3.up) * direction; + + MapData.Direction GetDirection(Vector3 direction) { + float angle = Vector3.SignedAngle(Vector3.forward, direction, Vector3.up); + float deltaAngle = GetRoundAngleOffset(direction); + + int index = (Mathf.RoundToInt(angle + deltaAngle) / 60 + (Mathf.Sign(angle) < 0 ? 6 : 0)) % 6; + return (MapData.Direction) index; } void FixedUpdate() { @@ -71,4 +103,21 @@ public class PlayerMovement : MonoBehaviour { moveInput = input; } + + void OnDrawGizmos() { + Gizmos.color = Color.blue; + + /*for (float x = -20 ; x < 20; x += 0.5f) + for (float z = -20; z < 20; z += 0.5f) { + var coords = mapData.WorldToAxial(new Vector2(x, z)); + Gizmos.DrawSphere(mapData.AxialToWorld(coords) + Vector3.up, 0.2f); + }*/ + + var nearestAxial = mapData.WorldToAxial(new Vector2(transform.position.x, transform.position.z)); + Gizmos.DrawSphere(mapData.AxialToWorld(nearestAxial) + Vector3.up, 0.2f); + + Gizmos.color = Color.green; + var coords = MapData.GetNeighbor(nearestAxial, lastDirection); + Gizmos.DrawSphere(mapData.AxialToWorld(coords) + Vector3.up, 0.2f); + } } \ No newline at end of file