Start dynamic network

This commit is contained in:
william 2022-06-15 09:54:19 -04:00
parent a614f171c7
commit 6f1a3b91aa
5 changed files with 93 additions and 26 deletions

View File

@ -60,7 +60,7 @@
<usine type="usine-matiere" id="11" x="32" y="32"/>
<usine type="usine-aile" id="21" x="320" y="32"/>
<usine type="usine-assemblage" id="41" x="160" y="192"/>
<usine type="entrepot" id="51" x="640" y="192"/>
<usine type="entrepot" id="51" x="440" y="192"/> <!-- x=640 -->
<usine type="usine-matiere" id="13" x="544" y="576"/>
<usine type="usine-matiere" id="12" x="96" y="352"/>
<usine type="usine-moteur" id="31" x="320" y="352"/>

View File

@ -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() {

View File

@ -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();
}
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();
}
}

View File

@ -11,9 +11,10 @@ import java.util.Optional;
public class Warehouse extends Building implements WarehouseSubject {
private final WarehouseMetadata metadata;
private final Collection<Component> planes = new ArrayList<>();
private final Collection<WarehouseObserver> 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);
}
}

View File

@ -19,7 +19,7 @@ public class MainPanel extends JPanel {
@Serial
private static final long serialVersionUID = 1L;
private final Map<String, Image> buildingIcons = new HashMap<>();
private final Map<String, Map<BuildingState, Image>> buildingIcons = new HashMap<>();
private final Map<ComponentType, Image> componentsIcons = new HashMap<>();
private final Map<Integer, Building> buildingsById = new HashMap<>();
private final Collection<ComponentRoute> 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<ComponentRoute> 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<Route> 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<BuildingState, Image> 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 {