// Returns true if move was made, otherwise false // Turns decoupled from moves public boolean processMove(int direction) { int[] nextPos; // IMPORTANT: ROW, COL not X, Y switch (direction) { case MainActivity.LEFT: nextPos = new int[] {currentPos[0], currentPos[1] - 1}; break; case MainActivity.UP: nextPos = new int[] {currentPos[0] - 1, currentPos[1]}; break; case MainActivity.RIGHT: nextPos = new int[] {currentPos[0], currentPos[1] + 1}; break; case MainActivity.DOWN: nextPos = new int[] {currentPos[0] + 1, currentPos[1]}; break; default: nextPos = new int[2]; break; } List<int[]> tiles = currentRoom.getTiles(); for (int i = 0; i < tiles.size(); i++) { int[] tile = tiles.get(i); if (tile[0] == nextPos[0] && tile[1] == nextPos[1]) { currentPos = nextPos; path.add(nextPos); lastDirection = direction; return true; } } List<Door> doors = currentRoom.getDoors(); for (int i = 0; i < doors.size(); i++) { Door door = doors.get(i); if (door.pos[0] == currentPos[0] && door.pos[1] == currentPos[1] && door.direction == direction) { // Log.e("Door", "ee"); currentRoom = rooms.get(door.destination); currentPos = nextPos; path.add(nextPos); lastDirection = direction; return true; } } return false; }
/** * Takes a loaded XML document and creates a new World out of it. At the moment loads NPCs but not * Player Characters. * * @param XMLFilePrefix * @param latestDate * @param df * @return */ public void restoreStateFromXML(Document doc) { Game.setWorld(new World()); NodeList wmessageList = doc.getElementsByTagName("welcome-message"); Element wmessageElement = (Element) wmessageList.item(0); NodeList wmessageChildList = wmessageElement.getChildNodes(); if (((Node) wmessageChildList.item(0)) != null) welcomeMessage = ((Node) wmessageChildList.item(0)).getNodeValue().trim(); NodeList gmessageList = doc.getElementsByTagName("goodbye-message"); Element gmessageElement = (Element) gmessageList.item(0); NodeList gmessageChildList = gmessageElement.getChildNodes(); if (((Node) gmessageChildList.item(0)) != null) goodbyeMessage = ((Node) gmessageChildList.item(0)).getNodeValue().trim(); // ------ LOAD ROOMS ------- // NodeList listOfRooms = doc.getElementsByTagName("room"); int totalRooms = listOfRooms.getLength(); if (Game.isDebug()) Game.logMessage("Number of rooms read: " + totalRooms); for (int s = 0; s < listOfRooms.getLength(); s++) { Node firstRoomNode = listOfRooms.item(s); if (firstRoomNode.getNodeType() == Node.ELEMENT_NODE) { Element firstRoomElement = (Element) firstRoomNode; // ------- NodeList nameList = firstRoomElement.getElementsByTagName("name"); Element nameElement = (Element) nameList.item(0); NodeList textNameList = nameElement.getChildNodes(); if (Game.isDebug()) Game.logMessage("Name : " + ((Node) textNameList.item(0)).getNodeValue().trim()); String thisName = ((Node) textNameList.item(0)).getNodeValue().trim(); // ------- NodeList descriptionList = firstRoomElement.getElementsByTagName("description"); Element descriptionElement = (Element) descriptionList.item(0); NodeList textDescList = descriptionElement.getChildNodes(); if (Game.isDebug()) Game.logMessage("Description : " + ((Node) textDescList.item(0)).getNodeValue().trim()); String thisDesc = ((Node) textDescList.item(0)).getNodeValue().trim(); Game.getWorld().getRooms().add(new Room(thisName, thisDesc)); // ------ LOAD DOORS ------- // List<Door> newDoors = Door.loadStateFromXML(firstRoomElement); // ------ LOAD STANDARD ITEMS ------- // List<Item> newItems = Item.loadStateFromXML(firstRoomElement); // ----- LOAD GCS -------------// List<GameCharacter> newGCs = GameCharacter.loadStateFromXML(firstRoomElement); // ----- LOAD NPCS -------------// // List<NPCharacter> newNPCs = NPCharacter.loadStateFromXML(firstRoomElement); // ------ LOAD MAP ITEMS ------- // List<Item> newMapItems = MapItem.loadStateFromXML(firstRoomElement); for (int k = 0; k < newDoors.size(); k++) Game.getWorld() .getRooms() .get(Game.getWorld().getRooms().size() - 1) .objectEntered(newDoors.get(k)); for (int k = 0; k < newItems.size(); k++) newItems .get(k) .moveTo(Game.getWorld().getRooms().get(Game.getWorld().getRooms().size() - 1)); for (int k = 0; k < newMapItems.size(); k++) newMapItems .get(k) .moveTo(Game.getWorld().getRooms().get(Game.getWorld().getRooms().size() - 1)); for (int k = 0; k < newGCs.size(); k++) newGCs.get(k).moveTo(Game.getWorld().getRooms().get(0)); } } for (Room room : Game.getWorld().getRooms()) { for (Door door : room.getDoors()) { if (!door.findTarget(Game.getWorld().getRooms())) { Game.logError( "Warning: Failed at door " + door.getName() + " in room " + room.getName() + " due to bad target name " + door.getTargetName(), null); // return null; } } } for (Room room : Game.getWorld().getRooms()) for (GameCharacter gc : room.getGameCharacters()) gc.lastRoomFromID(); /* // Bert as yet not in any save file List<Dialogue> convo2x = new ArrayList<Dialogue>(); convo2x.add(new Dialogue(1,"\"I will heal you.\"","heal 10 actor")); convo2x.add(new Dialogue(2,"\"You said Verb\"","shout dance!")); List<Dialogue> convo1x = new ArrayList<Dialogue>(); convo1x.add(new Dialogue(1,"\"You said Cheese\"")); convo1x.add(new Dialogue(2,"\"You said Apples\"")); List<Dialogue> convox = new ArrayList<Dialogue>(); convox.add(new Dialogue(1,"\"I am Bert, an NPC in this world.\"\n1 Cheese\n2 Apples", convo1x)); convox.add(new Dialogue(2,"\"What do you need?\"\n1 Healing, please.\n2 A verb, please.", convo2x)); newWorld.getRooms().get(1).objectEntered(new NPCharacter("Bert","A small green man.","\"Hi there!\"\n1 Tell me about yourself.\n2 Help me!","Farewell then!",convox)); */ }