PlayerActor.cs 8.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

public class PlayerActor : MonoBehaviour {
	private const int MAX_STEPS = 128;

	private bool isMoving;

	private enum MovementDirection {
		Left = -1,
		Right = 1
	}

	private struct RouteSnapshot {
		public Vector3 position;
		public Orientation orientation;
	}

	/// <summary>
	/// Custom Modulo that does not return negative values
	/// </summary>
	/// <param name="i">Input value</param>
	/// <param name="m">Divisior</param>
	/// <returns>i % m</returns>
	private static int Mod(int i, int m) {
		int tmp = i % m;
		return tmp < 0 ? tmp + m : tmp;
	}

33 34
	[SerializeField] private GameObject trackingPoint;

35 36 37 38
	[SerializeField] private Tilemap tilemap;

	[SerializeField] private Orientation orientation = Orientation.Up;

39 40
	[SerializeField] private MovementDirection facing = MovementDirection.Right;

41
	[SerializeField] public InputProvider inputProvider;
42

43 44 45 46 47 48 49 50
	private void Start() {
		Cinemachine.CinemachineVirtualCamera cam = Camera.main.GetComponent<Cinemachine.CinemachineVirtualCamera>();

		if (cam != null) {
			cam.Follow = trackingPoint.transform;
		}
	}

51 52 53 54 55
	private void Update() {
		if (isMoving) return;

		transform.position = tilemap.GetCellCenterWorld(tilemap.WorldToCell(transform.position));

56
		float direction = inputProvider.MoveDirection.x;
57 58 59 60 61

		if (direction < 0) {
			StartCoroutine(Move(MovementDirection.Left));
		} else if (direction > 0) {
			StartCoroutine(Move(MovementDirection.Right));
62
		} else if (inputProvider.JumpPressed) {
Markus Schlösser's avatar
Markus Schlösser committed
63
			StartCoroutine(Jump());
64
		}
65 66 67 68 69 70

		if (inputProvider.PeekingPressed) {
			trackingPoint.transform.localPosition = Vector3.up * 9;
		} else {
			trackingPoint.transform.localPosition = Vector3.zero;
		}
71 72
	}

Markus Schlösser's avatar
Markus Schlösser committed
73
	private void UpdateOrientation() {
74 75 76 77 78 79 80 81
		Vector3 euler = new Vector3(0, 0, (int) orientation * -90.0f);
		if (orientation == Orientation.Left || orientation == Orientation.Right) {
			euler.x = facing == MovementDirection.Right ? 0 : 180.0f;
		} else if (orientation == Orientation.Up || orientation == Orientation.Down) {
			euler.y = facing == MovementDirection.Right ? 0 : 180.0f;
		}

		transform.rotation = Quaternion.Euler(euler);
Markus Schlösser's avatar
Markus Schlösser committed
82 83
	}

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
	private Vector3Int GetDirectionVector(Orientation o) {
		switch (o) {
			case Orientation.Up: return Vector3Int.up;
			case Orientation.Left: return Vector3Int.left;
			case Orientation.Right: return Vector3Int.right;
			case Orientation.Down: return Vector3Int.down;
			default:
				throw new ArgumentOutOfRangeException();
		}
	}

	private IEnumerator Move(MovementDirection direction) {
		isMoving = true;

		List<RouteSnapshot> route = new List<RouteSnapshot>();

		Orientation o = orientation;
		Vector3Int celllPos = tilemap.WorldToCell(transform.position);
		bool isCompleteRoute = false;
		bool isValidRoute = true;

		int numSteps = 0;

		while (!isCompleteRoute && isValidRoute) {
			if (numSteps++ > MAX_STEPS) {
				isValidRoute = false;
				break;
			}

			Vector3Int directionVec = GetDirectionVector((Orientation) Mod((int) o + (int) direction, 4));
			Vector3Int downVec = GetDirectionVector((Orientation) Mod((int) o + 2, 4));

			Orientation enterFace = (Orientation) Mod((int) o - (int) direction, 4);

			Vector3Int adjacentCellPos = celllPos + directionVec;
			Vector3Int adjacentDownCellPos = adjacentCellPos + downVec;
			RotatedTile adjacentTile = tilemap.GetTile<RotatedTile>(adjacentCellPos);
			RotatedTile adjacentDownTile = tilemap.GetTile<RotatedTile>(adjacentDownCellPos);

			Debug.DrawLine(tilemap.GetCellCenterWorld(celllPos), tilemap.GetCellCenterWorld(adjacentCellPos), adjacentTile == null ? Color.red : Color.green);
			Debug.DrawLine(tilemap.GetCellCenterWorld(celllPos), tilemap.GetCellCenterWorld(adjacentDownCellPos), adjacentDownTile == null ? Color.red : Color.green);

			if (adjacentTile != null) {
				switch (direction) {
					case MovementDirection.Left:
						if (adjacentTile[enterFace].enter == OnEnterAction.MoveConcaveRight) {
							// TODO: Add animation
							celllPos += directionVec;
							o = (Orientation) Mod((int) o + 1, 4); // turn right
						} else {
							isValidRoute = false;
						}

						break;

					case MovementDirection.Right:
						if (adjacentTile[enterFace].enter == OnEnterAction.MoveConcaveLeft) {
							// TODO: Add animation
							celllPos += directionVec;
							o = (Orientation) Mod((int) o - 1, 4); // turn left
						} else {
							isValidRoute = false;
						}

						break;

					default:
						throw new ArgumentOutOfRangeException(nameof(direction), direction, null);
				}
			} else if (adjacentDownTile != null) {
				switch (direction) {
					case MovementDirection.Left:
						switch (adjacentDownTile[o].left) {
							case OnWalkOverAction.Invalid:
								isValidRoute = false;
								break;

							case OnWalkOverAction.MoveToCenter:
								// TODO: Add animation
								celllPos += directionVec;
								isCompleteRoute = true;
								break;

							case OnWalkOverAction.MoveConvex:
								// TODO: Add animation
								celllPos += directionVec + directionVec + downVec;
								o = (Orientation) Mod((int) o - 1, 4); // turn left
								break;

							default:
								throw new ArgumentOutOfRangeException();
						}

						break;

					case MovementDirection.Right:
						switch (adjacentDownTile[o].right) {
							case OnWalkOverAction.Invalid:
								isValidRoute = false;
								break;

							case OnWalkOverAction.MoveToCenter:
								// TODO: Add animation
								celllPos += directionVec;
								isCompleteRoute = true;
								break;

							case OnWalkOverAction.MoveConvex:
								// TODO: Add animation
								celllPos += directionVec + directionVec + downVec;
								o = (Orientation) Mod((int) o + 1, 4); // turn right
								break;

							default:
								throw new ArgumentOutOfRangeException();
						}

						break;

					default:
						throw new ArgumentOutOfRangeException(nameof(direction), direction, null);
				}
			} else {
				isValidRoute = false;
			}

			if (isValidRoute) {
				route.Add(new RouteSnapshot {
					position = tilemap.GetCellCenterWorld(celllPos),
					orientation = o
				});
			}
		}

		if (isValidRoute) {
219
			facing = direction;
220 221 222
			foreach (RouteSnapshot v in route) {
				transform.position = v.position;
				orientation = v.orientation;
Markus Schlösser's avatar
Markus Schlösser committed
223
				UpdateOrientation();
224 225 226 227 228 229
				yield return new WaitForSeconds(0.25f);
			}
		}

		isMoving = false;
	}
Markus Schlösser's avatar
Markus Schlösser committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

	private IEnumerator Jump() {
		isMoving = true;
		Vector3Int celllPos = tilemap.WorldToCell(transform.position);
		Vector3Int direction = GetDirectionVector(orientation);
		RotatedTile tile = null;
		for (int i = 0; i < MAX_STEPS; ++i) {
			celllPos += direction;
			RotatedTile hit = tilemap.GetTile<RotatedTile>(celllPos);
			if (hit != null) {
				tile = hit;
				break;
			}
		}

		if (tile != null) {
			Debug.DrawLine(tilemap.GetCellCenterWorld(tilemap.WorldToCell(transform.position)), tilemap.GetCellCenterWorld(celllPos), Color.red);
			OnLandAction action = tile[(Orientation) Mod((int) orientation + 2, 4)].land;

			yield return new WaitForSeconds(0.25f);
			switch (action) {
				case OnLandAction.None:
					transform.position = celllPos - direction;
					orientation = (Orientation)Mod((int)orientation + 2, 4);
					UpdateOrientation();
					break;

				case OnLandAction.MoveConcaveLeft:
					transform.position = celllPos;
					orientation = (Orientation)Mod((int)orientation + 2, 4);
					UpdateOrientation();
					yield return new WaitForSeconds(0.25f);
					yield return Move(MovementDirection.Left);
					break;

				case OnLandAction.MoveConcaveRight:
					transform.position = celllPos;
					orientation = (Orientation)Mod((int)orientation + 2, 4);
					UpdateOrientation();
					yield return new WaitForSeconds(0.25f);
					yield return Move(MovementDirection.Right);
					break;

				case OnLandAction.MoveConvexLeft:
					transform.position = celllPos + GetDirectionVector((Orientation)Mod((int)orientation + 1, 4));
					orientation = (Orientation) Mod((int) orientation + 1, 4);
					UpdateOrientation();
					yield return new WaitForSeconds(0.25f);
					yield return Move(MovementDirection.Left);
					break;

				case OnLandAction.MoveConvexRight:
					transform.position = celllPos + GetDirectionVector((Orientation)Mod((int)orientation - 1, 4));
					orientation = (Orientation)Mod((int)orientation - 1, 4);
					UpdateOrientation();
					yield return new WaitForSeconds(0.25f);
					yield return Move(MovementDirection.Right);
					break;

				default:
					throw new ArgumentOutOfRangeException();
			}
		}

		isMoving = false;
	}
296
}