1
\$\begingroup\$

I currently have a libGDX game where I have an entity manager class which is called in the gamescreen. Instead, I would like to utilise it in a gameworld class and a gamerenderer class, but I cannot figure out how to split it up (if that makes sense). I want to do this so that I can manipulate my player class separately and not as a part of the entity manager. Previous attempts have failed because taking 'player' away meant that checkCollisions stopped working (hence why I want to use gameworld and gamerenderer classes instead). Does anyone have any ideas on how to achieve this?

Here is my entity manager class:

public class EntityManager {
    private final Array<Entity> entities = new Array<Entity>();
    private final Player player;

    public EntityManager(int amount) {
        player = new Player(new Vector2(110, 15), new Vector2(0, 35));
        for (int i = 0; i < amount; i++) {
            float x = MathUtils.random(0, ManicMeltdowns.WIDTH - TextureManager.DROPS.getWidth());
            float y = MathUtils.random(ManicMeltdowns.HEIGHT, ManicMeltdowns.HEIGHT * 3);
            float speed = 2;
            addEntity(new Drops(new Vector2 (x, y), new Vector2(0, -speed)));
        }
    }

    public void update() {
        for (Entity e : entities)
            e.update();
        player.update();
        checkCollisions();
    }

    public void render(SpriteBatch sb) {
        for (Entity e : entities)
            e.render(sb);
        player.render(sb);
    }

    private void checkCollisions() {
        for (Drops e : getDrops()) {
            if (e.getBounds().overlaps(player.rectangle)) {
                ScreenManager.setScreen(new GameOverScreen());
            }
        }
    }

    public void addEntity(Entity entity) {
        entities.add(entity);
    }

    private Array<Drops> getDrops() {
        Array<Drops> ret = new Array<Drops>();
        for (Entity e : entities)
            if (e instanceof Drops)
                ret.add((Drops)e);
        return ret;
    }
}

Thanks.

\$\endgroup\$
  • \$\begingroup\$ i have no idea what you try to achieve but. did you read articles about "Entity Component System"? it provides a good separation of code and loose-binding \$\endgroup\$ – Erez Mar 11 '15 at 11:11
  • \$\begingroup\$ Use something like Artemis ODB or Ashley. Also, the whole point of an ECS is to have systems making stuff for you on components, so Iterating entities themselves is of no good use. \$\endgroup\$ – Gustavo Maciel Aug 12 '15 at 22:27
1
\$\begingroup\$

I'm no too sure of the overall design of your game, but I don't see why you would need GameWorld or GameRenderer classes since Screens generally accomplish such tasks.

To separate the player and keep collision detection you can simply require the instance as an argument:

private void checkCollisions(Player player) {
    for (Drops e : getDrops()) {
        if (e.getBounds().overlaps(player.rectangle)) {
            ScreenManager.setScreen(new GameOverScreen());
        }
    }
}
\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.