/** * update is an overwritten method of Observer, used in reaction to a change happening in an * observed class. In this case it updates the instance variable HashMap itemMap, removing * references to the Item at the old location, and adding it's reference to the new location. It * also updates the locations that the Player has visited, adding the most recent Location first * in the linked list. Note: more objects than Items and Player could be added later * * @param o : an Observable object (which should be an GameObject object of some sort) * @param arg : an Object (which should be a Location object) */ public void update(Observable o, Object arg) { /** Item update code* */ if (o instanceof Item && arg instanceof Integer) { Item item = (Item) o; int oldLocation = (Integer) arg; int newLocation = item.getObjectLocation(); // remove old item from array list ArrayList<Item> newList = itemMap.get((Integer) oldLocation); if (newList != null) { // For loop while there is another element for (Iterator<Item> it = newList.iterator(); it.hasNext(); ) { if (item.getObjectID() == it.next().getObjectID()) { it.remove(); } // End if matching id } // End for loop // Update the item map with the new status with the item removed itemMap.put((Integer) oldLocation, newList); } // if list not null // If not 'destroyed', which is sent to location 9999 if (!(newLocation == 9999)) { // add the item to the new location's array list if (!(itemMap.containsKey((Integer) newLocation))) { newList = new ArrayList<Item>(); newList.add(item); itemMap.put((Integer) newLocation, newList); } else { newList = itemMap.get((Integer) newLocation); // not checking for duplicates yet, add the item to the list newList.add(item); // Update the item map with the new status itemMap.put((Integer) newLocation, newList); } // End contains key } // End if not destroyed } // End if valid instances of Item and Integer /** Player update code* */ if (o instanceof Player && arg instanceof Integer) { Player player = (Player) o; int location = player.getObjectLocation(); // Add new locations the player visits Last in the linked list, // which makes last element always the current playerList.addLast(location); } // End if valid instances of Player and Integer }