IEnumerator placeItemsInBagAndDrop(BagProperties bagProperties, List <BagContentProperties> items, String randomSeed) { Vector3 bagSize = bagProperties.placingCube.transform.localScale; foreach (BagContentProperties item in items) { item.transform.parent = bagProperties.contents.transform; Vector3 objectSize = item.objectSize; item.transform.localPosition = new Vector3(ItsRandom.randomPlusMinus(0f, (bagSize.x - objectSize.x) / 2f, randomSeed), bagProperties.halfBagHeight, ItsRandom.randomPlusMinus(0f, (bagSize.z - objectSize.z) / 2f, randomSeed)); item.transform.localScale = Vector3.one; bagProperties.bagContents.Add(item); bagProperties.freezeContents(true); yield return(new WaitForSeconds(0.1f)); } yield return(new WaitForSeconds(0.6f)); dropBag(Vector3.zero); }
private bool findPlaceForItemInBag(BagContentPropertiesBase item, BagProperties bagProperties, String randomSeed, int tries = 10, bool inBottom = false) { Vector3 bagSize = ( inBottom ? bagProperties.hiddenObjectsPlacingCube.transform.localScale : bagProperties.placingCube.transform.localScale ); Vector3 objectSize = item.transform.localRotation * item.objectSize; bool objectsCollide = true; List <Collider> allOtherColliders = bagProperties.bagContents.Aggregate(new List <Collider>(), (accumulator, otherItem) => { accumulator.AddRange(otherItem.GetComponents <Collider>().ToList()); return(accumulator); }); // Try to position object in bag for a number of times while (objectsCollide && tries-- > 0) { bool haveAdjustedPos = false; // Randomize position for object item.transform.localPosition = new Vector3(ItsRandom.randomPlusMinus(0f, (bagSize.x - objectSize.x) / 2f, randomSeed), ItsRandom.randomPlusMinus(0f, (bagSize.y - objectSize.y) / 2f, randomSeed), ItsRandom.randomPlusMinus(0f, (bagSize.z - objectSize.z) / 2f, randomSeed)) + ( inBottom ? bagProperties.hiddenObjectsPlacingCube.transform.localPosition : bagProperties.placingCube.transform.localPosition ); if (inBottom) { objectsCollide = false; break; } while (true) { List <Collider> bagContentColliders = item.GetComponents <Collider>().ToList(); Vector3 direction = Vector3.zero; float distance = 0f; objectsCollide = bagContentColliders.Find(ownCollider => { return(allOtherColliders.Find(otherCollider => { return Physics.ComputePenetration(ownCollider, item.transform.localPosition, item.transform.localRotation, otherCollider, otherCollider.gameObject.transform.localPosition, otherCollider.gameObject.transform.localRotation, out direction, out distance); }) != null); }) != null; if (objectsCollide) { if (haveAdjustedPos) { break; } // Move this object a bit and try again item.transform.localPosition += direction * distance; // Make sure we are still in the bag if (Misc.isInside(item.transform.localPosition, objectSize, bagSize)) { haveAdjustedPos = true; } else { break; } } else { goto afterCollideDetection; } } } afterCollideDetection: if (objectsCollide) { Debug.Log(item + ", NOK"); } return(!objectsCollide); }