From dfb7ef449474e66242d29d86ea348474fe1c2133 Mon Sep 17 00:00:00 2001 From: Stefan-5422 <32109571+Stefan-5422@users.noreply.github.com> Date: Thu, 24 Feb 2022 15:10:30 +0100 Subject: [PATCH] Keep track of loaded chunks --- src/main/java/com/yes/yes/world/Chunk.java | 5 +-- src/main/java/com/yes/yes/world/World.java | 39 +++++++++++++------ src/main/resources/com/yes/yes/main-view.fxml | 2 +- 3 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/yes/yes/world/Chunk.java b/src/main/java/com/yes/yes/world/Chunk.java index 5dece6e..02dcdcb 100644 --- a/src/main/java/com/yes/yes/world/Chunk.java +++ b/src/main/java/com/yes/yes/world/Chunk.java @@ -7,8 +7,8 @@ import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; public class Chunk extends GridPane { - private static final int CHUNK_SIZE = 8; - private static final double ENTITY_SIZE = 50; + public static final int CHUNK_SIZE = 8; + public static final int ENTITY_SIZE = 50; private final Entity[] data = new Entity[CHUNK_SIZE * CHUNK_SIZE]; public Chunk() { @@ -23,7 +23,6 @@ public class Chunk extends GridPane { Rectangle rect = new Rectangle(ENTITY_SIZE, ENTITY_SIZE); - //rect.setFill(Color.color((i+j*16)/257d,(i+j*16)/257d, (i+j*16)/257d,1)); //DEBUG CODE rect.setFill(null); rect.setStroke(Color.BLACK); rect.strokeWidthProperty().set(0.5); diff --git a/src/main/java/com/yes/yes/world/World.java b/src/main/java/com/yes/yes/world/World.java index 399c7ca..bfa57c4 100644 --- a/src/main/java/com/yes/yes/world/World.java +++ b/src/main/java/com/yes/yes/world/World.java @@ -1,36 +1,53 @@ package com.yes.yes.world; import com.yes.yes.utils.Coordinate; +import com.yes.yes.utils.exceptions.ChunkAlreadyExistsException; +import javafx.scene.layout.ColumnConstraints; import javafx.scene.layout.GridPane; +import javafx.scene.layout.RowConstraints; import java.util.HashMap; public class World extends GridPane { private final HashMap chunks = new HashMap<>(); + private final HashMap loadedChunks = new HashMap<>(); public World() { super(); - - for (int x = 0; x < 3; x++) {//DEBUG CODE - for (int y = 0; y < 3; y++) { - chunks.put(new Coordinate(x, y), new Chunk()); - this.add(chunks.get(new Coordinate(x, y)), x, y); - } + //HACK: Move the world by Integer.MAX_VALUE/-1000 in order to avoid -indexes + // So we need to generate all the column indexes to make alignment correct + for (int i = 0; i < (Integer.MAX_VALUE/1000/(Chunk.CHUNK_SIZE*Chunk.ENTITY_SIZE)); i++) { + this.getRowConstraints().add(new RowConstraints(Chunk.CHUNK_SIZE*Chunk.ENTITY_SIZE)); + this.getColumnConstraints().add(new ColumnConstraints(Chunk.CHUNK_SIZE*Chunk.ENTITY_SIZE)); } - - this.unload(new Coordinate(2, 2)); //END DEBUG CODE } public void unload(Coordinate pos) { - this.getChildren().remove(chunks.get(pos)); + Chunk chunk = getChunk(pos); + this.getChildren().remove(chunk); + loadedChunks.remove(pos); + + System.out.println("Unloading: " + pos.x + "; " + pos.y); } public void load(Coordinate pos) { - this.add(chunks.get(pos), pos.x, pos.y); + if(loadedChunks.containsKey(pos)) return; + + Chunk chunk = getChunk(pos); + this.add(chunk, pos.x, pos.y); + loadedChunks.put(pos, chunk); + System.out.println("Loading: " + pos.x + "; " + pos.y); + } + + public void addChunk(Coordinate pos) throws ChunkAlreadyExistsException { + if (chunks.get(pos) == null) { + chunks.put(pos, new Chunk()); + } else { + throw new ChunkAlreadyExistsException(); + } } public Chunk getChunk(Coordinate pos) { - Chunk c = chunks.get(pos); if (c == null) { c = new Chunk(); diff --git a/src/main/resources/com/yes/yes/main-view.fxml b/src/main/resources/com/yes/yes/main-view.fxml index 943c89f..2b46388 100644 --- a/src/main/resources/com/yes/yes/main-view.fxml +++ b/src/main/resources/com/yes/yes/main-view.fxml @@ -5,6 +5,6 @@ -