EnviromentTravelManagerBehaviour.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. using UnityEngine.Events;
  6. using WebTools.Datas.Behaviours;
  7. using SimpleFirebaseUnity;
  8. using Newtonsoft.Json;
  9. using Newtonsoft.Json.Linq;
  10. using WebTools.Environments.Datas;
  11. namespace WebTools.Environments.Behaviours
  12. {
  13. /*public class EnviromentTravelManagerBehaviour : MonoBehaviour
  14. {
  15. public delegate void OnMarkerTravel(OnlineMapsMarkerBase marker);
  16. public static event OnMarkerTravel OnMarkerTravelSuccess;
  17. public delegate void OnCanTravelToMarker (OnlineMapsMarkerBase marker);
  18. public static event OnCanTravelToMarker OnCanTravelToSuccess;
  19. [Header("ENVIRONMENTS")]
  20. [SerializeField]
  21. protected OnlineMaps onlineMaps;
  22. [Header("PLAYERLOCATION")]
  23. [SerializeField]
  24. private OnlineMapsMarkerBase playerLocation;
  25. [Header("TRAVEL")]
  26. [SerializeField]
  27. private OnlineMapsMarkerBase fromPlace;
  28. [SerializeField]
  29. private OnlineMapsMarkerBase toPlace;
  30. [SerializeField]
  31. private List<OnlineMapsVector2d> travelPoints = new List<OnlineMapsVector2d>();
  32. [SerializeField]
  33. private string currentCity= "Newyork";
  34. [Header("MARKER")]
  35. [SerializeField]
  36. private List<Texture2D> texturesMarkers;
  37. [SerializeField]
  38. [Range(0,999999)]
  39. private int markerSpeed = 60;
  40. [SerializeField]
  41. private bool lookToMarker = false;
  42. [Header("ROUTE")]
  43. [SerializeField]
  44. private Color32 routeColor;
  45. [SerializeField]
  46. private int routerWidth;
  47. private int pointIndex = -1;
  48. private double progress;
  49. private void Start()
  50. {
  51. GetCityMarker ();
  52. GetMarkers ();
  53. EnableMarketEvents ();
  54. InitMarker ();
  55. }
  56. private void Update()
  57. {
  58. Travel ();
  59. }
  60. private void GetCityMarker()
  61. {
  62. var marker = DataManagerBehaviour.Instance.GetCurrentEnvironments ().FirstOrDefault (e => e.GetId () == currentCity).GetMarket();
  63. onlineMaps.position = marker.position;
  64. }
  65. private void GetMarkers()
  66. {
  67. var markers = DataManagerBehaviour.Instance.GetCurrentEnvironments ().FirstOrDefault (e => e.GetId () == currentCity).GetMarkets ();
  68. foreach (var m in markers)
  69. {
  70. print("marker " + m.id);
  71. var t = texturesMarkers.ElementAtOrDefault((int)m.type);
  72. var mar = onlineMaps.AddMarker (m.position,t,m.id);
  73. mar.type = m.type;
  74. mar.travelplaces = m.travelplaces;
  75. }
  76. }
  77. private void EnableMarketEvents()
  78. {
  79. foreach (OnlineMapsMarker marker in onlineMaps.markers)
  80. {
  81. marker.OnClick += OnMarkerClick;
  82. }
  83. }
  84. private void InitMarker()
  85. {
  86. playerLocation = onlineMaps.markers.FirstOrDefault(m=> m.type == EnvironmentType.PLAYER);
  87. }
  88. private void Travel()
  89. {
  90. onlineMaps.Redraw();
  91. if (pointIndex == -1)
  92. return;
  93. OnlineMapsVector2d p1 = travelPoints[pointIndex];
  94. OnlineMapsVector2d p2 = travelPoints[travelPoints.Count - 1]; //travelPoints[pointIndex + 1];
  95. double dx, dy;
  96. OnlineMapsUtils.DistanceBetweenPoints(p1.x, p1.y, p2.x, p2.y, out dx, out dy);
  97. double stepDistance = Math.Sqrt(dx * dx + dy * dy);
  98. double totalTime = stepDistance / markerSpeed * 3600;
  99. //progress += Time.deltaTime / totalTime;
  100. progress += Time.deltaTime;
  101. OnlineMapsVector2d position;
  102. if (progress < 1)
  103. {
  104. position = OnlineMapsVector2d.Lerp(p1, p2, progress);
  105. playerLocation.SetPosition(position.x, position.y);
  106. }
  107. else
  108. {
  109. position = p2;
  110. playerLocation.SetPosition(position.x, position.y);
  111. //pointIndex++;
  112. progress = 0;
  113. //if (pointIndex >= travelPoints.Count - 1)
  114. //{
  115. pointIndex = -1;
  116. onlineMaps.RemoveAllDrawingElements();
  117. OnTravelComplete (toPlace);
  118. //}
  119. }
  120. if (lookToMarker)
  121. {
  122. onlineMaps.SetPosition(position.x, position.y);
  123. }
  124. onlineMaps.Redraw();
  125. }
  126. public void InitFindPlace()
  127. {
  128. fromPlace = onlineMaps.markers.FirstOrDefault(m=>m.position.x == playerLocation.position.x && m.position.y == playerLocation.position.y
  129. && m.type != EnvironmentType.PLAYER);
  130. OnFindRouteComplete(fromPlace, toPlace);
  131. // OnlineMapsGoogleDirections.Find(fromPlace.position, toPlace.position).OnComplete += OnFindRouteComplete;
  132. }
  133. private List<OnlineMapsVector2d> GetPointstoTravel(OnlineMapsMarkerBase fromplace, OnlineMapsMarkerBase toplace)
  134. {
  135. List<OnlineMapsVector2d> p = new List<OnlineMapsVector2d> ();
  136. var froms = onlineMaps.markers.FirstOrDefault (m => m.id == fromplace.id);
  137. if (froms != null)
  138. {
  139. var tos = froms.travelplaces.FirstOrDefault (t=>t.GetId()== toplace.id);
  140. if (tos != null)
  141. {
  142. p = tos.GetPoints ();
  143. }
  144. }
  145. return p;
  146. }
  147. protected virtual void OnFindRouteComplete(/*string response OnlineMapsMarkerBase fromplace, OnlineMapsMarkerBase toplace)
  148. {
  149. travelPoints = GetPointstoTravel(fromplace, toplace);
  150. OnlineMapsDrawingLine route = new OnlineMapsDrawingLine(travelPoints, routeColor, routerWidth);
  151. onlineMaps.AddDrawingElement(route);
  152. pointIndex = 0;
  153. /*List<OnlineMapsDirectionStep> steps = OnlineMapsDirectionStep.TryParse(response);
  154. if (steps == null)
  155. {
  156. Debug.Log("Something wrong");
  157. Debug.Log(response);
  158. return;
  159. }
  160. List<OnlineMapsVector2d> previospoints = OnlineMapsDirectionStep.GetPointsD (steps);
  161. previospoints.Insert(0,new OnlineMapsVector2d(fromPlace.position.x,fromPlace.position.y));
  162. previospoints.Add (new OnlineMapsVector2d(toPlace.position.x,toPlace.position.y));
  163. travelPoints = previospoints;
  164. OnlineMapsDrawingLine route = new OnlineMapsDrawingLine(travelPoints, routeColor,routerWidth);
  165. OnlineMaps.instance.AddDrawingElement(route);
  166. SimpleFirebaseUnity.Firebase f= SimpleFirebaseUnity.Firebase.CreateNew (FirebaseInstance.Me.GetDataBaseUrl ());
  167. string json = JsonConvert.SerializeObject (new EnvironmentPoints(toPlace.id,travelPoints));
  168. f.Child ("Environments").Child (currentCity).Child("markers").Child (fromPlace.id).Child("travelplaces").Child(toPlace.id).SetValue(json,true);
  169. pointIndex = 0;
  170. }
  171. private void OnMarkerClick(OnlineMapsMarkerBase marker)
  172. {
  173. if (pointIndex == -1 && (marker.id.Trim() != playerLocation.id.Trim()))
  174. {
  175. toPlace = marker;
  176. OnCanTravelTo (toPlace);
  177. }
  178. }
  179. protected virtual void OnTravelComplete(OnlineMapsMarkerBase marker)
  180. {
  181. playerLocation.position = marker.position;
  182. if(OnMarkerTravelSuccess != null)
  183. OnMarkerTravelSuccess (marker);
  184. }
  185. protected virtual void OnCanTravelTo(OnlineMapsMarkerBase marker)
  186. {
  187. if(OnCanTravelToSuccess != null)
  188. OnCanTravelToSuccess (marker);
  189. }
  190. }*/
  191. }