diff --git a/src/ressources/configuration.xml b/src/ressources/configuration.xml
index c28ef8b..f7bf5ed 100644
--- a/src/ressources/configuration.xml
+++ b/src/ressources/configuration.xml
@@ -60,7 +60,7 @@
-
+
@@ -74,4 +74,4 @@
-
\ No newline at end of file
+
diff --git a/src/simulation/ComponentRoute.java b/src/simulation/ComponentRoute.java
index 09caebe..6a7dca2 100644
--- a/src/simulation/ComponentRoute.java
+++ b/src/simulation/ComponentRoute.java
@@ -1,7 +1,7 @@
package simulation;
public class ComponentRoute {
- public static final int SPEED = 1;
+ public static final int SPEED = 3;
private final Component component;
private final Building outputBuilding;
@@ -22,7 +22,7 @@ public class ComponentRoute {
}
private int normalizedDirection(int from, int to) {
- return Integer.compare(to, from);
+ return Integer.compare(to, from) * SPEED;
}
public void updatePosition() {
@@ -35,7 +35,14 @@ public class ComponentRoute {
}
public boolean isTransitFinished() {
- return x == outputBuilding.getX() && y == outputBuilding.getY();
+ boolean xSame = directionX == 0 ||
+ (directionX > 0 && x >= outputBuilding.getX()) ||
+ (directionX < 0 && x <= outputBuilding.getX());
+ boolean ySame = directionY == 0 ||
+ (directionY > 0 && y >= outputBuilding.getY()) ||
+ (directionY < 0 && y <= outputBuilding.getY());
+
+ return xSame && ySame;
}
public Component getComponent() {
diff --git a/src/simulation/Factory.java b/src/simulation/Factory.java
index f0359c7..4e97d64 100644
--- a/src/simulation/Factory.java
+++ b/src/simulation/Factory.java
@@ -20,6 +20,9 @@ public class Factory extends Building {
// Les usines sans entrées devraient toujours produire
isProductionStarted = metadata.getInputs().size() == 0;
+ if (isProductionStarted) {
+ productionTicks = metadata.getProductionInterval();
+ }
}
@Override
@@ -27,12 +30,26 @@ public class Factory extends Building {
if (!isProductionStarted) return Optional.empty();
productionTicks++;
+ float productionInterval = metadata.getProductionInterval();
+ if (productionTicks < productionInterval) {
+
+ if (productionTicks / productionInterval >= (2 / 3f)) {
+ state = BuildingState.FULL;
+ } else if (productionTicks / productionInterval >= (1 / 3f)) {
+ state = BuildingState.TWO_THIRD;
+ } else {
+ state = BuildingState.ONE_THIRD;
+ }
- if (metadata.getProductionInterval() < productionTicks) {
return Optional.empty();
}
- isProductionStarted = false;
+ if (metadata.getInputs().size() > 0) {
+ isProductionStarted = false;
+ }
+
+ productionTicks = 0;
+ state = BuildingState.EMPTY;
Component newComponent = new Component(metadata.getOutput().getType());
return Optional.of(newComponent);
}
@@ -44,7 +61,7 @@ public class Factory extends Building {
inputCount = inputsCount.get(input.getType());
}
- inputsCount.put(input.getType(), inputCount);
+ inputsCount.put(input.getType(), inputCount + 1);
if (hasEnoughComponents()) {
startProduction();
@@ -70,4 +87,26 @@ public class Factory extends Building {
isProductionStarted = true;
productionTicks = 0;
}
+
+ @Override
+ public String toString() {
+ StringBuilder builder = new StringBuilder("[");
+
+ if (!inputsCount.isEmpty()) {
+ for (ComponentType type : inputsCount.keySet()) {
+ builder.append(type.toString())
+ .append(": ")
+ .append(inputsCount.get(type));
+ }
+ }
+
+ if (isProductionStarted) {
+ builder.append(" (interval: ")
+ .append(productionTicks)
+ .append(')');
+ }
+
+ builder.append(']');
+ return builder.toString();
+ }
}
diff --git a/src/simulation/Warehouse.java b/src/simulation/Warehouse.java
index 8b73b5c..06926aa 100644
--- a/src/simulation/Warehouse.java
+++ b/src/simulation/Warehouse.java
@@ -11,9 +11,10 @@ import java.util.Optional;
public class Warehouse extends Building implements WarehouseSubject {
private final WarehouseMetadata metadata;
- private final Collection planes = new ArrayList<>();
private final Collection observers = new ArrayList<>();
+ private int planeCount = 0;
+
public Warehouse(int id, String type, int x, int y, WarehouseMetadata metadata) {
super(id, type, x, y);
this.metadata = metadata;
@@ -26,7 +27,7 @@ public class Warehouse extends Building implements WarehouseSubject {
@Override
public void processInput(Component input) {
- planes.add(input);
+ planeCount++;
}
private void sellComponent() {
@@ -41,7 +42,7 @@ public class Warehouse extends Building implements WarehouseSubject {
}
public void notifyObservers() {
- boolean isFull = planes.size() >= getCapacity();
+ boolean isFull = planeCount >= getCapacity();
observers.forEach(o -> o.update(isFull));
}
@@ -51,4 +52,11 @@ public class Warehouse extends Building implements WarehouseSubject {
return metadata.getInput().getCapacity();
}
+
+ @Override
+ public String toString() {
+ boolean isFull = planeCount >= getCapacity();
+
+ return String.format("[planes: %d, full: %s]", planeCount, isFull);
+ }
}
diff --git a/src/view/MainPanel.java b/src/view/MainPanel.java
index 232b5d1..ef92e57 100644
--- a/src/view/MainPanel.java
+++ b/src/view/MainPanel.java
@@ -19,7 +19,7 @@ public class MainPanel extends JPanel {
@Serial
private static final long serialVersionUID = 1L;
- private final Map buildingIcons = new HashMap<>();
+ private final Map> buildingIcons = new HashMap<>();
private final Map componentsIcons = new HashMap<>();
private final Map buildingsById = new HashMap<>();
private final Collection componentRoutes = new ArrayList<>();
@@ -48,13 +48,15 @@ public class MainPanel extends JPanel {
buildingIcons.clear();
configuration.getMetadata().values().forEach(m -> {
try {
- loadBuildingImage(m);
+ loadBuildingImages(m);
} catch (IOException e) {
System.err.println("Could not load simulation icons");
}
});
}
+ int x = 0;
+
@Override
public void paint(Graphics g) {
super.paint(g);
@@ -64,40 +66,44 @@ public class MainPanel extends JPanel {
simulationData.getRoutes().forEach(r -> paintRoute(r, g));
simulationData.getBuildings().forEach(b -> paintBuilding(b, g));
+ Collection removedComponentRoutes = new ArrayList<>();
for (ComponentRoute route : componentRoutes) {
paintComponentRoute(route, g);
- // TODO: Not working
if (route.isTransitFinished()) {
- componentRoutes.remove(route);
+ removedComponentRoutes.add(route);
+ route.sendToOutputBuilding();
}
}
- componentRoutes.forEach(r -> paintComponentRoute(r, g));
+
+ // Prevent concurrency problems
+ componentRoutes.removeAll(removedComponentRoutes);
}
private void paintBuilding(Building building, Graphics g) {
building.update().ifPresent(component -> addComponentRoute(component, building));
- Image icon = buildingIcons.get(building.getType());
+ Image icon = buildingIcons.get(building.getType()).get(building.getState());
g.drawImage(icon, building.getX(), building.getY(), null);
+
+ g.drawString(building.toString(), building.getX() - 20, building.getY() - 5);
}
private void addComponentRoute(Component component, Building fromBuilding) {
- Optional oroute = simulationData.getRoutes().stream()
+ Route route = simulationData.getRoutes().stream()
.filter(r -> r.getFrom() == fromBuilding.getId())
- .findFirst();
+ .findFirst().get();
- Route route = oroute.get();
Building toBuilding = buildingsById.get(route.getTo());
componentRoutes.add(new ComponentRoute(component, toBuilding, fromBuilding.getX(), fromBuilding.getY()));
}
private void paintRoute(Route route, Graphics g) {
Building fromBuilding = buildingsById.get(route.getFrom());
- Image fromIcon = buildingIcons.get(fromBuilding.getType());
+ Image fromIcon = buildingIcons.get(fromBuilding.getType()).get(fromBuilding.getState());
Building toBuilding = buildingsById.get(route.getTo());
- Image toIcon = buildingIcons.get(toBuilding.getType());
+ Image toIcon = buildingIcons.get(toBuilding.getType()).get(fromBuilding.getState());
g.drawLine(
fromBuilding.getX() + fromIcon.getWidth(null) / 2,
@@ -116,11 +122,18 @@ public class MainPanel extends JPanel {
g.drawImage(componentIcon, route.getX(), route.getY(), null);
}
- private void loadBuildingImage(BuildingMetadata metadata) throws IOException {
- String iconPath = metadata.getIconsPaths().get(BuildingState.EMPTY);
- Image image = loadImage(iconPath);
+ private void loadBuildingImages(BuildingMetadata metadata) throws IOException {
+ Map statesIcons = new HashMap<>();
+ for (BuildingState state : BuildingState.values()) {
+ statesIcons.put(state, loadBuildingImage(metadata, state));
+ }
- buildingIcons.put(metadata.getType(), image);
+ buildingIcons.put(metadata.getType(), statesIcons);
+ }
+
+ private Image loadBuildingImage(BuildingMetadata metadata, BuildingState state) throws IOException {
+ String iconPath = metadata.getIconsPaths().get(state);
+ return loadImage(iconPath);
}
private Image loadImage(String path) throws IOException {