Keep track of loaded chunks

This commit is contained in:
Stefan-5422
2022-02-24 15:10:30 +01:00
parent 27065159a8
commit dfb7ef4494
3 changed files with 31 additions and 15 deletions
+2 -3
View File
@@ -7,8 +7,8 @@ import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle; import javafx.scene.shape.Rectangle;
public class Chunk extends GridPane { public class Chunk extends GridPane {
private static final int CHUNK_SIZE = 8; public static final int CHUNK_SIZE = 8;
private static final double ENTITY_SIZE = 50; public static final int ENTITY_SIZE = 50;
private final Entity[] data = new Entity[CHUNK_SIZE * CHUNK_SIZE]; private final Entity[] data = new Entity[CHUNK_SIZE * CHUNK_SIZE];
public Chunk() { public Chunk() {
@@ -23,7 +23,6 @@ public class Chunk extends GridPane {
Rectangle rect = new Rectangle(ENTITY_SIZE, ENTITY_SIZE); 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.setFill(null);
rect.setStroke(Color.BLACK); rect.setStroke(Color.BLACK);
rect.strokeWidthProperty().set(0.5); rect.strokeWidthProperty().set(0.5);
+28 -11
View File
@@ -1,36 +1,53 @@
package com.yes.yes.world; package com.yes.yes.world;
import com.yes.yes.utils.Coordinate; 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.GridPane;
import javafx.scene.layout.RowConstraints;
import java.util.HashMap; import java.util.HashMap;
public class World extends GridPane { public class World extends GridPane {
private final HashMap<Coordinate, Chunk> chunks = new HashMap<>(); private final HashMap<Coordinate, Chunk> chunks = new HashMap<>();
private final HashMap<Coordinate, Chunk> loadedChunks = new HashMap<>();
public World() { public World() {
super(); super();
//HACK: Move the world by Integer.MAX_VALUE/-1000 in order to avoid -indexes
for (int x = 0; x < 3; x++) {//DEBUG CODE // So we need to generate all the column indexes to make alignment correct
for (int y = 0; y < 3; y++) { for (int i = 0; i < (Integer.MAX_VALUE/1000/(Chunk.CHUNK_SIZE*Chunk.ENTITY_SIZE)); i++) {
chunks.put(new Coordinate(x, y), new Chunk()); this.getRowConstraints().add(new RowConstraints(Chunk.CHUNK_SIZE*Chunk.ENTITY_SIZE));
this.add(chunks.get(new Coordinate(x, y)), x, y); 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) { 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) { 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) { public Chunk getChunk(Coordinate pos) {
Chunk c = chunks.get(pos); Chunk c = chunks.get(pos);
if (c == null) { if (c == null) {
c = new Chunk(); c = new Chunk();
@@ -5,6 +5,6 @@
<?import javafx.scene.layout.VBox?> <?import javafx.scene.layout.VBox?>
<?import javafx.scene.control.Button?> <?import javafx.scene.control.Button?>
<VBox alignment="CENTER" spacing="20.0" xmlns:fx="http://javafx.com/fxml" <VBox spacing="20.0" xmlns:fx="http://javafx.com/fxml"
fx:controller="com.yes.yes.MainController" fx:id="root"> fx:controller="com.yes.yes.MainController" fx:id="root">
</VBox> </VBox>