DKL9 GitList
Repositories
DKL9 home
voxels
Code
Commits
Branches
Tags
Search
Tree:
3f13a9c
Branches
Tags
master
voxels
src
player.py
Command-line options and tweak textures
dkl9
commited
3f13a9c
at 2025-184 01:10:44
player.py
Blame
History
Raw
from libs import * import interactive import huds class Player(interactive.InteractiveCamera): def __init__(self, **kwargs): super().__init__(**kwargs) self.hud["fps"] = huds.fps() self.hud["skybox"] = huds.skybox() self.hud["sun"] = huds.sun() self.hud["hand"] = huds.hand() self.hud["shl"] = huds.selection() self.hand = 1 self.collides = True self.falls = True def update_motion(self, pressed, mg): netv = [0, 0, 0] for i in range(3): for s in range(1, -1, -1): sf = (-1) ** s cd = 0.3 * sf * self.vaa_mat()[i] hit = self.collides and mg.voxel_at(self.pos + cd) if i == 1 and sf == -1 and self.falls and not hit: netv[i] = self.vaa_vel[i] - conf["gravity"] / conf["fps"] if i != 1 or not self.falls or mg.voxel_at(self.pos - cd): netv[i] += sf * any(pressed[j] for j in ARROWS[2 * i + s]) if hit: netv[i] = sf * min(0, sf * netv[i]) self.vaa_vel = vec(netv) def set_hand(self, new_hand): self.hand = int(new_hand) hg = self.hud["hand"] hg.faces = [(a, self.hand) for (a, _) in hg.faces] for i in range(len(VERTEX_ATTRS)): update_attr(hg, i) def handle_event(self, ev, mg, scene): super().handle_event(ev, mg) match ev.type: case pygame.KEYUP: if ev.unicode == "\x1b": self.set_focus(False) elif "1" <= ev.unicode <= "9": self.set_hand(int(ev.unicode)) elif ev.unicode == "n": self.collides = not self.collides elif ev.unicode == "f": self.falls = not self.falls case pygame.MOUSEBUTTONUP if ev.button == 1: self.set_focus(True) case pygame.MOUSEBUTTONDOWN if self.get_focus(): t, n = self.target(scene) if t: cr = t.click(ev.button, n, self.hand) if cr is not None: cr(self) def draw_hud(self, wrs, perf): self.hud["skybox"].pos = self.pos c, ap = self.target(wrs) if c: self.hud["shl"].pos = c.pos + ap[1] + axis2offset(ap[0], 0.52) self.hud["shl"].orient[:3, :3] = np.sign(ap[0]) * np.roll( np.identity(3), abs(ap[0]) + 1, 0 ) else: self.hud["shl"].pos = vec([0, 0, 0]) if 0.9 < perf < 1.1: perf_col = GREEN elif perf > 0.5: perf_col = YELLOW else: perf_col = RED self.hud["fps"].col = perf_col update_attr(self.hud["fps"], "col") for ent in self.hud.values(): self.draw(ent)