Escape Game. Putting inventory objects in random game objects?
Old user from yayo_99
1) Obtain a list of all your inventory objects and another list of all your game objects that can act as containers.
2) For each of your inventory objects, generate a random index into the container object list and insert the inventory object into the container object.
3) If a container object becomes full (whether it can have one or many objects is irrelevant), remove it from your container object list
Make sure that you generate random numbers based on the current size of the container list, not the starting size. I don't know if there are any specific difficulties with this approach in regards to using AS3, but this process should be applicable to any language.
Old user from yayo_99
var containerList = gameObjects.OfType(ContainerObject);
Foreach(inventoryItem in inventoryList)
{
var index = Random.nextInt(containerList.Count)
containerList[index].AddToInventory(inventoryItem)
If (containList[index].IsFull){
containerList.removeAt(index)
}
}
Note: if your container objects can only have one item in them, you can get rid of the if statement and just use containerList.removeAt(index)
Please forgive any formatting issues - I'm writing this from my phone.
Note you can't just chuck any item in any container and be done with it. That way you will run into a situation where the key to a locked drawer is in that very same drawer. The game is absolutely unwinnable when that sort of thing happens. So you will have to add some logic to ensure that every item can be reached with the previously findable items.
JDean's method is a good start to that direction though, so go ahead and make it work first. After that you modify it so you begin just those containers that can be opened without any items and items that are needed to open those containers that can be reached without any items. When you add an item to a container, you also add the container that that item opens to the list of containers, and items that are needed to open containers that can be reached after opening the first container to the list of items.
Old user from yayo_99