Rename some method names for better clarity

This commit is contained in:
Stone_Red
2022-04-07 14:18:07 +02:00
parent e68366d4a5
commit 96abb1ace5
3 changed files with 33 additions and 25 deletions
@@ -27,14 +27,14 @@ public class PlayerManager {
Scene scene = world.getScene(); Scene scene = world.getScene();
scene.setOnKeyPressed(this::ProcessKeyPress); scene.setOnKeyPressed(this::ProcessKeyPress);
scene.widthProperty().addListener((e) -> loadAllOnScreen()); scene.widthProperty().addListener((e) -> redrawChunks());
scene.heightProperty().addListener((e) -> loadAllOnScreen()); scene.heightProperty().addListener((e) -> redrawChunks());
world.setOnMouseClicked(this::ProcessClick); world.setOnMouseClicked(this::ProcessClick);
world.setTranslateX(Integer.MAX_VALUE / -5000d); world.setTranslateX(Integer.MAX_VALUE / -5000d);
world.setTranslateY(Integer.MAX_VALUE / -5000d); world.setTranslateY(Integer.MAX_VALUE / -5000d);
loadAllOnScreen(); redrawChunks();
} }
private void ProcessClick(MouseEvent mouse) { private void ProcessClick(MouseEvent mouse) {
@@ -75,47 +75,49 @@ public class PlayerManager {
} }
Size loadedChunks = getAmountOfLoadedChunks(); Size preferredAmountOfChunks = getPreferredAmountOfChunks();
Coordinate chunkPosition = getCurrentChunkPosition(); Coordinate chunkPosition = getCurrentChunkPosition();
if (chunkPosition.x < chunkPos.x) { if (chunkPosition.x < chunkPos.x) {
for (int i = 0; i < loadedChunks.y; i++) { for (int i = 0; i < preferredAmountOfChunks.y; i++) {
world.load(new Coordinate(chunkPosition.x, chunkPosition.y + i)); world.loadChunk(new Coordinate(chunkPosition.x, chunkPosition.y + i));
world.unload(new Coordinate(chunkPos.x + loadedChunks.x - 1, chunkPosition.y + i)); world.unloadChunk(new Coordinate(chunkPos.x + preferredAmountOfChunks.x - 1, chunkPosition.y + i));
} }
} }
if (chunkPosition.x > chunkPos.x) { if (chunkPosition.x > chunkPos.x) {
for (int i = 0; i < loadedChunks.y; i++) { for (int i = 0; i < preferredAmountOfChunks.y; i++) {
world.load(new Coordinate(chunkPosition.x + loadedChunks.x - 1, chunkPosition.y + i)); world.loadChunk(new Coordinate(chunkPosition.x + preferredAmountOfChunks.x - 1, chunkPosition.y + i));
world.unload(new Coordinate(chunkPos.x, chunkPosition.y + i)); world.unloadChunk(new Coordinate(chunkPos.x, chunkPosition.y + i));
} }
} }
if (chunkPosition.y > chunkPos.y) { if (chunkPosition.y > chunkPos.y) {
for (int i = 0; i < loadedChunks.x; i++) { for (int i = 0; i < preferredAmountOfChunks.x; i++) {
world.load(new Coordinate(chunkPosition.x + i, chunkPosition.y + loadedChunks.y - 1)); world.loadChunk(new Coordinate(chunkPosition.x + i, chunkPosition.y + preferredAmountOfChunks.y - 1));
world.unload(new Coordinate(chunkPosition.x + i, chunkPos.y)); world.unloadChunk(new Coordinate(chunkPosition.x + i, chunkPos.y));
} }
} }
if (chunkPosition.y < chunkPos.y) { if (chunkPosition.y < chunkPos.y) {
for (int i = 0; i < loadedChunks.x; i++) { for (int i = 0; i < preferredAmountOfChunks.x; i++) {
world.load(new Coordinate(chunkPosition.x + i, chunkPosition.y)); world.loadChunk(new Coordinate(chunkPosition.x + i, chunkPosition.y));
world.unload(new Coordinate(chunkPosition.x + i, chunkPos.y + loadedChunks.y - 1)); world.unloadChunk(new Coordinate(chunkPosition.x + i, chunkPos.y + preferredAmountOfChunks.y - 1));
} }
} }
chunkPos = new Coordinate(chunkPosition.x, chunkPosition.y); chunkPos = new Coordinate(chunkPosition.x, chunkPosition.y);
} }
private void loadAllOnScreen() { private void redrawChunks() {
Size loadedChunks = getAmountOfLoadedChunks(); Size preferredAmountOfChunks = getPreferredAmountOfChunks();
Coordinate chunkPosition = getCurrentChunkPosition(); Coordinate chunkPosition = getCurrentChunkPosition();
for (int y = 0; y < loadedChunks.y; y++) { world.unloadAllChunks();
for (int x = 0; x < loadedChunks.x; x++) {
world.load(new Coordinate(chunkPosition.x + x, chunkPosition.y + y)); for (int y = 0; y < preferredAmountOfChunks.y; y++) {
for (int x = 0; x < preferredAmountOfChunks.x; x++) {
world.loadChunk(new Coordinate(chunkPosition.x + x, chunkPosition.y + y));
} }
} }
} }
@@ -129,7 +131,7 @@ public class PlayerManager {
return new Coordinate(chunkX, chunkY); return new Coordinate(chunkX, chunkY);
} }
private Size getAmountOfLoadedChunks() { private Size getPreferredAmountOfChunks() {
int loadedChunksX = (int) Math.ceil(world.getScene().getWidth() / Chunk.CHUNK_SIZE / Chunk.ENTITY_SIZE) + 1; int loadedChunksX = (int) Math.ceil(world.getScene().getWidth() / Chunk.CHUNK_SIZE / Chunk.ENTITY_SIZE) + 1;
int loadedChunksY = (int) Math.ceil(world.getScene().getHeight() / Chunk.CHUNK_SIZE / Chunk.ENTITY_SIZE) + 1; int loadedChunksY = (int) Math.ceil(world.getScene().getHeight() / Chunk.CHUNK_SIZE / Chunk.ENTITY_SIZE) + 1;
+8 -2
View File
@@ -22,13 +22,13 @@ public class World extends GridPane {
} }
} }
public void unload(Coordinate pos) { public void unloadChunk(Coordinate pos) {
Chunk chunk = getChunk(pos); Chunk chunk = getChunk(pos);
this.getChildren().remove(chunk); this.getChildren().remove(chunk);
loadedChunks.remove(pos); loadedChunks.remove(pos);
} }
public void load(Coordinate pos) { public void loadChunk(Coordinate pos) {
if (loadedChunks.containsKey(pos)) return; if (loadedChunks.containsKey(pos)) return;
Chunk chunk = getChunk(pos); Chunk chunk = getChunk(pos);
@@ -52,4 +52,10 @@ public class World extends GridPane {
} }
return c; return c;
} }
public void unloadAllChunks() {
for (Coordinate coordinate : chunks.keySet()) {
unloadChunk(coordinate);
}
}
} }
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.VBox?> <?import javafx.scene.layout.VBox?>
<VBox spacing="20.0" xmlns:fx="http://javafx.com/fxml" <VBox xmlns:fx="http://javafx.com/fxml" spacing="20.0"
fx:controller="com.yes.yes.MainController" fx:id="root"> fx:controller="com.yes.yes.MainController" fx:id="root">
</VBox> </VBox>