summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/GravityAction.java
blob: f77e6458cc5f72a26a5ba6686759a52a769a08f3 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package NET.worlds.scape;

import NET.worlds.core.Std;
import java.io.IOException;

public class GravityAction extends Action {
   public float cycleTime = 0.0F;
   int startTime;
   public int force = 300;
   public float xDest = 0.0F;
   public float yDest = 0.0F;
   public float zDest = 0.0F;
   static final float epsilon = 2.0F;
   int lastFrameTime;
   float initialDistance = 0.0F;
   Point3 initialPoint;
   protected boolean gravityEnd = true;
   private static Object classCookie = new Object();

   public void startGravity() {
      this.startTime = Std.getRealTime();
      this.gravityEnd = false;
      this.lastFrameTime = 0;
   }

   private float distance(float tx, float ty, float tz, float cx, float cy, float cz) {
      float dx = tx - cx;
      float dy = ty - cy;
      float dz = tz - cz;
      return (float)Math.sqrt(dx * dx + dy * dy + dz * dz);
   }

   public void doGravity(Pilot pilotObject, WObject o) {
      int currentFrameTime = Std.getRealTime();
      float r = this.distance(pilotObject.getX(), pilotObject.getY(), pilotObject.getZ(), o.getX(), o.getY(), o.getZ());
      if (this.lastFrameTime == 0) {
         this.lastFrameTime = currentFrameTime;
      }

      float distanceTraveled = this.distance(
         this.initialPoint.x, this.initialPoint.y, this.initialPoint.z, pilotObject.getX(), pilotObject.getY(), pilotObject.getZ()
      );
      this.initialDistance = this.distance(this.initialPoint.x, this.initialPoint.y, this.initialPoint.z, o.getX(), o.getY(), o.getZ());
      if (r > 2.0F && distanceTraveled <= this.initialDistance) {
         float dx = o.getX() - pilotObject.getX();
         if (dx != 0.0) {
            float new_dx = (currentFrameTime - this.lastFrameTime) * this.force * dx / (r * r);
            if (Math.abs(new_dx) < Math.abs(dx)) {
               dx = new_dx;
            }
         }

         float dy = o.getY() - pilotObject.getY();
         if (dy != 0.0) {
            float new_dy = (currentFrameTime - this.lastFrameTime) * this.force * dy / (r * r);
            if (Math.abs(new_dy) < Math.abs(dy)) {
               dy = new_dy;
            }
         }

         float dz = o.getZ() - pilotObject.getZ();
         if (dz != 0.0) {
            float new_dz = (currentFrameTime - this.lastFrameTime) * this.force * dz / (r * r);
            if (Math.abs(new_dz) < Math.abs(dz)) {
               dz = new_dz;
            }
         }

         pilotObject.moveBy(dx, dy, dz);
      } else {
         pilotObject.moveTo(o.getX(), o.getY(), o.getZ());
         this.gravityEnd = true;
      }

      this.lastFrameTime = currentFrameTime;
   }

   @Override
   public Persister trigger(Event e, Persister seqID) {
      Object owner = this.getOwner();
      if (owner != null && owner instanceof WObject) {
         WObject o = (WObject)owner;
         Pilot pilot = Pilot.getActive();
         if (pilot == null) {
            return null;
         } else if (pilot.getRoom() != o.getRoom()) {
            if (this.gravityEnd && pilot instanceof HoloPilot) {
               HoloPilot hp = (HoloPilot)pilot;
               hp.returnSmoothDriver();
            }

            return null;
         } else {
            if (this.gravityEnd) {
               this.startGravity();
               if (pilot instanceof HoloPilot) {
                  HoloPilot hp = (HoloPilot)pilot;
                  hp.removeSmoothDriver();
                  this.initialPoint = new Point3(pilot.getPosition());
                  this.initialDistance = this.distance(this.initialPoint.x, this.initialPoint.y, this.initialPoint.z, o.getX(), o.getY(), o.getZ());
               }
            }

            this.doGravity(pilot, o);
            if (this.gravityEnd) {
               if (pilot instanceof HoloPilot) {
                  HoloPilot hp = (HoloPilot)pilot;
                  hp.returnSmoothDriver();
               }

               return null;
            } else {
               return this;
            }
         }
      } else {
         return null;
      }
   }

   @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 = IntegerPropertyEditor.make(new Property(this, index, "Force"));
            } else if (mode == 1) {
               ret = new Integer(this.force);
            } else if (mode == 2) {
               this.force = (Integer)value;
            }
            break;
         default:
            ret = super.properties(index, offset + 1, mode, value);
      }

      return ret;
   }

   @Override
   public String toString() {
      return super.toString() + "[Force " + this.force + "]";
   }

   @Override
   public void saveState(Saver s) throws IOException {
      s.saveVersion(4, classCookie);
      super.saveState(s);
      s.saveInt(this.force);
   }

   @Override
   public void restoreState(Restorer r) throws IOException, TooNewException {
      switch (r.restoreVersion(classCookie)) {
         case 0:
            super.restoreState(r);
            r.restore();
            this.cycleTime = r.restoreFloat();
            this.force = r.restoreInt();
            break;
         case 1:
            super.restoreState(r);
            this.cycleTime = r.restoreFloat();
            this.force = r.restoreInt();
            break;
         case 2:
            super.restoreState(r);
            this.cycleTime = r.restoreFloat();
            this.force = r.restoreInt();
            this.xDest = r.restoreFloat();
            this.yDest = r.restoreFloat();
            break;
         case 3:
            super.restoreState(r);
            this.force = r.restoreInt();
            this.xDest = r.restoreFloat();
            this.yDest = r.restoreFloat();
            this.zDest = r.restoreFloat();
            break;
         case 4:
            super.restoreState(r);
            this.force = r.restoreInt();
            break;
         default:
            throw new TooNewException();
      }
   }
}