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

1    /** 
2     * Created by IntelliJ IDEA. 
3     * User: jgervasio 
4     * Date: Apr 20, 2004 
5     * Time: 12:23:04 PM 
6     * To change this template use Options | File Templates. 
7     */ 
8    package j3d.cr325.killTheStudent; 
9     
10   import com.sun.j3d.utils.universe.PlatformGeometry; 
11   import com.sun.j3d.utils.geometry.Cylinder; 
12   import com.sun.j3d.utils.geometry.Sphere; 
13    
14   import javax.media.j3d.*; 
15   import javax.vecmath.Color3f; 
16   import javax.vecmath.Vector3d; 
17   import javax.vecmath.Matrix3d; 
18   import javax.vecmath.Point3d; 
19    
20   import j3d.cr325.killTheStudent.MyLevittownDoomKeyboard; 
21    
22   public class TheGun { 
23    
24       static TransformGroup gunXfmGrp = getTransformGroup(); 
25       static TransformGroup aimXfmGrp = new TransformGroup(); 
26       static TransformGroup bulletXfmGrp = getTransformGroup(); 
27       static PositionInterpolator bulletPosInt = null; 
28       static Alpha ballAlpha = new Alpha(1, 0, 0, 500, 0, 0); 
29       static BranchGroup bullet = new BranchGroup(); 
30       private static BoundingSphere bounds 
31               = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); 
32    
33       public static PlatformGeometry buildGunPlatform() { 
34           PlatformGeometry pg = new PlatformGeometry(); 
35           BranchGroup theGun = new BranchGroup(); 
36           Appearance gunApp = new Appearance(); 
37           Color3f ambientColour = new Color3f(0.5f, 0.5f, 0.5f); 
38           Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f); 
39           Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f); 
40           Color3f diffuseColour = new Color3f(0.5f, 0.5f, 0.5f); 
41           float shininess = 20.0f; 
42           gunApp.setMaterial(new Material(ambientColour, emissiveColour, 
43                   diffuseColour, specularColour, shininess)); 
44    
45           TransformGroup barrel = new TransformGroup(); 
46    
47           Transform3D gunXfm = new Transform3D(); 
48           Transform3D barrelXfm = new Transform3D(); 
49           barrelXfm.set(new Vector3d(1, -2, 0)); 
50           barrel.setTransform(barrelXfm); 
51           Matrix3d gunXfmMat = new Matrix3d(); 
52           gunXfmMat.rotX(Math.PI / 2); 
53           gunXfm.set(gunXfmMat); 
54           gunXfmGrp.setTransform(gunXfm); 
55    
56           barrel.addChild(new Cylinder(0.3f, 7.0f, gunApp)); 
57           gunXfmGrp.addChild(barrel); 
58           theGun.addChild(gunXfmGrp); 
59    
60           //pg.addChild(aim()); 
61           pg.addChild(theGun); 
62           return pg; 
63       } 
64    
65       public static BranchGroup buildGun(MyLevittownDoomKeyboard myLevittownDoomKeyboard) { 
66           BranchGroup theGun = new BranchGroup(); 
67           Appearance bulletApp = new Appearance(); 
68           Color3f ambientColour = new Color3f(0.5f, 0.5f, 0.5f); 
69           Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f); 
70           Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f); 
71           Color3f diffuseColour = new Color3f(0.5f, 0.5f, 0.5f); 
72           float shininess = 20.0f; 
73           bulletApp.setMaterial(new Material(ambientColour, emissiveColour, 
74                   diffuseColour, specularColour, shininess)); 
75    
76           TransformGroup initTransformGroup = new TransformGroup(); 
77           TransformGroup barrel = new TransformGroup(); 
78    
79           //Transform3D gunXfm = new Transform3D(); 
80           Transform3D barrelXfm = new Transform3D(); 
81           barrelXfm.set(new Vector3d(1, 0, -5.5)); 
82           barrel.setTransform(barrelXfm); 
83           //Matrix3d gunXfmMat = new Matrix3d(); 
84           //gunXfmMat.rotX(Math.PI / 2); 
85           //gunXfm.set(gunXfmMat); 
86           //gunXfmGrp.setTransform(gunXfm); 
87           // lets try this.. 
88           initTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); 
89           initTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
90           myLevittownDoomKeyboard.keyNavBeh.setBulletTransformGroup(initTransformGroup); 
91    
92           gunXfmGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
93           //gunXfmGrp.addChild(new Box(1.0f, 1.0f, 0.5f, bulletApp)); 
94           barrel.addChild(new Sphere(0.2f, bulletApp)); 
95           gunXfmGrp.addChild(barrel); 
96           theGun.addChild(initTransformGroup); 
97           initTransformGroup.addChild(gunXfmGrp); 
98           return theGun; 
99       } 
100   
101      public static Sphere getBulletBall() { 
102          return bulletBall; 
103      } 
104   
105      private static Sphere bulletBall = null; 
106   
107      protected static BranchGroup getBulletGroup(MyLevittownDoomKeyboard myLevittownDoomKeyboard) { 
108          Appearance bulletAppearance = new Appearance(); 
109          Color3f ambientColour = new Color3f(1.0f, 0.0f, 0.0f); 
110          Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f); 
111          Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f); 
112          Color3f diffuseColour = new Color3f(1.0f, 0.0f, 0.0f); 
113          float shininess = 20.0f; 
114          bulletAppearance.setMaterial(new Material(ambientColour, emissiveColour, 
115                  diffuseColour, specularColour, shininess)); 
116   
117          bulletBall = new Sphere(0.2f, bulletAppearance); 
118   
119          TransformGroup ballMovXfmGrp = new TransformGroup(); 
120          ballMovXfmGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); 
121          ballMovXfmGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
122          bulletBall.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ); 
123          myLevittownDoomKeyboard.keyNavBeh.setBulletTransformGroup(ballMovXfmGrp); 
124   
125          ballMovXfmGrp.addChild(bulletBall); 
126          bullet.addChild(ballMovXfmGrp); 
127   
128          //bulletXfmGrp.addChild(bulletBall); 
129          //bullet.addChild(bulletXfmGrp); 
130   
131          return bullet; 
132   
133      } 
134   
135      private static TransformGroup getTransformGroup(){ 
136          TransformGroup tg = new TransformGroup(); 
137          tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); 
138          tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
139          tg.setCapability(TransformGroup.ALLOW_LOCAL_TO_VWORLD_READ); 
140          return tg; 
141      } 
142   
143      public static BranchGroup aim() { 
144          BranchGroup aim = new BranchGroup(); 
145          TransformGroup aimTg = new TransformGroup(); 
146   
147          Appearance aimAppearance = new Appearance(); 
148          Color3f ambientColour = new Color3f(1.0f, 0.0f, 1.0f); 
149          Color3f emissiveColour = new Color3f(0.0f, 0.0f, 1.0f); 
150          Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f); 
151          Color3f diffuseColour = new Color3f(1.0f, 0.0f, 1.0f); 
152          float shininess = 20.0f; 
153          aimAppearance.setMaterial(new Material(ambientColour, emissiveColour, 
154                  diffuseColour, specularColour, shininess)); 
155   
156          Sphere aimSphere = new Sphere(0.3f, aimAppearance); 
157   
158          Transform3D aimXfm = new Transform3D(); 
159          aimXfm.set(new Vector3d(0, 0, -50)); 
160          aimTg.setTransform(aimXfm); 
161          aimTg.addChild(aimSphere); 
162          aimXfmGrp.addChild(aimTg); 
163          aim.addChild(aimXfmGrp); 
164   
165          return aim; 
166      } 
167   
168       public static BranchGroup bullet() { 
169          BranchGroup bullet = new BranchGroup(); 
170          TransformGroup bulletTg = new TransformGroup(); 
171   
172          Appearance bulletAppearance = new Appearance(); 
173          Color3f ambientColour = new Color3f(1.0f, 0.0f, 1.0f); 
174          Color3f emissiveColour = new Color3f(0.0f, 0.0f, 1.0f); 
175          Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f); 
176          Color3f diffuseColour = new Color3f(1.0f, 0.0f, 1.0f); 
177          float shininess = 20.0f; 
178          bulletAppearance.setMaterial(new Material(ambientColour, emissiveColour, 
179                  diffuseColour, specularColour, shininess)); 
180   
181          Sphere bulletSphere = new Sphere(0.3f, bulletAppearance); 
182   
183          Transform3D bulletXfm = new Transform3D(); 
184          bulletXfm.set(new Vector3d(1, 0, -5.5)); 
185          bulletTg.setTransform(bulletXfm); 
186          bulletTg.addChild(bulletSphere); 
187          bulletXfmGrp.addChild(bulletTg); 
188          bullet.addChild(bulletXfmGrp); 
189          bulletPosInt = new PositionInterpolator(ballAlpha, 
190                  bulletTg, 
191                  new Transform3D(), 0.0f, 50.0f); 
192          bulletPosInt.setSchedulingBounds(bounds); 
193          bullet.addChild(bulletPosInt); 
194   
195          return bullet; 
196      } 
197      public static TransformGroup getGunXfmGrp() { 
198          return gunXfmGrp; 
199      } 
200   
201  } 
202