Menu

Inside the Demo

This post goes briefly over the GameCycle subclass of the Demo App, a simple rotating cube scene.

public class GameWorld extends GameCycle {
static final int FPS = 30;
static final int TB = 100;
public GameWorld(TaskChannel supervisor, GameHost host, ResourceLoader rl, ViewHost glgv) {
    super("AGEDemo", FPS, TB, supervisor, host, rl, glgv);
    // initialize viewing parameters
    final float zp = -12f;
    rr.setCameraPosition(0f, 2f, zp);
    rr.setNearFar(1f, 10f - zp);
}
static final Vec4 blue = new Vec4(0f, 0f, 1f, 1f);
static final Vec4 green = new Vec4(0f, 1f, 0f, 1f);
static final Vec4 red = new Vec4(1f, 0f, 0f, 1f);
static final Vec4 WHITE = new Vec4(1f, 1f, 1f, 1f);
static final Vec4[] cubecolors = {
        blue, blue, WHITE,
        WHITE, blue, blue,
        WHITE, WHITE, WHITE,
};
static final Vec4[] cubecolors2 = {
        green, green, WHITE,
        WHITE, green, green,
        WHITE, WHITE, WHITE,
};
static final Vec4[] cubecolors3 = {
        red, red, WHITE,
        WHITE, red, red,
        WHITE, WHITE, WHITE,
};

/**
 * Helper to make a cube Game Object.
 * @param name
 * @param uc
 * @param x
 * @param y
 * @param z
 * @param scale
 * @param colors Color list for PerVertexMaterial.
 * @return
 */
GameObject makeACube(String name, Geometry uc, float x, float y, float z, float scale, Vec4[] colors) {
    final DrawableGameObject cube = new DrawableGameObject(name, true, uc, 0);
    final Material mx = new PerVertexMaterial(colors);
    final Transform tx = new Transform(x, y, z, scale);
    cube.set(Constants.Property.MATERIAL, mx);
    cube.set(Constants.Property.TRANSFORM, tx);
    cube.setVisible(true);
    return cube;
}
@Override
protected void gameStarting() {
}

/**
 * Handle incoming objects after they are loaded.
 */
@Override
protected void objectLoaded(GameObject arg0, Exception ex) {
    if(ex == null && arg0 instanceof SceneInstallSentinel) {
        // scene is done installing; display it
        startScene(((SceneInstallSentinel)arg0).scene);
    }
}
/**
 * Assemble the scene via the install pipeline.
 */
@Override
protected void startLoading(boolean isreload) {
    try {
        // list of binding targets; everyone in this scene uses the same list
        final String[] BIND_TO = { "Scene" };
        // must install scene first so everyone can find it
        final Scene scene = new Scene("Scene", 8);
        install(scene);
        // install the XYZ axes
        // this demonstrates direct use of DGO without subclassing
        final DrawableGameObject axes = new DrawableGameObject("Axes", false, new AxisGeometry(), 100);
        axes.set(Constants.Property.MATERIAL, new AxisGeometry.AxisMaterial());
        axes.setVisible(true);
        install(axes, BIND_TO);
        // make a model; all cubes share it
        final UnitCube uc = new UnitCube();
        // make some cubes and install them
        GameObject go;
        go = makeACube("Cube1", uc, 0f, 0f, 0f, 2f, cubecolors3);
        install(go, BIND_TO);
        go = makeACube("Cube2", uc, 4f, 0f, 0f, 2f, cubecolors2);
        install(go, BIND_TO);
        go = makeACube("Cube3", uc, 0f, 4f, 0f, 3f, cubecolors);
        install(go, BIND_TO);
        go = makeACube("Cube4", uc, -2f, -4f, 4f, 5f, cubecolors2);
        install(go, BIND_TO);
        go = makeACube("Cube5", uc, -4f, 0f, 0f, 2f, cubecolors);
        install(go, BIND_TO);
        // this component is a timer that animates the cubes' Z-axis rotation
        final GameObject anim = new CubeAnimator("CubeAnimator", new String[] { "Cube1", "Cube2", "Cube3", "Cube4", "Cube5" });
        install(anim, BIND_TO);
        // this component is a timer that animates the camera's eye position in the ZX plane
        final GameObject canim = new CameraAnimator("CameraAnimator", this.rr);
        install(canim, BIND_TO);
        // install the sentinel so we know it's done
        // note: no binding targets here
        install(new SceneInstallSentinel("Scene.Done", scene));
    } catch (Exception e1) {
    }
}
}
Posted by g-dollar 2013-05-08 | Draft

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.