|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectadventure.game.GameState
public class GameState
The GameState class is the heart of the adventure game. It contains code for setting up and managing the rooms and items, for handling user commands, and for updating the display.
The adventure game works on a three-tier system.
Students can add additional commands to the GameState, but they should do so only if that command is universal. If the command is specific to a room or item, it should be handled in the relevant room or item class.
In my design every room in the game is a separate class. Every room class will have exactly one room object created from it. Each room object has a name (as a String) that can be used to identify that room. Given a room name as a String, you can get that Room object by calling the appropriate method in this class.
Room objects get to process commands immediately after the GameState object. Only the current room object gets a chance to process the user's commands. Rooms are responsible for movement commands, and students should add other commands to rooms only if the command will change some state in that room (such as opening a door).
Room objects also have a method for redrawing the view. This is entirely optional.
In my design every item in the game is also a separate class. Every item class will have exactly item object created from it. Each item object has a name (as a String) that can be used to identify that item. Given a item name as a String, you can get that Item object by calling the appropriate method in this class.
Item objects process commands last. Only items in the current room and items in the user's inventory get a chance to process the user's commands.
Item objects are responsibile for handling interactions between items and describing and drawing themselves.
output.addText("Hello"); output.update();
| Field Summary | |
|---|---|
Room |
currentRoom
The current location of the player, as a Room object. |
OutputPanel |
output
Contains the OutputPanel object that corresponds to the text area of the GUI display. |
| Constructor Summary | |
|---|---|
GameState(javax.swing.JTextField input,
OutputPanel output)
The adventure game GameState constructor. |
|
| Method Summary | |
|---|---|
void |
actionPerformed(java.awt.event.ActionEvent arg0)
This is an event listener method that listens to the user's text field. |
void |
addNewItem(Item newItemObject,
java.lang.String roomName)
A method to add an item to the game. |
void |
addNewRoom(Room newRoomObject)
A method to add a room to the game. |
void |
draw(java.awt.Graphics g,
int windowWidth,
int windowHeight)
The method to draw the game. |
java.util.List<Item> |
getInventoryItems()
Returns a list of item objects that the player is carrying. |
Item |
getItem(java.lang.String itemName)
A method to get an Item (as an Item object) given its name. |
Room |
getLocation(Item item)
A method to get the location of an Item. |
Room |
getRoom(java.lang.String roomName)
A method to get a room (as a Room object) from its name. |
java.util.List<Item> |
getRoomItems(Room room)
Returns a list of item objects that exist in some particular room. |
void |
setCurrentRoom(Room newCurrentRoom)
A method to set the current room. |
void |
setCurrentRoom(java.lang.String roomName)
A method to set the current room. |
void |
setItemLocation(Item item,
Room room)
A method to set the location of an item. |
void |
setItemLocation(Item item,
java.lang.String roomName)
A method to set the location of an item. |
void |
setItemLocation(java.lang.String itemName,
Room room)
A method to set the location of an item. |
void |
setItemLocation(java.lang.String itemName,
java.lang.String roomName)
A method to set the location of an item. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public OutputPanel output
public Room currentRoom
| Constructor Detail |
|---|
public GameState(javax.swing.JTextField input,
OutputPanel output)
Students should not call this constructor directly (or create GameState objects), the GUI creates a GameState object when it is built.
input - the text field from which the user's input will come.output - the panel where all output text goes.| Method Detail |
|---|
public void actionPerformed(java.awt.event.ActionEvent arg0)
Students should not call this method directly.
actionPerformed in interface java.awt.event.ActionListenerarg0 - unused, ignoredpublic void addNewRoom(Room newRoomObject)
This method is currently only called from the GameState constructor, but it could be called at other times.
newRoomObject - the room to be added
public void addNewItem(Item newItemObject,
java.lang.String roomName)
newItemObject - the item to be addedroomName - the room in which the item is to be placedpublic void setCurrentRoom(java.lang.String roomName)
Use the name of the room (as a String) as the parameter to this method.
roomName - the name of the room that is to be set as the current roompublic void setCurrentRoom(Room newCurrentRoom)
Use a room object as the parameter to this method.
newCurrentRoom - the Room object that will become the current room
public void setItemLocation(java.lang.String itemName,
java.lang.String roomName)
itemName - the name of the itemroomName - the name of the room in which the item is to be placed
public void setItemLocation(java.lang.String itemName,
Room room)
itemName - the name of the itemroom - the room object in which the item is to be placed
public void setItemLocation(Item item,
java.lang.String roomName)
item - the item to be placed.roomName - the name of the room in which the item is to be placed.
public void setItemLocation(Item item,
Room room)
item - the item to be placedroom - the room in which the item is to be placedpublic java.util.List<Item> getRoomItems(Room room)
room - any room, including the inventory room
public java.util.List<Item> getInventoryItems()
public Room getRoom(java.lang.String roomName)
roomName - the name of the room requested
public Item getItem(java.lang.String itemName)
itemName - the name of the item
public Room getLocation(Item item)
item - the item in question
public void draw(java.awt.Graphics g,
int windowWidth,
int windowHeight)
g - the Graphics object to do the drawingwindowWidth - the width of the windowwindowHeight - the height of the window
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||