Search code examples
javaslick2d

Java | Slick2D | Entity system / game object system collisions


Few days ago I started to work on a 'simple' 2D game. I tried to do it with 'entity system'. I got class 'GameObject' which extends all other objects like trees, enemies (slimes) and so on. All of these game objects are stored in a arrayList called 'gameObjects'. Then I'm using a for loop to iterate through all objects from a list and calling their basic functions like update() and draw(). Until now everything works even though I'm not 100% sure why. The problem is that for some reason I can't do the same with collisions.

I know this topic was discussed many times here but even though after a many days I can't solve this. Can someone help me please? Also, I apologize for my English.

Game class:

public class Game extends BasicGame
{
    public Game()
    {
        super("Game");
    }

    public void init(GameContainer gameContainer) throws SlickException
    {
        World.init();
    }

    public void update(GameContainer gameContainer, int delta) throws SlickException
    {
        World.update();
    }

    public void render(GameContainer gameContainer, Graphics g) throws SlickException
    {
        World.draw(g);
    }
}

GameObject class:

public abstract class GameObject
{
     protected void update()
     {
     }

     protected void draw()
     {
     }
}

Tree class:

public class Tree extends GameObject
{
     public float x, y;
     private static Image tree;

     public Tree(float x, float y)
     {
         this.x = x;
         this.y = y;
         tree = Resources.miscSheet.getSprite(2, 0);
     }

     public void draw()
     {
         tree.draw(x, y)
     }         
}

Slime class:

public class Slime extends GameObject
{
    public static float x;
    public static float y;
    private static Animation slimeAnim;

    public Slime(int x, int y)
    {
        this.x = x;
        this.y = y;
        // My own method for loading animation.
        slimeAnim = Sprite.getAnimation(Resources.slimeSheet, 0, 0, 5, 300);
    }

    public void update()
    {
        // *Random movement here*
    }

    public void draw()
    {
        slimeAnim.draw(x, y);
    }
}

World class:

public class World
{
    public static List<GameObject> gameObjects = new ArrayList<GameObject>();

    public static void init()
    {
        Tree tree = new Tree(0, 0);
        Tree tree2 = new Tree(200, 200);
        Slime slime = new Slime(80, 80);
        gameObjects.add(tree);
        gameObjects.add(tree2);
        gameObjects.add(slime);
    }

    public static void update()
    {
        for (int i = 0; i < gameObjects.size(); i++)
        {
            GameObject o = gameObjects.get(i);
            o.update();
        }
    }

    public static void draw(Graphics g)
    {
        g.setBackground(new Color(91, 219, 87));

        for (int i = 0; i < gameObjects.size(); i++)
        {
            GameObject o = gameObjects.get(i);
            o.draw();
        }
    }
}

Main class:

public class Main
{
    public static AppGameContainer container;

    public static void main(String[] args) throws SlickException
    {
        container = new AppGameContainer(new Game());
        container.setDisplayMode(1024, 600, false);
        container.setShowFPS(false);
        container.start();
    }
}

I deleted all my previous collision attempts and I skipped some other unnecessary things. How can I now please implement collisions for example between trees and slimes?


Solution

  • What I usually do is inheriting from Rectangle on my top objectClass. If your GameObject class inherits from Rectangle you'll have intersects method in every instance which gives you a way to easily detect collision.

    class GameObject extends Rectangle{}
    

    The problem will be to do the test between all your objects. It will be quite heavy but still possible.

    for (int i = 0; i < gameObjects.size(); i++) { 
        for (int j = i; j < gameObjects.size(); j++) {
            if (gameObjects.get(i-1).intersects(gameObjects.get(j)) {
                // I don't know what you want to do here        
            }
        }
    }
    

    This way you compare object A to object B only once.