Пример #1
0
    private IEnumerator RandomSpawnLoop()
    {
        while (true)
        {
            if (randomCarSpawningLoopActive)
            {
                TrafficLaneData randomCarLane = TrafficManager.Instance.GetRandomLane(SpawnType.CAR, maximumCarsInLane);


                if (randomCarLane != null)
                {
                    SpawnObject(SpawnType.CAR, randomCarLane.id);
                }
            }

            if (randomBicycleSpawningLoopActive)
            {
                TrafficLaneData randomBicycleLane = TrafficManager.Instance.GetRandomLane(SpawnType.BICYCLE, maximumBicyclesInLane);

                if (randomBicycleLane != null)
                {
                    SpawnObject(SpawnType.BICYCLE, randomBicycleLane.id);
                }
            }

            if (randomBusSpawningLoopActive)
            {
                TrafficLaneData randomBusLane = TrafficManager.Instance.GetRandomLane(SpawnType.BUS, maximumBussesInLane);

                if (randomBusLane != null)
                {
                    SpawnObject(SpawnType.BUS, randomBusLane.id);
                }
            }

            if (randomPedestrianSpawningLoopActive)
            {
                TrafficLaneData randomPedestrianLane = TrafficManager.Instance.GetRandomLane(SpawnType.PEDESTRIAN, maximumPedestriansInLane);

                if (randomPedestrianLane != null)
                {
                    SpawnObject(SpawnType.PEDESTRIAN, randomPedestrianLane.id);
                }
            }

            if (randomTrainSpawningLoopActive)
            {
                TrafficLaneData randomTrainLane = TrafficManager.Instance.GetRandomLane(SpawnType.TRAIN, maximumTrainsInLane);

                if (randomTrainLane != null)
                {
                    SpawnObject(SpawnType.TRAIN, randomTrainLane.id);
                }
            }


            yield return(new WaitForSeconds(spawnRate));
        }
    }
Пример #2
0
    public TrafficLaneData GetRandomLane(SpawnManager.SpawnType entityType, int maxNrOfEnttitiesInLane = -1)
    {
        int             randomIndex = 0;
        TrafficLaneData trafficLane = null;

        switch (entityType)
        {
        case SpawnManager.SpawnType.BICYCLE:
            randomIndex = Random.Range(0, bicycleLanes.Count);
            trafficLane = bicycleLanes[randomIndex];
            break;

        case SpawnManager.SpawnType.BUS:
            randomIndex = Random.Range(0, busLanes.Count);
            trafficLane = busLanes[randomIndex];
            break;

        case SpawnManager.SpawnType.CAR:
            randomIndex = Random.Range(0, carLanes.Count);
            trafficLane = carLanes[randomIndex];
            break;

        case SpawnManager.SpawnType.TRAIN:
            randomIndex = Random.Range(0, trainLanes.Count);
            trafficLane = trainLanes[randomIndex];

            break;

        case SpawnManager.SpawnType.PEDESTRIAN:
            randomIndex = Random.Range(0, pedestrianLanes.Count);
            trafficLane = pedestrianLanes[randomIndex];

            break;

        default:
            Debug.LogError("Spawntype " + entityType + " not recognized!");

            break;
        }

        if (maxNrOfEnttitiesInLane == -1)
        {
            return(trafficLane);
        }

        if (trafficLane.NumberOfEntitiesInLane >= maxNrOfEnttitiesInLane)
        {
            return(null);
        }

        return(trafficLane);
    }
Пример #3
0
    public void DecreaseNumberOfCarsInLaneByOne(int laneId)
    {
        TrafficLaneData laneData = FindLaneDataById(laneId);

        if (laneData == null)
        {
            Debug.LogError("Failed to decrease total count of cars in lane " + laneId);
            return;
        }

        laneData.NumberOfEntitiesInLane--;

        Client.Instance.SendStateData();
    }
Пример #4
0
    public TrafficLaneData FindLaneDataById(int id)
    {
        for (int i = 0; i < TrafficLanes.Count; i++)
        {
            TrafficLaneData trafficLane = TrafficLanes[i];

            if (trafficLane == null)
            {
                continue;
            }

            if (trafficLane.id == id)
            {
                return(trafficLane);
            }
        }

        return(null);
    }
Пример #5
0
    public void InitEntityAtLane(int laneId, GameObject objectToSpawn)
    {
        TrafficLaneData laneData = FindLaneDataById(laneId);

        if (laneData == null)
        {
            Debug.LogError("Lane id " + laneId + " not found!");
            return;
        }

        WaypointManager waypointManager = laneData.GetRandomWaypointManager();

        if (waypointManager == null)
        {
            Debug.LogError("No waypointmanager assigned for lane " + laneId);
            return;
        }


        if (waypointManager.waypointNodes.Count < 1)
        {
            Debug.LogError("No waypoint nodes set!");
            return;
        }


        WaypointAgent waypointAgent = objectToSpawn.GetComponent <WaypointAgent>();

        if (waypointAgent == null)
        {
            Debug.LogError("Entity " + objectToSpawn + " is missing WaypointAgent component!");
            return;
        }

        //Set spawn location and rotation
        Transform spawnLocation = waypointManager.waypointNodes[0].transform;

        objectToSpawn.transform.rotation = spawnLocation.rotation;
        objectToSpawn.transform.position = spawnLocation.position;

        //Assign waypoint systems.
        waypointAgent.WaypointSystem = waypointManager;

        //Reset waypointsystem
        waypointAgent.ResetWaypointTargetToFirst();

        //Update the lane data, add this waypointagent to the lane data list.
        laneData.AddWaypointAgent(waypointAgent);
        laneData.NumberOfEntitiesInLane++;

        //Assign traffic lane id to the waypointagent
        waypointAgent.TrafficLaneId = laneData.id;


        //Add any trafficlights assigned from the lane to the waypoint trafficlight queue.
        //This is the order of trafficlights the waypoint agent will drive through
        for (int i = 0; i < laneData.trafficLights.Count; i++)
        {
            waypointAgent.TrafficlightQueue.Enqueue(laneData.trafficLights[i]);
        }
        waypointAgent.AssignNextTrafficlight();



        //When finished configuring, activate the waypoint system.
        waypointAgent.WaypointSystemActivated = true;


        //Send update to controller with newest state data.
        ClientInstance.SendStateData();
    }