Sep 3, 2014

43. States and Controls

Here AppStates State1, State2, and State3 are used to control Control1, Control2, and Control3, respectively. In the main application, at different times we have different states active, resulting in different actions on a geometry object.




Control1 rotates a spatial.


// Control1.java

package mygame;

import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.control.AbstractControl;

class Control1 extends AbstractControl{
    @Override
    protected void controlUpdate(float tpf) {
        spatial.rotate(tpf,tpf,tpf);
    }
    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }
}



Control2 moves a spatial in +x and -x.


// Control2.java

package mygame;

import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.control.AbstractControl;

public class Control2 extends AbstractControl{

    private float current;
    private int sign = -1;
        
    @Override
    protected void controlUpdate(float tpf) {
        current = spatial.getLocalTranslation().x;
        if (current<-2f) {
            sign = 1;
        }
        if (current>2f) {
            sign = -1;
        }
        spatial.move(5*sign*tpf,0,0);
    }
    
    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }
}



Control 3 moves a spatial in +y and -y.


// Control3.java

package mygame;

import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.control.AbstractControl;

class Control3 extends AbstractControl {
    
    private float current;
    private int sign = -1;
    
    @Override
    protected void controlUpdate(float tpf) {
        current = spatial.getLocalTranslation().y;
        if (current<-2f) {
            sign = 1;
        }
        if (current>2f) {
            sign = -1;
        }
        spatial.move(0,5*sign*tpf,0);
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }
}



In State1, Geometry "Box" is set to Control1, during the time state is active.


// State1.java

package mygame;

import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.scene.Geometry;

public class State1 extends AbstractAppState{
    
    private SimpleApplication app;
    private Geometry geom;
    private Control1 control1 = new Control1();
        
    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        super.initialize(stateManager, app);
        this.app = (SimpleApplication)app;
        geom = (Geometry)this.app.getRootNode().getChild("Box");
        geom.addControl(control1);
    }
    
      
    @Override
    public void stateAttached(AppStateManager stateManager) {
        super.stateAttached(stateManager);
        System.out.println("Attached - 1");
    }
 
    @Override
    public void update(float tpf) {
        super.update(tpf);
    }

    @Override
    public void stateDetached(AppStateManager stateManager) {
        super.stateDetached(stateManager);
        System.out.println("Detached - 1");
    }
 
    @Override
    public void cleanup() {
        super.cleanup();
        geom.removeControl(control1);
    }
}



In State1, Geometry "Box" is set to Control2, during the time state is active.


// State2.java

package mygame;

import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.scene.Geometry;

public class State2 extends AbstractAppState{
    
    private SimpleApplication app;
    private Geometry geom;
    private Control2 control2 = new Control2();
        
    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        super.initialize(stateManager, app);
        this.app = (SimpleApplication)app;
        geom = (Geometry)this.app.getRootNode().getChild("Box");
        geom.addControl(control2);
    }
    
    @Override
    public void stateAttached(AppStateManager stateManager) {
        super.stateAttached(stateManager);
        System.out.println("Attached - 2");
    }
 
    @Override
    public void update(float tpf) {
        super.update(tpf);
    }

    @Override
    public void stateDetached(AppStateManager stateManager) {
        super.stateDetached(stateManager);
        System.out.println("Detached - 2");
    }
 
    @Override
    public void cleanup() {
        super.cleanup();
        this.geom.removeControl(control2);
    }
}



In State1, Geometry "Box" is set to Control3, during the time state is active.


// State3.java

package mygame;

import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.scene.Geometry;

public class State3 extends AbstractAppState{
    
    private SimpleApplication app;
    private Geometry geom;
    private Control3 control3 = new Control3();

    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        super.initialize(stateManager, app);
        this.app = (SimpleApplication)app;
        geom = (Geometry)this.app.getRootNode().getChild("Box");
        geom.addControl(control3);
    }
    
    @Override
    public void stateAttached(AppStateManager stateManager) {
        super.stateAttached(stateManager);
        System.out.println("Attached - 3");
    }
 
    @Override
    public void update(float tpf) {
        super.update(tpf);
    }

    @Override
    public void stateDetached(AppStateManager stateManager) {
        super.stateDetached(stateManager);
        System.out.println("Detached - 3");
    }
 
    @Override
    public void cleanup() {
        super.cleanup();
        this.geom.removeControl(control3);
    }
}



// JMonkey43.java

package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.system.AppSettings;

public class JMonkey43 extends SimpleApplication {
    public static void main(String[] args) {
        AppSettings settings = new AppSettings(true);
        settings.setFrameRate(60);
        settings.setTitle(
                "Box under different controls at different times!");
        JMonkey43 app = new JMonkey43();
        app.setSettings(settings);
        app.start();
    }
    
    private int counter = 0;
    Geometry geom;

    @Override
    public void simpleInitApp() {
        viewPort.setBackgroundColor(ColorRGBA.White);
        setDisplayFps(false);
        setDisplayStatView(false);
        flyCam.setEnabled(false);
        Box b = new Box(.5f, .5f, .5f);
        geom = new Geometry("Box", b);
        Material mat = new Material( 
            assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");
        geom.setMaterial(mat);
        geom.setLocalTranslation(2,2,2);
        rootNode.attachChild(geom);
    }

    @Override
    public void simpleUpdate(float tpf) {
        counter++;
        if (counter==50) {
            stateManager.attach(new State1());
        }
        if (counter==200) {
            stateManager.detach(stateManager.getState(State1.class));
            stateManager.attach(new State2());
        }
        if (counter==350) {
            stateManager.detach(stateManager.getState(State2.class));
            stateManager.attach(new State3());
        }
        if (counter==500) {
            stateManager.detach(stateManager.getState(State3.class));
        }
        if (counter==600) {
            stateManager.attach(new State1());
            stateManager.attach(new State2());
            stateManager.attach(new State3());
        }
        if (counter==750) {
            stateManager.detach(stateManager.getState(State3.class));
            stateManager.detach(stateManager.getState(State2.class));
            stateManager.detach(stateManager.getState(State1.class));
        }
    }
    @Override
    public void simpleRender(RenderManager rm) {
    }
}


Output:


No comments:

Post a Comment