Sign in to follow this  
jeteran

Escape Game. Putting inventory objects in random game objects?

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this