package NET.worlds.scape; public class HandsOffDriver extends SwitchableBehavior implements FrameHandler, AnimatedActionHandler, KeyDownHandler, KeyUpHandler { Point2 _destPoint; float _destYaw; float _velocity; int _lastFrameTime = 0; AnimatedActionHandlerImp handler; int _state = 0; Transform target = null; float cameraPan = 0.0F; float cameraZoom = 0.0F; int cameraPanning = 0; int cameraZooming = 0; static final float cameraPanRate = 45.0F; static final float cameraZoomRate = 100.0F; static final int noState = 0; static final int turnToDestination = 1; static final int moveToDestination = 2; static final int faceDestination = 3; public HandsOffDriver() { this.handler = new AnimatedActionHandlerImp(); } public float getCameraPan() { return this.cameraPan; } public float getCameraZoom() { return this.cameraZoom; } public void setTarget(Transform t) { this.target = t; } @Override public void addCallback(AnimatedActionCallback c) { this.handler.addCallback(c); } @Override public void removeCallback(AnimatedActionCallback c) { this.handler.removeCallback(c); } @Override public void notifyCallbacks(int completionCode) { this.handler.notifyCallbacks(completionCode); } public void setDestPos(Point2 point, float yaw, float velocity) { if (point == null) { System.out.println("Point is null"); } this._destPoint = point; this._destYaw = yaw; this._velocity = velocity; this._state = 1; } @Override public boolean handle(FrameEvent e) { int now = e.time; float dt = (now - this._lastFrameTime) / 1000.0F; this._lastFrameTime = now; this.cameraZoom = this.cameraZoom + dt * 100.0F * this.cameraZooming; this.cameraPan = this.cameraPan + dt * 45.0F * this.cameraPanning; Transform tgt; if (this.target == null) { if (!(e.receiver instanceof Pilot)) { System.out.println("Receiver not pilot..."); return true; } Pilot pilot = (Pilot)e.receiver; if (!pilot.isActive()) { System.out.println("Pilot not active..."); return true; } tgt = pilot; } else { tgt = this.target; } if (dt <= 0.0F) { System.out.println("Negative dt"); return true; } else { if (dt > 0.33F) { dt = 0.33F; } this.moveObject(tgt, dt); return true; } } private void yawLevel(Transform pilot, float theta) { float currentYaw = pilot.getYaw(); Point3Temp currentSpinAxis = Point3Temp.make(); float currentSpin = pilot.getSpin(currentSpinAxis); Point3Temp currentPosition = pilot.getPosition(); pilot.makeIdentity().moveTo(currentPosition).yaw(-theta); pilot.yaw(currentYaw); pilot.spin(currentSpinAxis, currentSpin); } private void moveObject(Transform pilot, float dt) { if (pilot != null && this._destPoint != null) { switch (this._state) { case 1: { Point3Temp dest = Point3Temp.make(this._destPoint.x, this._destPoint.y, pilot.getZ()); Point3Temp src = pilot.getPosition(); double destYaw = Math.atan2(dest.y - src.y, dest.x - src.x); destYaw = (Math.PI / 2) - destYaw; destYaw *= 180.0 / Math.PI; this.yawLevel(pilot, (float)destYaw); this._state = 2; break; } case 2: { Point3Temp dest = Point3Temp.make(this._destPoint.x, this._destPoint.y, pilot.getZ()); Point3Temp pos = pilot.getPosition(); float cm = dt * this._velocity; Point3Temp dP = Point3Temp.make(dest); dP.minus(pilot.getPosition()); if (dP.length() <= cm) { pilot.moveTo(dest); this._state = 3; } else { dP.normalize(); pilot.moveBy(dP.times(cm)); } break; } case 3: this.yawLevel(pilot, this._destYaw); this._state = 0; this.notifyCallbacks(0); } } } @Override public boolean handle(KeyDownEvent e) { if (e.key == '\ue326') { this.cameraZooming = -1; } else if (e.key == '\ue328') { this.cameraZooming = 1; } else if (e.key == '\ue325') { this.cameraPanning = -1; } else if (e.key == '\ue327') { this.cameraPanning = 1; } return true; } @Override public boolean handle(KeyUpEvent e) { if (e.key == '\ue326' || e.key == '\ue328') { this.cameraZooming = 0; } else if (e.key == '\ue325' || e.key == '\ue327') { this.cameraPanning = 0; } else if (e.key == '\ue31b') { this.notifyCallbacks(0); } return true; } }