Skip to content

Initial Setup

ia97lies edited this page Sep 7, 2017 · 7 revisions

Let's start with the main part...

package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.renderer.RenderManager;

public class Main extends SimpleApplication {

    public static void main(String[] args) {
        Main app = new Main();
        app.start();
    }

    public Main() {
    }

    @Override
    public void simpleInitApp() {
    }

    @Override
    public void simpleUpdate(float tpf) {
    }

    @Override
    public void simpleRender(RenderManager rm) {
    }
}

Hit F6 and start your game. After selecting the resolution you only see a black screen. Not very chilling indeed.

#The beef It is all about the the entity system so lets setup and initialize Zay ES before we head into the game itself.

package mygame;

import com.jme3.app.state.AbstractAppState;
import com.simsilica.es.EntityData;
import com.simsilica.es.base.DefaultEntityData;

public class EntityDataState extends AbstractAppState {
    private EntityData entityData;

    public EntityDataState() {
        this(new DefaultEntityData());
    }

    public EntityDataState( EntityData ed ) {
        this.entityData = ed;
    }

    public EntityData getEntityData() {
        return entityData;
    }

    @Override
    public void cleanup() {
        entityData.close();
        entityData = null; // cannot be reused
    }
}

There is not much to say about this application state. As it is brain dead simple.

#Black screen? I mean... hello?! Indeed a game without visual representation is not much fun. Furthermore it always helps to see something on the screen and gives feedback to keep us happy and motivated.

##A star ship We need models which we could do with blender and import it into JME3.

First we need a space ship, so fire up your blender and do that. Well, I do not want to waste your time so I have one on my google drive to share with you. Doesn't mean you can not come up with our own model though.

If you have your model, place the blend file in the assets "Models" folder, then just right click your blender model in JME3 and select "Convert to j3o Binary".

##Model factory After you took that hurdle with blender a factory to get the spatial in JME3 would be greate, it makes things simpler.

package mygame;

import com.jme3.asset.AssetManager;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
 
public class ModelFactory {
    private final AssetManager assetManager;
 
    public ModelFactory(AssetManager assetManager) {
        this.assetManager = assetManager;
    }

    public Spatial create(String name) {
        Node visual = new Node("Visual");
        Node model = (Node) assetManager.loadModel("Models/" + name + ".j3o");
        visual.attachChild(model);
        return visual;
    }
}

This implies that we have our models unter "Project Assets" (in real it the folder's name is "assets") and there in the folder "Models". I think needs not much explanation I let speak the code for itself. Of course you can do your factory better, more OO or not at all, I don't care.

##Quick and dirty So now we want to see that ship on the screen. For this we start with the visual system, I called it VisualAppState and it looks as follow

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.light.DirectionalLight;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Spatial;

public class VisualAppState extends AbstractAppState {
    private SimpleApplication app;
    private ModelFactory modelFactory;
 
    @Override
    public void initialize(AppStateManager stateManager, Application app) {
        super.initialize(stateManager, app);
        this.app = (SimpleApplication) app;
        app.getCamera().lookAt(Vector3f.UNIT_Z, Vector3f.UNIT_Y);
        app.getCamera().setLocation(new Vector3f(0, 0, 60));
        DirectionalLight light = new DirectionalLight();
        light.setDirection(new Vector3f(1, 1, -1));
        this.app.getRootNode().addLight(light);
        modelFactory = new ModelFactory(this.app.getAssetManager());
        Spatial myVisual = modelFactory.create("SpaceShip");
        this.app.getRootNode().attachChild(myVisual);
    }

    @Override
    public void cleanup() {
    }

    @Override
    public void update(float tpf) {
    }
}

I assume your space ship is stored in the assets/Models/SpaceShip.j3o file.

If your model do have material then you will need a light, I took a directional light here. And register that in the Main class as follow.

    public Main() {
        super(new VisualAppState(),
                new EntityDataState(),);
    }

That's it, fire up your game with F6 and see. I agree there is not much about a ES so far but we are now prepared to do the first real entity system, actually we have to put some flesh on the VisualAppState and add some kind of a GameAppState.

Home | Next

Clone this wiki locally