Skip to content
Snippets Groups Projects
Commit b09248f5 authored by Yanis O's avatar Yanis O
Browse files

[Fix] Les entités se superposent correctement

parent a2dd7f6d
No related branches found
No related tags found
No related merge requests found
......@@ -13,8 +13,8 @@ import javafx.stage.Stage;
public class SimulatorApplication extends javafx.application.Application {
private static final String VIEW_RESOURCE_PATH = "/view/view.fxml";
private static final String APP_NAME = "Firefighter simulator";
private static final int ROW_COUNT = 22;
private static final int COLUMN_COUNT = 22;
private static final int ROW_COUNT = 20;
private static final int COLUMN_COUNT = 20;
private static final int BOX_WIDTH = 25;
private static final int BOX_HEIGHT = 25;
public static final int INITIAL_FIRE_COUNT = 3;
......
......@@ -78,7 +78,11 @@ public interface Board<S> {
public void addEntityAtSquare(Entity entity, Position position);
//Return if the position is completely free
public boolean isPositionEmpty(Position position);
public void addEntityAtSquare(Entity entity, Position position, boolean replaceStates);
//Return if the position is available for the specified priority
public boolean isPositionFree(Position position, int priority);
}
package model;
import java.util.ArrayList;
import java.util.List;
import javafx.scene.paint.Color;
import util.Position;
public class EmptyEntity implements Entity{
private final int priority=3;
private Position position;
private int age;
public EmptyEntity(Position position){
this.position = position;
this.age = 0;
}
public EmptyEntity(Position position, int age){
this.position = position;
this.age = age;
}
@Override
public List<Position> nextTurn(Board<Square> board) {
return new ArrayList<Position>();
}
@Override
public Position getPosition() {
return this.position;
}
@Override
public void setPosition(Position p) {
this.position = p;
}
@Override
public int getAge() {
return age;
}
@Override
public void setAge(int age){
this.age = age;
}
@Override
public void incrementAge() {
this.age = age + 1;
}
@Override
public Color getViewColor() {
return Color.WHITE;
}
@Override
public int getPriority(){return this.priority;}
}
\ No newline at end of file
......@@ -11,7 +11,7 @@ public class EmptySquare implements Entity {
private Position position;
private final Color viewColor = Color.WHITE;
private int age;
private final int priotity = 0;
private final int priotity = -1;
public EmptySquare(Position p) {
this.position = p;
......
......@@ -7,7 +7,7 @@ public abstract class EntityScenario implements Scenario{
public void initScenario(Matrix<Square> matrix){
for(int x = 0; x < matrix.getRows(); x++){
for(int y = 0; y < matrix.getColumns(); y++){
Square s = new Square(new Position(x, y), new EmptyEntity(new Position(x,y)));
Square s = new Square(new Position(x, y), new EmptySquare(new Position(x,y)));
matrix.set(x,y, s);
}
}
......
......@@ -44,14 +44,14 @@ public class FireFighterScenario extends EntityScenario implements Board<Square>
positions.add(new Position(x, y));
}
}
setSquare(new Square(new Position(0,0), new MotorizedFireFighter(new Position(0,0), this)));
//setSquare(new Square(new Position(0,0), new MotorizedFireFighter(new Position(0,0), this)));
while (fireCount < initialFireCount || fireFighterCount < initialFireFightersCount || cloudCount < intialCloudCount) {
Collections.shuffle(positions); // Mélange les positions pour un parcours aléatoire
for (Position pos : positions) {
if (getStates(pos).isEmpty()) {
if (fireCount < initialFireCount && random.nextInt(100) < chance) {
setSquare(new Square(pos, new Fire(pos, this, 1)));
setSquare(new Square(pos, new Fire(pos, 0)));
fireCount++;
if (fireCount == initialFireCount && fireFighterCount == initialFireFightersCount && cloudCount == initialCloudCount && moutainCount == initialMoutainCount) {
return;
......@@ -117,21 +117,11 @@ public class FireFighterScenario extends EntityScenario implements Board<Square>
}
public void addEntityAtSquare(Entity entity, Position position) {
if (!(getStates(position).isEmpty())) {
return;
}
if (doesPositionExist(position)) {
matrix.get(position.x(), position.y()).addEntity(entity);
}
}
public void addEntityAtSquare(Entity entity, Position position, boolean replaceStates) {
if (!(getStates(position).isEmpty()) && !replaceStates) {
return;
}
matrix.get(position.x(), position.y()).addEntity(entity);
;
}
public int rowCount() {
return matrix.getRows();
......@@ -149,6 +139,7 @@ public class FireFighterScenario extends EntityScenario implements Board<Square>
public List<Position> updateToNextGeneration() {
ArrayList<Position> changedPositions = new ArrayList<>();
Iterator<Square> iterator = matrix.iterator();
while (iterator.hasNext()) {
Square s = iterator.next();
if (s.isEmpty())
......@@ -162,16 +153,63 @@ public class FireFighterScenario extends EntityScenario implements Board<Square>
}
List<Entity> entities = new ArrayList<>(s.getEntities());
for (Entity e : entities) {
if(e.getAge() >= stepNumber()-1){
continue;
}
e.incrementAge();
changedPositions.addAll(e.nextTurn(this));
}
}
// Increment the step counter
this.step = this.step + 1;
// matrix.displayMatrix();
// Check if it's time to spawn an AirTanker
if (this.step % 8 == 0) {
// Spawn an AirTanker at a random edge position
spawnAirTanker(changedPositions);
}
return changedPositions;
}
// Helper method to spawn an AirTanker
private void spawnAirTanker(List<Position> changedPositions) {
System.out.println("Spawning AirTanker");
Random rand = new Random();
int edge = rand.nextInt(4); // 0: top, 1: bottom, 2: left, 3: right
Position position = null;
if (edge == 0) { // Top edge (x == 0)
int y = rand.nextInt(columnCount()-1);
position = new Position(0, y);
} else if (edge == 1) { // Bottom edge (x == rowCount() - 1)
int y = rand.nextInt(columnCount());
position = new Position(rowCount() - 1, y);
} else if (edge == 2) { // Left edge (y == 0)
int x = rand.nextInt(rowCount()-1);
position = new Position(x, 0);
} else if (edge == 3) { // Right edge (y == columnCount() - 1)
int x = rand.nextInt(rowCount()-1);
position = new Position(x, columnCount() -1);
} else {
// This else block is technically not necessary because edge will always be between 0 and 3
throw new IllegalStateException("Unexpected edge value: " + edge);
}
System.out.println("Position: " + position.toString());
// Create a new AirTanker
AirTanker airTanker = new AirTanker(position, this);
System.out.println(" direction : " + airTanker.getDirection());
// Add the AirTanker to the board
addEntityAtSquare(airTanker, position);
// Record the changed position
changedPositions.add(position);
}
public Position getNearestEntity(Position fromPos, Class<?> entityType) {
int rows = matrix.getRows();
int cols = matrix.getColumns();
......@@ -228,4 +266,15 @@ public class FireFighterScenario extends EntityScenario implements Board<Square>
public boolean isPositionEmpty(Position position) {
return getStates(position).isEmpty();
}
@Override
public boolean isPositionFree(Position position, int priority) {
List<Entity> entities = matrix.get(position.x(), position.y()).getEntities();
for(Entity e : entities){
if(e.getPriority() == priority){
return false;
}
}
return true;
}
}
......@@ -40,7 +40,7 @@ public class Square {
}
public boolean isEmpty(){
return entities.isEmpty() ||(entities.size() == 1 && entities.get(0) instanceof EmptyEntity);
return entities.isEmpty() ||(entities.size() == 1 && entities.get(0) instanceof EmptySquare);
}
public int getMinimalAge(){
......@@ -73,27 +73,21 @@ public class Square {
if (entities.isEmpty()) {
return Color.WHITE;
} else {
int sumRed = 0, sumGreen = 0, sumBlue = 0;
int maxPriority = -1; // Assuming priorities are non-negative
Color c = Color.WHITE;
// Iterate over entities to find the one with the highest priority
for (Entity e : entities) {
Color color = e.getViewColor();
if (color != null) {
if(sumRed == 255 & sumGreen == 255 & sumBlue == 255){
continue;
}
sumRed += color.getRed();
sumGreen += color.getGreen();
sumBlue += color.getBlue();
// Check for null entities if necessary
if (e != null && e.getPriority() > maxPriority) {
maxPriority = e.getPriority();
c = e.getViewColor();
}
}
int count = entities.size();
sumRed /= count;
sumGreen /= count;
sumBlue /= count;
Color color = new Color((double)(sumRed),(double)(sumGreen),(double)(sumBlue), 1.0);
return color;
return c;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment