123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using System.Linq;
- using UnityEngine.Events;
- using WebTools.Datas.Behaviours;
- using SimpleFirebaseUnity;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using WebTools.Environments.Datas;
- namespace WebTools.Environments.Behaviours
- {
- /*public class EnviromentTravelManagerBehaviour : MonoBehaviour
- {
- public delegate void OnMarkerTravel(OnlineMapsMarkerBase marker);
- public static event OnMarkerTravel OnMarkerTravelSuccess;
- public delegate void OnCanTravelToMarker (OnlineMapsMarkerBase marker);
- public static event OnCanTravelToMarker OnCanTravelToSuccess;
- [Header("ENVIRONMENTS")]
- [SerializeField]
- protected OnlineMaps onlineMaps;
- [Header("PLAYERLOCATION")]
- [SerializeField]
- private OnlineMapsMarkerBase playerLocation;
- [Header("TRAVEL")]
- [SerializeField]
- private OnlineMapsMarkerBase fromPlace;
- [SerializeField]
- private OnlineMapsMarkerBase toPlace;
- [SerializeField]
- private List<OnlineMapsVector2d> travelPoints = new List<OnlineMapsVector2d>();
- [SerializeField]
- private string currentCity= "Newyork";
- [Header("MARKER")]
- [SerializeField]
- private List<Texture2D> texturesMarkers;
- [SerializeField]
- [Range(0,999999)]
- private int markerSpeed = 60;
- [SerializeField]
- private bool lookToMarker = false;
- [Header("ROUTE")]
- [SerializeField]
- private Color32 routeColor;
- [SerializeField]
- private int routerWidth;
- private int pointIndex = -1;
- private double progress;
- private void Start()
- {
- GetCityMarker ();
- GetMarkers ();
- EnableMarketEvents ();
- InitMarker ();
-
- }
-
- private void Update()
- {
- Travel ();
- }
-
- private void GetCityMarker()
- {
- var marker = DataManagerBehaviour.Instance.GetCurrentEnvironments ().FirstOrDefault (e => e.GetId () == currentCity).GetMarket();
- onlineMaps.position = marker.position;
- }
- private void GetMarkers()
- {
- var markers = DataManagerBehaviour.Instance.GetCurrentEnvironments ().FirstOrDefault (e => e.GetId () == currentCity).GetMarkets ();
- foreach (var m in markers)
- {
- print("marker " + m.id);
- var t = texturesMarkers.ElementAtOrDefault((int)m.type);
- var mar = onlineMaps.AddMarker (m.position,t,m.id);
- mar.type = m.type;
- mar.travelplaces = m.travelplaces;
- }
-
- }
- private void EnableMarketEvents()
- {
- foreach (OnlineMapsMarker marker in onlineMaps.markers)
- {
- marker.OnClick += OnMarkerClick;
- }
- }
- private void InitMarker()
- {
- playerLocation = onlineMaps.markers.FirstOrDefault(m=> m.type == EnvironmentType.PLAYER);
- }
-
- private void Travel()
- {
- onlineMaps.Redraw();
- if (pointIndex == -1)
- return;
- OnlineMapsVector2d p1 = travelPoints[pointIndex];
- OnlineMapsVector2d p2 = travelPoints[travelPoints.Count - 1]; //travelPoints[pointIndex + 1];
- double dx, dy;
- OnlineMapsUtils.DistanceBetweenPoints(p1.x, p1.y, p2.x, p2.y, out dx, out dy);
- double stepDistance = Math.Sqrt(dx * dx + dy * dy);
- double totalTime = stepDistance / markerSpeed * 3600;
- //progress += Time.deltaTime / totalTime;
- progress += Time.deltaTime;
- OnlineMapsVector2d position;
- if (progress < 1)
- {
- position = OnlineMapsVector2d.Lerp(p1, p2, progress);
- playerLocation.SetPosition(position.x, position.y);
- }
- else
- {
- position = p2;
- playerLocation.SetPosition(position.x, position.y);
- //pointIndex++;
- progress = 0;
- //if (pointIndex >= travelPoints.Count - 1)
- //{
- pointIndex = -1;
- onlineMaps.RemoveAllDrawingElements();
- OnTravelComplete (toPlace);
- //}
- }
- if (lookToMarker)
- {
- onlineMaps.SetPosition(position.x, position.y);
- }
-
- onlineMaps.Redraw();
- }
- public void InitFindPlace()
- {
- fromPlace = onlineMaps.markers.FirstOrDefault(m=>m.position.x == playerLocation.position.x && m.position.y == playerLocation.position.y
- && m.type != EnvironmentType.PLAYER);
- OnFindRouteComplete(fromPlace, toPlace);
- // OnlineMapsGoogleDirections.Find(fromPlace.position, toPlace.position).OnComplete += OnFindRouteComplete;
- }
- private List<OnlineMapsVector2d> GetPointstoTravel(OnlineMapsMarkerBase fromplace, OnlineMapsMarkerBase toplace)
- {
- List<OnlineMapsVector2d> p = new List<OnlineMapsVector2d> ();
- var froms = onlineMaps.markers.FirstOrDefault (m => m.id == fromplace.id);
- if (froms != null)
- {
- var tos = froms.travelplaces.FirstOrDefault (t=>t.GetId()== toplace.id);
- if (tos != null)
- {
- p = tos.GetPoints ();
- }
- }
- return p;
- }
- protected virtual void OnFindRouteComplete(/*string response OnlineMapsMarkerBase fromplace, OnlineMapsMarkerBase toplace)
- {
- travelPoints = GetPointstoTravel(fromplace, toplace);
- OnlineMapsDrawingLine route = new OnlineMapsDrawingLine(travelPoints, routeColor, routerWidth);
- onlineMaps.AddDrawingElement(route);
- pointIndex = 0;
- /*List<OnlineMapsDirectionStep> steps = OnlineMapsDirectionStep.TryParse(response);
- if (steps == null)
- {
- Debug.Log("Something wrong");
- Debug.Log(response);
- return;
- }
- List<OnlineMapsVector2d> previospoints = OnlineMapsDirectionStep.GetPointsD (steps);
- previospoints.Insert(0,new OnlineMapsVector2d(fromPlace.position.x,fromPlace.position.y));
- previospoints.Add (new OnlineMapsVector2d(toPlace.position.x,toPlace.position.y));
- travelPoints = previospoints;
- OnlineMapsDrawingLine route = new OnlineMapsDrawingLine(travelPoints, routeColor,routerWidth);
- OnlineMaps.instance.AddDrawingElement(route);
- SimpleFirebaseUnity.Firebase f= SimpleFirebaseUnity.Firebase.CreateNew (FirebaseInstance.Me.GetDataBaseUrl ());
- string json = JsonConvert.SerializeObject (new EnvironmentPoints(toPlace.id,travelPoints));
- f.Child ("Environments").Child (currentCity).Child("markers").Child (fromPlace.id).Child("travelplaces").Child(toPlace.id).SetValue(json,true);
- pointIndex = 0;
- }
-
- private void OnMarkerClick(OnlineMapsMarkerBase marker)
- {
-
- if (pointIndex == -1 && (marker.id.Trim() != playerLocation.id.Trim()))
- {
- toPlace = marker;
- OnCanTravelTo (toPlace);
- }
- }
- protected virtual void OnTravelComplete(OnlineMapsMarkerBase marker)
- {
- playerLocation.position = marker.position;
- if(OnMarkerTravelSuccess != null)
- OnMarkerTravelSuccess (marker);
- }
- protected virtual void OnCanTravelTo(OnlineMapsMarkerBase marker)
- {
- if(OnCanTravelToSuccess != null)
- OnCanTravelToSuccess (marker);
- }
-
- }*/
- }
|