As the title says, I'm having a problem with objects overlapping. I want them to be able to overlap because I need object X to be on top of object Y to get a point and if I remove object X from being on top of Y, I remove the said point. My problem then is, if object Y is created before object X, once I place object X on top of Y, I can no longer move it and it always outputs to the console, Object Y. I was wondering if there would be an easier way to fix this.
I try moving forward but the Box doesn't budge:
[
This is the code that I'm using to generate the level Data
private List<ImageTile> createLevel(){
ArrayList<Movable> aux1 = new ArrayList<>();
ArrayList<Immovable> aux2 = new ArrayList<>();
try {
Scanner sc = new Scanner(new File ("levels/level" + levelID + ".txt"));
String line = "";
while(sc.hasNextLine()) {
for (int y = 0; y < HEIGHT; y++) {
line = sc.nextLine();
for(int x = 0; x < WIDTH ; x++) {
levelObjects.add(new floor(new Point2D(x,y), "Floor", 0));//adding the floor before anything else
char letter = line.charAt(x); // checking the character in the X coordinate
if (letter == 'E') { // in case, there's a E, that's the starting position of the player
player = new Forklift(new Point2D(x,y), "Forklift_U", 2);
levelObjects.add(player);
objects.add(player);
} else if(letter != ' ') {
AbstractObject obj = ObjectCreation.readChar(letter, x, y); // going to look for the object in Factory to be put in the X and Y position
if(obj instanceof Immovable) {
aux2.add((Immovable) obj);
}else if(obj instanceof Movable) {
aux1.add((Movable) obj);
}
// comp.comprator(obj, obj);
objects.add(obj);
levelObjects.add(obj);//implementing said object into the Level
}
}
}
}
sc.close(); //Closing Scanner
} catch (FileNotFoundException e) {
System.out.println("No levels found!");
}
still = aux2;
moving = aux1;
return levelObjects;
}
Then I'm checking with the general move function if the box( or any object part of the instance Movable) can move to the next position
public void move(Direction direction) {
Vector2D pos = direction.asVector(); // Transforming the direction in a vector right off the bat.
Point2D currentPosition = getPosition(); // getting the current position of either player or object.
Point2D nextPosition = currentPosition.plus(pos); // the next position as to which the player or the object intend to go to.
if(isTransposable(nextPosition)) { // calls the function to see if the object is able to move to the next position, this prevents the boxes from pushing up into the walls and from into each other {
setPosition(nextPosition); //if it can, then the object will go to the next position
}
}
And this is to check whether or not the object can advance to the next position;
public boolean isTransposable(Point2D pos) { // is the object able to move to the next position
for(AbstractObject obj1 : Game.getInstance().getObjects()) { // for-each to get all the objects, with the exception of the floor, from the game.
if((obj1.isBlockable() && obj1.getPosition().equals(pos))){ // if the object is able to block and if it's position is the same as the nextPosition.
return false;
}
}
return true; // otherwise it's able to be walked on.
}
It was a simple case of having two lists and one having priority over the other. In the picture, you can the box is on top of the target, but as soon as that happened, the engine would read the tile that came first, in this case being the Target one. Since that one is immovable it meant that it could not be pushed/moved by any means. Regardless if you had anything on top of it that could.
A way I used to fix it, was to simply check if the object was of class movable
public boolean isMovable(Point2D pos) {
for(AbstractObject obj1 : Game.getInstance().getObjects()) {
if((obj1.isMovable() && instanceof Movable)){
return true;
}
}
return false;
}