diff options
Diffstat (limited to 'NET/worlds/scape/Sensor.java')
| -rw-r--r-- | NET/worlds/scape/Sensor.java | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/NET/worlds/scape/Sensor.java b/NET/worlds/scape/Sensor.java new file mode 100644 index 0000000..8fb989d --- /dev/null +++ b/NET/worlds/scape/Sensor.java @@ -0,0 +1,99 @@ +package NET.worlds.scape; + +import java.io.IOException; +import java.util.Enumeration; +import java.util.Vector; + +public class Sensor extends SuperRoot { + protected Vector<Action> actions = new Vector<Action>(); + 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<Action> 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<Action> 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); + } + } + } +} |