Escape Game. Putting inventory objects in random game objects?

Started by
4 comments, last by jeteran 8 years, 6 months ago
Hello everyone. I am making a small "escape game" in Flash AS3. The game has objects that are stored in many places of the game (key in a jar, code in a bookshelf, and so on) To make it more interesting, I want to grab those inventory objects (a key, a code and a lockpick) and put them randomly in my game objects (jar, bookshelf, picture, box, lamp, teapot) How would you achieve it? I tried using many approaches, but I didn't come up with the correct one. Thanks everyone!
Advertisement
I do not know Flash so I can't give you source code, but this would be my general process.

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.
Thanks jdean300 for your response. To be honest, I got stuck in the part 3) Sorry my noobness, but can you make me a micro prototype in any language you like? I can follow up the logic. Thanks once again!
var inventoryList = gameObjects.OfType(InventoryObject);
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.

Hello guys, thanks for your response. Jdean300 thanks for the code. I am looking forward to implementing it using the AS3 language. Monophonic you got me there, I got a little more confused here. But I will first try to implement the code before continuing. Thanks once again!

This topic is closed to new replies.

Advertisement