/Users/lyon/j4p/src/j3d/cr325/GunBehavior.java

1    package j3d.cr325; 
2     
3    import javax.media.j3d.*; 
4    import javax.vecmath.Matrix3d; 
5    import javax.vecmath.Vector3d; 
6    import java.awt.*; 
7    import java.awt.event.KeyEvent; 
8    import java.util.Enumeration; 
9     
10   class GunBehavior extends Behavior { 
11       /** 
12        * The separate criteria that trigger this behaviour 
13        */ 
14       protected WakeupCriterion theCriterion; 
15       /** 
16        * The alpha that is used to 'fire' the ball 
17        */ 
18       protected Alpha theGunAlpha; 
19       /** 
20        * Used to animate the ball 
21        */ 
22       protected PositionInterpolator theInterpolator; 
23       /** 
24        * Used to calculate the current direction of the gun 
25        */ 
26       protected int aim = 0; 
27       /** 
28        * This is used to rotate the gun 
29        */ 
30       protected TransformGroup aimXfmGrp; 
31       protected TransformGroup viewTransformGroup; 
32       /** 
33        * Used to aim the ball 
34        */ 
35       protected Matrix3d aimShotMat = new Matrix3d(); 
36       /** 
37        * Used to aim the gun 
38        */ 
39    
40       protected Matrix3d aimGunMat = new Matrix3d(); 
41       protected Matrix3d viewMat = new Matrix3d(); 
42       /** 
43        * Used to define the ball's direction 
44        */ 
45       protected Transform3D aimShotXfm = new Transform3D(); 
46       /** 
47        * Used to define the gun's direction 
48        */ 
49       protected Transform3D aimGunXfm = new Transform3D(); 
50       protected Transform3D viewTransform = new Transform3D(); 
51    
52       /** 
53        * Set up the data for the behaviour. 
54        * 
55        * @param a1        Alpha that drives the ball's animation. 
56        * @param pi        PositionInterpolator used for the ball. 
57        * @param gunRotGrp TransformGroup that is used to rotate the gun. 
58        * @param theBounds Bounds that define the active region for this behaviour. 
59        */ 
60       public GunBehavior(BulletBehavior bb, 
61                          TransformGroup gunRotGrp, Bounds theBounds) { 
62           theGunAlpha = bb.getAlpha(); 
63           theInterpolator = bb.getBulletInterpolator(); 
64           setSchedulingBounds(theBounds); 
65           aimXfmGrp = gunRotGrp; 
66       } 
67    
68       /** 
69        * This sets up the criteria for triggering the behaviour. 
70        * We simple want to wait for a key to be pressed. 
71        */ 
72       public void initialize() { 
73           theCriterion = new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED); 
74           wakeupOn(theCriterion); 
75       } 
76    
77       /** 
78        * left key cursor rotate left, right cursor key 
79        * rotate right, spacebar fire. 
80        * 
81        * @param criteria 
82        */ 
83       public void processStimulus(Enumeration criteria) { 
84           while (criteria.hasMoreElements()) { 
85               processInput(criteria); 
86           } 
87           wakeupOn(theCriterion); 
88       } 
89    
90       private void processInput(Enumeration criteria) { 
91           WakeupCriterion theCriterion = (WakeupCriterion) criteria.nextElement(); 
92           if (theCriterion instanceof WakeupOnAWTEvent) { 
93               AWTEvent[] triggers = ((WakeupOnAWTEvent) theCriterion).getAWTEvent(); 
94               //Check if it's a keyboard event 
95               if (triggers[0] instanceof KeyEvent) { 
96                   processKeyEvent(triggers); 
97               } 
98           } 
99       } 
100   
101      private void processKeyEvent(AWTEvent[] triggers) { 
102          int keyPressed = ((KeyEvent) triggers[0]).getKeyCode(); 
103          if (keyPressed == KeyEvent.VK_LEFT) { 
104              left(); 
105          } else if (keyPressed == KeyEvent.VK_RIGHT) { 
106              right(); 
107   
108          } else if (keyPressed == KeyEvent.VK_UP) { 
109              //up(); 
110   
111          } else if (keyPressed == KeyEvent.VK_DOWN) { 
112              //down(); 
113          } else if (keyPressed == KeyEvent.VK_SPACE) { 
114              //It's the spacebar so reset the start time 
115              //of the ball's animation 
116              theGunAlpha.setStartTime(System.currentTimeMillis()); 
117          } 
118      } 
119   
120      int x = 0; 
121      private void up() { 
122          translateGun(x); 
123          x++; 
124      } 
125   
126      private void down(){ 
127          translateGun(x); 
128          x--; 
129      } 
130   
131   
132      private void left() { 
133          //It's a left key so move the turret 
134          //and the aim of the gun left unless 
135          //we're at our maximum angle 
136          if (aim < 8) 
137              aim += 1; 
138          rotateGun(); 
139      } 
140   
141      private void translateGun(int x){ 
142          aimShotMat.add(x); 
143          aimGunMat.add(x); 
144          final Vector3d v3d = new Vector3d(x,0,0); 
145          aimShotXfm.setTranslation(v3d); 
146          aimGunXfm.setTranslation(v3d); 
147          aimXfmGrp.setTransform(aimGunXfm); 
148          theInterpolator.setTransformAxis(aimShotXfm); 
149      } 
150   
151      private void rotateGun() { 
152          aimShotMat.rotY(((aim / 32.0) + 0.5) * Math.PI); 
153          aimGunMat.rotZ(((aim / -32.0)) * Math.PI); 
154          aimShotXfm.setRotation(aimShotMat); 
155          aimGunXfm.setRotation(aimGunMat); 
156          aimXfmGrp.setTransform(aimGunXfm); 
157          theInterpolator.setTransformAxis(aimShotXfm); 
158      } 
159   
160      private void right() { 
161          //It's the right key so do the same but rotate right 
162          if (aim > -8) 
163              aim -= 1; 
164          rotateGun(); 
165      } 
166  } 
167   
168   
169