/** * Insert in order in allReferencesDataControl attribute * * @param element * The element container to be added * @param hasLayer * Take either layer or depth value to order value * @param playerLayer * Take the layer if player has it, or the y position if the * player has not layer. * * @return i returns the position where the element has been inserted. It * will be use to reassign layer */ public int insertInOrder(ElementContainer element, bool hasLayer) { bool added = false; int i = 0; bool empty = allReferencesDataControl.Count == 0; // While the element has not been added, and // we haven't checked every previous element while (!added && (i < allReferencesDataControl.Count || empty)) { if (!empty) { if (hasLayer) { if (element.getLayer() <= allReferencesDataControl[i].getLayer()) { allReferencesDataControl.Insert(i, element); added = true; } } else { if (element.getY() <= Mathf.Round(allReferencesDataControl[i].getY())) { allReferencesDataControl.Insert(i, element); reassignLayerAllReferencesDataControl(i); added = true; } } i++; } else { allReferencesDataControl.Insert(i, element); if (!hasLayer) { reassignLayerAllReferencesDataControl(i); } added = true; i++; } } // If the element wasn't added, add it in the last position if (!added) { //element.setLayer(i); allReferencesDataControl.Add(element); if (!hasLayer) { reassignLayerAllReferencesDataControl(i - 1); } } return(i - 1); }