home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-09-25 | 4.2 KB | 181 lines |
- // $Id: ufo.java,v 1.2 1996/08/26 12:15:07 sugino Exp $
- //
- // ufo.java
- //
- // Copyright(C) 1996 Sony Corporation. All rights reserved.
- //
- import vrml.*;
- import vrml.field.*;
- import vrml.node.*;
-
- public class ufo extends Script{
- SFVec3f ufo_trans ;
- SFBool setTimerEnabled ;
-
- int flg = 0;
- int height = 0;
-
- public void initialize(){
- ufo_trans = (SFVec3f) getEventOut("ufoTr");
- setTimerEnabled = (SFBool) getEventOut( "setTimerEnabled" );
- }
-
- public void processEvent(Event e){
- String name = e.getName();
- if(name.equals("clicked")){
- clicked ( (ConstSFBool) e.getValue(), e.getTimeStamp()) ;
- }
- if(name.equals("move")){
- move ( (ConstSFTime) e.getValue(), e.getTimeStamp()) ;
- }
- }
-
- public void clicked ( ConstSFBool ev, double time ){
- // when the button is pressed, return immediately.
- if( ev.getValue() == true ) return;
-
- // when the button is released...
- switch ( flg ){
- case ( 0 ): // landing, set to rising
- flg = 1;
- start();
- break;
- case ( 2 ): // flying, set to swinging
- flg = 3;
- start();
- break;
- case ( 3 ): // swinging, set to falling
- flg = 4;
- break;
- default:
- break;
- }
- }
-
- public void move ( ConstSFTime ev, double time ){
- switch ( flg ) {
- case ( 1 ):
- // rising
- rise();
- if( height >= 20 ){
- flg = 2;
- stop();
- }
- break;
- case ( 3 ):
- // swinging
- swing();
- break;
- case ( 4 ):
- // go back.
- if(height > 0){
- gobackFall();
- }else if(height < 0){
- gobackRise();
- }
- if( height == 0 ){
- flg = 0;
- stop();
- }
- break;
- default:
- break;
- }
- }
-
- float movepara[] = new float[3];
- public void rise(){
-
- movepara[ 1 ] += 2.0f;
- ufo_trans.setValue( movepara );
- height = height + 2;
- }
-
- public void gobackFall(){
-
- movepara[ 1 ] += -1.0f;
- ufo_trans.setValue( movepara );
- height = height - 1;
- }
-
- public void gobackRise(){
-
- movepara[ 1 ] += 1.0f;
- ufo_trans.setValue( movepara) ;
- height = height + 1;
- }
-
- public void swing(){
- // random swinging.
- int randomNum = (int)(6 * Math.random());
-
- switch(randomNum){
- case 0:
- swingLeft();
- break;
- case 1:
- swingRight();
- break;
- case 2:
- swingUp();
- break;
- case 3:
- swingDown();
- break;
- case 4:
- swingRise();
- break;
- case 5:
- swingFall();
- break;
- default:
- // do nothing.
- }
- }
-
- public void swingLeft(){
- movepara[ 0 ] += -2.0f;
- ufo_trans.setValue( movepara ) ;
- }
-
- public void swingRight(){
- movepara[ 0 ] += 2.0f;
- ufo_trans.setValue( movepara ) ;
- }
-
- public void swingUp(){
- movepara[ 1 ] += 2.0f;
- ufo_trans.setValue( movepara ) ;
- height = height + 2;
- }
-
- public void swingDown(){
- movepara[ 1 ] += -2.0f;
- ufo_trans.setValue( movepara ) ;
- height = height - 2;
- }
-
- public void swingRise(){
- movepara[ 1 ] += 5.0f;
- ufo_trans.setValue( movepara ) ;
- height = height + 5;
- }
-
- public void swingFall(){
- movepara[ 1 ] += -5.0f;
- ufo_trans.setValue( movepara ) ;
- height = height - 5;
- }
-
- public void start(){
- System.out.println("start() enter.");
- setTimerEnabled.setValue( true );
- }
- public void stop(){
- System.out.println("stop() enter.");
- setTimerEnabled.setValue( false );
- }
- }
-
-
-