123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- using System;
- using System.Linq;
- using JetBrains.Annotations;
- using Pools;
- using Roads;
- using UnityEngine;
- namespace Routes
- {
- public class Turn : MonoBehaviour
- {
- public RoadDirection comingFrom;
- public RoadDirection goingTo;
-
- [CanBeNull] private Pool arrowPool;
- private bool arrowPoolAvailable;
- private TurnDirection turnDirection;
-
- private void Awake()
- {
- arrowPool = FindObjectsOfType<Pool>().FirstOrDefault(o => o.CompareTag("ArrowPool"));
- arrowPoolAvailable = arrowPool != null;
- if (!arrowPoolAvailable) Debug.LogWarning("Arrow Pool not found");
- }
- private void Start()
- {
- if (comingFrom == RoadDirection.None || goingTo == RoadDirection.None) return;
- turnDirection = TurnDirectionMapper.GetTurnDirection(comingFrom, goingTo);
- var items = new[] {RoadDirection.West, RoadDirection.North, RoadDirection.East, RoadDirection.South};
- foreach (var position in items.Where(i => i != comingFrom && i != goingTo))
- {
- AddArrows(position);
- }
- }
- protected virtual Transform RoadDirectionToTransform(RoadDirection position)
- {
- throw new NotImplementedException();
- }
-
- private void AddArrows(RoadDirection position)
- {
- if (!arrowPoolAvailable) return;
- Transform t = RoadDirectionToTransform(position);
- System.Diagnostics.Debug.Assert(arrowPool != null, nameof(arrowPool) + " != null");
- var arrows = arrowPool.GetItem();
- SetRotation(arrows, position);
- arrows.transform.position = t.position;
- }
- private void SetRotation(GameObject arrows, RoadDirection position)
- {
- //by default, the arrow shows to north
- switch (position)
- {
- case RoadDirection.West:
- {
- if (turnDirection == TurnDirection.Left ||
- turnDirection == TurnDirection.Straight && goingTo == RoadDirection.South)
- {
- arrows.transform.Rotate(Vector3.up, 90);
- }
- else if (turnDirection == TurnDirection.Right ||
- turnDirection == TurnDirection.Straight && goingTo == RoadDirection.North)
- {
- arrows.transform.Rotate(Vector3.up, 90);
- }
- break;
- }
- case RoadDirection.North:
- if (turnDirection == TurnDirection.Left ||
- turnDirection == TurnDirection.Straight && goingTo == RoadDirection.East)
- {
- arrows.transform.Rotate(Vector3.up, 180);
- }
- break;
- case RoadDirection.East:
- if (turnDirection == TurnDirection.Right ||
- turnDirection == TurnDirection.Straight && goingTo == RoadDirection.South)
- {
- arrows.transform.Rotate(Vector3.up, 90);
- }
- else if (turnDirection == TurnDirection.Left ||
- turnDirection == TurnDirection.Straight && goingTo == RoadDirection.North)
- {
- arrows.transform.Rotate(Vector3.up, -90);
- }
- break;
- case RoadDirection.South:
- if (turnDirection == TurnDirection.Right ||
- turnDirection == TurnDirection.Straight && goingTo == RoadDirection.West)
- {
- arrows.transform.Rotate(Vector3.up, 180);
- }
- break;
- case RoadDirection.None:
- throw new ArgumentException("RoadDirection.None not allowed for adding arrows");
- default:
- throw new ArgumentOutOfRangeException(nameof(position), position, null);
- }
- }
- }
- }
|