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

1    package j3d.cr325; 
2     
3    import javax.media.j3d.*; 
4    import java.util.Enumeration; 
5     
6    /** 
7     * This is used in the SimpleGame application. 
8     * It defines the behaviour for the duck, which is the 
9     * target in the shooting game.  If something collides 
10    * with the duck, it swaps a switch value to 'kill' the duck 
11    * The duck is revived when it's alpha value passes through zero. 
12    * 
13    * @author I.J.Palmer 
14    * @version 1.0 
15    */ 
16   public class TargetBehavior extends Behavior { 
17       /** 
18        * The shape that is being watched for collisions. 
19        */ 
20       protected Node collidingShape; 
21       /** 
22        * The separate criteria that trigger this behaviour 
23        */ 
24       protected WakeupCriterion[] theCriteria; 
25       /** 
26        * The result of the 'OR' of the separate criteria 
27        */ 
28       protected WakeupOr oredCriteria; 
29       /** 
30        * The switch that is used to swap the duck shapes 
31        */ 
32       protected Switch theSwitch; 
33       /** 
34        * The alpha generator that drives the animation 
35        */ 
36       protected Alpha theTargetAlpha; 
37       /** 
38        * Defines whether the duck is dead or alive 
39        */ 
40       protected boolean dead = false; 
41    
42    
43       /** 
44        * This sets up the data for the behaviour. 
45        * 
46        * @param theShape  Node that is to be watched for collisions. 
47        * @param sw        Switch that is used to swap shapes. 
48        * @param a1        Alpha that drives the duck's animation. 
49        * @param theBounds Bounds that define the active region for this behaviour. 
50        */ 
51       public TargetBehavior(Node theShape, Switch sw, Alpha a1, Bounds theBounds) { 
52           collidingShape = theShape; 
53           theSwitch = sw; 
54           theTargetAlpha = a1; 
55           setSchedulingBounds(theBounds); 
56       } 
57    
58       /** 
59        * This sets up the criteria for triggering the behaviour. 
60        * It creates an collision crtiterion and a time elapsed criterion, OR's these 
61        * together and then sets the OR'ed criterion as the wake up 
62        * condition. 
63        */ 
64       public void initialize() { 
65           theCriteria = new WakeupCriterion[2]; 
66           theCriteria[0] = new WakeupOnCollisionEntry(collidingShape); 
67           theCriteria[1] = new WakeupOnElapsedTime(1); 
68           oredCriteria = new WakeupOr(theCriteria); 
69           wakeupOn(oredCriteria); 
70       } 
71    
72       /** 
73        * This is where the work is done. 
74        * If there is a collision, then if the duck is 
75        * alive we switch to the dead duck.  If the duck 
76        * was already dead then we take no action. 
77        * The other case we need to check for is when the 
78        * alpha value is zero, when we need to set the duck back 
79        * to the live one for its next traversal of the screen. 
80        * Finally, the wake up condition is set 
81        * to be the OR'ed criterion again. 
82        */ 
83       public void processStimulus(Enumeration criteria) { 
84           while (criteria.hasMoreElements()) { 
85               WakeupCriterion theCriterion = (WakeupCriterion) criteria.nextElement(); 
86               if (theCriterion instanceof WakeupOnCollisionEntry) { 
87                   //There'sa collision so if the duck is alive swap 
88                   //it to the dead one 
89                   if (dead == false) { 
90                       theSwitch.setWhichChild(1); 
91                       dead = true; 
92                   } 
93               } else if (theCriterion instanceof WakeupOnElapsedTime) { 
94                   //If there isn't a collision, then check the alpha 
95                   //value and if it's zero, revive the duck 
96                   if (theTargetAlpha.value() < 0.1) { 
97                       theSwitch.setWhichChild(0); 
98                       dead = false; 
99                   } 
100              } 
101   
102          } 
103          wakeupOn(oredCriteria); 
104      } 
105  } 
106   
107   
108