简体   繁体   English

Java | Slick2D | 实体系统/游戏对象系统的碰撞

[英]Java | Slick2D | Entity system / game object system collisions

Few days ago I started to work on a 'simple' 2D game. 几天前,我开始研究“简单”的2D游戏。 I tried to do it with 'entity system'. 我试图用“实体系统”来做。 I got class 'GameObject' which extends all other objects like trees, enemies (slimes) and so on. 我得到了“ GameObject”类,该类扩展了所有其他对象,例如树木,敌人(史莱姆)等。 All of these game objects are stored in a arrayList called 'gameObjects'. 所有这些游戏对象都存储在一个名为“ gameObjects”的arrayList中。 Then I'm using a for loop to iterate through all objects from a list and calling their basic functions like update() and draw(). 然后,我使用一个for循环遍历列表中的所有对象,并调用它们的基本功能,如update()和draw()。 Until now everything works even though I'm not 100% sure why. 到现在为止,即使我不确定100%为何,一切仍然可行。 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: GameObject类:

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? 我现在如何才能实现树木和粘液之间的碰撞?

What I usually do is inheriting from Rectangle on my top objectClass. 我通常要做的是从顶级对象类的Rectangle继承。 If your GameObject class inherits from Rectangle you'll have intersects method in every instance which gives you a way to easily detect collision. 如果您的GameObject类是从Rectangle继承的,则每个实例中都将具有intersects方法,这为您提供了一种轻松检测碰撞的方法。

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. 这样,您只将对象A与对象B比较一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM