123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using JetBrains.Annotations;
- using Pools;
- using Roads;
- using UnityEngine;
- using Valve.VR.InteractionSystem;
- namespace Routes
- {
- public abstract class Turn : MonoBehaviour
- {
- public RoadDirection comingFrom;
- public RoadDirection goingTo;
- [CanBeNull] private Pool arrowPool;
- private bool arrowPoolAvailable;
- private TurnDirection turnDirection;
- private List<GameObject> usedArrows = new List<GameObject>();
- public Action OnTriggerExitBicycle { get; set; }
- 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()
- {
- UpdateArrows();
- }
- protected abstract Transform RoadDirectionToTransform(RoadDirection position);
- private void AddArrows(RoadDirection position, GameObject arrows)
- {
- if (!arrowPoolAvailable) return;
- var t = RoadDirectionToTransform(position);
- SetRotation(arrows, position);
- arrows.transform.position = t.position;
- }
- private void SetRotation(GameObject arrows, RoadDirection position)
- {
- var t = arrows.transform;
- //by default, the arrows show to east
- switch (position)
- {
- case RoadDirection.West:
- {
- if (turnDirection == TurnDirection.Left ||
- turnDirection == TurnDirection.Straight && goingTo == RoadDirection.South)
- {
- t.rotation = Quaternion.Euler(0, 90, 0);
- //arrows.transform.Rotate(Vector3.up, 90);
- }
- else if (turnDirection == TurnDirection.Right ||
- turnDirection == TurnDirection.Straight && goingTo == RoadDirection.North)
- {
- t.rotation = Quaternion.Euler(0, 270, 0);
- //arrows.transform.Rotate(Vector3.up, 90);
- }
- break;
- }
- case RoadDirection.North:
- if (turnDirection == TurnDirection.Left ||
- turnDirection == TurnDirection.Straight && goingTo == RoadDirection.West)
- {
- t.rotation = Quaternion.Euler(0, 180, 0);
- //arrows.transform.Rotate(Vector3.up, 180);
- }
- break;
- case RoadDirection.East:
- if (turnDirection == TurnDirection.Right ||
- turnDirection == TurnDirection.Straight && goingTo == RoadDirection.South)
- {
- t.rotation = Quaternion.Euler(0, 90, 0);
- //arrows.transform.Rotate(Vector3.up, 90);
- }
- else if (turnDirection == TurnDirection.Left ||
- turnDirection == TurnDirection.Straight && goingTo == RoadDirection.North)
- {
- t.rotation = Quaternion.Euler(0, 270, 0);
- //arrows.transform.Rotate(Vector3.up, -90);
- }
- break;
- case RoadDirection.South:
- if (turnDirection == TurnDirection.Right ||
- turnDirection == TurnDirection.Straight && goingTo == RoadDirection.West)
- {
- t.rotation = Quaternion.Euler(0, 180, 0);
- //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);
- }
- }
- public void UpdateArrows()
- {
- if (comingFrom == RoadDirection.None || goingTo == RoadDirection.None)
- {
- if (!arrowPoolAvailable) return;
- usedArrows.ForEach(o =>
- {
- // ReSharper disable once PossibleNullReferenceException
- arrowPool.ReturnToPool(o);
- });
- usedArrows.Clear();
- return;
- }
- turnDirection = TurnDirectionMapper.GetTurnDirection(comingFrom, goingTo);
- var items = new[] {RoadDirection.West, RoadDirection.North, RoadDirection.East, RoadDirection.South};
- var usedArrowIndex = 0;
- foreach (var position in items.Where(i => i != comingFrom && i != goingTo))
- {
-
- GameObject arrows;
- if (usedArrowIndex < usedArrows.Count)
- {
- arrows = usedArrows[usedArrowIndex];
- }
- else
- {
- // ReSharper disable once PossibleNullReferenceException
- arrows = arrowPool.GetItem();
- usedArrows.Add(arrows);
- }
- AddArrows(position, arrows);
- usedArrowIndex++;
- }
- }
- private void OnTriggerExit(Collider other)
- {
- if (other.CompareTag("bike"))
- {
- OnTriggerExitBicycle();
- }
- }
- private void OnCollisionEnter(Collision other)
- {
- Debug.Log("Colission");
- }
- private void OnTriggerEnter(Collider other)
- {
- Debug.Log("Trigger");
- }
- }
- }
|