Java |光滑二维 |实体系统/游戏对象系统碰撞
Java | Slick2D | Entity system / game object system collisions
几天前,我开始研究 'simple' 2D 游戏。我试着用 'entity system' 来做。我得到 class 'GameObject' 扩展了所有其他对象,如树木、敌人(粘液)等。所有这些游戏对象都存储在一个名为 'gameObjects' 的数组列表中。然后我使用 for 循环遍历列表中的所有对象并调用它们的基本函数,如 update() 和 draw()。到现在为止一切正常,尽管我不是 100% 确定原因。问题是由于某些原因我不能对碰撞做同样的事情。
我知道这个话题在这里讨论了很多次,但即使过了很多天我也无法解决这个问题。有谁可以帮助我吗?另外,我为我的英语道歉。
游戏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);
}
}
游戏对象class:
public abstract class GameObject
{
protected void update()
{
}
protected void draw()
{
}
}
树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)
}
}
史莱姆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);
}
}
世界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();
}
}
}
主要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();
}
}
我删除了所有之前的碰撞尝试,并跳过了一些其他不必要的东西。我现在该如何实现碰撞,例如树木和粘液之间的碰撞?
我通常做的是在我的顶级objectClass 上继承Rectangle。如果您的 GameObject class 继承自 Rectangle,您将在每个实例中都有 intersects
方法,这使您可以轻松检测碰撞。
class GameObject extends Rectangle{}
问题是在所有对象之间进行测试。会很重,但还是可以的。
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
}
}
}
这样你只比较对象 A 和对象 B 一次。
几天前,我开始研究 'simple' 2D 游戏。我试着用 'entity system' 来做。我得到 class 'GameObject' 扩展了所有其他对象,如树木、敌人(粘液)等。所有这些游戏对象都存储在一个名为 'gameObjects' 的数组列表中。然后我使用 for 循环遍历列表中的所有对象并调用它们的基本函数,如 update() 和 draw()。到现在为止一切正常,尽管我不是 100% 确定原因。问题是由于某些原因我不能对碰撞做同样的事情。
我知道这个话题在这里讨论了很多次,但即使过了很多天我也无法解决这个问题。有谁可以帮助我吗?另外,我为我的英语道歉。
游戏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);
}
}
游戏对象class:
public abstract class GameObject
{
protected void update()
{
}
protected void draw()
{
}
}
树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)
}
}
史莱姆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);
}
}
世界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();
}
}
}
主要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();
}
}
我删除了所有之前的碰撞尝试,并跳过了一些其他不必要的东西。我现在该如何实现碰撞,例如树木和粘液之间的碰撞?
我通常做的是在我的顶级objectClass 上继承Rectangle。如果您的 GameObject class 继承自 Rectangle,您将在每个实例中都有 intersects
方法,这使您可以轻松检测碰撞。
class GameObject extends Rectangle{}
问题是在所有对象之间进行测试。会很重,但还是可以的。
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
}
}
}
这样你只比较对象 A 和对象 B 一次。