Example #1
0
        /*
         * This method copies the existing car from the old to the new carpark. If number of cars is greater than
         * capacity of the new carpark, then they are destroyed. This is a coroutine which provides a callback so the
         * animation can be queued up.
         */
        private IEnumerator CopyVehiclesToNewCarpark(ListCarparkManager newCarparkManager,
                                                     List <IsoTransform> newCarpark, Action <bool> callback)
        {
            yield return(StartCoroutine(TransitionManager.SpawnCarparkEffect(newCarparkManager.GroundTiles,
                                                                             newCarparkManager.Decorations)));

            if (_currentListCarpark != null)
            {
                int completed = 0;
                int expected  = ActiveCarpark.Count;
                for (int i = 0; i < ActiveCarpark.Count; i++)
                {
                    if (GetVehicleAtPosition(ConvertTileToPosition(ActiveCarpark[i]), out GameObject vehicle))
                    {
                        if (i < newCarpark.Count)
                        {
                            StartCoroutine(WriteToIndex(vehicle, ConvertTileToPosition(newCarparkManager.Carparks[i]),
                                                        status =>
                            {
                                if (status)
                                {
                                    completed++;
                                }
                                else
                                {
                                    Debug.Log("Failed to copy cars");
                                    callback(false);
                                }
                            }));
                        }
                        else
                        {
                            Destroy(i, status =>
                            {
                                if (status)
                                {
                                    completed++;
                                }
                                else
                                {
                                    Debug.Log("Failed to destroy car when not list is smaller");
                                    callback(false);
                                }
                            });
                        }
                    }
                    else
                    {
                        expected--;
                    }
                }

                yield return(new WaitUntil(() => completed == expected));

                Debug.Log("Finished copying cars");

                yield return(TransitionManager.PanCameraEffect(FindObjectOfType <Camera>(), _shiftAmount));

                yield return(StartCoroutine(TransitionManager.DestroyCarparkEffect(_currentListCarpark.GroundTiles,
                                                                                   _currentListCarpark.Decorations)));

                DestroyImmediate(_currentListCarpark.gameObject);
            }

            _currentListCarpark = newCarparkManager;
            ActiveCarpark       = newCarpark;
            SetNewSpawnPoint(_currentListCarpark.SpawnTile);
            SetNewDestroyPoint(_currentListCarpark.DestroyTile);
            GridGraph.UpdateGraph();
            callback?.Invoke(true);
        }