Пример #1
0
    public static Road CreateChargeSpotUp(int x, int y)
    {
        PowerStationRoad r = (PowerStationRoad)CreateChargeSpot(x, y, 270);

        r.up = true;
        return(r);
    }
Пример #2
0
    public void ReplanWithRecharge()
    {
        if (goingToCharge == true)
        {
            return;
        }
        //Find nearest power station
        PowerStationRoad closest = world.FindNearestPoweredPowerstation(controlledCar.transform.position.ToVector2());

        if (closest == null)
        {
            return;
        }
        if (closest.transform.position.Distance(transform.position) > ActiveTrip.end.transform.position.Distance(transform.position))
        {
            //end is closer than the station
            return;
        }

        Road onWhich = closest;

        //From current waypoint to charge
        suspededTrip = ActiveTrip;
        if (onWhich == null || controlledCar.road == null)
        {
            Debug.Log(onWhich);
            Debug.Log(controlledCar.road);
        }
        Road carRoad = world.FindNearestRoad(controlledCar.transform.position.ToVector2());

        ActiveTrip = CreateImmediateTrip(carRoad, onWhich, Trip.TripType.Charge);

        CreateJourney(carRoad, onWhich);
        goingToCharge = true;
    }
Пример #3
0
    public static Road CreateChargeSpotDown(int x, int y)
    {
        PowerStationRoad r = (PowerStationRoad)CreateChargeSpot(x, y, 90);

        r.down = true;
        return(r);
    }
Пример #4
0
    public static Road CreateChargeSpotRight(int x, int y)
    {
        PowerStationRoad r = (PowerStationRoad)CreateChargeSpot(x, y, 180);

        r.right = true;
        return(r);
    }
Пример #5
0
    public static Road CreateChargeSpotLeft(int x, int y)
    {
        PowerStationRoad r = (PowerStationRoad)CreateChargeSpot(x, y, 0);

        r.left = true;
        return(r);
    }
Пример #6
0
    public static Road CreateChargeSpot(int x, int y, float zRotation)
    {
        InitializePrefabs();
        GameObject road = (GameObject)SpoofInstantiate(chargeSpotPrefab);

        road.transform.position = new Vector3(x, y, 0);
        road.transform.Rotate(0, 0, zRotation);
        PowerStationRoad r = road.AddComponent <PowerStationRoad>();

        r.xPos = x;
        r.yPos = y;

        //Register power stations into the world!
        if (worldInstance == null)
        {
            worldInstance = GameObject.FindObjectOfType <World>();
        }
        if (worldInstance == null || worldInstance.powerStations == null)
        {
            throw new ApplicationException("World not instantiated");
        }
        PowerStation[] stations = r.GetComponentsInChildren <PowerStation>();
        worldInstance.powerStations.AddRange(stations);

        r.neighbourRoads = new List <Road>();
        return(r);
    }
Пример #7
0
    //Try to get a free station
    IEnumerator PollForStation(PowerStationRoad p)
    {
        while (p.GetEmptyPoweredStation() == null)
        {
            yield return(new WaitForSeconds(0.1f));
        }
        PowerStation station = p.GetEmptyPoweredStation();

        station.Occupy();
        chargingAt = station;
        controlledCar.GoToState(Car.CarState.driving);
    }
Пример #8
0
    //last few waypoints may have specific behavior
    private void BehaveLastWaypoints(LinkedList <Waypoint> points)
    {
        int count = points.Count;

        switch (ActiveTrip.type)
        {
        case Trip.TripType.Park: {
            ParkingSpotRoad p       = (ParkingSpotRoad)points.Last.Value.onRoad;
            ParkingSpace    parking = p.GetEmptySpace();
            if (parking == null)
            {
                Debug.LogError("Unhandled exception: parking full.");
                return;
            }
            Waypoint last = points.Last.Value;
            last.Set(parking.transform.position.ToVector2());
            break;
        }

        case Trip.TripType.Charge: {
            PowerStationRoad p = (PowerStationRoad)points.Last.Value.onRoad;
            if (count < 2)
            {
                //Set static collision avoidance
                controlledCar.SetCollisionAvoidance(Car.CollisionAvoidance.staticc);
                if (p.GetEmptyPoweredStation() == null)
                {
                    controlledCar.GoToState(Car.CarState.waiting);
                    StartCoroutine(PollForStation(p));
                }
            }
            if (count <= 1)
            {
                Debug.Log("Disabling collision avoidance");
                //set dynamic collision avoidance
                controlledCar.SetCollisionAvoidance(Car.CollisionAvoidance.disabled);
            }
            break;
        }
        }
    }
Пример #9
0
    void EndActiveTrip()
    {
        ActiveTrip.completed = true;
        TripInProgress       = false;
        controlledCar.GoToState(Car.CarState.waiting);

        switch (ActiveTrip.type)
        {
        case Trip.TripType.Park: {
            ParkingSpace s = ((ParkingSpotRoad)ActiveTrip.end).GetEmptySpace();
            //Reserve the parking space
            s.Occupy();
            parkedAt = s;
            state    = State.parked;

            controlledCar.Park(s);
            break;
        }

        case Trip.TripType.Charge: {
            PowerStationRoad s = world.FindNearestPoweredPowerstation(controlledCar.transform.position.ToVector2());
            if (s == null)
            {
                Debug.LogError("No power stations! Handle that.");
            }
            PowerStation ps = s.GetEmptyPoweredStation();
            //no empty station available
            //this is actually obsolete.
            if (ps == null)
            {
                Debug.LogError("All stations full (handled in coroutine.)!");
            }
            else
            {
                chargingAt = ps;
                ps.Occupy();
            }

            charging = true;
            state    = State.charging;

            controlledCar.Recharge(chargingAt);
            return;
        }

        case Trip.TripType.Cruise: {
            state = State.enRoute;
            break;
        }
        }

        //Create next journey
        CreateJourney();
        if (ActiveTrip == null)
        {
            Finished = true;
            Game.Instance.OnCarFinished(controlledCar);
            return;
        }
        SetWakeUp(ActiveTrip.departure);
    }