Skip to content

Commit

Permalink
simulation canvas working
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrMakarewicz committed Dec 22, 2020
1 parent 8f09adb commit 7444623
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 34 deletions.
28 changes: 18 additions & 10 deletions src/sample/AnimalTileFactory.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package sample;

import simulation.Animal;
import simulation.AnimalEnergyComparator;
import simulation.Simulation;

public class AnimalTileFactory{
private Simulation simulation;
AnimalTileFactory(Simulation simulation){
this.simulation = simulation;
}
public TerrainTile getTile(int x, int y){
if (simulation.plantBoard.isPlanted(x,y)){
if (simulation.jungle.contains(x,y))
return TerrainTile.JUNGLEPLANTED;
else return TerrainTile.SAVANNAHPLANTED;
}
else{
if(simulation.jungle.contains(x,y))
return TerrainTile.JUNGLE;
else return TerrainTile.SAVANNAH;
public AnimalTile getTile(int x, int y){
if (simulation.animalBoard.noAnimalsAt(x,y))
return null;
else {
Animal animal = simulation.animalBoard.get(x,y).stream().max(new AnimalEnergyComparator()).get();
double energy = animal.getEnergy();
if (energy < 10) return AnimalTile.ANIMAL1;
if (energy < 20) return AnimalTile.ANIMAL2;
if (energy < 30) return AnimalTile.ANIMAL3;
if (energy < 40) return AnimalTile.ANIMAL4;
if (energy < 50) return AnimalTile.ANIMAL5;
if (energy < 60) return AnimalTile.ANIMAL6;
if (energy < 70) return AnimalTile.ANIMAL7;
if (energy < 80) return AnimalTile.ANIMAL8;
if (energy < 90) return AnimalTile.ANIMAL9;
else return AnimalTile.ANIMAL10;
}
}
}
Expand Down
28 changes: 22 additions & 6 deletions src/sample/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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;
Expand All @@ -18,7 +20,7 @@ public class Main extends Application {

@Override
public void start(Stage stage) throws Exception{
Simulation s1 = new Simulation("Symulacja",50,50,0.2,12,22,13,200);
Simulation s1 = new Simulation("Symulacja",15,15,0.1,2,50,8,80);
displaySimulation(s1,stage);
}

Expand All @@ -33,14 +35,28 @@ public void displaySimulation(Simulation simulation, Stage stage) throws Interru
root.getChildren().add(canvas);
stage.setScene(simulationScene);
stage.setResizable(false);

simulation.start();
canvas.update();
stage.show();
Task<Integer> task = new Task<>() {
@Override protected Integer call() throws Exception {
int iterations;
for (iterations = 0; iterations < 100000; iterations++) {
if (isCancelled()) {
break;
}
simulation.simulateOneDay();
canvas.update();
System.out.println("CANVAS UPDATE");
Thread.sleep(50);
}
return iterations;
}
};
Thread th = new Thread(task);
th.start();

for (int i = 0; i<100; i++){
canvas.update();
Thread.sleep(1000);
simulation.simulateOneDay();
}

}

Expand Down
22 changes: 18 additions & 4 deletions src/sample/SimulationCanvas.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,46 @@
package sample;

import javafx.scene.canvas.Canvas;
import simulation.Location;
import simulation.Simulation;

import java.util.Locale;

public class SimulationCanvas extends Canvas {
private final Simulation simulation;
private final TerrainTileFactory terrainTileFactory;
private final AnimalTileFactory animalTileFactory;
private final int tileSize = 5;
private final int tileSize = 10;
SimulationCanvas(Simulation simulation){
super(simulation.getWidth()*10,simulation.getHeight()*10);
this.simulation = simulation;
this.terrainTileFactory = new TerrainTileFactory(simulation);
this.animalTileFactory = new AnimalTileFactory(simulation);
}
public void update(){
public void drawBackground(){
for (int i = 0; i<simulation.getWidth(); i++){
for (int j = 0; j<simulation.getHeight(); j++){
drawTile(i,j, terrainTileFactory.getTile(i,j));
drawTile(i,j,animalTileFactory.getTile(i,j));
if (!simulation.animalBoard.noAnimalsAt(i,j))
drawTile(i,j, animalTileFactory.getTile(i,j));
}
}
}
public void update(){
drawBackground();
// for (Location location : simulation.animalBoard.getAnimalLocations()){
// int x = location.getX();
// int y = location.getY();
// drawTile(x,y,animalTileFactory.getTile(x,y));
// }


}
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);
//System.out.println("Drawing "+tile.toString()+" at "+drawX+" "+drawY);
}

}
7 changes: 7 additions & 0 deletions src/simulation/AnimalBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public void remove(Animal animal) {
locationAnimalMap.remove(locationToRemove);
}
}
public boolean noAnimalsAt(int x, int y){
return noAnimalsAt(new Location(x,y));
}

public List<Location> getAnimalLocations(){
return this.locationAnimalMap.keySet().stream().collect(Collectors.toList());
}

public boolean noAnimalsAt(Location location){
return locationAnimalMap.get(location) == null;
Expand Down
2 changes: 1 addition & 1 deletion src/simulation/Jungle.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class Jungle {
Jungle(int boardWidth, int boardHeight, double jungleRatio) throws InvalidRectangleException{
this(
(int) Math.round((1.0 - Math.sqrt(jungleRatio))*0.5*boardWidth),
(int) Math.round((1.0 + Math.sqrt(jungleRatio))*0.5*boardWidth),
(int) Math.round((1.0 - Math.sqrt(jungleRatio))*0.5*boardHeight),
(int) Math.round((1.0 + Math.sqrt(jungleRatio))*0.5*boardWidth),
(int) Math.round((1.0 + Math.sqrt(jungleRatio))*0.5*boardHeight));
}

Expand Down
13 changes: 1 addition & 12 deletions src/simulation/PlantBoard.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,15 @@ public void plant(int x, int y){
plant(new Location(x,y));
}
public void plant(Location location){
if (isPlanted(location)){
int plantsNum = getPlantedSpots().get(location);
getPlantedSpots().remove(location);
getPlantedSpots().put(location,plantsNum+1);
}
else{
getPlantedSpots().put(location,1);
}
getPlantedSpots().put(location,1);
}
public void unplant(Location location) throws UnplantingUnplantedLocationException{
if (! isPlanted(location)){
throw new UnplantingUnplantedLocationException("Trying to unplant an unplanted location"
+ "x: " + location.getX() + "y: "+ location.getY() + "\n");
}
else {
int plantsNum = getPlantedSpots().get(location);
getPlantedSpots().remove(location);
if (plantsNum != 1){
getPlantedSpots().put(location,plantsNum-1);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/simulation/Simulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public Location toBoardLimits(Location location){
}
public void eatPlants() throws UnplantingUnplantedLocationException{
for (Location location : plantBoard.getPlantedLocations()) {
plantBoard.unplant(location);
List<Animal> animals = animalBoard.get(location);
if (!animals.isEmpty()) {
plantBoard.unplant(location);
double highestEnergy = animals.stream()
.max(new AnimalEnergyComparator())
.get()
Expand Down

0 comments on commit 7444623

Please sign in to comment.