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