Skip to content

Commit 348e2c1

Browse files
committed
modify chunk loading
1 parent 3b9f07b commit 348e2c1

File tree

3 files changed

+49
-131
lines changed

3 files changed

+49
-131
lines changed

src/ui/screen/GameScreen.cs

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -183,46 +183,44 @@ public override void onKeyDown(IKeyboard keyboard, Key key, int scancode) {
183183
return;
184184
}
185185

186-
if (key == Key.F3) {
187-
debugScreen = !debugScreen;
188-
}
189-
190-
// reload chunks
191-
if (key == Key.A && keyboard.IsKeyPressed(Key.F3)) {
192-
remeshWorld(Settings.instance.renderDistance);
193-
}
194-
195-
if (key == Key.F) {
196-
world.worldIO.save(world, world.name);
197-
}
198-
199-
if (key == Key.G) {
200-
world?.Dispose();
201-
world = WorldIO.load("level1");
202-
Game.instance.resize(new Vector2D<int>(Game.width, Game.height));
203-
}
204-
205-
if (key == Key.F9) {
206-
MemoryUtils.cleanGC();
207-
}
208-
209-
if (key == Key.E) {
210-
if (world.inMenu) {
211-
backToGame();
212-
}
213-
else {
214-
switchToMenu(new InventoryMenu(new Vector2D<int>(0, 32)));
215-
((InventoryMenu)currentMenu!).setup();
216-
world.inMenu = true;
217-
Game.instance.unlockMouse();
186+
switch (key) {
187+
case Key.F3:
188+
debugScreen = !debugScreen;
189+
break;
190+
// reload chunks
191+
case Key.A when keyboard.IsKeyPressed(Key.F3):
192+
remeshWorld(Settings.instance.renderDistance);
193+
break;
194+
case Key.F:
195+
world.worldIO.save(world, world.name);
196+
break;
197+
case Key.G:
198+
world?.Dispose();
199+
world = WorldIO.load("level1");
200+
Game.instance.resize(new Vector2D<int>(Game.width, Game.height));
201+
break;
202+
case Key.F9:
203+
MemoryUtils.cleanGC();
204+
break;
205+
case Key.E: {
206+
if (world.inMenu) {
207+
backToGame();
208+
}
209+
else {
210+
switchToMenu(new InventoryMenu(new Vector2D<int>(0, 32)));
211+
((InventoryMenu)currentMenu!).setup();
212+
world.inMenu = true;
213+
Game.instance.unlockMouse();
214+
}
215+
break;
218216
}
219-
}
220-
221-
if (key == Key.Space) {
222-
if (Game.permanentStopwatch.ElapsedMilliseconds < world.player.spacePress + Constants.flyModeDelay * 1000) {
223-
world.player.flyMode = !world.player.flyMode;
217+
case Key.Space: {
218+
if (Game.permanentStopwatch.ElapsedMilliseconds < world.player.spacePress + Constants.flyModeDelay * 1000) {
219+
world.player.flyMode = !world.player.flyMode;
220+
}
221+
world.player.spacePress = Game.permanentStopwatch.ElapsedMilliseconds;
222+
break;
224223
}
225-
world.player.spacePress = Game.permanentStopwatch.ElapsedMilliseconds;
226224
}
227225

228226

src/world/Player.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,6 @@ public void loadChunksAroundThePlayer(int renderDistance) {
132132
world.sortChunks();
133133
}
134134

135-
public void loadChunksAroundThePlayerLoading(int renderDistance) {
136-
var blockPos = position.toBlockPos();
137-
var chunk = World.getChunkPos(new Vector2D<int>(blockPos.X, blockPos.Z));
138-
world.loadChunksAroundChunkLoading(chunk, renderDistance);
139-
world.sortChunks();
140-
}
141-
142135
public void onChunkChanged() {
143136
//Console.Out.WriteLine("chunk changed");
144137
loadChunksAroundThePlayer(Settings.instance.renderDistance);

src/world/World.cs

Lines changed: 13 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public class World : IDisposable {
5252

5353
public Random random;
5454

55-
// max. 5 msec in each frame for chunkload
56-
private const long MAX_CHUNKLOAD_FRAMETIME = 10;
55+
private const long MAX_CHUNKLOAD_FRAMETIME = 20;
5756
private const long MAX_LIGHT_FRAMETIME = 10;
5857
private const int SPAWNCHUNKS_SIZE = 1;
5958
private const int MAX_TICKING_DISTANCE = 128;
@@ -125,7 +124,7 @@ public void loadAroundPlayer() {
125124
// create terrain
126125
//genTerrainNoise();
127126
// separate loop so all data is there
128-
player.loadChunksAroundThePlayerLoading(Settings.instance.renderDistance);
127+
player.loadChunksAroundThePlayer(Settings.instance.renderDistance);
129128
}
130129

131130

@@ -331,68 +330,6 @@ public void addToChunkLoadQueue(ChunkCoord chunkCoord, ChunkStatus level) {
331330
/// TODO unload chunks which are renderDistance + 2 away (this is bigger to prevent chunk flicker)
332331
/// </summary>
333332
public void loadChunksAroundChunk(ChunkCoord chunkCoord, int renderDistance) {
334-
// load +2 chunks around renderdistance
335-
for (int x = chunkCoord.x - renderDistance - 2; x <= chunkCoord.x + renderDistance + 2; x++) {
336-
for (int z = chunkCoord.z - renderDistance - 2; z <= chunkCoord.z + renderDistance + 2; z++) {
337-
var coord = new ChunkCoord(x, z);
338-
if (coord.distanceSq(chunkCoord) <= (renderDistance + 2) * (renderDistance + 2)) {
339-
addToChunkLoadQueue(coord, ChunkStatus.GENERATED);
340-
}
341-
}
342-
}
343-
// populate around renderDistance + 1
344-
// light around renderDistance + 1
345-
for (int x = chunkCoord.x - renderDistance - 1; x <= chunkCoord.x + renderDistance + 1; x++) {
346-
for (int z = chunkCoord.z - renderDistance - 1; z <= chunkCoord.z + renderDistance + 1; z++) {
347-
var coord = new ChunkCoord(x, z);
348-
if (coord.distanceSq(chunkCoord) <= (renderDistance + 1) * (renderDistance + 1)) {
349-
addToChunkLoadQueue(coord, ChunkStatus.POPULATED);
350-
addToChunkLoadQueue(coord, ChunkStatus.LIGHTED);
351-
}
352-
}
353-
}
354-
// finally, mesh around renderDistance
355-
for (int x = chunkCoord.x - renderDistance; x <= chunkCoord.x + renderDistance; x++) {
356-
for (int z = chunkCoord.z - renderDistance; z <= chunkCoord.z + renderDistance; z++) {
357-
var coord = new ChunkCoord(x, z);
358-
if (coord.distanceSq(chunkCoord) <= renderDistance * renderDistance) {
359-
addToChunkLoadQueue(coord, ChunkStatus.MESHED);
360-
}
361-
}
362-
}
363-
364-
// unload chunks which are far away
365-
foreach (var chunk in chunks.Values) {
366-
var playerChunk = player.getChunk();
367-
var coord = chunk.coord;
368-
// if distance is greater than renderDistance + 3, unload
369-
if (playerChunk.distanceSq(coord) >= (renderDistance + 3) * (renderDistance + 3)) {
370-
unloadChunk(coord);
371-
}
372-
}
373-
}
374-
375-
public void loadChunksAroundChunkLoading(ChunkCoord chunkCoord, int renderDistance) {
376-
// load +2 chunks around renderdistance
377-
for (int x = chunkCoord.x - renderDistance - 2; x <= chunkCoord.x + renderDistance + 2; x++) {
378-
for (int z = chunkCoord.z - renderDistance - 2; z <= chunkCoord.z + renderDistance + 2; z++) {
379-
var coord = new ChunkCoord(x, z);
380-
if (coord.distanceSq(chunkCoord) <= (renderDistance + 2) * (renderDistance + 2)) {
381-
addToChunkLoadQueue(coord, ChunkStatus.GENERATED);
382-
}
383-
}
384-
}
385-
// populate around renderDistance + 1
386-
// light around renderDistance + 1
387-
for (int x = chunkCoord.x - renderDistance - 1; x <= chunkCoord.x + renderDistance + 1; x++) {
388-
for (int z = chunkCoord.z - renderDistance - 1; z <= chunkCoord.z + renderDistance + 1; z++) {
389-
var coord = new ChunkCoord(x, z);
390-
if (coord.distanceSq(chunkCoord) <= (renderDistance + 1) * (renderDistance + 1)) {
391-
addToChunkLoadQueue(coord, ChunkStatus.POPULATED);
392-
addToChunkLoadQueue(coord, ChunkStatus.LIGHTED);
393-
}
394-
}
395-
}
396333
// finally, mesh around renderDistance
397334
for (int x = chunkCoord.x - renderDistance; x <= chunkCoord.x + renderDistance; x++) {
398335
for (int z = chunkCoord.z - renderDistance; z <= chunkCoord.z + renderDistance; z++) {
@@ -415,26 +352,6 @@ public void loadChunksAroundChunkLoading(ChunkCoord chunkCoord, int renderDistan
415352
}
416353

417354
public void loadChunksAroundChunkImmediately(ChunkCoord chunkCoord, int renderDistance) {
418-
// load +2 chunks around renderdistance
419-
for (int x = chunkCoord.x - renderDistance - 2; x <= chunkCoord.x + renderDistance + 2; x++) {
420-
for (int z = chunkCoord.z - renderDistance - 2; z <= chunkCoord.z + renderDistance + 2; z++) {
421-
var coord = new ChunkCoord(x, z);
422-
if (coord.distanceSq(chunkCoord) <= (renderDistance + 2) * (renderDistance + 2)) {
423-
loadChunk(coord, ChunkStatus.GENERATED);
424-
}
425-
}
426-
}
427-
// populate around renderDistance + 1
428-
// light around renderDistance + 1
429-
for (int x = chunkCoord.x - renderDistance - 1; x <= chunkCoord.x + renderDistance + 1; x++) {
430-
for (int z = chunkCoord.z - renderDistance - 1; z <= chunkCoord.z + renderDistance + 1; z++) {
431-
var coord = new ChunkCoord(x, z);
432-
if (coord.distanceSq(chunkCoord) <= (renderDistance + 1) * (renderDistance + 1)) {
433-
loadChunk(coord, ChunkStatus.POPULATED);
434-
loadChunk(coord, ChunkStatus.LIGHTED);
435-
}
436-
}
437-
}
438355
// finally, mesh around renderDistance
439356
for (int x = chunkCoord.x - renderDistance; x <= chunkCoord.x + renderDistance; x++) {
440357
for (int z = chunkCoord.z - renderDistance; z <= chunkCoord.z + renderDistance; z++) {
@@ -519,13 +436,23 @@ public Chunk loadChunk(ChunkCoord chunkCoord, ChunkStatus status) {
519436
generator.generate(chunkCoord);
520437
}
521438
if (status >= ChunkStatus.POPULATED && (!hasChunk || (hasChunk && chunks[chunkCoord].status < ChunkStatus.POPULATED))) {
522-
//Console.Out.WriteLine($"chunkload {chunk.GetHashCode()}");
439+
// load adjacent first
440+
loadChunk(new ChunkCoord(chunkCoord.x - 1, chunkCoord.z), ChunkStatus.GENERATED);
441+
loadChunk(new ChunkCoord(chunkCoord.x + 1, chunkCoord.z), ChunkStatus.GENERATED);
442+
loadChunk(new ChunkCoord(chunkCoord.x, chunkCoord.z - 1), ChunkStatus.GENERATED);
443+
loadChunk(new ChunkCoord(chunkCoord.x, chunkCoord.z + 1), ChunkStatus.GENERATED);
444+
523445
generator.populate(chunkCoord);
524446
}
525447
if (status >= ChunkStatus.LIGHTED && (!hasChunk || (hasChunk && chunks[chunkCoord].status < ChunkStatus.LIGHTED))) {
526448
chunks[chunkCoord].lightChunk();
527449
}
528450
if (status >= ChunkStatus.MESHED && (!hasChunk || (hasChunk && chunks[chunkCoord].status < ChunkStatus.MESHED))) {
451+
// load adjacent first
452+
loadChunk(new ChunkCoord(chunkCoord.x - 1, chunkCoord.z), ChunkStatus.LIGHTED);
453+
loadChunk(new ChunkCoord(chunkCoord.x + 1, chunkCoord.z), ChunkStatus.LIGHTED);
454+
loadChunk(new ChunkCoord(chunkCoord.x, chunkCoord.z - 1), ChunkStatus.LIGHTED);
455+
loadChunk(new ChunkCoord(chunkCoord.x, chunkCoord.z + 1), ChunkStatus.LIGHTED);
529456
chunks[chunkCoord].meshChunk();
530457
}
531458
return chunks[chunkCoord];

0 commit comments

Comments
 (0)