package NET.worlds.scape; import java.io.IOException; import java.util.Enumeration; public class PitchDriver extends SwitchableBehavior implements KeyUpHandler, KeyDownHandler, FrameHandler, Persister, MomentumBehavior { protected float pitch_force; protected float pitch_vel; protected int lastTime; protected float maxdvpitch = 166.0F; protected float pitch_damp = -5.0F; protected float minpitch_vel = 3.0F; protected float pitch_key = 500.0F; private static Object cookie = new Object(); public void applyFrameForce(Event e) { if (e.receiver instanceof Pilot) { Camera cam = ((Pilot)e.receiver).getMainCamera(); int now = e.time; float dt = (now - this.lastTime) / 1000.0F; if (!(dt <= 0.0F)) { this.lastTime = now; float dTheta = this.pitch_vel * dt; float dv = 0.0F; if (this.pitch_force != 0.0F) { dv += this.pitch_force * dt; dv = this.maxdvpitch * (float)Math.atan(dv / this.maxdvpitch); this.pitch_vel += dv; dTheta += dv * dt; } if (dTheta != 0.0F) { Point3Temp axis = Point3Temp.make(); float angle = cam.lookAround.getSpin(axis); if (axis.x < 0.0F) { angle = -angle; } if (angle + dTheta > 90.0F) { dTheta = 90.0F - angle; } else if (angle + dTheta < -90.0F) { dTheta = -90.0F - angle; } cam.lookAround.spin(1.0F, 0.0F, 0.0F, dTheta); } this.pitch_vel = (float)(this.pitch_vel * Math.exp(this.pitch_damp * dt)); if (Math.abs(this.pitch_vel) < this.minpitch_vel) { this.pitch_vel = 0.0F; } } } } @Override public boolean handle(FrameEvent e) { this.applyFrameForce(e); return true; } public void resetPitch(Event e) { Camera cam = ((Pilot)e.receiver).getMainCamera(); cam.lookAround.makeIdentity(); } @Override public boolean handle(KeyDownEvent e) { float pitch; if (e.getKey() == '\ue321') { pitch = this.pitch_key; } else { if (e.getKey() != '\ue322') { if (e.getKey() == '\ue324') { this.resetPitch(e); } return true; } pitch = -this.pitch_key; } this.applyFrameForce(e); this.pitch_force = pitch; return true; } @Override public boolean handle(KeyUpEvent e) { if (e.getKey() == '\ue321' || e.getKey() == '\ue322') { this.applyFrameForce(e); this.pitch_force = 0.0F; } return true; } @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 = FloatPropertyEditor.make(new Property(this, index, "Pitch max acceleration")); } else if (mode == 1) { ret = new Float(this.maxdvpitch); } else if (mode == 2) { this.maxdvpitch = ((Float)value).intValue(); } break; case 1: if (mode == 0) { ret = FloatPropertyEditor.make(new Property(this, index, "Pitch rate damping")); } else if (mode == 1) { ret = new Float(this.pitch_damp); } else if (mode == 2) { this.pitch_damp = ((Float)value).intValue(); } break; case 2: if (mode == 0) { ret = FloatPropertyEditor.make(new Property(this, index, "Pitch min rate")); } else if (mode == 1) { ret = new Float(this.minpitch_vel); } else if (mode == 2) { this.minpitch_vel = ((Float)value).intValue(); } break; case 3: if (mode == 0) { ret = FloatPropertyEditor.make(new Property(this, index, "Pitch rate increment for up/down arrow keys")); } else if (mode == 1) { ret = new Float(this.pitch_key); } else if (mode == 2) { this.pitch_key = ((Float)value).intValue(); } break; default: ret = super.properties(index, offset + 12, mode, value); } return ret; } @Override public void saveState(Saver s) throws IOException { s.saveVersion(1, cookie); super.saveState(s); s.saveFloat(this.pitch_vel); s.saveFloat(this.maxdvpitch); s.saveFloat(this.pitch_damp); s.saveFloat(this.minpitch_vel); s.saveFloat(this.pitch_key); } @Override public void restoreState(Restorer r) throws IOException, TooNewException { switch (r.restoreVersion(cookie)) { case 0: r.setOldFlag(); super.restoreState(r); r.restoreFloat(); this.pitch_vel = r.restoreFloat(); this.maxdvpitch = r.restoreFloat(); this.pitch_damp = r.restoreFloat(); this.minpitch_vel = r.restoreFloat(); this.pitch_key = r.restoreFloat(); break; case 1: super.restoreState(r); this.pitch_vel = r.restoreFloat(); this.maxdvpitch = r.restoreFloat(); this.pitch_damp = r.restoreFloat(); this.minpitch_vel = r.restoreFloat(); this.pitch_key = r.restoreFloat(); break; default: throw new TooNewException(); } } @Override public void transferFrom(Enumeration oldMBs) { while (oldMBs.hasMoreElements()) { SuperRoot sr = (SuperRoot)oldMBs.nextElement(); if (sr instanceof PitchDriver) { PitchDriver pd = (PitchDriver)sr; this.pitch_vel = pd.pitch_vel; this.pitch_force = pd.pitch_force; this.lastTime = pd.lastTime; break; } } } }