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

1    package j3d.cr325; 
2     
3    import com.sun.j3d.utils.applet.MainFrame; 
4    import com.sun.j3d.utils.universe.SimpleUniverse; 
5    import j3d.Utils; 
6     
7    import javax.media.j3d.*; 
8    import javax.vecmath.Point3d; 
9    import java.applet.Applet; 
10   import java.awt.*; 
11    
12   /** 
13    * Create a simple game, to shoot the students. 
14    */ 
15   public final class SimpleGame extends Applet { 
16    
17       private final BoundingSphere bsBounds 
18               = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); 
19       /** 
20        * Alpha used to drive the target animation.  
21        */ 
22       private final Alpha targetAlpha = new Alpha(-1, 0, 0, 3000, 0, 0); 
23       /** 
24        * Used to drive the bullet animation. 
25        */ 
26       private BulletBehavior bb = new BulletBehavior(); 
27    
28    
29       /** 
30        * Used to rotate the gun. 
31        */ 
32       private final TransformGroup gunTransformGroup = Shapes.getReadWriteTransformGroup(); 
33    
34       private BranchGroup getContentBranch() { 
35           final BranchGroup rootBranchGroup = new BranchGroup(); 
36           rootBranchGroup.addChild(Shapes.getTreeBranchGroup()); 
37           rootBranchGroup.addChild(Shapes.getHousesBranchGroup()); 
38           rootBranchGroup.addChild(Shapes.getBackgroundBranchGroup()); 
39           final Node theTarget = Target.getTargetGroup(); 
40           rootBranchGroup.addChild(theTarget); 
41           rootBranchGroup.addChild(Bullet.getBulletGroup(bb)); 
42           rootBranchGroup.addChild(new TargetBehavior(theTarget, 
43                   Target.getTargetSwitch(), 
44                   targetAlpha, 
45                   getBsBounds())); 
46           rootBranchGroup.addChild(new GunBehavior(bb, 
47                   gunTransformGroup, 
48                   getBsBounds())); 
49           rootBranchGroup.addChild(Shapes.getGunBranchGroup(gunTransformGroup)); 
50           final BoundingSphere bsBounds = getBsBounds(); 
51           Utils.addLights(bsBounds, rootBranchGroup); 
52           return rootBranchGroup; 
53       } 
54    
55       private SimpleGame() { 
56           setLayout(new BorderLayout()); 
57           final GraphicsConfiguration gc = 
58                   SimpleUniverse.getPreferredConfiguration(); 
59           final Canvas3D c3d 
60                   = new Canvas3D(gc); 
61           final Locale l 
62                   = new Locale(new VirtualUniverse()); 
63           l.addBranchGraph(Shapes.buildViewBranch(c3d)); 
64           l.addBranchGraph(getContentBranch()); 
65           setLayout(new BorderLayout()); 
66           add(BorderLayout.CENTER, c3d); 
67       } 
68    
69       /** 
70        * Ignore args. 
71        * 
72        * @param args ignored. 
73        */ 
74       public static void main(final String[] args) { 
75           new MainFrame(createSimpleGame(), 400, 400); 
76       } 
77    
78       private static SimpleGame createSimpleGame() { 
79           return new SimpleGame(); 
80       } 
81    
82       private BoundingSphere getBsBounds() { 
83           return bsBounds; 
84       } 
85    
86   } 
87