JavaRanch » Java Forums »
Java »
Game Development
| Author |
Locked door help
|
Martin vanPutten
Ranch Hand
Joined: Mar 26, 2006
Posts: 124
|
|
I'm building a neat little game where the user can type commands to go places and pick items up and whatnot. But I want to create a locked door into the construction room and only if the player picks up the key can they enter this key... otherwise the user cannot enter this room. I have many classes that all compile right and run great. Can anybody help me figure out how to create this locked room in construction room (to get there go west northeast northwest and northwest again). [ March 08, 2007: Message edited by: Bear Bibeault ]
|
Live And Let Learn.
|
 |
Gregg Bolinger
Sheriff
Joined: Jul 11, 2001
Posts: 15040
|
|
Well Martin, that is quite a bit of code and quite honestly, I can't imagine a lot of people wanting to traverse through all that. We all have limited time here. It would be better for us if you could pinpoint your issue a bit more and narrow the code we need to look at down quite a bit. Without looking at the code and just brainstorming here is what I might do. If I had an inventory class I might have 1 or more keys. A door would have a simple boolean property of locked = true|false. And maybe a property of which key it needs. When I encounter a locked door, a) check my inventory for said key and change boolean of locked or b) upon submitting a key to a locked door, change the boolean of locked. Only allow passage through a door if the locked boolean is false. Does that help?
|
My Blog | DZone Articles
|
 |
Justin Fox
Ranch Hand
Joined: Jan 24, 2006
Posts: 799
|
|
you can add certain properties to a player with the hashmap. if they find say "magical key" add it to their hash map / inventory. if the person comes across the door, search their person for the key if no key no get in lol. Justin
|
You down with OOP? Yeah you know me!
|
 |
Gregg Bolinger
Sheriff
Joined: Jul 11, 2001
Posts: 15040
|
|
Originally posted by Justin Fox: you can add certain properties to a player with the hashmap. if they find say "magical key" add it to their hash map / inventory. if the person comes across the door, search their person for the key if no key no get in lol. Justin
How is that different than what I suggested? :roll:
|
 |
Justin Fox
Ranch Hand
Joined: Jan 24, 2006
Posts: 799
|
|
i didnt read yours Justin
|
 |
Gregg Bolinger
Sheriff
Joined: Jul 11, 2001
Posts: 15040
|
|
Originally posted by Justin Fox: i didnt read yours Justin
Exactly.
|
 |
Gavin Tranter
Ranch Hand
Joined: Jan 01, 2007
Posts: 333
|
|
You could create a Lock class, the lock could have an ID, and a state (open or locked), a method to check the state and to set the state open or closed. (providing for both allows for in game events to lock the door again, like not being able to escape a room before a foe is defeated). Then you need a Key class with a matching ID. As others have suggested give the door a method to detected if the door is locked or not. This method is just a call though to a method in the Lock class that determines if the lock is open or closed. If the code detects that the player has the key with the right ID the door opens. (A basic search of all object of type Key in the users inventry.) The advantage of this method is that you can reuse the lock and key for any item, not just doors. Also it allows for doors to have different types of locks, perhaps having two keys. As locking a "thing" is rather generic. It could be implemented either using aggregation (as described above) or as an interface (Lockable), and some form of key interface (Keyable? ). Either method allows for different objects to be locked or to be given the ability to unlock items, for example a magic wand, you could lock the use of the magic wand until some other item is found, or a magic wand could be used to open certain items (doors, chests etc). Its probably a bit more code initially, but I think as your game grows it might prove more helpful in the long run. hope this helps G [ March 19, 2007: Message edited by: Gavin Tranter ] [ March 19, 2007: Message edited by: Gavin Tranter ]
|
 |
Gregg Bolinger
Sheriff
Joined: Jul 11, 2001
Posts: 15040
|
|
I like Gavin's approach. And don't forget about lock picking.
|
 |
Gavin Tranter
Ranch Hand
Joined: Jan 01, 2007
Posts: 333
|
|
Locks that can be picked?? hmmmm Oh, thanks I have set up some classes, and it seems to work quite well, obivously they they are a bit mickey (or mini if you prefer) mouse. [ March 19, 2007: Message edited by: Gavin Tranter ]
|
 |
 |
|
|
subject: Locked door help
|
|
|
|