Skip to content

Instantly share code, notes, and snippets.

@calebarg
Created May 23, 2023 04:55
Show Gist options
  • Select an option

  • Save calebarg/f3f8f7b0dfed69f0b8a9f73d6a4e935d to your computer and use it in GitHub Desktop.

Select an option

Save calebarg/f3f8f7b0dfed69f0b8a9f73d6a4e935d to your computer and use it in GitHub Desktop.
Prototype chunk loader for my voxel game
# PROTOTYPE CHUNK LOADING ALOGRITHM THING
# Caleb Barger - 05/22/2023
import time
import os
CHUNK_CAP = 100
d_chunk_poses = [
(0, 1, 0), # Up
(0, -1, 0), # Down
(-1, 0, 0), # Left
(1, 0, 0), # Right
# (0, 0, -1), # Forward
# (0, 0, 1), # Backward
]
def queueChunks(load_queue, current_chunk, loaded_chunks, loaded_chunk_count):
for d_chunk_pos in d_chunk_poses:
if len(load_queue) >= CHUNK_CAP - loaded_chunk_count: # Remaining space
return;
next_up = (current_chunk[0] + d_chunk_pos[0],
current_chunk[1] + d_chunk_pos[1],
current_chunk[2] + d_chunk_pos[2])
if (next_up not in loaded_chunks and next_up not in load_queue): # Don't queue loaded chunks or queued chunks.
load_queue.insert(0, next_up)
def loadChunks(loaded_chunks, start_chunk):
loaded_chunk_count = 1
loaded_chunks[0] = start_chunk
load_queue = []
current_chunk = start_chunk
queueChunks(load_queue, current_chunk, loaded_chunks, loaded_chunk_count)
while loaded_chunk_count < CHUNK_CAP:
next_chunk = load_queue.pop()
loaded_chunks[loaded_chunk_count] = next_chunk
current_chunk = next_chunk
queueChunks(load_queue, current_chunk, loaded_chunks, loaded_chunk_count)
loaded_chunk_count += 1
loaded_chunks = []
for chunk_index in range(0, CHUNK_CAP):
loaded_chunks.append((0, 0, 0))
start_chunk = (0, 0, 0) # Player chunk
loadChunks(loaded_chunks, start_chunk)
board_width = 80
board_height = 80
board = ['.'] * board_height * board_width
for chunk in loaded_chunks:
col = chunk[0] + 40
row = chunk[1] + 40
board[row * board_width + col] = '#'
os.system('cls')
for row in range(0, board_height):
for col in range(0, board_width):
print(board[row * board_width + col], end='', flush=True)
print()
time.sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment