Skip to content

Commit

Permalink
added simulation canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
PiotrMakarewicz committed Dec 21, 2020
1 parent 305f6d2 commit 8f09adb
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 17 deletions.
26 changes: 26 additions & 0 deletions src/sample/AnimalTile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package sample;

import javafx.scene.image.Image;

public enum AnimalTile implements Tile{
ANIMAL1("images/animal-1.png"),
ANIMAL2("images/animal-2.png"),
ANIMAL3("images/animal-3.png"),
ANIMAL4("images/animal-4.png"),
ANIMAL5("images/animal-5.png"),
ANIMAL6("images/animal-6.png"),
ANIMAL7("images/animal-7.png"),
ANIMAL8("images/animal-8.png"),
ANIMAL9("images/animal-9.png"),
ANIMAL10("images/animal-10.png");

private final String imagePath;

AnimalTile(String imagePath){
this.imagePath = imagePath;
}

public Image getImage(){
return new Image(this.imagePath);
}
}
23 changes: 23 additions & 0 deletions src/sample/AnimalTileFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package sample;

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;
}
}
}

29 changes: 28 additions & 1 deletion src/sample/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,46 @@
import javafx.application.Application;
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;
import simulation.SimulationErrorException;

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);
displaySimulation(s1,stage);
}

public void displaySimulation(Simulation simulation, Stage stage) throws InterruptedException, SimulationErrorException {
Group root = new Group();
double width = simulation.getWidth()*10;
double height = simulation.getHeight()*10;
Scene simulationScene = new Scene(root, width, height, Color.BLACK);

final SimulationCanvas canvas = new SimulationCanvas(simulation);

root.getChildren().add(canvas);
stage.setScene(simulationScene);
stage.setResizable(false);
simulation.start();
stage.show();

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

}

public static void main(String[] args) {
launch(args);
Expand Down
32 changes: 32 additions & 0 deletions src/sample/SimulationCanvas.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package sample;

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

public class SimulationCanvas extends Canvas {
private final Simulation simulation;
private final TerrainTileFactory terrainTileFactory;
private final AnimalTileFactory animalTileFactory;
private final int tileSize = 5;
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(){
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));
}
}
}
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);
}

}
4 changes: 4 additions & 0 deletions src/sample/SimulationWindowController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package sample;

public class SimulationWindowController {
}
19 changes: 19 additions & 0 deletions src/sample/TerrainTile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package sample;

import javafx.scene.image.Image;

public enum TerrainTile implements Tile{
JUNGLE("images/jungle-tile.png"),
JUNGLEPLANTED("images/jungle-tile-planted.png"),
SAVANNAH("images/savannah-tile.png"),
SAVANNAHPLANTED("images/savannah-tile-planted.png");
private final String imagePath;

TerrainTile(String imagePath){
this.imagePath = imagePath;
}

public Image getImage(){
return new Image(this.imagePath);
}
}
22 changes: 22 additions & 0 deletions src/sample/TerrainTileFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package sample;

import simulation.Simulation;

public class TerrainTileFactory {
private Simulation simulation;
TerrainTileFactory(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;
}
}
}
7 changes: 7 additions & 0 deletions src/sample/Tile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package sample;

import javafx.scene.image.Image;

public interface Tile {
public Image getImage();
}
7 changes: 7 additions & 0 deletions src/simulation/InvalidStartingParametersException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package simulation;

public class InvalidStartingParametersException extends Exception {
InvalidStartingParametersException(String errorMessage){
super(errorMessage);
}
}
11 changes: 6 additions & 5 deletions src/simulation/Jungle.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ public class Jungle {
private final int x2;
private final int y1;
private final int y2;
Jungle(int boardWidth, int boardHeight, double jungleratio){
this.x1 = (int) Math.round((1.0 - jungleratio)*0.5*boardWidth);
this.x2 = (int) Math.round((1.0 + jungleratio)*0.5*boardWidth);
this.y1 = (int) Math.round((1.0 - jungleratio)*0.5*boardHeight);
this.y2 = (int) Math.round((1.0 + jungleratio)*0.5*boardWidth);
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*boardHeight));
}

Jungle(int x1, int y1, int x2, int y2) throws InvalidRectangleException{
Expand Down
28 changes: 17 additions & 11 deletions src/simulation/Simulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ public class Simulation {
public final PlantBoard plantBoard = new PlantBoard();
private final int width;
private final int height;
private final Jungle jungle;
public final Jungle jungle;
private final double moveEnergy;
private final double plantEnergy;
private final int initialAnimalsNum;
private final double initialEnergy;

public Simulation(String name, int width, int height, double jungleRatio, double moveEnergy, double plantEnergy, int initialAnimalsNum, double initialEnergy) {
public Simulation(String name, int width, int height, double jungleRatio, double moveEnergy, double plantEnergy, int initialAnimalsNum, double initialEnergy) throws InvalidStartingParametersException{
this.name = name;
this.width = width;
this.height = height;
this.initialEnergy = initialEnergy;
this.jungle = new Jungle(width,height,jungleRatio);
try {
this.jungle = new Jungle(width, height, jungleRatio);
} catch(InvalidRectangleException e){
throw new InvalidStartingParametersException(e.toString() + " This may have occurred due to jungleRatio or board dimensions being too small.");
}
this.moveEnergy = moveEnergy;
this.plantEnergy = plantEnergy;
this.minimalReproduceEnergy = initialEnergy/2;
Expand All @@ -38,12 +42,12 @@ private void addPlants(){
boolean plantedOutsideJungle = false;
while (!plantedInJungle || !plantedOutsideJungle){
Location location = Location.getRandom(width,height);
if(jungle.contains(location) && !plantedInJungle){
if(jungle.contains(location) && !plantedInJungle && animalBoard.noAnimalsAt(location)){
plantBoard.plant(location);
System.out.println("Added in-jungle plant at "+location.toString());
plantedInJungle = true;
}
else if(!jungle.contains(location) && !plantedOutsideJungle){
else if(!jungle.contains(location) && !plantedOutsideJungle && animalBoard.noAnimalsAt(location)){
plantBoard.plant(location);
System.out.println("Added outside-jungle plant at "+location.toString());
plantedOutsideJungle = true;
Expand Down Expand Up @@ -136,12 +140,6 @@ public void addInitialAnimals(){

public void start(){
addInitialAnimals();
try {
for (int i = 0; i < 25; i++)
simulateOneDay();
} catch (SimulationErrorException e){
System.out.println(e.toString());
}
}

public void simulateOneDay() throws SimulationErrorException{
Expand Down Expand Up @@ -172,4 +170,12 @@ public int getCurrentDay() {
public String getName() {
return name;
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}
}

0 comments on commit 8f09adb

Please sign in to comment.