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;
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);
+28 -11
View File
@@ -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<Coordinate, Chunk> chunks = new HashMap<>();
private final HashMap<Coordinate, Chunk> 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();
@@ -5,6 +5,6 @@
<?import javafx.scene.layout.VBox?>
<?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">
</VBox>