/* * 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++; }
/* * 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++; }