Key.java
jmud-0.11/src/Key.java · 2.6 KB
From the archive jmud-0_11.tgz
Download the archive « Back to the archive contents
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.NoSuchElementException;
import util.list.Vector;
import misc.Separators;
import integer.IntPair;
import string.StrUtil;
class Key extends Item {
// para poupar memoria, sera' o mesmo do prototipo...
// entao, nao podera' ser modificado;
// se modificacao for futuramente necessaria, as
// chaves terao de copiar os vetores de referencia a portas dos prototipos
// (uma outra solucao seria copiar apenas quando ha' alteracao)
private Vector doorReferences = null;
Key(KeyProto kp) {
super(kp);
doorReferences = kp.doorReferences;
}
protected void finalize() {
super.finalize();
}
int getType() {
return T_KEY;
}
// Pode esta chave abrir uma porta de uma dada sala?
boolean canUnlockDoor(Room rm, Door dr) {
int room = rm.getId();
int direction = dr.getDirection();
for (Enumeration enum = doorReferences.elements(); enum.hasMoreElements(); ) {
IntPair pair = (IntPair) enum.nextElement();
if (pair.getValue1() == room && pair.getValue2() == direction)
return true;
}
Room destRoom = dr.getDestinationRoom();
int destId = destRoom.getId();
int invDir = Door.getInverseDir(direction);
Door invDoor = null;
try {
if (destRoom.findDoorByDir(invDir).getDestinationRoom() == rm)
for (Enumeration enum = doorReferences.elements();
enum.hasMoreElements(); ) {
IntPair pair = (IntPair) enum.nextElement();
if (pair.getValue1() == destId && pair.getValue2() == invDir)
return true;
}
}
catch(NoSuchElementException e) {
}
return false;
}
/////////////
// Sheetable:
private String listDoors() {
String list = "";
for (Enumeration enum = doorReferences.elements(); enum.hasMoreElements(); ) {
IntPair pair = (IntPair) enum.nextElement();
list += Separators.NL + pair.getValue1() + " " + Door.getDirectionName(pair.getValue2());
}
return list;
}
public String getSheet() {
return super.getSheet() + Separators.NL + "Portas:" + StrUtil.wrapList(listDoors(), "Nenhuma");
}
//
/////////////
private void saveDoors(BufferedWriter writer) throws IOException {
for (Enumeration enum = doorReferences.elements(); enum.hasMoreElements(); ) {
IntPair pair = (IntPair) enum.nextElement();
writer.write((pair.getValue1() - ((KeyProto) prototype).savedZoneBase)+ " " + Door.getDirectionName(pair.getValue2()).charAt(0)); writer.newLine();
}
writer.write(Separators.EOR); writer.newLine();
}
void save(BufferedWriter writer) throws IOException {
super.save(writer);
saveDoors(writer);
}
}