summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/Sensor.java
blob: 8fb989dbecb6db4fbcf7fd8fa5eb950b51740271 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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);
         }
      }
   }
}