|
1 | 1 | import math |
2 | 2 | import random |
| 3 | +from cProfile import Profile |
3 | 4 | import pyglet |
4 | 5 |
|
5 | 6 | pyglet.options["shadow_window"] = False |
@@ -50,6 +51,32 @@ def update(self, delta_time): |
50 | 51 |
|
51 | 52 | self.player.update(delta_time) |
52 | 53 |
|
| 54 | + # Load the closest chunk which hasn't been loaded yet. |
| 55 | + |
| 56 | + x, y, z = self.player.position |
| 57 | + closest_chunk = None |
| 58 | + min_distance = math.inf |
| 59 | + |
| 60 | + for chunk_pos, chunk in self.world.chunks.items(): |
| 61 | + if chunk.loaded: |
| 62 | + continue |
| 63 | + |
| 64 | + cx, cy, cz = chunk_pos |
| 65 | + |
| 66 | + cx *= CHUNK_WIDTH |
| 67 | + cy *= CHUNK_HEIGHT |
| 68 | + cz *= CHUNK_LENGTH |
| 69 | + |
| 70 | + dist = (cx - x) ** 2 + (cy - y) ** 2 + (cz - z) ** 2 |
| 71 | + |
| 72 | + if dist < min_distance: |
| 73 | + min_distance = dist |
| 74 | + closest_chunk = chunk |
| 75 | + |
| 76 | + if closest_chunk is not None: |
| 77 | + closest_chunk.update_subchunk_meshes() |
| 78 | + closest_chunk.update_mesh() |
| 79 | + |
53 | 80 | def on_draw(self): |
54 | 81 | self.player.update_matrices() |
55 | 82 |
|
@@ -220,6 +247,38 @@ def run(self): |
220 | 247 | pyglet.app.run() |
221 | 248 |
|
222 | 249 |
|
| 250 | +def sample_initial_loading_time(): |
| 251 | + with Profile() as profiler: |
| 252 | + Game() |
| 253 | + profiler.create_stats() |
| 254 | + profiler.dump_stats("stats.prof") |
| 255 | + |
| 256 | + for k in profiler.stats.keys(): |
| 257 | + # file line name |
| 258 | + file, _, name = k |
| 259 | + |
| 260 | + if "world.py" in file and name == "__init__": |
| 261 | + # ? ncalls time cumtime parent |
| 262 | + _, _, _, cumtime, _ = profiler.stats[k] |
| 263 | + break |
| 264 | + |
| 265 | + else: |
| 266 | + raise Exception("Couldn't find work init stats!") |
| 267 | + |
| 268 | + return cumtime |
| 269 | + |
| 270 | + |
| 271 | +def benchmark_initial_loading_time(): |
| 272 | + n = 10 |
| 273 | + samples = [sample_initial_loading_time() for _ in range(n)] |
| 274 | + mean = sum(samples) / n |
| 275 | + |
| 276 | + print(mean) |
| 277 | + exit() |
| 278 | + |
| 279 | + |
223 | 280 | if __name__ == "__main__": |
| 281 | + # benchmark_initial_loading_time() |
| 282 | + |
224 | 283 | game = Game() |
225 | 284 | game.run() |
0 commit comments