Skip to content

Commit

Permalink
moved images, added json loading
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrMakarewicz committed Dec 22, 2020
1 parent c8a5db7 commit 56beeda
Show file tree
Hide file tree
Showing 22 changed files with 49 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/*
*.iml
test/*
test/*
out/*
9 changes: 9 additions & 0 deletions parameters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"width": 50,
"height": 50,
"jungleRatio": 0.05,
"moveEnergy": 1.0,
"initialEnergy": 100.0,
"plantEnergy": 50.0,
"initialAnimals": 10
}
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
2 changes: 1 addition & 1 deletion src/sample/AnimalTileFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import simulation.Simulation;

public class AnimalTileFactory{
private Simulation simulation;
private final Simulation simulation;
AnimalTileFactory(Simulation simulation){
this.simulation = simulation;
}
Expand Down
16 changes: 2 additions & 14 deletions src/sample/Main.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
package sample;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import simulation.Simulation;
Expand All @@ -20,7 +13,7 @@ public class Main extends Application {

@Override
public void start(Stage stage) throws Exception{
Simulation s1 = new Simulation("Symulacja",100,100,0.1,1,100,8,80);
Simulation s1 = ParametersLoader.load("parameters.json");
displaySimulation(s1,stage);
}

Expand All @@ -43,7 +36,7 @@ public void displaySimulation(Simulation simulation, Stage stage) throws Interru
Task<Integer> task = new Task<>() {
@Override protected Integer call() throws Exception {
int iterations;
for (iterations = 0; iterations < 100000; iterations++) {
for (iterations = 0; true; iterations++) {
if (isCancelled()) {
break;
}
Expand All @@ -59,14 +52,9 @@ public void displaySimulation(Simulation simulation, Stage stage) throws Interru
};
Thread th = new Thread(task);
th.start();


}

public static void main(String[] args) {
launch(args);
// Simulation simulation = new Simulation("Sysad",3,3,0.3,5,100,8, 100);
// simulation.start();
//launch(args);
}
}
33 changes: 33 additions & 0 deletions src/sample/ParametersLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package sample;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import simulation.InvalidStartingParametersException;
import simulation.Simulation;

import java.io.IOException;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Path;

public class ParametersLoader {
public static Simulation load(String path) throws IOException, ParseException, InvalidStartingParametersException {
System.out.println(Path.of(path).toAbsolutePath());
String string = Files.readString(Path.of(path));

JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject) parser.parse(string);
return new Simulation(
"simulation",
((Long) jsonObject.get("width")).intValue(),
((Long) jsonObject.get("height")).intValue(),
(Double) jsonObject.get("jungleRatio"),
(Double) jsonObject.get("moveEnergy"),
(Double) jsonObject.get("plantEnergy"),
((Long) jsonObject.get("initialAnimals")).intValue(),
(Double) jsonObject.get("initialEnergy")
);
}
}
3 changes: 0 additions & 3 deletions src/sample/SimulationCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import simulation.Location;
import simulation.Simulation;

import java.util.Locale;

public class SimulationCanvas extends Canvas {
private final Simulation simulation;
private final TerrainTileFactory terrainTileFactory;
Expand Down Expand Up @@ -56,7 +54,6 @@ private void drawTile(int x, int y, Tile tile){
int drawX = x* tileSize;
int drawY = y* tileSize;
this.getGraphicsContext2D().drawImage(tile.getImage(),drawX,drawY);
//System.out.println("Drawing "+tile.toString()+" at "+drawX+" "+drawY);
}

}
2 changes: 1 addition & 1 deletion src/sample/TerrainTileFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import simulation.Simulation;

public class TerrainTileFactory {
private Simulation simulation;
private final Simulation simulation;
TerrainTileFactory(Simulation simulation){
this.simulation = simulation;
}
Expand Down
2 changes: 1 addition & 1 deletion src/sample/Tile.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import javafx.scene.image.Image;

public interface Tile {
public Image getImage();
Image getImage();
}

0 comments on commit 56beeda

Please sign in to comment.