1
\$\begingroup\$

I'm making a game in libGDX wich the main focus is "recursion", so I can play multiple instances of the game in itself, flowing through "layers" of games. Currently its mechanics work perfectly, but I'm only drawing the current layer of the game. I want to make the game a little more immersive so I got the idea to draw all layers on top of each other, becoming smaller and smaller as the player advances in layers. I have an image here of what I'm aiming to do:

enter image description here

Ok, that editing was pretty lame, but I think you'll get what I'm saying. What I have done is make an Object called "Recursive" which has custom states of Menustate and GameState, which I draw with SpriteBatches.

Code:

public class Recursive{

public static final float SCALE = 45;

public static ArrayList<Body> forRemoval = new ArrayList<Body>();

public Recursive child;
public Recursive parent;
public int levelCreated;
OrthographicCamera camera;

public GameState game;

ArrayList<RecState> states;
int currentState = 0;
Matrix4 transform;

public Recursive(Recursive parent, GameState game, int levelCreated){
    this.parent = parent;
    this.game = game;
    this.levelCreated = levelCreated;

    camera = new OrthographicCamera();
    camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    camera.transform(transform);    
    camera.zoom = 1f/Recursive.SCALE;

    states = new ArrayList<RecState>();
    states.add(new MenuState(this));
    states.add(new RecursiveGameState(this));

    states.get(currentState).create();


}

public int currentLevel(){
    return ((RecursiveGameState)states.get(1)).getCurrentLevel();
}

public void create() {
    states.get(currentState).create();

}

public void resetLevel(){
    ((RecursiveGameState)states.get(1)).resetLevel();
}

public void reload(String map){
    ((RecursiveGameState)states.get(1)).canReload(map);
}

public void render(SpriteBatch sb){

    sb.setProjectionMatrix(camera.combined);
    states.get(currentState).render(sb);

}
public void update(float delta){
    camera.update();
    states.get(currentState).update(delta);
    for(Body b : forRemoval){
        if(b.getUserData() instanceof Enemy){
            ((RecursiveGameState)states.get(1)).enemies.remove(b.getUserData());
        }           
        b.getWorld().destroyBody(b);
    }
    forRemoval.clear();


}

public void dispose(){
    states.get(currentState).dispose();
}

public void changeState(int state){
    currentState = state;
    create();
}

public void forward(){
    game.forward();
}

public void backward(){
    game.backward();
}

public void resetGame(){

    states.set(1, new RecursiveGameState(this));
    child = null;
}

public boolean hasSave(){
    return ((RecursiveGameState) states.get(1)).hasBeenCreated;
}
public OrthographicCamera getCamera() {
    return camera;
}

public void connected(Controller controller) {

}

public void disconnected(Controller controller) {

}

public boolean buttonDown(Controller controller, int buttonCode) {
    return states.get(currentState).buttonDown(controller, buttonCode);
}

public boolean buttonUp(Controller controller, int buttonCode) {
    return states.get(currentState).buttonUp(controller, buttonCode);
}

public boolean axisMoved(Controller controller, int axisCode, float value) {
    return states.get(currentState).axisMoved(controller, axisCode, value);
}

public boolean povMoved(Controller controller, int povCode, PovDirection value) {
    return states.get(currentState).povMoved(controller, povCode, value);
}

public boolean xSliderMoved(Controller controller, int sliderCode, boolean value) {
    return states.get(currentState).xSliderMoved(controller, sliderCode, value);
}

public boolean ySliderMoved(Controller controller, int sliderCode, boolean value) {
    return states.get(currentState).ySliderMoved(controller, sliderCode, value);
}

public boolean accelerometerMoved(Controller controller, int accelerometerCode, Vector3 value) {
    return states.get(currentState).accelerometerMoved(controller, accelerometerCode, value);
}



}

And within the RecursiveGameState I draw the next Recursive child, if it is not null, creating a new Sprite Batch:

public void render(SpriteBatch sb){
    if(rec.child != null){
        SpriteBatch inner = new SpriteBatch();
        rec.child.render(inner);
    }
}

This currently draws what I want, but all layers are the same size, like this:

enter image description here

(The yellow marked area is the first layer, and the red marked is the second layer, drawn over the first.)

Is there any way I can change the SpriteBatch object so I can resize it, cutting the edges and drawing the second layer a little bit smaller on top of the first one?

I have already tried to scale the projection Matrix, but it does not cut off the edges, also different things in the game have different projections (like the GUI, and the Box2D Renderer and the Sprites (doors and bombs)

(You can only see the first one because I didn't put any background, but I'll soon put it and make it more like a rectangle, with ways of the player to actually understand that are different layers.)

\$\endgroup\$
  • \$\begingroup\$ Why not just use a PerspectiveCamera and draw the layers at different Z distances? This way the game handles the scaling for you \$\endgroup\$ – Charanor Mar 4 '17 at 19:43
  • \$\begingroup\$ but then i'll have to deal with methods like "setToOrtho()" and the variable "zoom" i'm using to make sprites the right size, is there any way i can change those methods to relative ones in PerspectiveCamera? \$\endgroup\$ – vilok600 Mar 4 '17 at 20:18
  • \$\begingroup\$ also something i noticed is that, even if i manage to draw at diferent zs, i would need to make the smaller screen go backwards , that way, the layers on the front would be rendered first, and it should be rendered last \$\endgroup\$ – vilok600 Mar 4 '17 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.