/Users/lyon/j4p/src/j3d/cr325/killTheStudent/MyKeyBehavior.java

1    package j3d.cr325.killTheStudent; 
2     
3    import gui.run.RunJob; 
4     
5    import javax.media.j3d.*; 
6    import javax.vecmath.Vector3d; 
7    import java.awt.*; 
8    import java.awt.event.KeyEvent; 
9    import java.util.Enumeration; 
10    
11    
12   /** 
13    * MyKeyBehavior is a generic behavior class to take key presses and move a 
14    * TransformGroup through a Java3D scene. The actions resulting from the key strokes 
15    * are modified by using the Ctrl, Alt and Shift keys. 
16    * <p/> 
17    * (version 1.0) reconstructed class to make more generic. 
18    * <p/> 
19    * MODIFIED: 
20    * 
21    * @author Andrew AJ Cain, Swinburne University, Australia 
22    *         <acain@it.swin.edu.au> 
23    *         <p/> 
24    *         edited from code by: 
25    *         Gary S. Moss <moss@arl.mil> 
26    *         U. S. Army Research Laboratory 
27    *         <p/> 
28    *         CLASS NAME: 
29    *         MyKeyBehavior 
30    *         <p/> 
31    *         PUBLIC FEATURES: 
32    *         // Data 
33    *         <p/> 
34    *         // Constructors 
35    *         <p/> 
36    *         // Methods: 
37    *         <p/> 
38    *         COLLABORATORS: 
39    * @version 1.0, 25 September 1998 aajc 
40    */ 
41   public class MyKeyBehavior extends Behavior { 
42       protected static final double FAST_SPEED = 2.0; 
43       protected static final double NORMAL_SPEED = 1.0; 
44       protected static final double SLOW_SPEED = 0.1; 
45    
46       private TransformGroup viewTransformGroup; 
47       private Transform3D viewTransform3D; 
48       private WakeupCondition keyCriterion; 
49    
50       private final static double TWO_PI = (2.0 * Math.PI); 
51       private double eps = 32; 
52       private double rotateXAmount = Math.PI / eps; 
53       private double rotateYAmount = Math.PI / eps; 
54       private double rotateZAmount = Math.PI / eps; 
55    
56       private double moveRate = 0.1; 
57       private double speed = NORMAL_SPEED; 
58    
59       private int forwardKey = KeyEvent.VK_UP; 
60       private int backKey = KeyEvent.VK_DOWN; 
61       private int leftKey = KeyEvent.VK_LEFT; 
62       private int rightKey = KeyEvent.VK_RIGHT; 
63       private int spaceKey = KeyEvent.VK_SPACE; 
64    
65       TransformGroup gunTransformGroup = null; 
66       TransformGroup bulletTransformGroup = null; 
67       TransformGroup targetTransformGroup = null; 
68    
69       public void setTargetTransformGroup(TransformGroup targetTransformGroup) { 
70           this.targetTransformGroup = targetTransformGroup; 
71       } 
72    
73       public void setGunTransformGroup() { 
74           this.gunTransformGroup = TheGun.getGunXfmGrp(); 
75       } 
76    
77       public void setBulletTransformGroup(TransformGroup bulletTransformGroup) { 
78           this.bulletTransformGroup = bulletTransformGroup; 
79       } 
80    
81       public MyKeyBehavior(TransformGroup viewTransformGroup) { 
82           this.viewTransformGroup = viewTransformGroup; 
83           viewTransform3D = new Transform3D(); 
84    
85       } 
86    
87       public void initialize() { 
88           WakeupCriterion[] keyEvents = new WakeupCriterion[2]; 
89    
90           keyEvents[0] = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED); 
91           keyEvents[1] = new WakeupOnAWTEvent(KeyEvent.KEY_RELEASED); 
92           keyCriterion = new WakeupOr(keyEvents); 
93           wakeupOn(keyCriterion); 
94       } 
95    
96    
97       public void processStimulus(Enumeration criteria) { 
98           WakeupCriterion wakeup; 
99           AWTEvent[] event; 
100   
101          while (criteria.hasMoreElements()) { 
102              wakeup = (WakeupCriterion) criteria.nextElement(); 
103              if (!(wakeup instanceof WakeupOnAWTEvent)) 
104                  continue; 
105   
106              event = ((WakeupOnAWTEvent) wakeup).getAWTEvent(); 
107              for (int i = 0; i < event.length; i++) { 
108                  if (event[i].getID() == KeyEvent.KEY_PRESSED) { 
109                      processKeyEvent((KeyEvent) event[i]); 
110                  } 
111              } 
112          } 
113          wakeupOn(keyCriterion); 
114      } 
115   
116      protected void processKeyEvent(KeyEvent ke) { 
117   
118          int keycode = ke.getKeyCode(); 
119   
120          if (ke.isShiftDown()) 
121              speed = FAST_SPEED; 
122          else 
123              speed = NORMAL_SPEED; 
124          if (ke.isAltDown()) 
125              altMove(keycode); 
126          else if (ke.isControlDown()) 
127              controlMove(keycode); 
128          else 
129              standardMove(keycode); 
130          //moveGun(); 
131      } 
132   
133   
134      //moves forward backward or rotates left right 
135      private void standardMove(int kc) { 
136          System.out.println(kc); 
137          if (kc == forwardKey) 
138              moveForward(); 
139          else if (kc == backKey) 
140              moveBackward(); 
141          else if (kc == leftKey) 
142              rotLeft(); 
143          else if (kc == rightKey) 
144              rotRight(); 
145          else if (kc == spaceKey) 
146              shoot(); 
147      } 
148   
149      //moves left right, rotate up down 
150      protected void altMove(int keycode) { 
151          if (keycode == forwardKey) 
152              rotUp(); 
153          else if (keycode == backKey) 
154              rotDown(); 
155          else if (keycode == leftKey) 
156              moveLeft(); 
157          else if (keycode == rightKey) 
158              moveRight(); 
159      } 
160   
161      //move up down, rot left right 
162      protected void controlMove(int keycode) { 
163          if (keycode == forwardKey) 
164              moveUp(); 
165          else if (keycode == backKey) 
166              moveDown(); 
167          else if (keycode == leftKey) 
168              rollLeft(); 
169          else if (keycode == rightKey) 
170              rollRight(); 
171      } 
172   
173      private void moveForward() { 
174          doMove(new Vector3d(0.0, 0.0, -getMovementRate())); 
175      } 
176   
177      private void moveBackward() { 
178          doMove(new Vector3d(0.0, 0.0, getMovementRate())); 
179      } 
180   
181      private void moveLeft() { 
182          doMove(new Vector3d(-getMovementRate(), 0.0, 0.0)); 
183      } 
184   
185      private void moveRight() { 
186          doMove(new Vector3d(getMovementRate(), 0.0, 0.0)); 
187      } 
188   
189      private void moveUp() { 
190          doMove(new Vector3d(0.0, getMovementRate(), 0.0)); 
191      } 
192   
193      private void moveDown() { 
194          doMove(new Vector3d(0.0, -getMovementRate(), 0.0)); 
195      } 
196   
197      protected void rotRight() { 
198          doRotateY(getRotateRightAmount(), getRotateRightAmount()); 
199      } 
200   
201      protected void rotUp() { 
202          doRotateX(getRotateUpAmount(), getRotateUpAmount()); 
203      } 
204   
205      protected void rotLeft() { 
206          doRotateY(getRotateLeftAmount(), getRotateLeftAmount()); 
207      } 
208   
209      protected void rotDown() { 
210          doRotateX(getRotateDownAmount(), getRotateDownAmount()); 
211      } 
212   
213      protected void rollLeft() { 
214          doRotateZ(getRollLeftAmount(), getRollLeftAmount()); 
215      } 
216   
217      protected void rollRight() { 
218          doRotateZ(getRollRightAmount(), getRollRightAmount()); 
219      } 
220   
221      private void shoot() { 
222          MyLevittownDoomKeyboard.joeSwitch.setWhichChild(0); 
223          Transform3D tempT3d = new Transform3D(); 
224          viewTransformGroup.getTransform(tempT3d); 
225          Vector3d v3d = new Vector3d(); 
226          tempT3d.get(v3d); 
227          //System.out.println("v3d"+v3d); 
228          bulletTransformGroup.setTransform(tempT3d); 
229          i = .6; 
230          new RunJob(0.01, false, 75) { 
231              public void run() { 
232                  bullet(); 
233                  if (closeToTarget()) { 
234                      System.out.println("target HIT"); 
235                      MyLevittownDoomKeyboard.joeSwitch.setWhichChild(1); 
236                  } 
237   
238              } 
239          }; 
240      } 
241   
242      protected void doRotateY(double radians, double theMove) { 
243          viewTransformGroup.getTransform(viewTransform3D); 
244          Transform3D toMove = new Transform3D(); 
245          toMove.rotY(radians); 
246          viewTransform3D.mul(toMove); 
247          viewTransformGroup.setTransform(viewTransform3D); 
248   
249      } 
250   
251      protected void doRotateX(double radians, double theMove) { 
252          viewTransformGroup.getTransform(viewTransform3D); 
253          Transform3D toMove = new Transform3D(); 
254          toMove.rotX(radians); 
255          viewTransform3D.mul(toMove); 
256          viewTransformGroup.setTransform(viewTransform3D); 
257      } 
258   
259      protected void doRotateZ(double radians, double theMove) { 
260          viewTransformGroup.getTransform(viewTransform3D); 
261          Transform3D toMove = new Transform3D(); 
262          toMove.rotZ(radians); 
263          viewTransform3D.mul(toMove); 
264          viewTransformGroup.setTransform(viewTransform3D); 
265      } 
266   
267      protected void doMove(Vector3d theMove) { 
268          viewTransformGroup.getTransform(viewTransform3D); 
269          Transform3D theMoveTransform3d = new Transform3D(); 
270          theMoveTransform3d.setTranslation(theMove); 
271          viewTransform3D.mul(theMoveTransform3d); 
272          // this is where you need to set the gun 
273          // transform group too: 
274          // gunTransformGroup.setTransform(transform3D); 
275          viewTransformGroup.setTransform(viewTransform3D); 
276   
277      } 
278   
279      private double i = .6; 
280   
281      private void bullet() { 
282          Toolkit.getDefaultToolkit().beep(); 
283          Transform3D tempT3d = new Transform3D(); 
284          bulletTransformGroup.getTransform(tempT3d); 
285          Transform3D toMove = new Transform3D(); 
286          Vector3d bulletMove = new Vector3d(i, 1, 1); 
287          if (i > 0) 
288              i = i - .2; 
289   
290          bulletMove.add(new Vector3d(0, -1, -2)); 
291          toMove.setTranslation(bulletMove); 
292   
293          tempT3d.mul(toMove); 
294          bulletTransformGroup.setTransform(tempT3d); 
295      } 
296   
297      private boolean closeToTarget() { 
298          double sqLen = getBulletTargetDistance(); 
299          if (sqLen < 2) return true; 
300          return false; 
301      } 
302   
303      private double getBulletTargetDistance() { 
304          Vector3d targetVector = new Vector3d();   // location of target 
305          Vector3d bulletVector = new Vector3d(); 
306          Transform3D bulletTransform = new Transform3D(); 
307          TheGun.getBulletBall().getLocalToVworld(bulletTransform); 
308          bulletTransform.get(bulletVector); 
309          //System.out.print("bv "+bulletVector + ","); 
310          Transform3D targetTransform = new Transform3D(); 
311          j3d.cr325.killTheStudent.Utils.getSphere().getLocalToVworld(targetTransform); 
312          targetTransform.get(targetVector); 
313          //System.out.println(targetVector); 
314          bulletVector.sub(targetVector); 
315          //System.out.println(bulletVector); 
316          double sqLen = Math.sqrt(bulletVector.lengthSquared()); 
317          //System.out.println(sqLen); 
318          return sqLen; 
319      } 
320   
321   
322      protected double getMovementRate() { 
323          return moveRate * speed; 
324      } 
325   
326      protected double getRollLeftAmount() { 
327          return rotateZAmount * speed; 
328      } 
329   
330      protected double getRollRightAmount() { 
331          return -rotateZAmount * speed; 
332      } 
333   
334      protected double getRotateUpAmount() { 
335          return rotateYAmount * speed; 
336      } 
337   
338      protected double getRotateDownAmount() { 
339          return -rotateYAmount * speed; 
340      } 
341   
342      protected double getRotateLeftAmount() { 
343          return rotateYAmount * speed; 
344      } 
345   
346      protected double getRotateRightAmount() { 
347          return -rotateYAmount * speed; 
348      } 
349   
350      public void setRotateXAmount(double radians) { 
351          rotateXAmount = radians; 
352      } 
353   
354      public void setRotateYAmount(double radians) { 
355          rotateYAmount = radians; 
356      } 
357   
358      public void setRotateZAmount(double radians) { 
359          rotateZAmount = radians; 
360      } 
361   
362      public void setMovementRate(double meters) { 
363          moveRate = meters; // Travel rate in meters/frame 
364      } 
365   
366      public void setForwardKey(int key) { 
367          forwardKey = key; 
368      } 
369   
370      public void setBackKey(int key) { 
371          backKey = key; 
372      } 
373   
374      public void setLeftKey(int key) { 
375          leftKey = key; 
376      } 
377   
378      public void setRightKey(int key) { 
379          rightKey = key; 
380      } 
381  } 
382