diff options
| author | Fuwn <[email protected]> | 2026-02-12 22:33:32 -0800 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-02-12 22:33:32 -0800 |
| commit | c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9 (patch) | |
| tree | df9f48bf128a6c0186a8e91857d6ff30fe0e9f18 /NET/worlds/scape/GravityAction.java | |
| download | worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.tar.xz worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.zip | |
Initial commit
Diffstat (limited to 'NET/worlds/scape/GravityAction.java')
| -rw-r--r-- | NET/worlds/scape/GravityAction.java | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/NET/worlds/scape/GravityAction.java b/NET/worlds/scape/GravityAction.java new file mode 100644 index 0000000..f77e645 --- /dev/null +++ b/NET/worlds/scape/GravityAction.java @@ -0,0 +1,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(); + } + } +} |