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 travelPoints = new List(); [SerializeField] private string currentCity= "Newyork"; [Header("MARKER")] [SerializeField] private List 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 GetPointstoTravel(OnlineMapsMarkerBase fromplace, OnlineMapsMarkerBase toplace) { List p = new List (); 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 steps = OnlineMapsDirectionStep.TryParse(response); if (steps == null) { Debug.Log("Something wrong"); Debug.Log(response); return; } List 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); } }*/ }