Class Structure for a Text Adventure Game
Members - Reputation: 268
Posted 06 December 2012 - 04:50 PM
connection* listOfConnections //pointer to head of linked list
location* destinationLocation //pointer to a location
item* listOfItems //pointer to head of linked list
So At this point I'm trying to figure out if this is a good class structure or if I need more or less classes.
How do I abstract the concept of an action or a relationship? Like using a key to open a door. Should I have an additional class for objects separate from items? (for doors)
Should the objects class have state bools or should I make another class for states and then have a pointer to a linked list of states in each instance of an object class.
Is there any point in having an inventory class if the item class already has a 'playerHas' bool? Or is the better question if there is any point in having the 'playerHas' bool if there is already a linked list of items that is the player's inventory.
It seems like concepts like actions "use key on door" and "press button" need to be abstracted and added to the class structure as well.
But I've never done anything like this so I'm not sure how best to proceed.
Members - Reputation: 1826
Posted 06 December 2012 - 05:31 PM
You'll be back here asking how you have other characters carry things...
There are a couple of reasonable approaches. One is to have a true tree structure where objects can contain/consist or otherwise relate to other objects; rooms are objects which contain items and characters, characters can "hold" or "wear" objects, which in turn can contain other objects.
Lets you handle "examine gun in pocket of coat"
A simpler, but still useful mechanism, much used in games in the 8 and 16 bit eras is to give each object a "location" field which is an integer. If it's positive, the object is present in that numbered location. If it's negative, the object is being carried by character number abs(location). Location zero doesn't exist, so objects can be removed from the game world. Other location numbers can be "special" in various ways.
It then gets reasonably easy to implement "give" and "take" and then you can have puzzles where you need to stun the guard to take his sword (because when he's unconscious he doesn't attack you if you try to take objects off him) and use it to cut the rope...
Just give each item a map of properties, string->string. When a player runs "Examine X", you can check X to see if it has a "examine" property. Otherwise it's "nothing special".
You can then encode several other useful things. If the item has "get_message" you can't get the item, but the string is the message to print instead. If the item has a "text" property, then "Read X" works on it... and so on.
That's how you make the door -- it has a "get_message". Now that the door is an object, handling "Use key on door" is easy. It's a special case of "Use X on Y"...
Back in the 8/16bit days, you'd have a single routine for each verb. The routine would check the objects. So first off you check if the objects are "present" (held by the player or in the current location) and then you do; if ob1 = key and ob2 = door then unlock_door()
Unlock_door() would clear the "blocked" bit in that link and swap the "locked door" object for the "opened door" object. (by swapping their locations).
These days, if your language supports them, you may want a set of maps which map tuples of (verb_number, object) -> function and (verbnumber, obj, preposition, obj) -> function
Members - Reputation: 387
Posted 07 December 2012 - 02:51 AM
Probably not as big an issue now as it used to be but games can chug a bit if you get a particularly wild spin-up of objects going all at once. Make sure that room with 17 connections each implementing interfaces to 214 other objects gets de-allocated.
Members - Reputation: 127
Posted 07 December 2012 - 03:26 PM
Crossbones+ - Reputation: 4064
Posted 08 December 2012 - 12:55 PM
I made a post in another thread where I show an example of a simple text adventure structure. It basically defines Items, Rooms, and the Player, and you create the rooms and give it items. It's made to be updated, but it's a generic start.
BTW, in your code modules, it looks like you plan on using a linked list; why not use std::vector instead?
---(Old Blog, still has good info): 2dGameMaking
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)