48 lines
1.2 KiB
Java
48 lines
1.2 KiB
Java
package backend;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashSet;
|
|
import java.util.Set;
|
|
|
|
public class World {
|
|
private Matrix matrix;
|
|
private Set<Agent> entities;
|
|
|
|
public World(int width, int height) {
|
|
this.matrix = new Matrix(width, height);
|
|
this.entities = new HashSet<>();
|
|
}
|
|
|
|
public Matrix getGrid() {
|
|
return matrix;
|
|
}
|
|
|
|
public Set<Agent> getEntities() {
|
|
return entities;
|
|
}
|
|
|
|
public void addAgent(Agent Agent) {
|
|
entities.add(Agent);
|
|
}
|
|
|
|
public void removeAgent(Agent Agent) {
|
|
entities.remove(Agent);
|
|
}
|
|
|
|
public void updateGrid(ArrayList<Integer> surviveRules, ArrayList<Integer> birthRules, boolean loopingBorder) {
|
|
matrix.updateCells(surviveRules, birthRules, loopingBorder);
|
|
}
|
|
|
|
|
|
public Set<Agent> processAgentTurns(Simulator simulator) {
|
|
Set<Agent> survivingEntities = new HashSet<>();
|
|
for (Agent agent : entities) {
|
|
if (agent.performAction(simulator.getNeighbors(agent), simulator)) {
|
|
survivingEntities.add(agent);
|
|
}
|
|
}
|
|
entities = survivingEntities;
|
|
return entities;
|
|
}
|
|
}
|