Hi!
Can any one explain how WakeupOnBehavior works, a code snippet would be very helpful and would be ok to use it for a character animation to synchronise the movements of varioius limbs
Thanks
Hi!
Can any one explain how WakeupOnBehavior works, a code snippet would be very helpful and would be ok to use it for a character animation to synchronise the movements of varioius limbs
Thanks
In a game project, I used Behavior posts to comunicate the Movement Behavior (wakeuponAWTEvent) with the bullet Animation Behavior (wakeupOnEllapsedTime).
In my case, I called the constructor setting the Behavior to listen.
When the player press the fire/attack button the Movement Behavior posts an id indicating to the bullet animation Behavior to start its animation/simulation.
When the bullet hits something the Animation Behavior post an id indicating wich object was hit.
Now the code:
class BulletAnim extends Behavior{
...
private WakeupOr wor;
private WakeupOnBehaviorPost waiting;
public BulletAnim(...){
...
WakeupCriterion[] criterio = new WakeupCriterion[2];
criterio[0]= new WakeupOnElapsedTime(1000/60);
criterio[1]=new WakeupOnCollisionEntry(sphere,WakeupOnCollisionEntry.USE_GEOMETRY);
wor=new WakeupOr(criterio);
}
public void setPartner(Behavior b){
//Here I set the movement Behavior as the source of the posts.
//I will receive any id (id == 0).
waiting = new WakeupOnBehaviorPost(b, 0);
}
public void initialize()
{
this.wakeupOn(waiting);
}
public void processStimulus(Enumeration criteria){
boolean moving = false;
while (criteria.hasMoreElements()) {
WakeupCriterion wakeup = (WakeupCriterion) criteria.nextElement();
if(wakeup instanceof WakeupOnBehaviorPost){
//now it wakes up with the time or collision
moving = true ;
}
else if(wakeup instanceof WakeupOnElapsedTime){
//do the animation
...
moving = true ;
}
else if(wakeup instanceof WakeupOnCollisionEntry){
//get the object hit
int objectId = ... //Collision info.
postId(objectId); //send the signal to the movement behavior
moving = false;
}
}
if(moving)
this.wakeupOn(wor);
else
this.wakeupOn(waiting);
}
}
class TankMovement extends Behavior{
...
private WakeupOnAWTEvent trigger;
private WakeupOnBehaviorPost waiting;
public TankMovement(...){
...
trigger=new WakeupOnAWTEvent(KeyEvent.KEY_PRESSED);
}
public void setPartner(Behavior b){
//Here I set the movement Behavior as the source of the posts.
//I will receive any id (id == 0).
waiting = new WakeupOnBehaviorPost(b, 0);
}
public void initialize()
{
this.wakeupOn(trigger);
}
public void processStimulus(Enumeration criteria){
boolean moving = false;
while (criteria.hasMoreElements()) {
WakeupCriterion wakeup = (WakeupCriterion) criteria.nextElement();
if(wakeup instanceof WakeupOnBehaviorPost){
//now it wakes up returns to receive input
moving = true ;
}
else if(wakeup instanceof WakeupOnAWTEvent){
//receive and process the input
...
if(shot!=0){
postId(shot); //send the signal
moving = false ;
}
}
}
if(moving)
this.wakeupOn(trigger);
else
this.wakeupOn(waiting);
}
}
Then in the scene creation:
...
BulletAnim anim = new BulletAnim(...);
...
TankMovement tankMove= new TankMovement(...);
...
//set the partners first
anim.setPartner(tankMove);
tankMove.setPartner(anim);
...
//then add to the scenegraph
bg1.addChild(anim);
bg2.addChild(tankMove);
...
For the character animation, you can use the constructor without Behavior and send signals to all the behaviors, each will know when and how much to move.
Rafael