Start dynamic network

This commit is contained in:
FyloZ 2022-06-12 21:15:56 -04:00
parent 9445638440
commit a614f171c7
Signed by: william
GPG Key ID: 835378AE9AF4AE97
5 changed files with 60 additions and 26 deletions

View File

@ -182,8 +182,8 @@ public class XmlConfigurationParser implements ConfigurationParser {
Collection<Route> routes = new ArrayList<>(); Collection<Route> routes = new ArrayList<>();
for (Element elem : new IterableElementList(routesElement.getChildNodes())) { for (Element elem : new IterableElementList(routesElement.getChildNodes())) {
int from = Integer.parseInt(elem.getAttribute(ATTRIBUTE_TO)); int from = Integer.parseInt(elem.getAttribute(ATTRIBUTE_FROM));
int to = Integer.parseInt(elem.getAttribute(ATTRIBUTE_FROM)); int to = Integer.parseInt(elem.getAttribute(ATTRIBUTE_TO));
routes.add(new Route(from, to)); routes.add(new Route(from, to));
} }

View File

@ -1,8 +1,13 @@
package simulation; package simulation;
public class ComponentRoute { public class ComponentRoute {
public static final int SPEED = 1;
private final Component component; private final Component component;
private final Building outputBuilding; private final Building outputBuilding;
private final int directionX;
private final int directionY;
private int x; private int x;
private int y; private int y;
@ -11,6 +16,26 @@ public class ComponentRoute {
this.outputBuilding = outputBuilding; this.outputBuilding = outputBuilding;
this.x = x; this.x = x;
this.y = y; this.y = y;
directionX = normalizedDirection(x, outputBuilding.getX());
directionY = normalizedDirection(y, outputBuilding.getY());
}
private int normalizedDirection(int from, int to) {
return Integer.compare(to, from);
}
public void updatePosition() {
x += directionX;
y += directionY;
}
public void sendToOutputBuilding() {
outputBuilding.processInput(component);
}
public boolean isTransitFinished() {
return x == outputBuilding.getX() && y == outputBuilding.getY();
} }
public Component getComponent() { public Component getComponent() {
@ -21,11 +46,11 @@ public class ComponentRoute {
return outputBuilding; return outputBuilding;
} }
public void setX(int x) { public int getX() {
this.x = x; return x;
} }
public void setY(int y) { public int getY() {
this.y = y; return y;
} }
} }

View File

@ -11,12 +11,15 @@ public class Factory extends Building {
private final FactoryMetadata metadata; private final FactoryMetadata metadata;
private final Map<ComponentType, Integer> inputsCount = new HashMap<>(); private final Map<ComponentType, Integer> inputsCount = new HashMap<>();
private boolean isProductionStarted = false; private boolean isProductionStarted;
private long productionTicks = 0L; private long productionTicks = 0L;
public Factory(int id, String type, int x, int y, FactoryMetadata metadata) { public Factory(int id, String type, int x, int y, FactoryMetadata metadata) {
super(id, type, x, y); super(id, type, x, y);
this.metadata = metadata; this.metadata = metadata;
// Les usines sans entrées devraient toujours produire
isProductionStarted = metadata.getInputs().size() == 0;
} }
@Override @Override

View File

@ -7,6 +7,7 @@ import metadata.WarehouseMetadata;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Optional;
public class Warehouse extends Building implements WarehouseSubject { public class Warehouse extends Building implements WarehouseSubject {
private final WarehouseMetadata metadata; private final WarehouseMetadata metadata;
@ -19,7 +20,8 @@ public class Warehouse extends Building implements WarehouseSubject {
} }
@Override @Override
public void update() { public Optional<Component> update() {
return Optional.empty();
} }
@Override @Override

View File

@ -4,8 +4,8 @@ import configuration.SimulationConfiguration;
import configuration.SimulationConfigurationSingleton; import configuration.SimulationConfigurationSingleton;
import configuration.SimulationData; import configuration.SimulationData;
import metadata.BuildingMetadata; import metadata.BuildingMetadata;
import simulation.*;
import simulation.Component; import simulation.Component;
import simulation.*;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.swing.*; import javax.swing.*;
@ -55,42 +55,39 @@ public class MainPanel extends JPanel {
}); });
} }
private final float componentSpeed = 1f;
private int componentX = 0;
private int componentY = 0;
@Override @Override
public void paint(Graphics g) { public void paint(Graphics g) {
super.paint(g); super.paint(g);
// On ajoute à la position le delta x et y de la vitesse
// position.translate(speed.x, speed.y);
// g.fillRect(position.x, position.y, size, size);
if (configuration == null) return; if (configuration == null) return;
simulationData.getRoutes().forEach(r -> paintRoute(r, g)); simulationData.getRoutes().forEach(r -> paintRoute(r, g));
simulationData.getBuildings().forEach(b -> paintBuilding(b, g)); simulationData.getBuildings().forEach(b -> paintBuilding(b, g));
Image image = componentsIcons.get(ComponentType.METAL); for (ComponentRoute route : componentRoutes) {
g.drawImage(image, componentX, componentY, null); paintComponentRoute(route, g);
componentX += 1 * componentSpeed; // TODO: Not working
componentY += 1 * componentSpeed; if (route.isTransitFinished()) {
componentRoutes.remove(route);
}
}
componentRoutes.forEach(r -> paintComponentRoute(r, g));
} }
private void paintBuilding(Building building, Graphics g) { private void paintBuilding(Building building, Graphics g) {
building.update().ifPresent(component -> addComponentRoute(component, building.getId())); building.update().ifPresent(component -> addComponentRoute(component, building));
Image icon = buildingIcons.get(building.getType()); Image icon = buildingIcons.get(building.getType());
g.drawImage(icon, building.getX(), building.getY(), null); g.drawImage(icon, building.getX(), building.getY(), null);
} }
private void addComponentRoute(Component component, Building fromBuilding) { private void addComponentRoute(Component component, Building fromBuilding) {
Route route = simulationData.getRoutes().stream() Optional<Route> oroute = simulationData.getRoutes().stream()
.filter(r -> r.getFrom() == fromBuilding.getId()) .filter(r -> r.getFrom() == fromBuilding.getId())
.findFirst().get(); .findFirst();
Route route = oroute.get();
Building toBuilding = buildingsById.get(route.getTo()); Building toBuilding = buildingsById.get(route.getTo());
componentRoutes.add(new ComponentRoute(component, toBuilding, fromBuilding.getX(), fromBuilding.getY())); componentRoutes.add(new ComponentRoute(component, toBuilding, fromBuilding.getX(), fromBuilding.getY()));
} }
@ -102,14 +99,21 @@ public class MainPanel extends JPanel {
Building toBuilding = buildingsById.get(route.getTo()); Building toBuilding = buildingsById.get(route.getTo());
Image toIcon = buildingIcons.get(toBuilding.getType()); Image toIcon = buildingIcons.get(toBuilding.getType());
g.drawLine(fromBuilding.getX() + fromIcon.getWidth(null) / 2, fromBuilding.getY() + fromIcon.getHeight(null) / 2, toBuilding.getX() + toIcon.getWidth(null) / 2, toBuilding.getY() + toIcon.getHeight(null) / 2); g.drawLine(
fromBuilding.getX() + fromIcon.getWidth(null) / 2,
fromBuilding.getY() + fromIcon.getHeight(null) / 2,
toBuilding.getX() + toIcon.getWidth(null) / 2,
toBuilding.getY() + toIcon.getHeight(null) / 2
);
} }
private void paintComponentRoute(ComponentRoute route, Graphics g) { private void paintComponentRoute(ComponentRoute route, Graphics g) {
ComponentType componentType = route.getComponent().getType(); ComponentType componentType = route.getComponent().getType();
Image componentIcon = componentsIcons.get(componentType); Image componentIcon = componentsIcons.get(componentType);
Vector route.updatePosition();
g.drawImage(componentIcon, route.getX(), route.getY(), null);
} }
private void loadBuildingImage(BuildingMetadata metadata) throws IOException { private void loadBuildingImage(BuildingMetadata metadata) throws IOException {