Exemple #1
0
        /*
         * Used by the circular implementation to dequeue a car (where the head tile moves and the cars does not need
         * to be copied)
         */
        private IEnumerator CircularDequeue(Action <bool> callback)
        {
            NumElements--;
            yield return(StartCoroutine(Destroy(ConvertTileToPosition(ActiveCarpark[_head]), status =>
            {
                if (status)
                {
                    if (NumElements == 0)
                    {
                        _head = 0;
                        HeadTile.SetActive(false);
                    }
                    else
                    {
                        _head = ++_head % ActiveCarpark.Count;
                        HeadTile.GetComponent <IsoTransform>().Position = ActiveCarpark[_head].Position;
                    }

                    callback(true);
                }
                else
                {
                    Debug.Log("Failed to dequeue vehicle");
                    callback(false);
                }
            })));
        }
        /*
         * This method pops the head element from the stack DS and shifts head element to the next (if it exist)
         */
        public void Pop(Action <bool> callback)
        {
            if (NumElements != 0)
            {
                NumElements--;
                StartCoroutine(Destroy(ConvertTileToPosition(ActiveCarpark[NumElements]), status =>
                {
                    if (status)
                    {
                        if (NumElements == 0)
                        {
                            HeadTile.SetActive(false);
                        }
                        else
                        {
                            HeadTile.GetComponent <IsoTransform>().Position = ActiveCarpark[NumElements - 1].Position;
                        }

                        callback(true);
                    }
                    else
                    {
                        Debug.Log("Failed to pop vehicle");
                        callback(false);
                    }
                }));
            }
            else
            {
                Debug.Log("Stack is empty");
                callback(true);
            }
        }
 /*
  * This method adds a new car into the carpark, shifts the headtile (mimic operation of a stack).
  */
 public void Push(GameObject vehicle, Action <bool> callback)
 {
     StartCoroutine(WriteToIndex(vehicle, ConvertTileToPosition(ActiveCarpark[NumElements]), callback));
     HeadTile.GetComponent <IsoTransform>().Position = ActiveCarpark[NumElements].Position;
     HeadTile.SetActive(true);
     NumElements++;
 }
Exemple #4
0
 /*
  * This method adds a new car into the carpark, mimic operation of a queue.
  */
 public void Enqueue(GameObject vehicle, Action <bool> callback)
 {
     HeadTile.GetComponent <IsoTransform>().Position = ActiveCarpark[_head].Position;
     HeadTile.SetActive(true);
     StartCoroutine(WriteToIndex(vehicle,
                                 ConvertTileToPosition(ActiveCarpark[(NumElements + _head) % ActiveCarpark.Count]), callback));
     NumElements++;
 }
Exemple #5
0
        /*
         * Used by the default implementation to dequeue a car (where the head tile does not move) and all cars
         * shifts across by 1.
         */
        private IEnumerator LinkedDequeue(Action <bool> callback)
        {
            NumElements--;
            StartCoroutine(Destroy(ConvertTileToPosition(ActiveCarpark[_head]), status =>
            {
                if (status)
                {
                    if (NumElements == 0)
                    {
                        HeadTile.SetActive(false);
                    }
                }
                else
                {
                    Debug.Log("Failed to dequeue vehicle");
                    callback(false);
                }
            }));
            for (int i = 1; i <= NumElements; i++)
            {
                if (GetVehicleAtPosition(ConvertTileToPosition(ActiveCarpark[i]), out GameObject vehicle))
                {
                    yield return(MoveTo(vehicle, ConvertTileToPosition(ActiveCarpark[i - 1]),
                                        status =>
                    {
                        if (!status)
                        {
                            Debug.Log("Failed to copy vehicles");
                            callback(false);
                        }
                    }));
                }
            }

            Debug.Log("Successfully copied vehicles");
            callback(true);
        }