DKL9 GitList
Repositories
DKL9 home
voxels
Code
Commits
Branches
Tags
Search
Tree:
396b134
Branches
Tags
master
voxels
world.py
Big code cleanup and better arrow keys
dkl9
commited
396b134
at 2025-179 22:38:38
world.py
Blame
History
Raw
from libs import * from collections import defaultdict import region class WorldHolder(): def __init__(self): self.regions = {} def fetch(self, pos): if pos in self.regions: return self.regions[pos] else: wr = region.WorldRegion() wr.pos = vec(pos) self.regions[pos] = wr self.world_natural(wr) wr.remesh() wr.onclick = (lambda s, b, c, h: self.click_region(s, b, c, h)) return wr def voxel_at(self, pos, voxel_type=None): try: r = next(iter(self.regions.values())).r except StopIteration: r = REGION_SIZE pr, pf = np.divmod(pos + 0.5, r) wr = self.fetch(tuple(int(r * c) for c in pr)) it = tuple(int(c) for c in pf) if voxel_type is None: return wr.grid[*it] else: old = wr.grid[*it] wr.grid[*it] = voxel_type wr.dirty = True return old def click_region(self, wr, button, coords, hand): match button: case 1: wr.set_voxel(coords[1]) case 2: return (lambda pl: pl.set_hand(wr.grid[*coords[1]])) case 3: offset = axis2offset(coords[0]) p = wr.set_voxel(coords[1] + offset, hand) if p is not None: rc = wr.pos + p // wr.r * wr.r awr = self.fetch(tuple(int(x) for x in rc)) p = p % wr.r if not awr.grid[*p]: awr.set_voxel(p, hand) if button % 2 == 1: for i in range(len(VERTEX_ATTRS)): update_attr(wr, i)