public void AddObjectInside(ShiftABGameObject obj) { _underObjectsHeight += obj.GetBounds().size.y; }
bool DefineObjectLabel(ref ShiftABGameObject nextObject, ref LinkedList<ShiftABGameObject> stack) { ShiftABGameObject objectBelow = null; if(stack.Count > 0) { objectBelow = stack.Last.Value; } else { // If the object below is the ground nextObject.Label = Random.Range(0, GameWorld.Instance.Templates.Length); // There is a chance to double the object if(nextObject.Type != (int)BlockTypes.Podium && nextObject.Type != (int)BlockTypes.Circle && Random.value < _duplicateProbability) nextObject.IsDouble = true; return true; } // Get list of objects that can be stacked List<int> stackableObjects = GetStackableObjects(objectBelow); while(stackableObjects.Count > 0) { nextObject.UnderObjectsHeight = 0f; nextObject.IsDouble = false; nextObject.Label = stackableObjects[Random.Range(0, stackableObjects.Count - 1)]; // Check if there is no stability problems if(nextObject.Type == (int)BlockTypes.Box) { // If next object is a box, check if it can enclose the underneath objects LinkedListNode<ShiftABGameObject> currentObj = stack.Last; float underObjectsHeight = 0f; while(currentObj != null) { Bounds objBelowBounds = currentObj.Value.GetBounds(); if(objBelowBounds.size.x < nextObject.GetEmptyScapeInside().x) { if(underObjectsHeight + objBelowBounds.size.y < nextObject.GetEmptyScapeInside().y) { nextObject.AddObjectInside(currentObj.Value); underObjectsHeight += objBelowBounds.size.y; currentObj = currentObj.Previous; } else break; } else break; } nextObject.UnderObjectsHeight = underObjectsHeight; // Holding object is the ground, so it is safe if(currentObj == null) return true; // Holding object is bigger, so it is safe if(currentObj.Value.GetArea() >= nextObject.GetArea()) return true; } else { // There is a chance to double the object if(objectBelow.GetBounds().size.x >= 2f * nextObject.GetBounds().size.x) if(nextObject.Type != (int)BlockTypes.Podium && nextObject.Type != (int)BlockTypes.Circle && Random.value < _duplicateProbability) nextObject.IsDouble = true; if(objectBelow.GetArea() > nextObject.GetArea()) return true; } stackableObjects.Remove(nextObject.Label); } return false; }