IEnumerator placeItemsInBag(BagProperties bagProperties, int amount, List <BagContentProperties> toBePlacedInTrays, Person person, String randomSeed) { int yieldEveryXthItem = 5; int yieldCounter = yieldEveryXthItem; // float lastCycleStart = Time.realtimeSinceStartup; Vector3 bagSize = bagProperties.placingCube.transform.localScale; bagProperties.placingCube.SetActive(true); Bounds bagBounds = bagProperties.placingCube.GetComponent <Collider>().bounds; bagProperties.placingCube.SetActive(false); // Debug.Log(bagBounds); // TODO - Hidden items, make sure to check person config on frequency of putting these items in // TODO - For now, always put one item GameObject hiddenObj = ItsRandom.pickRandom <BagContentType>(bagContentTypesHidden.ToList(), randomSeed).contentObj; GameObject hiddenItem = Instantiate(hiddenObj); hiddenItem.transform.parent = bagProperties.transform; BagContentProperties hiddenItemProperties = hiddenItem.GetComponent <BagContentProperties> (); // Randomize place in bottom of bag findPlaceForItemInBag(hiddenItemProperties, bagProperties, randomSeed, int.MaxValue, true); bagProperties.bagContents.Add(hiddenItemProperties); // TODO - This code block is only made for forcing an illegal item GameObject gunObj = ItsRandom.pickRandom <BagContentType>(bagContentTypesIllegal.ToList(), randomSeed).contentObj; GameObject gun = Instantiate(gunObj); gun.transform.parent = bagProperties.contents.transform; BagContentProperties gunProperties = gun.GetComponent <BagContentProperties> (); // Randomize place in bag findPlaceForItemInBag(gunProperties, bagProperties, randomSeed, int.MaxValue, false); bagProperties.bagContents.Add(gunProperties); // TODO - END for (int i = 0; i < amount; i++) { List <int> weights = bagContentTypesLegal.Select(obj => obj.frequency).ToList(); List <GameObject> gameObjects = bagContentTypesLegal.Select(obj => obj.contentObj).ToList(); // TODO - below are the REAL CODE - NOT ABOVE // List<int> weights = bagContentTypes.Select(obj => obj.frequency).ToList(); // List<GameObject> gameObjects = bagContentTypes.Select(obj => obj.contentObj).ToList(); GameObject randomGameObject = ItsRandom.pickRandomWithWeights(weights, gameObjects, randomSeed); // Check if item should be in tray, or not instantiated by any other reason (eg. not place 3 guns in bag...) bool acceptItem = true; // TODO - Do this! // bool acceptItem = randomGameObject.GetComponent<BagContentInstantiationRules>()(toBePlacedInTrays, person); if (!acceptItem) { i--; continue; } GameObject contentPiece = Instantiate(randomGameObject); contentPiece.transform.parent = bagProperties.contents.transform; // Randomly rotate 90°-angle // TODO - When rotation turned on, objects seem to fall outside bag // contentPiece.transform.localRotation = Quaternion.Euler(0f, 90f * Misc.random.Next(), 0f); BagContentProperties bagContentProperties = contentPiece.GetComponent <BagContentProperties> (); // Randomize place in bag bool itemFitsInBag = findPlaceForItemInBag(bagContentProperties, bagProperties, randomSeed, 10, false); if (itemFitsInBag) { bagContentProperties.person = person; bagProperties.bagContents.Add(bagContentProperties); // Trigger "random"-functions on it RandomInterface[] randomInterfaces = contentPiece.GetComponents <RandomInterface>(); foreach (RandomInterface randomInterface in randomInterfaces) { randomInterface.run(); } } else { Debug.Log("Item removed: " + contentPiece); contentPiece.transform.parent = null; Destroy(contentPiece); } /* * yieldCounter--; * if (yieldCounter == 0) { * yieldCounter = yieldEveryXthItem; * // if (lastCycleStart + MAX_ITEM_PLACE_CYCLE_SECONDS < Time.realtimeSinceStartup) { * Debug.Log("YIELD"); * yield return null; * // // TODO - Compact items by code (move downwards) * // yield return null; * // lastCycleStart = Time.realtimeSinceStartup; * } */ } Debug.Log("Items in bag: " + bagProperties.bagContents.Count()); // Debug.Break(); yield return(null); }