I have a class called World that has an ArrayList
called entities, the data is then added to the list and displayed on the map. The ArrayList
needs accessible from another class called Player that also needs to iterate through the list to be able to check if the is intersecting it. What I want to know is how to make the entities ArrayList
accessible to the Player class.
the list in the World.java is
public List<Entity> entities; // contains all entities
the ArrayList is being initialized in World.java
entities = new ArrayList<Entity>();// initialise list to new array list
data being hardcoded into this list in World.java
Transform r1 = new Transform();
r1.position.x = 4;
r1.position.y = -4;
Transform r2 = new Transform();
r2.position.x = 5;
r2.position.y = -7;
Transform r3 = new Transform();
r3.position.x = 11;
r3.position.y = -12;
Transform r4 = new Transform();
r4.position.x = 10;
r4.position.y = -8;
Transform t1 = new Transform();
t1.position.x = 3;
t1.position.y = -7;
Transform t2 = new Transform();
t2.position.x = 5;
t2.position.y = -5;
Transform t3 = new Transform();
t3.position.x = 13;
t3.position.y = -13;
Transform t4 = new Transform();
t4.position.x = 7;
t4.position.y = -5;
Transform t5 = new Transform();
t4.position.x = 8;
t4.position.y = -9;
Transform t6 = new Transform();
t4.position.x = 14;
t4.position.y = -14;
entities.add(new Player(new Transform()));// add entity player
entities.add(new Rock(r1));// add entity rock
entities.add(new Rock(r3));// add entity rock
entities.add(new Rock(r2));// add entity rock
entities.add(new Rock(r4));// add entity rock
entities.add(new Tree(t1));// add entity rock
entities.add(new Tree(t3));// add entity rock
entities.add(new Tree(t2));// add entity rock
entities.add(new Tree(t4));// add entity rock
entities.add(new Tree(t5));// add entity rock
entities.add(new Tree(t6));// add entity rock
the list being iterated in World.java
for (Entity entity : entities) {// itaarate through all the entities in list of entities
entity.render(shader, camera, this);// render each entity
if (!entity.isActive())
entities.remove(entity);
}
Player.java class
public class Player extends Entity {
public ArrayList<Entity> entities; // contains all entities
public static final int IDLE = 0;
public static final int RUN = 1;
public Player(Transform transform) {
super(4, transform);
// number of images, fps, file
setAnimation(IDLE, new Animations(4, 2, "player/idle"));// player standing
setAnimation(RUN, new Animations(4, 4, "player/run"));// player walking
}
@Override
public void update(float delta, Window window, Camera camera, World world) {
Vector2f movement = new Vector2f();// gets new position
if (window.getInput().isKeyDown(GLFW.GLFW_KEY_A))
movement.add(-10 * delta, 0);// move character 10 units fr every frame
if (window.getInput().isKeyDown(GLFW.GLFW_KEY_D))
movement.add(10 * delta, 0);// move character 10 units fr every frame
if (window.getInput().isKeyDown(GLFW.GLFW_KEY_W))
movement.add(0, 10 * delta);// move character 10 units fr every frame
if (window.getInput().isKeyDown(GLFW.GLFW_KEY_S))
movement.add(0, -10 * delta);// move character 10 units fr every frame
Rectangle cb = new Rectangle();
Rectangle ar = new Rectangle();
int arSize = 20;
ar.width = arSize;
ar.height = arSize;
if (window.getInput().isKeyDown(GLFW.GLFW_KEY_UP))
ar.x = cb.x + cb.width / 2 - arSize / 2;
ar.y = cb.y - arSize;
if (window.getInput().isKeyDown(GLFW.GLFW_KEY_DOWN))
ar.x = cb.x + cb.width / 2 - arSize / 2;
ar.y = cb.y + cb.height;
if (window.getInput().isKeyDown(GLFW.GLFW_KEY_LEFT))
ar.x = cb.x - arSize;
ar.y = cb.y + cb.height / 2 - arSize / 2;
if (window.getInput().isKeyDown(GLFW.GLFW_KEY_RIGHT))
ar.x = cb.x + cb.width;
ar.y = cb.y + cb.height / 2 - arSize / 2;
for (Entity entity : ) {// itaarate through all the entities in list of entities
}
move(movement);
if (movement.x != 0 || movement.y != 0)
useAnimation(RUN);
else
useAnimation(IDLE);
camera.getPosition().lerp(transform.position.mul(-world.getScale(), new Vector3f()), 0.1f);// follows the player
}
}
for example say this is your world class has arraylist in it
Now from class Player