public Vector3 NextWaypoint(ref CarHeading heading, out IntersectionController intersection, AI_CarController car)
    {
        carsCount--;
        RemoveLightListener(heading, car);
        Vector3   waypoint = new Vector3();
        Direction dir      = Direction.Undefined;
        int       compare  = 10;

        foreach (var neighbor in neighbors)
        {
            if (neighbor.Value.carsCount < compare)
            {
                if (NoGoDirection(heading) == neighbor.Key)
                {
                    continue;
                }
                else
                {
                    compare = neighbor.Value.carsCount;
                    dir     = neighbor.Key;
                }
            }
        }
        intersection = neighbors[dir];
        intersection.AddCarToIntersection();
        switch (dir)
        {
        case Direction.North:
            waypoint = intersection.North.position;
            heading  = CarHeading.North;
            break;

        case Direction.South:
            waypoint = intersection.South.position;
            heading  = CarHeading.South;
            break;

        case Direction.East:
            waypoint = intersection.East.position;
            heading  = CarHeading.East;
            break;

        case Direction.West:
            waypoint = intersection.West.position;
            heading  = CarHeading.West;
            break;
        }
        intersection.AddCarToStreetLightEvent(heading, car);
        return(waypoint);
    }
    void RemoveLightListener(CarHeading direction, AI_CarController car)
    {
        switch (direction)
        {
        case CarHeading.North:
        case CarHeading.South:
            NorthSouthLightChange -= car.LightChange;
            break;

        case CarHeading.West:
        case CarHeading.East:
            EastWestLightChange -= car.LightChange;
            break;
        }
    }
    public void AddCarToStreetLightEvent(CarHeading heading, AI_CarController car)
    {
        switch (heading)
        {
        case CarHeading.North:
        case CarHeading.South:
            NorthSouthLightChange += car.LightChange;
            break;

        case CarHeading.East:
        case CarHeading.West:
            EastWestLightChange += car.LightChange;
            break;
        }
    }
    Direction NoGoDirection(CarHeading heading)
    {
        switch (heading)
        {
        case CarHeading.North:
            return(Direction.South);

        case CarHeading.South:
            return(Direction.North);

        case CarHeading.East:
            return(Direction.West);

        case CarHeading.West:
            return(Direction.East);
        }
        return(Direction.Undefined);
    }
Beispiel #5
0
    void FindInitialWaypoint()
    {
        float north = (intersection.North.gameObject != null) ? Vector3.Distance(transform.position, intersection.North.position) : 500f;
        float south = (intersection.South.gameObject != null) ? Vector3.Distance(transform.position, intersection.South.position) : 500f;
        float east  = (intersection.East.gameObject != null) ? Vector3.Distance(transform.position, intersection.East.position) : 500f;
        float west  = (intersection.West.gameObject != null) ? Vector3.Distance(transform.position, intersection.West.position) : 500f;

        float shortest = 400f;

        if (north <= shortest)
        {
            waypoint = intersection.North.position;
            heading  = CarHeading.North;
            intersection.AddCarToStreetLightEvent(heading, this);
            shortest = north;
        }
        if (south <= shortest)
        {
            waypoint = intersection.South.position;
            heading  = CarHeading.South;
            intersection.AddCarToStreetLightEvent(heading, this);
            shortest = south;
        }
        if (east <= shortest)
        {
            waypoint = intersection.East.position;
            heading  = CarHeading.East;
            intersection.AddCarToStreetLightEvent(heading, this);
            shortest = east;
        }
        if (west <= shortest)
        {
            waypoint = intersection.West.position;
            heading  = CarHeading.West;
            intersection.AddCarToStreetLightEvent(heading, this);
        }
    }