示例#1
0
        /*
         * This method is used to spawn a car at the defined spawn point. This is a coroutine which provides a callback
         * so the animation can be queued up.
         *
         * If the input vehicleType is VehicleType.random then it spawns a random vehicle out of all spawnable one (ones
         * that are not in the carpark). Otherwise, it will spawned the specified VehicleType if it is not already spawned.
         *
         * The separate _customSpawnableVehicles list is for managing custom assets (e.g. garbage A->E)
         */
        protected IEnumerator Spawn(VehicleType vehicleType, Action<Tuple<VehicleType, GameObject>> callback = null)
        {
            foreach (var vehicle in _activeVehicles.Keys)
            {
                if (vehicle.GetComponent<IsoTransform>().Position.Equals(_spawnPoint))
                {
                    Debug.Log("Cannot spawn vehicle, a vehicle exist at spawn");
                    callback?.Invoke(null);
                    yield break;
                }
            }

            // Getting a random vehicle which is spawnable
            if (vehicleType == VehicleType.random)
            {
                if (!Randomiser.RandomValuesFromDict(_spawnableVehicles, out vehicleType))
                {
                    Debug.Log("No vehicle left to spawn");
                    callback?.Invoke(null);
                }
            }

            // Try to spawn the defined vehicleType (from spawnable list first then custom list)
            if (_spawnableVehicles.TryGetValue(vehicleType, out var vehicleAsset))
            {
                GameObject obj = Instantiate(vehicleAsset);
                obj.GetComponent<IsoTransform>().Position = _spawnPoint;
                obj.GetComponent<CustomAStarAgent>().Graph = FindObjectOfType<CustomGridGraph.CustomGridGraph>();
                if (_activeVehicles.TryAdd(obj, vehicleType))
                {
                    _spawnableVehicles.TryRemove(vehicleType, out _);
                    callback?.Invoke(new Tuple<VehicleType, GameObject>(vehicleType, obj));
                }
                else
                {
                    DestroyImmediate(obj);
                    callback?.Invoke(null);
                }
            }
            else if (_customSpawnableVehicles.TryGetValue(vehicleType, out vehicleAsset))
            {
                GameObject obj = Instantiate(vehicleAsset);
                obj.GetComponent<IsoTransform>().Position = _spawnPoint;
                obj.GetComponent<CustomAStarAgent>().Graph = FindObjectOfType<CustomGridGraph.CustomGridGraph>();
                if (_activeVehicles.TryAdd(obj, vehicleType))
                {
                    _customSpawnableVehicles.TryRemove(vehicleType, out _);
                    callback?.Invoke(new Tuple<VehicleType, GameObject>(vehicleType, obj));
                }
                else
                {
                    DestroyImmediate(obj);
                    callback?.Invoke(null);
                }
            }
            else
            {
                callback?.Invoke(null);
                Debug.Log("Cannot spawn vehicle, this vehicle type already spawned");
            }

            Debug.Log($"There are {_activeVehicles.Count} active vehicles");
        }