Original Post
So ive been working on getting working collision with simple rectangle.intersects() stuff and i can get 1 rectangle to intersect but if i place more than 1 of my entities the first entity loses the collision compatablity and my player can only collide with the 2nd entity i have placed i was wondering if i could get a review of my code and maybe some help with this.
Here is my Main class featuring the collision detection in the update method
my player class
and the entity class
thanks for any help and replies you may have =D
Here is my Main class featuring the collision detection in the update method
package RPG;
import java.util.ArrayList;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class Play extends BasicGameState {
Devmode Devmode;
Player player;
public ArrayList<Entity> entites;
public boolean terrainCollision = false;
public Play(int state) {
}
@Override
public void init(GameContainer gc, StateBasedGame game) throws SlickException {
Devmode = new Devmode();
entites = new ArrayList<Entity>();
Entity e1 = new Entity("e1", 32, 32);
entites.add(e1);
Entity e2 = new Entity("e2", 64, 64);
entites.add(e2);
player = new Player(600, 400);
}
@Override
public void render(GameContainer gc, StateBasedGame game, Graphics g) throws SlickException {
Devmode.render(gc, game, g);
player.render(gc, game, g);
for (Entity entity : entites) {
entity.render(gc, game, g);
}
if (terrainCollision) {
g.drawString("asdasd", 200, 200);
}
}
@Override
public void update(GameContainer gc, StateBasedGame game, int delta) throws SlickException {
Input input = gc.getInput();
Devmode.update(gc, game, delta);
player.update(gc, game, delta);
for (Entity entity : entites) {
entity.update(gc, game, delta);
}
for (Entity entity : entites) {
if (player.HitBox.intersects(entity.HitBox)) {
terrainCollision = true;
}
if (!player.HitBox.intersects(entity.HitBox)) {
terrainCollision = false;
}
}
}
@Override
public int getID() {
return 1;
}
}
my player class
package RPG;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.StateBasedGame;
public class Player {
public float x, y;
public Rectangle HitBox;
public Entity entity;
public Player(float posx, float posy) {
this.x = posx;
this.y = posy;
HitBox = new Rectangle(posx, posy, 32, 32);
}
public void render(GameContainer gc, StateBasedGame game, Graphics g) {
g.drawString("Player", x, y - 18);
g.setColor(Color.yellow);
g.drawRect(x, y, 32, 32);
g.setColor(Color.white);
}
public void update(GameContainer gc, StateBasedGame game, int delta) {
Input input = gc.getInput();
HitBox = new Rectangle(x, y, 32, 32);
if (input.isKeyDown(Input.KEY_A)) {
x -= 0.3f * delta;
}
if (input.isKeyDown(Input.KEY_W)) {
y -= 0.3f * delta;
}
if (input.isKeyDown(Input.KEY_S)) {
y += 0.3f * delta;
}
if (input.isKeyDown(Input.KEY_D)) {
x += 0.3f * delta;
}
}
}
and the entity class
package RPG;
import java.util.ArrayList;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.StateBasedGame;
public class Entity {
public float x, y;
public Image image;
public String name;
public Rectangle HitBox;
public Entity(String n, float posx, float posy) {
this.x = posx;
this.y = posy;
this.name = n;
HitBox = new Rectangle(x, y, 32, 32);
}
public void render(GameContainer gc, StateBasedGame game, Graphics g) {
g.drawString(name, x, y - 18);
g.setColor(Color.red);
g.drawRect(x, y, 32, 32);
g.setColor(Color.white);
}
public void update(GameContainer gc, StateBasedGame game, int delta) {
}
}
thanks for any help and replies you may have =D