我有一个基于文本的冒险游戏,玩家输入命令并在房间地图中移动。我遇到一个问题,无论输入什么命令,玩家当前所在的房间都保持不变。我的walk [Direction]命令有效,但是它不会更新它移动到的新房间的游戏模型,它仍然是当前房间。
我已经研究了侦听器和可观察对象,但是我不确定如何在游戏中实现它。我使用了JavaFXML。
这是解释玩家输入到GUI上文本字段中的输入的方法的一部分,它在控制界面的Controller类中:
void InterpretInput(KeyEvent e) throws NullPointerException {
if (e.getCode().equals(KeyCode.ENTER)) {
//setUpGame();
Output.clear();
String input = Input.getText();
Command c = CommandParser.parse(input);
//c.getCommandType();//is this reference to the first word >yes
//assert c != null;
//String arg = null;
if (c != null) {
String arg = c.getArg();
//}
// reference to second word?
//ct = CommandType.fromString(ct.toString());
try {
switch (c.getCommandType()) {
case WALK:
Room newRoom = curGame.WalkCommand(input, curPlayer);
if (newRoom == null) {
Output.appendText("There is no room in that direction!");
} else {
Output.appendText("You have now moved to " + newRoom.getName());
curPlayer.setCurrentRoom(newRoom);
}
Input.clear();
break;
case LOOK:
curRoom = curGame.LookCommand(input, curPlayer);
if (curRoom.getLightsOn())
Output.appendText("You are currently in room: " + curRoom.getName() + "Room Description : " + curRoom.getDescription());
else {
Output.appendText("The room is completely dark! Try finding a torch to light up the room to see your surroundings!");
}
Input.clear();
break;
在控制器类中,游戏也已设置:
Game curGame = new Game();
Room curRoom = curGame.getGizaRoom();
Player curPlayer = new Player(curRoom);
在Game类中,该类设置了游戏并具有命令的动作,walk命令看起来像这样:(部分)
public Room WalkCommand(String arg, Player curGame){
Room curRoom = curGame.getCurrentRoom();
Command c = CommandParser.parse(arg);
Direction d = Direction.fromString(c.getArg());
//Direction c = Direction.fromString();
;
Room newRoom;
switch (d) {
case EAST:
newRoom = goEast(curRoom);
curGame.setCurrentRoom(newRoom);
return newRoom;
public Room goEast(Room curRoom) {
Room newRoom;
newRoom = curRoom.getRoom(EAST);
if (newRoom != null) {
return newRoom;
}
else
return null;
}
我将如何使用侦听器或可观察列表来确保玩家在玩游戏时当前所在的房间得到更新?