Beispiel #1
0
    private void Start()
    {
        if (Home == null)
        {
            Home = SpawnController.SpawnPoints[Randoms.Next(0, SpawnController.SpawnPoints.Length)];
        }

        if (Home.IsBlocked)
        {
            Kill();
        }

        if (Destination == null)
        {
            Destination = SpawnController.DestinationPoints[Randoms.Next(0, SpawnController.SpawnPoints.Length)];
        }

        gameObject.transform.position = Home.transform.position;
        gameObject.transform.rotation = Home.transform.rotation;
        Routes        = GenerateRoutes(Home, Destination);
        SelectedRoute = GameConfig.UseDijkstra ? Routes.OrderBy(i => i.Weight).FirstOrDefault() : Routes.OrderBy(i => i.NodeCount).LastOrDefault();
        if (SelectedRoute == null)
        {
            Kill();
        }
        NextDestination = SelectedRoute.First();
        RunPath         = true;
        name            = VehicleColor.ToString();
        foreach (var r in gameObject.GetComponentsInChildren <MeshRenderer>())
        {
            r.material.shader = Shader.Find("_Color");
            r.material.SetColor("_Color", VehicleColor);
            r.material.shader = Shader.Find("Specular");
            r.material.SetColor("_SpecColor", VehicleColor);
        }

        SpawnController.Log += this + "\n";
    }
Beispiel #2
0
    public void FixedUpdate()
    {
        if (!RunPath)
        {
            return;
        }

        if (EnableCarDespawning)
        {
            if (MaxSpeed > 0)
            {
                IdleTime = 0;
            }
            else
            {
                IdleTime += Time.deltaTime;
                if (IdleTime > 10f)
                {
                    Kill();
                }
            }
        }

        if (NextDestination != null)
        {
            if (Vector3.Distance(transform.position, NextDestination.Value.transform.position) > 2f)
            {
                RaycastHit hit;
                transform.LookAt(NextDestination.Value.transform.position);
                if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.down), out hit, 10, 3))
                {
                    if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, 20,
                                        3))
                    {
                        Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 20,
                                      Color.yellow);
                        if (hit.distance < 10 && hit.distance > 5)
                        {
                            MaxSpeed -= 0.1f;
                        }
                        else if (hit.distance < 5)
                        {
                            MaxSpeed -= 2f;
                        }

                        if (MaxSpeed <= 0)
                        {
                            MaxSpeed = 0;
                        }
                    }
                    else
                    {
                        Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 20,
                                      Color.white);
                        MaxSpeed += AccelerationSpeed;
                        if (MaxSpeed > NextDestination.Value.Speedlimit)
                        {
                            MaxSpeed = NextDestination.Value.Speedlimit;
                        }
                    }

                    gameObject.transform.position = gameObject.transform.position + gameObject.transform.forward * (Time.deltaTime * MaxSpeed);
                }
            }
            else
            {
                RouteProgress++;
                NextDestination.Value.IsBlocked = false;
                NextDestination.Value.Weight   -= 20;
                NextDestination = SelectedRoute.ElementAtOrDefault(RouteProgress);
                if (NextDestination == null)
                {
                    return;
                }
                NextDestination.Value.IsBlocked = true;
                NextDestination.Value.Weight   += 20;
            }
        }
        else
        {
            RunPath = false;
            Kill();
        }
    }
Beispiel #3
0
        public async void OnNewLocationClicked(string newName, string newLocation, double newLatitude, double newLongitude)
        {
            if (SelectedRoute != null)
            {
                if (!ShowNewLocationFields)
                {
                    ShowNewLocationFields = true;
                }
                else
                {
                    if (string.IsNullOrEmpty(newName))
                    {
                        Message = "A name is required, try again.";
                        return;
                    }

                    if (string.IsNullOrEmpty(newLocation))
                    {
                        Message = "A location is required, try again.";
                        return;
                    }

                    IsLoading = true;
                    Message   = "Processing, please wait.";
                    TravelLocation newTravelLocation = new TravelLocation(newName, newLocation, newLatitude, newLongitude);

                    //REST
                    try
                    {
                        HttpClient httpClient = new HttpClient();
                        Uri        uri        = new Uri(BASE_URL + "User/addTravelLocation/" + UserName + "/" + TravelPlan.Name + "/" + SelectedRoute.Name);

                        HttpStringContent content = new HttpStringContent(
                            JsonConvert.SerializeObject(newTravelLocation),
                            UnicodeEncoding.Utf8,
                            "application/json");

                        HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(
                            uri,
                            content);

                        httpResponseMessage.EnsureSuccessStatusCode();
                        var httpResponseBody = await httpResponseMessage.Content.ReadAsStringAsync();

                        Message = httpResponseBody;

                        if (httpResponseBody.Split(" ")[0] != "Error:")
                        {
                            SelectedRoute.AddTravelLocation(newTravelLocation);
                            RaisePropertyChanged("LocationList");
                            RequestMapUpdate?.Invoke(this, new EventArgs());
                        }
                    }
                    catch (Exception ex)
                    {
                        Message = ex.Message;
                    }

                    ShowNewRouteFields = false;
                    IsLoading          = false;
                }
            }
        }