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(); } } }