So basically I have a rectangle that i need to collide with two objects i have some crappy code written for the collisions but i needed assistance so im asking the internet here is the code.
package dev.codeon.movementtesting.assets;
import java.awt.Graphics;
import java.util.ArrayList;
import dev.codeon.movementtesting.input.Input;
public class Player extends Entity {
private ArrayList<Entity> entities;
public boolean canUp = true;
public boolean canDown = true;
public boolean canLeft = true;
public boolean canRight = true;
public int speed = 1;
public Player(ArrayList<Entity> entities, String id) {
this.id = id;
this.entities = entities;
}
public void tick() {
checkDirection();
if(canUp && Input.up) {
y -= speed;
}
if(canLeft && Input.left) {
x -= speed;
}
if(canDown && Input.down) {
y += speed;
}
if(canRight && Input.right) {
x += speed;
}
}
public void render(Graphics g) {
g.drawRect(x - (width / 2), y - (height / 2), width, height);
}
public void checkDirection() {
for(int i = 0; i < entities.size(); i++) {
Entity temp = entities.get(i);
if(temp.id != id) {
if((x > temp.x + (temp.x / 2) && x < temp.x - (temp.x / 2))) {
canDown = true;
} else {
canDown = false;
System.out.println("false");
}
if() {
canLeft = true;
} else {
canLeft = false;
}
if((temp.x - (temp.width / 2) > x + (width / 2)) || temp.y + (height / 2) < y) {
canRight = true;
} else {
canRight = false;
}
if(temp.y + (temp.height / 2) < y - (height / 2)) {
canUp = true;
} else {
canUp = false;
}
if(temp.y - (temp.height / 2) > y + (height / 2) || temp.x - (width / 2) > x) {
canDown = true;
} else {
canDown = false;
}
}
}
}
}
I want it to not move up down left or right based on the collisions, canUp and so on i would like nice answers if you need something else please ask ill be here for three hours thank you
Comments
Post a Comment