package NET.worlds.scape; import java.io.IOException; import java.util.Enumeration; import java.util.Vector; public class Sensor extends SuperRoot { protected Vector actions = new Vector(); private static Object classCookie = new Object(); public void addAction(Action o) { this.actions.addElement(o); } public void deleteAction(Action o) { this.actions.removeElement(o); } public void deleteActions() { while (this.actions.size() > 0) { this.deleteAction(this.actions.elementAt(0)); } } public Enumeration getActions() { return this.actions.elements(); } public int countActions() { return this.actions.size(); } public void trigger(Event event) { RunningActionHandler.trigger(this.actions, this.getWorld(), event); } @Override public Object properties(int index, int offset, int mode, Object value) throws NoSuchPropertyException { Object ret = null; switch (index - offset) { case 0: if (mode == 0) { ret = ObjectPropertyAdder.make(new VectorProperty(this, index, "Targets"), this.getRoot(), "NET.worlds.scape.Action"); } else if (mode == 1) { ret = this.actions.clone(); } else if (mode == 4) { this.deleteAction((Action)value); } else if (mode == 3) { this.addAction((Action)value); } break; default: ret = super.properties(index, offset + 1, mode, value); } return ret; } @Override public void saveState(Saver s) throws IOException { s.saveVersion(2, classCookie); super.saveState(s); s.saveVector(this.actions); } @Override public void restoreState(Restorer r) throws IOException, TooNewException { this.restoreStateVers(r); } protected int restoreStateVers(Restorer r) throws IOException, TooNewException { int vers = r.restoreVersion(classCookie); switch (vers) { case 1: case 2: super.restoreState(r); case 0: this.actions = r.restoreVectorActions(); return vers; default: throw new TooNewException(); } } @Override public void postRestore(int version) { super.postRestore(version); Enumeration en = this.getActions(); while (en.hasMoreElements()) { Action a = en.nextElement(); if (this.getOwner() != null && a.getOwner() == null) { System.out.println("Reparenting orphan action " + a.getName()); WObject w = (WObject)this.getOwner(); w.addAction(a); } } } }