I am making my first game in LibGdx and it's going well. Atleast I think it is. But I want to implement a better collision system... My current collision looks like this:
List<Rectangle> bounds= new ArrayList<Rectangle>();
for (int i = 0; i < 250; i++)
{
for (int j = 0; j < 250; j++)
{
TiledMapTileLayer cur = (TiledMapTileLayer) map.getLayers().get(1);
Cell cell3 = new Cell();
if (cur.getCell(i, j) != null)
{
cell3 = cur.getCell(i, j);
System.out.println("Found a chest detector at: " + i + ", " + j
+ ", " + cell3.getTile().getId());
bounds.add(new Rectangle(i * 64, j * 64, 64, 64));
}
}
}
// CHECKING FOR TREES
for (int i = 0; i < bounds.size(); i++)
{
if (bounds.get(i).overlaps(player.getBounds()))
{
int x = (int) bounds.get(i).x / 64;
int y = (int) bounds.get(i).y / 64;
TiledMapTileLayer cur = (TiledMapTileLayer) map.getLayers()
.get(1);
Cell cell = cur.getCell(x, y);
if (cell.getTile().getProperties().containsKey("Green"))
{
if (Gdx.input.isKeyPressed(Keys.SPACE))
{
System.out.println("You've cut down a tree!");
}
}
if (cell.getTile().getProperties().containsKey("Gray"))
{
player.reAdjust();
}
}
}
As you can see when the player OVERLAPS with a tree that has a property of "Gray" a method called reAdjust is called; That method is in the player class:
First I define a String movement and set it to "";
String movement = "";
After that I add the movement code:
if (Gdx.input.isKeyPressed(Keys.W))
{
if (curEnergy > 0)
{
if (Gdx.input.isKeyPressed(Keys.SHIFT_LEFT))
{
position.y += 2.5f;
movement = "up";
curEnergy--;
currentFrame = animation.getKeyFrame(12 + stateTime);
}
}
else
{
allowRegen = true;
checkIfEnergyIsZero();
}
position.y += 2f;
movement = "up";
currentFrame = animation.getKeyFrame(12 + stateTime);
}
if (Gdx.input.isKeyPressed(Keys.A))
{
if (curEnergy > 0)
{
if (Gdx.input.isKeyPressed(Keys.SHIFT_LEFT))
{
position.x -= 2.5f;
movement = "left";
curEnergy--;
currentFrame = animation.getKeyFrame(4 + stateTime);
}
}
else
{
allowRegen = true;
checkIfEnergyIsZero();
}
position.x -= 2f;
movement = "left";
currentFrame = animation.getKeyFrame(4 + stateTime);
}
if (Gdx.input.isKeyPressed(Keys.D))
{
if (curEnergy > 0)
{
if (Gdx.input.isKeyPressed(Keys.SHIFT_LEFT))
{
position.x += 2.5f;
movement = "right";
curEnergy--;
currentFrame = animation.getKeyFrame(8 + stateTime);
}
}
else
{
allowRegen = true;
checkIfEnergyIsZero();
}
position.x += 2f;
movement = "right";
currentFrame = animation.getKeyFrame(8 + stateTime);
}
if (Gdx.input.isKeyPressed(Keys.S))
{
if (curEnergy > 0)
{
if (Gdx.input.isKeyPressed(Keys.SHIFT_LEFT))
{
position.y -= 2.5f;
movement = "down";
curEnergy--;
currentFrame = animation.getKeyFrame(0 + stateTime);
}
}
else
{
allowRegen = true;
checkIfEnergyIsZero();
}
position.y -= 2f;
movement = "down";
currentFrame = animation.getKeyFrame(0 + stateTime);
}
}
Please ignore the lines with curEnergy, that's just something I am working on... And finally, I make the reAdjust method:
public void reAdjust()
{
if (movement == "up") {
position.y -= 2f;
}
if (movement == "down") {
position.y += 2f;
}
if (movement == "right") {
position.x -= 2f;
}
if (movement == "left") {
position.x += 2f;
}
}
Now, my question is, is there a better way for the collision? I don't really like this one... If you need any other part of the code or if I just forgot something please tell me. Thank you very much!
EDIT: Here is a picture when I moved into the wall while pressing 2 move buttons: http://i1252.photobucket.com/albums/hh576/mateo72354/bugExample_zps72892da0.png