Constrained movement to tiles

This commit is contained in:
misabiko 2020-06-14 18:58:42 -04:00
parent 8b6a994b60
commit 1620a14ad5

View File

@ -6,14 +6,20 @@ using UnityEngine.InputSystem;
[RequireComponent(typeof(PlayerInput))] [RequireComponent(typeof(PlayerInput))]
public class PlayerMovement : MonoBehaviour { public class PlayerMovement : MonoBehaviour {
public PlayerData data; public PlayerData data;
public MapData mapData;
public TextMeshProUGUI freeAngleText;
public TextMeshProUGUI coordsText;
public TextMeshProUGUI directionText;
public bool tiledDirection = false;
new Rigidbody rigidbody; new Rigidbody rigidbody;
Vector2 moveInput; Vector2 moveInput;
Vector3 moveDirection; Vector3 moveDirection;
Transform camTransform; Transform camTransform;
public TextMeshProUGUI freeAngleText; MapData.Direction lastDirection;
public TextMeshProUGUI roundedAngleText;
void Awake() { void Awake() {
rigidbody = GetComponent<Rigidbody>(); rigidbody = GetComponent<Rigidbody>();
@ -36,17 +42,43 @@ public class PlayerMovement : MonoBehaviour {
camForward.Normalize(); camForward.Normalize();
//free moveDirection //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); 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; float deltaAngle = Mathf.Sign(angle) * 29;
deltaAngle -= (deltaAngle + angle) % 60; deltaAngle -= (deltaAngle + angle) % 60;
roundedAngleText.text = "RoundedAngle: " + Mathf.RoundToInt(angle + deltaAngle);
var rotation = Quaternion.AngleAxis(deltaAngle, Vector3.up); return deltaAngle;
moveDirection = rotation * moveDirection; }
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() { void FixedUpdate() {
@ -71,4 +103,21 @@ public class PlayerMovement : MonoBehaviour {
moveInput = input; 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);
}
} }