From 45f6f4398abc14a9aeb8c207f0960a02086d5f10 Mon Sep 17 00:00:00 2001 From: Jukitsu Date: Mon, 27 Jun 2022 18:09:26 +0200 Subject: [PATCH 01/14] Fix window close --- community/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/community/main.py b/community/main.py index 40743f3e..0380cea5 100644 --- a/community/main.py +++ b/community/main.py @@ -161,7 +161,7 @@ def on_close(self): for fence in self.fences: gl.glDeleteSync(fence) - pyglet.app.exit() + super().on_close() def update_f3(self, delta_time): """Update the F3 debug screen content""" From e4af9279ca748c7ce125098877e34b519f53a6fe Mon Sep 17 00:00:00 2001 From: anthony-63 <72637501+anthony-63@users.noreply.github.com> Date: Mon, 24 Oct 2022 19:26:49 -0400 Subject: [PATCH 02/14] Added odin implementation --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 115a266c..9fc57355 100644 --- a/README.md +++ b/README.md @@ -60,4 +60,5 @@ The community has several features and options that can be toggled in `options.p - **Nim implementation:** https://github.com/phargobikcin/nim-minecraft-clone - **Java implementation:** https://github.com/StartForKillerMC/JavaMinecraft - **C++ implementation:** https://github.com/Jukitsu/CppMinecraft-clone +- **Odin implementation:** https://github.com/anthony-63/lvo - **Lighting test:** https://github.com/Jukitsu/python-minecraft-clone-ep10-lighting From c2015907a7c37b85cb0cb1078ee448cf3c902ac3 Mon Sep 17 00:00:00 2001 From: Jakub Marcowski <37378746+Chubercik@users.noreply.github.com> Date: Tue, 15 Nov 2022 21:18:30 +0100 Subject: [PATCH 03/14] Remove the link to the lighting test from README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 9fc57355..ec4463e7 100644 --- a/README.md +++ b/README.md @@ -61,4 +61,3 @@ The community has several features and options that can be toggled in `options.p - **Java implementation:** https://github.com/StartForKillerMC/JavaMinecraft - **C++ implementation:** https://github.com/Jukitsu/CppMinecraft-clone - **Odin implementation:** https://github.com/anthony-63/lvo -- **Lighting test:** https://github.com/Jukitsu/python-minecraft-clone-ep10-lighting From b3af5c2737ed7a0b64d0adbe3aa5d909c28af85a Mon Sep 17 00:00:00 2001 From: Jukitsu <84381972+Jukitsu@users.noreply.github.com> Date: Sat, 31 Dec 2022 00:52:46 +0100 Subject: [PATCH 04/14] Update main.py --- community/main.py | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/community/main.py b/community/main.py index 0380cea5..aaf34b2c 100644 --- a/community/main.py +++ b/community/main.py @@ -174,7 +174,7 @@ def update_f3(self, delta_time): visible_quad_count = sum(chunk.mesh_quad_count for chunk in self.world.visible_chunks) self.f3.text = \ f""" -{round(pyglet.clock.get_fps())} FPS ({self.world.chunk_update_counter} Chunk Updates) {"inf" if not self.options.VSYNC else "vsync"}{"ao" if self.options.SMOOTH_LIGHTING else ""} +{round(1 / delta_time)} FPS ({self.world.chunk_update_counter} Chunk Updates) {"inf" if not self.options.VSYNC else "vsync"}{"ao" if self.options.SMOOTH_LIGHTING else ""} C: {visible_chunk_count} / {chunk_count} pC: {self.world.pending_chunk_update_count} pU: {len(self.world.chunk_building_queue)} aB: {chunk_count} Client Singleplayer @{round(delta_time * 1000)} ms tick {round(1 / delta_time)} TPS @@ -230,36 +230,16 @@ def on_draw(self): # Draw the F3 Debug screen if self.show_f3: - self.draw_f3() + self.f3.draw() # CPU - GPU Sync if not self.options.SMOOTH_FPS: - self.fences.append(gl.glFenceSync(gl.GL_SYNC_GPU_COMMANDS_COMPLETE, 0)) + # self.fences.append(gl.glFenceSync(gl.GL_SYNC_GPU_COMMANDS_COMPLETE, 0)) + # Broken in pyglet 2; glFenceSync is missing + pass else: gl.glFinish() - def draw_f3(self): - """Draws the f3 debug screen. Current uses the fixed-function pipeline since pyglet labels uses it""" - gl.glDisable(gl.GL_DEPTH_TEST) - gl.glUseProgram(0) - gl.glBindVertexArray(0) - gl.glMatrixMode(gl.GL_MODELVIEW) - gl.glPushMatrix() - gl.glLoadIdentity() - - gl.glMatrixMode(gl.GL_PROJECTION) - gl.glPushMatrix() - gl.glLoadIdentity() - gl.glOrtho(0, self.width, 0, self.height, -1, 1) - - self.f3.draw() - - gl.glPopMatrix() - - gl.glMatrixMode(gl.GL_MODELVIEW) - gl.glPopMatrix() - - # input functions def on_resize(self, width, height): @@ -279,7 +259,7 @@ def __init__(self): self.window = Window(config = self.config, width = 852, height = 480, caption = "Minecraft clone", resizable = True, vsync = options.VSYNC) def run(self): - pyglet.app.run() + pyglet.app.run(interval = 0) From a4c7c4ef6d1e4e28f0795222b27d6ce58ca2741e Mon Sep 17 00:00:00 2001 From: Arta <63291774+Arta48@users.noreply.github.com> Date: Tue, 7 Feb 2023 14:40:27 +0000 Subject: [PATCH 05/14] community: Make sprinting behaviour more similar to Minecraft - fixes #84 --- community/keyboard_mouse.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/community/keyboard_mouse.py b/community/keyboard_mouse.py index 419019dc..fbed18bc 100644 --- a/community/keyboard_mouse.py +++ b/community/keyboard_mouse.py @@ -14,6 +14,9 @@ def __init__(self, game): self.game.on_key_press = self.on_key_press self.game.on_key_release = self.on_key_release + self.w_pressed = False + self.ctrl_pressed = False + def on_mouse_press(self, x, y, button, modifiers): if not self.game.mouse_captured: self.game.mouse_captured = True @@ -43,12 +46,19 @@ def on_key_press(self, key, modifiers): if key == pyglet.window.key.D: self.start_move(self.MoveMode.RIGHT) elif key == pyglet.window.key.A: self.start_move(self.MoveMode.LEFT) - elif key == pyglet.window.key.W: self.start_move(self.MoveMode.FORWARD) + elif key == pyglet.window.key.W: + self.w_pressed = True + self.start_move(self.MoveMode.FORWARD) + if self.ctrl_pressed: + self.start_modifier(self.ModifierMode.SPRINT) elif key == pyglet.window.key.S: self.start_move(self.MoveMode.BACKWARD) elif key == pyglet.window.key.SPACE : self.start_move(self.MoveMode.UP) elif key == pyglet.window.key.LSHIFT: self.start_move(self.MoveMode.DOWN) - elif key == pyglet.window.key.LCTRL : self.start_modifier(self.ModifierMode.SPRINT) + elif key == pyglet.window.key.LCTRL: + self.ctrl_pressed = True + if self.w_pressed: + self.start_modifier(self.ModifierMode.SPRINT) elif key == pyglet.window.key.E: self.misc(self.MiscMode.SPAWN) elif key == pyglet.window.key.F: self.misc(self.MiscMode.FLY) @@ -67,9 +77,15 @@ def on_key_release(self, key, modifiers): if key == pyglet.window.key.D: self.end_move(self.MoveMode.RIGHT) elif key == pyglet.window.key.A: self.end_move(self.MoveMode.LEFT) - elif key == pyglet.window.key.W: self.end_move(self.MoveMode.FORWARD) + elif key == pyglet.window.key.W: + self.w_pressed = False + self.end_move(self.MoveMode.FORWARD) + self.end_modifier(self.ModifierMode.SPRINT) elif key == pyglet.window.key.S: self.end_move(self.MoveMode.BACKWARD) elif key == pyglet.window.key.SPACE : self.end_move(self.MoveMode.UP) elif key == pyglet.window.key.LSHIFT: self.end_move(self.MoveMode.DOWN) - elif key == pyglet.window.key.LCTRL : self.end_modifier(self.ModifierMode.SPRINT) + elif key == pyglet.window.key.LCTRL: + self.ctrl_pressed = False + if not self.w_pressed: + self.end_modifier(self.ModifierMode.SPRINT) From ec24af2faababad25d52f7ecd633013b2f5f2a5d Mon Sep 17 00:00:00 2001 From: brennop Date: Sun, 16 Apr 2023 15:18:59 -0300 Subject: [PATCH 06/14] Add lua implementation to README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index ec4463e7..a4298cb1 100644 --- a/README.md +++ b/README.md @@ -61,3 +61,4 @@ The community has several features and options that can be toggled in `options.p - **Java implementation:** https://github.com/StartForKillerMC/JavaMinecraft - **C++ implementation:** https://github.com/Jukitsu/CppMinecraft-clone - **Odin implementation:** https://github.com/anthony-63/lvo +- **Lua implementation:** https://github.com/brennop/lunarcraft From 0d3389a3a334b3173ed8c980402efc21a112917c Mon Sep 17 00:00:00 2001 From: drakeerv Date: Sun, 23 Apr 2023 18:22:20 -0400 Subject: [PATCH 07/14] Update controller.py to use pyglet 2 --- community/audio/readme.txt | 2 +- community/baseinput.py | 125 +++++++++++++++++++++ community/controller.py | 213 ++++++++++++++++-------------------- community/joystick.py | 144 ------------------------ community/keyboard_mouse.py | 4 +- community/main.py | 6 +- 6 files changed, 223 insertions(+), 271 deletions(-) create mode 100644 community/baseinput.py delete mode 100644 community/joystick.py diff --git a/community/audio/readme.txt b/community/audio/readme.txt index b14182ed..cbdccfbe 100644 --- a/community/audio/readme.txt +++ b/community/audio/readme.txt @@ -1 +1 @@ -For music download .ogg or other from https://archive.org/details/C418-MinecraftSoundtrackVolumeAlpha/ and put them into the music dir \ No newline at end of file +For music download .ogg (or other) from https://archive.org/details/C418-MinecraftSoundtrackVolumeAlpha/ and put them into the music dir \ No newline at end of file diff --git a/community/baseinput.py b/community/baseinput.py new file mode 100644 index 00000000..deb343e1 --- /dev/null +++ b/community/baseinput.py @@ -0,0 +1,125 @@ +import random +import player +import chunk +import hit + +from enum import IntEnum + +class BaseInput: + class InteractMode(IntEnum): + PLACE = 0 + BREAK = 1 + PICK = 2 + + class MiscMode(IntEnum): + RANDOM = 0 + SAVE = 1 + ESCAPE = 2 + SPEED_TIME = 3 + FULLSCREEN = 4 + FLY = 5 + TELEPORT = 6 + TOGGLE_F3 = 7 + TOGGLE_AO = 8 + + class MoveMode(IntEnum): + LEFT = 0 + RIGHT = 1 + DOWN = 2 + UP = 3 + BACKWARD = 4 + FORWARD = 5 + + class ModifierMode(IntEnum): + SPRINT = 0 + + def __init__(self, game): + self.game = game + + def interact(self, mode): + def hit_callback(current_block, next_block): + if mode == self.InteractMode.PLACE: self.game.world.try_set_block(current_block, self.game.holding, self.game.player.collider) + elif mode == self.InteractMode.BREAK: self.game.world.set_block(next_block, 0) + elif mode == self.InteractMode.PICK: self.game.holding = self.game.world.get_block_number(next_block) + + x, y, z = self.game.player.position + y += self.game.player.eyelevel + + hit_ray = hit.Hit_ray(self.game.world, self.game.player.rotation, (x, y, z)) + + while hit_ray.distance < hit.HIT_RANGE: + if hit_ray.step(hit_callback): + return True + return False + + def misc(self, mode): + if mode == self.MiscMode.RANDOM: + self.game.holding = random.randint(1, len(self.game.world.block_types) - 1) + elif mode == self.MiscMode.SAVE: + self.game.world.save.save() + elif mode == self.MiscMode.ESCAPE: + self.game.mouse_captured = False + self.game.set_exclusive_mouse(False) + elif mode == self.MiscMode.SPEED_TIME: + self.game.world.speed_daytime() + elif mode == self.MiscMode.FULLSCREEN: + self.game.toggle_fullscreen() + elif mode == self.MiscMode.FLY: + self.game.player.flying = not self.game.player.flying + elif mode == self.MiscMode.TELEPORT: + # how large is the world? + + max_y = 0 + + max_x, max_z = (0, 0) + min_x, min_z = (0, 0) + + for pos in self.game.world.chunks: + x, y, z = pos + + max_y = max(max_y, (y + 1) * chunk.CHUNK_HEIGHT) + + max_x = max(max_x, (x + 1) * chunk.CHUNK_WIDTH) + min_x = min(min_x, x * chunk.CHUNK_WIDTH) + + max_z = max(max_z, (z + 1) * chunk.CHUNK_LENGTH) + min_z = min(min_z, z * chunk.CHUNK_LENGTH) + + # get random X & Z coordinates to teleport the player to + + x = random.randint(min_x, max_x) + z = random.randint(min_z, max_z) + + # find height at which to teleport to, by finding the first non-air block from the top of the world + + for y in range(chunk.CHUNK_HEIGHT - 1, -1, -1): + if not self.game.world.get_block_number((x, y, z)): + continue + + self.game.player.teleport((x, y + 1, z)) + break + elif mode == self.MiscMode.TOGGLE_F3: + self.game.show_f3 = not self.game.show_f3 + elif mode == self.MiscMode.TOGGLE_AO: + self.game.world.toggle_AO() + + def update_move(self, axis): + self.game.player.input[axis] = round(max(-1, min(self.game.controls[axis], 1))) + + def start_move(self, mode): + axis = int((mode if mode % 2 == 0 else mode - 1) / 2) + self.game.controls[axis] += (-1 if mode % 2 == 0 else 1) + self.update_move(axis) + + def end_move(self, mode): + axis = int((mode if mode % 2 == 0 else mode - 1) / 2) + self.game.controls[axis] -= (-1 if mode % 2 == 0 else 1) + self.update_move(axis) + + def start_modifier(self, mode): + if mode == self.ModifierMode.SPRINT: + self.game.player.target_speed = player.SPRINTING_SPEED + + def end_modifier(self, mode): + if mode == self.ModifierMode.SPRINT: + self.game.player.target_speed = player.WALKING_SPEED \ No newline at end of file diff --git a/community/controller.py b/community/controller.py index 7972e38c..405ff1a6 100644 --- a/community/controller.py +++ b/community/controller.py @@ -1,124 +1,95 @@ -import random +import pyglet.input + +import baseinput import player -import chunk -import hit - -from enum import IntEnum - -class Controller: - class InteractMode(IntEnum): - PLACE = 0 - BREAK = 1 - PICK = 2 - - class MiscMode(IntEnum): - RANDOM = 0 - SAVE = 1 - ESCAPE = 2 - SPEED_TIME = 3 - FULLSCREEN = 4 - FLY = 5 - TELEPORT = 6 - TOGGLE_F3 = 7 - TOGGLE_AO = 8 - - class MoveMode(IntEnum): - LEFT = 0 - RIGHT = 1 - DOWN = 2 - UP = 3 - BACKWARD = 4 - FORWARD = 5 - - class ModifierMode(IntEnum): - SPRINT = 0 +import math +import time +class Controller(baseinput.BaseInput): def __init__(self, game): - self.game = game - - def interact(self, mode): - def hit_callback(current_block, next_block): - if mode == self.InteractMode.PLACE: self.game.world.try_set_block(current_block, self.game.holding, self.game.player.collider) - elif mode == self.InteractMode.BREAK: self.game.world.set_block(next_block, 0) - elif mode == self.InteractMode.PICK: self.game.holding = self.game.world.get_block_number(next_block) - - x, y, z = self.game.player.position - y += self.game.player.eyelevel - - hit_ray = hit.Hit_ray(self.game.world, self.game.player.rotation, (x, y, z)) - - while hit_ray.distance < hit.HIT_RANGE: - if hit_ray.step(hit_callback): - break - - def misc(self, mode): - if mode == self.MiscMode.RANDOM: - self.game.holding = random.randint(1, len(self.game.world.block_types) - 1) - elif mode == self.MiscMode.SAVE: - self.game.world.save.save() - elif mode == self.MiscMode.ESCAPE: - self.game.mouse_captured = False - self.game.set_exclusive_mouse(False) - elif mode == self.MiscMode.SPEED_TIME: - self.game.world.speed_daytime() - elif mode == self.MiscMode.FULLSCREEN: - self.game.toggle_fullscreen() - elif mode == self.MiscMode.FLY: - self.game.player.flying = not self.game.player.flying - elif mode == self.MiscMode.TELEPORT: - # how large is the world? - - max_y = 0 - - max_x, max_z = (0, 0) - min_x, min_z = (0, 0) - - for pos in self.game.world.chunks: - x, y, z = pos - - max_y = max(max_y, (y + 1) * chunk.CHUNK_HEIGHT) - - max_x = max(max_x, (x + 1) * chunk.CHUNK_WIDTH) - min_x = min(min_x, x * chunk.CHUNK_WIDTH) - - max_z = max(max_z, (z + 1) * chunk.CHUNK_LENGTH) - min_z = min(min_z, z * chunk.CHUNK_LENGTH) - - # get random X & Z coordinates to teleport the player to - - x = random.randint(min_x, max_x) - z = random.randint(min_z, max_z) - - # find height at which to teleport to, by finding the first non-air block from the top of the world - - for y in range(chunk.CHUNK_HEIGHT - 1, -1, -1): - if not self.game.world.get_block_number((x, y, z)): - continue - - self.game.player.teleport((x, y + 1, z)) - break - elif mode == self.MiscMode.TOGGLE_F3: - self.game.show_f3 = not self.game.show_f3 - elif mode == self.MiscMode.TOGGLE_AO: - self.game.world.toggle_AO() - - def update_move(self, axis): - self.game.player.input[axis] = round(max(-1, min(self.game.controls[axis], 1))) - - def start_move(self, mode): - axis = int((mode if mode % 2 == 0 else mode - 1) / 2) - self.game.controls[axis] += (-1 if mode % 2 == 0 else 1) - self.update_move(axis) - - def end_move(self, mode): - axis = int((mode if mode % 2 == 0 else mode - 1) / 2) - self.game.controls[axis] -= (-1 if mode % 2 == 0 else 1) - self.update_move(axis) - - def start_modifier(self, mode): - if mode == self.ModifierMode.SPRINT: - self.game.player.target_speed = player.SPRINTING_SPEED - - def end_modifier(self, mode): - if mode == self.ModifierMode.SPRINT: - self.game.player.target_speed = player.WALKING_SPEED \ No newline at end of file + super().__init__(game) + self.controller_manager = pyglet.input.ControllerManager() + + self.camera_sensitivity = 2.5 + self.joystick_deadzone = 0.25 + self.update_delay = 0.15 + self.last_update = 0 + + self.joystick_move = [0, 0] + self.joystick_look = [0, 0] + self.joystick_interact = [0, 0] + + self.main_controller = None + self.try_get_main_controller() + + @self.controller_manager.event + def on_connect(controller): + if self.main_controller is None: + self.new_main_controller(controller) + + print("Connect:", controller) + + @self.controller_manager.event + def on_disconnect(controller): + if self.main_controller == controller: + self.main_controller = None + self.try_get_main_controller() + + print("Disconnect:", controller) + + def try_get_main_controller(self): + if self.main_controller is None: + if len(self.controller_manager.get_controllers()) > 0: + self.new_main_controller(self.controller_manager.get_controllers()[0]) + + def new_main_controller(self, controller): + self.main_controller = controller + self.main_controller.open() + + @self.main_controller.event + def on_stick_motion(controller, stick, x, y): + if controller == self.main_controller: + if stick == "left": + self.joystick_move = [x, y] + elif stick == "right": + self.joystick_look = [x, y] + + def update_controller(self, delta_time): + if not self.game.mouse_captured or self.main_controller is None: + return + + self.joystick_move = self.apply_deadzone([self.main_controller.leftx, self.main_controller.lefty]) + self.joystick_look = self.apply_deadzone([self.main_controller.rightx, self.main_controller.righty]) + self.joystick_interact = [self.main_controller.lefttrigger, self.main_controller.righttrigger] + + self.game.player.rotation[0] += self.joystick_look[0] * self.camera_sensitivity * delta_time + self.game.player.rotation[1] += self.joystick_look[1] * self.camera_sensitivity * delta_time + + self.game.player.rotation[1] = max(-math.tau / 4, min(math.tau / 4, self.game.player.rotation[1])) + + if round(max(self.joystick_interact)) > 0 and (self.last_update + self.update_delay) <= time.process_time(): + if round(self.joystick_interact[0]) > 0: + if self.interact(self.InteractMode.BREAK): + self.main_controller.rumble_play_weak(1, duration=0.05) + if round(self.joystick_interact[1]) > 0: self.interact(self.InteractMode.PLACE) + + self.last_update = time.process_time() + + if self.main_controller.x: self.interact(self.InteractMode.PICK) + if self.main_controller.y: self.misc(self.MiscMode.RANDOM) + if self.main_controller.b: self.misc(self.MiscMode.SAVE) + + if self.main_controller.leftstick: + if self.game.player.target_speed == player.SPRINTING_SPEED: self.end_modifier(self.ModifierMode.SPRINT) + elif self.game.player.target_speed == player.WALKING_SPEED: self.start_modifier(self.ModifierMode.SPRINT) + + self.game.controls[0] = round(self.joystick_move[0]) + self.game.controls[1] = round(int(self.main_controller.a == True) - int(self.main_controller.rightstick == True)) + self.game.controls[2] = round(self.joystick_move[1]) + + for axis in range(3): self.update_move(axis) + + def apply_deadzone(self, value): + if abs(value[0]) < self.joystick_deadzone: value[0] = 0 + if abs(value[1]) < self.joystick_deadzone: value[1] = 0 + return value \ No newline at end of file diff --git a/community/joystick.py b/community/joystick.py deleted file mode 100644 index 455b136a..00000000 --- a/community/joystick.py +++ /dev/null @@ -1,144 +0,0 @@ -import pyglet.input - -import controller -import threading -import player -import math -import time - -class Joystick_controller(controller.Controller): - def __init__(self, game): - super().__init__(game) - self.init_joysticks(pyglet.input.get_joysticks()) - - self.camera_sensitivity = 0.007 - self.joystick_deadzone = 0.25 - self.update_delay = 0.15 - self.last_update = 0 - - self.joystick_move = [0, 0] - self.joystick_look = [0, 0] - self.joystick_interact = [0, 0] - - self.joystick_updater = threading.Thread(target=self.updater, daemon=True, name="Joystick Updater") - self.joystick_updater.start() - - def updater(self): - while True: - if len(pyglet.input.get_joysticks()) != len(self.joysticks): - self.init_joysticks(pyglet.input.get_joysticks()) - - time.sleep(2) - - def init_joysticks(self, joysticks): - self.joysticks = joysticks - - for joystick in self.joysticks: - joystick.on_joybutton_press = self.on_joybutton_press - joystick.on_joybutton_release = self.on_joybutton_release - joystick.on_joyaxis_motion = self.on_joyaxis_motion - joystick.on_joyhat_motion = self.on_joyhat_motion - joystick.open(exclusive=True) - - def update_controller(self): - if not self.game.mouse_captured or not self.joysticks: - return - - self.game.player.rotation[0] += self.joystick_look[0] * self.camera_sensitivity - self.game.player.rotation[1] += -self.joystick_look[1] * self.camera_sensitivity - - self.game.player.rotation[1] = max(-math.tau / 4, min(math.tau / 4, self.game.player.rotation[1])) - - if round(max(self.joystick_interact)) > 0 and (self.last_update + self.update_delay) <= time.process_time(): - if round(self.joystick_interact[0]) > 0: self.interact(self.InteractMode.BREAK) - if round(self.joystick_interact[1]) > 0: self.interact(self.InteractMode.PLACE) - - self.last_update = time.process_time() - - def on_joybutton_press(self, joystick, button): - if "xbox" in joystick.device.name.lower(): - if button == 1: self.misc(self.MiscMode.RANDOM) - elif button == 2: self.interact(self.InteractMode.PICK) - elif button == 3: self.misc(self.MiscMode.SAVE) - - elif button == 0: self.start_move(self.MoveMode.UP) - elif button == 9: self.start_move(self.MoveMode.DOWN) - - elif button == 8: - if self.game.player.target_speed == player.SPRINTING_SPEED: self.end_modifier(self.ModifierMode.SPRINT) - elif self.game.player.target_speed == player.WALKING_SPEED: self.start_modifier(self.ModifierMode.SPRINT) - - elif "wireless controller" == joystick.device.name.lower(): - if button == 2: self.misc(self.MiscMode.RANDOM) - elif button == 0: self.interact(self.InteractMode.PICK) - elif button == 3: self.misc(self.MiscMode.SAVE) - - elif button == 1: self.start_move(self.MoveMode.UP) - elif button == 11: self.start_move(self.MoveMode.DOWN) - - elif button == 10: - if self.game.player.target_speed == player.SPRINTING_SPEED: self.end_modifier(self.ModifierMode.SPRINT) - elif self.game.player.target_speed == player.WALKING_SPEED: self.start_modifier(self.ModifierMode.SPRINT) - - def on_joybutton_release(self, joystick, button): - if "xbox" in joystick.device.name.lower(): - if button == 0: self.end_move(self.MoveMode.UP) - elif button == 9: self.end_move(self.MoveMode.DOWN) - - elif "wireless controller" == joystick.device.name.lower(): - if button == 1: self.end_move(self.MoveMode.UP) - elif button == 11: self.end_move(self.MoveMode.DOWN) - - def on_joyaxis_motion(self, joystick, axis, value): - if abs(value) < self.joystick_deadzone: - value = 0 - - if "xbox" in joystick.device.name.lower(): - if axis == "x": - if math.ceil(value) > 0 and self.joystick_move[0] == 0: self.start_move(self.MoveMode.RIGHT) - elif math.floor(value) < 0 and self.joystick_move[0] == 0: self.start_move(self.MoveMode.LEFT) - elif value == 0 and math.ceil(self.joystick_move[0]) > 0: self.end_move(self.MoveMode.RIGHT) - elif value == 0 and math.floor(self.joystick_move[0]) < 0: self.end_move(self.MoveMode.LEFT) - - self.joystick_move[0] = value - elif axis == "y": - if math.ceil(value) > 0 and self.joystick_move[1] == 0: self.start_move(self.MoveMode.BACKWARD) - elif math.floor(value) < 0 and self.joystick_move[1] == 0: self.start_move(self.MoveMode.FORWARD) - elif value == 0 and math.ceil(self.joystick_move[1]) > 0: self.end_move(self.MoveMode.BACKWARD) - elif value == 0 and math.floor(self.joystick_move[1]) < 0: self.end_move(self.MoveMode.FORWARD) - - self.joystick_move[1] = value - - if axis == "rx": self.joystick_look[0] = value - if axis == "ry": self.joystick_look[1] = value - - if axis == "z": - if value < 0: self.joystick_interact[0] = -value - if value > 0: self.joystick_interact[1] = value - - elif "wireless controller" == joystick.device.name.lower(): - if axis == "x": - if math.ceil(value) > 0 and self.joystick_move[0] == 0: self.start_move(self.MoveMode.RIGHT) - elif math.floor(value) < 0 and self.joystick_move[0] == 0: self.start_move(self.MoveMode.LEFT) - elif value == 0 and math.ceil(self.joystick_move[0]) > 0: self.end_move(self.MoveMode.RIGHT) - elif value == 0 and math.floor(self.joystick_move[0]) < 0: self.end_move(self.MoveMode.LEFT) - - self.joystick_move[0] = value - elif axis == "y": - if math.ceil(value) > 0 and self.joystick_move[1] == 0: self.start_move(self.MoveMode.BACKWARD) - elif math.floor(value) < 0 and self.joystick_move[1] == 0: self.start_move(self.MoveMode.FORWARD) - elif value == 0 and math.ceil(self.joystick_move[1]) > 0: self.end_move(self.MoveMode.BACKWARD) - elif value == 0 and math.floor(self.joystick_move[1]) < 0: self.end_move(self.MoveMode.FORWARD) - - self.joystick_move[1] = value - - if axis == "z": self.joystick_look[0] = value - if axis == "rz": self.joystick_look[1] = value - - if axis == "rx": self.joystick_interact[0] = value - if axis == "ry": self.joystick_interact[1] = value - - print(axis) - - def on_joyhat_motion(self, joystick, hat_x, hat_y): - pass \ No newline at end of file diff --git a/community/keyboard_mouse.py b/community/keyboard_mouse.py index c1146707..9aca121e 100644 --- a/community/keyboard_mouse.py +++ b/community/keyboard_mouse.py @@ -1,9 +1,9 @@ import pyglet.window -import controller +import baseinput import math -class Keyboard_Mouse(controller.Controller): +class Keyboard_Mouse(baseinput.BaseInput): def __init__(self, game): super().__init__(game) diff --git a/community/main.py b/community/main.py index aaf34b2c..9aa966d5 100644 --- a/community/main.py +++ b/community/main.py @@ -22,7 +22,7 @@ import options import time -import joystick +import controller import keyboard_mouse from collections import deque @@ -125,7 +125,7 @@ def __init__(self, **args): self.controls = [0, 0, 0] # joystick stuff - self.joystick_controller = joystick.Joystick_controller(self) + self.controller = controller.Controller(self) # mouse and keyboard stuff self.keyboard_mouse = keyboard_mouse.Keyboard_Mouse(self) @@ -209,7 +209,7 @@ def update(self, delta_time): if not self.mouse_captured: self.player.input = [0, 0, 0] - self.joystick_controller.update_controller() + self.controller.update_controller(delta_time) self.player.update(delta_time) self.world.tick(delta_time) From 94bb39b4ee98eb249e82fd12b59bb340828bb437 Mon Sep 17 00:00:00 2001 From: drakeerv Date: Sun, 23 Apr 2023 18:23:10 -0400 Subject: [PATCH 08/14] make js demo more obvious --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a4298cb1..50adfcc3 100644 --- a/README.md +++ b/README.md @@ -62,3 +62,4 @@ The community has several features and options that can be toggled in `options.p - **C++ implementation:** https://github.com/Jukitsu/CppMinecraft-clone - **Odin implementation:** https://github.com/anthony-63/lvo - **Lua implementation:** https://github.com/brennop/lunarcraft +- **Javascript implementation:** https://github.com/drakeerv/js-minecraft-clone ([Demo](https://drakeerv.github.io/js-minecraft-clone/)) \ No newline at end of file From 2017e7057cb4b5381455bfc1f14f588f56e07b98 Mon Sep 17 00:00:00 2001 From: drakeerv Date: Mon, 24 Apr 2023 12:05:46 -0400 Subject: [PATCH 09/14] Revert "Update controller.py to use pyglet 2" This reverts commit 0d3389a3a334b3173ed8c980402efc21a112917c. --- community/audio/readme.txt | 2 +- community/baseinput.py | 125 --------------------- community/controller.py | 213 ++++++++++++++++++++---------------- community/joystick.py | 144 ++++++++++++++++++++++++ community/keyboard_mouse.py | 4 +- community/main.py | 6 +- 6 files changed, 271 insertions(+), 223 deletions(-) delete mode 100644 community/baseinput.py create mode 100644 community/joystick.py diff --git a/community/audio/readme.txt b/community/audio/readme.txt index cbdccfbe..b14182ed 100644 --- a/community/audio/readme.txt +++ b/community/audio/readme.txt @@ -1 +1 @@ -For music download .ogg (or other) from https://archive.org/details/C418-MinecraftSoundtrackVolumeAlpha/ and put them into the music dir \ No newline at end of file +For music download .ogg or other from https://archive.org/details/C418-MinecraftSoundtrackVolumeAlpha/ and put them into the music dir \ No newline at end of file diff --git a/community/baseinput.py b/community/baseinput.py deleted file mode 100644 index deb343e1..00000000 --- a/community/baseinput.py +++ /dev/null @@ -1,125 +0,0 @@ -import random -import player -import chunk -import hit - -from enum import IntEnum - -class BaseInput: - class InteractMode(IntEnum): - PLACE = 0 - BREAK = 1 - PICK = 2 - - class MiscMode(IntEnum): - RANDOM = 0 - SAVE = 1 - ESCAPE = 2 - SPEED_TIME = 3 - FULLSCREEN = 4 - FLY = 5 - TELEPORT = 6 - TOGGLE_F3 = 7 - TOGGLE_AO = 8 - - class MoveMode(IntEnum): - LEFT = 0 - RIGHT = 1 - DOWN = 2 - UP = 3 - BACKWARD = 4 - FORWARD = 5 - - class ModifierMode(IntEnum): - SPRINT = 0 - - def __init__(self, game): - self.game = game - - def interact(self, mode): - def hit_callback(current_block, next_block): - if mode == self.InteractMode.PLACE: self.game.world.try_set_block(current_block, self.game.holding, self.game.player.collider) - elif mode == self.InteractMode.BREAK: self.game.world.set_block(next_block, 0) - elif mode == self.InteractMode.PICK: self.game.holding = self.game.world.get_block_number(next_block) - - x, y, z = self.game.player.position - y += self.game.player.eyelevel - - hit_ray = hit.Hit_ray(self.game.world, self.game.player.rotation, (x, y, z)) - - while hit_ray.distance < hit.HIT_RANGE: - if hit_ray.step(hit_callback): - return True - return False - - def misc(self, mode): - if mode == self.MiscMode.RANDOM: - self.game.holding = random.randint(1, len(self.game.world.block_types) - 1) - elif mode == self.MiscMode.SAVE: - self.game.world.save.save() - elif mode == self.MiscMode.ESCAPE: - self.game.mouse_captured = False - self.game.set_exclusive_mouse(False) - elif mode == self.MiscMode.SPEED_TIME: - self.game.world.speed_daytime() - elif mode == self.MiscMode.FULLSCREEN: - self.game.toggle_fullscreen() - elif mode == self.MiscMode.FLY: - self.game.player.flying = not self.game.player.flying - elif mode == self.MiscMode.TELEPORT: - # how large is the world? - - max_y = 0 - - max_x, max_z = (0, 0) - min_x, min_z = (0, 0) - - for pos in self.game.world.chunks: - x, y, z = pos - - max_y = max(max_y, (y + 1) * chunk.CHUNK_HEIGHT) - - max_x = max(max_x, (x + 1) * chunk.CHUNK_WIDTH) - min_x = min(min_x, x * chunk.CHUNK_WIDTH) - - max_z = max(max_z, (z + 1) * chunk.CHUNK_LENGTH) - min_z = min(min_z, z * chunk.CHUNK_LENGTH) - - # get random X & Z coordinates to teleport the player to - - x = random.randint(min_x, max_x) - z = random.randint(min_z, max_z) - - # find height at which to teleport to, by finding the first non-air block from the top of the world - - for y in range(chunk.CHUNK_HEIGHT - 1, -1, -1): - if not self.game.world.get_block_number((x, y, z)): - continue - - self.game.player.teleport((x, y + 1, z)) - break - elif mode == self.MiscMode.TOGGLE_F3: - self.game.show_f3 = not self.game.show_f3 - elif mode == self.MiscMode.TOGGLE_AO: - self.game.world.toggle_AO() - - def update_move(self, axis): - self.game.player.input[axis] = round(max(-1, min(self.game.controls[axis], 1))) - - def start_move(self, mode): - axis = int((mode if mode % 2 == 0 else mode - 1) / 2) - self.game.controls[axis] += (-1 if mode % 2 == 0 else 1) - self.update_move(axis) - - def end_move(self, mode): - axis = int((mode if mode % 2 == 0 else mode - 1) / 2) - self.game.controls[axis] -= (-1 if mode % 2 == 0 else 1) - self.update_move(axis) - - def start_modifier(self, mode): - if mode == self.ModifierMode.SPRINT: - self.game.player.target_speed = player.SPRINTING_SPEED - - def end_modifier(self, mode): - if mode == self.ModifierMode.SPRINT: - self.game.player.target_speed = player.WALKING_SPEED \ No newline at end of file diff --git a/community/controller.py b/community/controller.py index 405ff1a6..7972e38c 100644 --- a/community/controller.py +++ b/community/controller.py @@ -1,95 +1,124 @@ -import pyglet.input - -import baseinput +import random import player -import math -import time +import chunk +import hit + +from enum import IntEnum + +class Controller: + class InteractMode(IntEnum): + PLACE = 0 + BREAK = 1 + PICK = 2 + + class MiscMode(IntEnum): + RANDOM = 0 + SAVE = 1 + ESCAPE = 2 + SPEED_TIME = 3 + FULLSCREEN = 4 + FLY = 5 + TELEPORT = 6 + TOGGLE_F3 = 7 + TOGGLE_AO = 8 + + class MoveMode(IntEnum): + LEFT = 0 + RIGHT = 1 + DOWN = 2 + UP = 3 + BACKWARD = 4 + FORWARD = 5 + + class ModifierMode(IntEnum): + SPRINT = 0 -class Controller(baseinput.BaseInput): def __init__(self, game): - super().__init__(game) - self.controller_manager = pyglet.input.ControllerManager() - - self.camera_sensitivity = 2.5 - self.joystick_deadzone = 0.25 - self.update_delay = 0.15 - self.last_update = 0 - - self.joystick_move = [0, 0] - self.joystick_look = [0, 0] - self.joystick_interact = [0, 0] - - self.main_controller = None - self.try_get_main_controller() - - @self.controller_manager.event - def on_connect(controller): - if self.main_controller is None: - self.new_main_controller(controller) - - print("Connect:", controller) - - @self.controller_manager.event - def on_disconnect(controller): - if self.main_controller == controller: - self.main_controller = None - self.try_get_main_controller() - - print("Disconnect:", controller) - - def try_get_main_controller(self): - if self.main_controller is None: - if len(self.controller_manager.get_controllers()) > 0: - self.new_main_controller(self.controller_manager.get_controllers()[0]) - - def new_main_controller(self, controller): - self.main_controller = controller - self.main_controller.open() - - @self.main_controller.event - def on_stick_motion(controller, stick, x, y): - if controller == self.main_controller: - if stick == "left": - self.joystick_move = [x, y] - elif stick == "right": - self.joystick_look = [x, y] - - def update_controller(self, delta_time): - if not self.game.mouse_captured or self.main_controller is None: - return - - self.joystick_move = self.apply_deadzone([self.main_controller.leftx, self.main_controller.lefty]) - self.joystick_look = self.apply_deadzone([self.main_controller.rightx, self.main_controller.righty]) - self.joystick_interact = [self.main_controller.lefttrigger, self.main_controller.righttrigger] - - self.game.player.rotation[0] += self.joystick_look[0] * self.camera_sensitivity * delta_time - self.game.player.rotation[1] += self.joystick_look[1] * self.camera_sensitivity * delta_time - - self.game.player.rotation[1] = max(-math.tau / 4, min(math.tau / 4, self.game.player.rotation[1])) - - if round(max(self.joystick_interact)) > 0 and (self.last_update + self.update_delay) <= time.process_time(): - if round(self.joystick_interact[0]) > 0: - if self.interact(self.InteractMode.BREAK): - self.main_controller.rumble_play_weak(1, duration=0.05) - if round(self.joystick_interact[1]) > 0: self.interact(self.InteractMode.PLACE) - - self.last_update = time.process_time() - - if self.main_controller.x: self.interact(self.InteractMode.PICK) - if self.main_controller.y: self.misc(self.MiscMode.RANDOM) - if self.main_controller.b: self.misc(self.MiscMode.SAVE) - - if self.main_controller.leftstick: - if self.game.player.target_speed == player.SPRINTING_SPEED: self.end_modifier(self.ModifierMode.SPRINT) - elif self.game.player.target_speed == player.WALKING_SPEED: self.start_modifier(self.ModifierMode.SPRINT) - - self.game.controls[0] = round(self.joystick_move[0]) - self.game.controls[1] = round(int(self.main_controller.a == True) - int(self.main_controller.rightstick == True)) - self.game.controls[2] = round(self.joystick_move[1]) - - for axis in range(3): self.update_move(axis) - - def apply_deadzone(self, value): - if abs(value[0]) < self.joystick_deadzone: value[0] = 0 - if abs(value[1]) < self.joystick_deadzone: value[1] = 0 - return value \ No newline at end of file + self.game = game + + def interact(self, mode): + def hit_callback(current_block, next_block): + if mode == self.InteractMode.PLACE: self.game.world.try_set_block(current_block, self.game.holding, self.game.player.collider) + elif mode == self.InteractMode.BREAK: self.game.world.set_block(next_block, 0) + elif mode == self.InteractMode.PICK: self.game.holding = self.game.world.get_block_number(next_block) + + x, y, z = self.game.player.position + y += self.game.player.eyelevel + + hit_ray = hit.Hit_ray(self.game.world, self.game.player.rotation, (x, y, z)) + + while hit_ray.distance < hit.HIT_RANGE: + if hit_ray.step(hit_callback): + break + + def misc(self, mode): + if mode == self.MiscMode.RANDOM: + self.game.holding = random.randint(1, len(self.game.world.block_types) - 1) + elif mode == self.MiscMode.SAVE: + self.game.world.save.save() + elif mode == self.MiscMode.ESCAPE: + self.game.mouse_captured = False + self.game.set_exclusive_mouse(False) + elif mode == self.MiscMode.SPEED_TIME: + self.game.world.speed_daytime() + elif mode == self.MiscMode.FULLSCREEN: + self.game.toggle_fullscreen() + elif mode == self.MiscMode.FLY: + self.game.player.flying = not self.game.player.flying + elif mode == self.MiscMode.TELEPORT: + # how large is the world? + + max_y = 0 + + max_x, max_z = (0, 0) + min_x, min_z = (0, 0) + + for pos in self.game.world.chunks: + x, y, z = pos + + max_y = max(max_y, (y + 1) * chunk.CHUNK_HEIGHT) + + max_x = max(max_x, (x + 1) * chunk.CHUNK_WIDTH) + min_x = min(min_x, x * chunk.CHUNK_WIDTH) + + max_z = max(max_z, (z + 1) * chunk.CHUNK_LENGTH) + min_z = min(min_z, z * chunk.CHUNK_LENGTH) + + # get random X & Z coordinates to teleport the player to + + x = random.randint(min_x, max_x) + z = random.randint(min_z, max_z) + + # find height at which to teleport to, by finding the first non-air block from the top of the world + + for y in range(chunk.CHUNK_HEIGHT - 1, -1, -1): + if not self.game.world.get_block_number((x, y, z)): + continue + + self.game.player.teleport((x, y + 1, z)) + break + elif mode == self.MiscMode.TOGGLE_F3: + self.game.show_f3 = not self.game.show_f3 + elif mode == self.MiscMode.TOGGLE_AO: + self.game.world.toggle_AO() + + def update_move(self, axis): + self.game.player.input[axis] = round(max(-1, min(self.game.controls[axis], 1))) + + def start_move(self, mode): + axis = int((mode if mode % 2 == 0 else mode - 1) / 2) + self.game.controls[axis] += (-1 if mode % 2 == 0 else 1) + self.update_move(axis) + + def end_move(self, mode): + axis = int((mode if mode % 2 == 0 else mode - 1) / 2) + self.game.controls[axis] -= (-1 if mode % 2 == 0 else 1) + self.update_move(axis) + + def start_modifier(self, mode): + if mode == self.ModifierMode.SPRINT: + self.game.player.target_speed = player.SPRINTING_SPEED + + def end_modifier(self, mode): + if mode == self.ModifierMode.SPRINT: + self.game.player.target_speed = player.WALKING_SPEED \ No newline at end of file diff --git a/community/joystick.py b/community/joystick.py new file mode 100644 index 00000000..455b136a --- /dev/null +++ b/community/joystick.py @@ -0,0 +1,144 @@ +import pyglet.input + +import controller +import threading +import player +import math +import time + +class Joystick_controller(controller.Controller): + def __init__(self, game): + super().__init__(game) + self.init_joysticks(pyglet.input.get_joysticks()) + + self.camera_sensitivity = 0.007 + self.joystick_deadzone = 0.25 + self.update_delay = 0.15 + self.last_update = 0 + + self.joystick_move = [0, 0] + self.joystick_look = [0, 0] + self.joystick_interact = [0, 0] + + self.joystick_updater = threading.Thread(target=self.updater, daemon=True, name="Joystick Updater") + self.joystick_updater.start() + + def updater(self): + while True: + if len(pyglet.input.get_joysticks()) != len(self.joysticks): + self.init_joysticks(pyglet.input.get_joysticks()) + + time.sleep(2) + + def init_joysticks(self, joysticks): + self.joysticks = joysticks + + for joystick in self.joysticks: + joystick.on_joybutton_press = self.on_joybutton_press + joystick.on_joybutton_release = self.on_joybutton_release + joystick.on_joyaxis_motion = self.on_joyaxis_motion + joystick.on_joyhat_motion = self.on_joyhat_motion + joystick.open(exclusive=True) + + def update_controller(self): + if not self.game.mouse_captured or not self.joysticks: + return + + self.game.player.rotation[0] += self.joystick_look[0] * self.camera_sensitivity + self.game.player.rotation[1] += -self.joystick_look[1] * self.camera_sensitivity + + self.game.player.rotation[1] = max(-math.tau / 4, min(math.tau / 4, self.game.player.rotation[1])) + + if round(max(self.joystick_interact)) > 0 and (self.last_update + self.update_delay) <= time.process_time(): + if round(self.joystick_interact[0]) > 0: self.interact(self.InteractMode.BREAK) + if round(self.joystick_interact[1]) > 0: self.interact(self.InteractMode.PLACE) + + self.last_update = time.process_time() + + def on_joybutton_press(self, joystick, button): + if "xbox" in joystick.device.name.lower(): + if button == 1: self.misc(self.MiscMode.RANDOM) + elif button == 2: self.interact(self.InteractMode.PICK) + elif button == 3: self.misc(self.MiscMode.SAVE) + + elif button == 0: self.start_move(self.MoveMode.UP) + elif button == 9: self.start_move(self.MoveMode.DOWN) + + elif button == 8: + if self.game.player.target_speed == player.SPRINTING_SPEED: self.end_modifier(self.ModifierMode.SPRINT) + elif self.game.player.target_speed == player.WALKING_SPEED: self.start_modifier(self.ModifierMode.SPRINT) + + elif "wireless controller" == joystick.device.name.lower(): + if button == 2: self.misc(self.MiscMode.RANDOM) + elif button == 0: self.interact(self.InteractMode.PICK) + elif button == 3: self.misc(self.MiscMode.SAVE) + + elif button == 1: self.start_move(self.MoveMode.UP) + elif button == 11: self.start_move(self.MoveMode.DOWN) + + elif button == 10: + if self.game.player.target_speed == player.SPRINTING_SPEED: self.end_modifier(self.ModifierMode.SPRINT) + elif self.game.player.target_speed == player.WALKING_SPEED: self.start_modifier(self.ModifierMode.SPRINT) + + def on_joybutton_release(self, joystick, button): + if "xbox" in joystick.device.name.lower(): + if button == 0: self.end_move(self.MoveMode.UP) + elif button == 9: self.end_move(self.MoveMode.DOWN) + + elif "wireless controller" == joystick.device.name.lower(): + if button == 1: self.end_move(self.MoveMode.UP) + elif button == 11: self.end_move(self.MoveMode.DOWN) + + def on_joyaxis_motion(self, joystick, axis, value): + if abs(value) < self.joystick_deadzone: + value = 0 + + if "xbox" in joystick.device.name.lower(): + if axis == "x": + if math.ceil(value) > 0 and self.joystick_move[0] == 0: self.start_move(self.MoveMode.RIGHT) + elif math.floor(value) < 0 and self.joystick_move[0] == 0: self.start_move(self.MoveMode.LEFT) + elif value == 0 and math.ceil(self.joystick_move[0]) > 0: self.end_move(self.MoveMode.RIGHT) + elif value == 0 and math.floor(self.joystick_move[0]) < 0: self.end_move(self.MoveMode.LEFT) + + self.joystick_move[0] = value + elif axis == "y": + if math.ceil(value) > 0 and self.joystick_move[1] == 0: self.start_move(self.MoveMode.BACKWARD) + elif math.floor(value) < 0 and self.joystick_move[1] == 0: self.start_move(self.MoveMode.FORWARD) + elif value == 0 and math.ceil(self.joystick_move[1]) > 0: self.end_move(self.MoveMode.BACKWARD) + elif value == 0 and math.floor(self.joystick_move[1]) < 0: self.end_move(self.MoveMode.FORWARD) + + self.joystick_move[1] = value + + if axis == "rx": self.joystick_look[0] = value + if axis == "ry": self.joystick_look[1] = value + + if axis == "z": + if value < 0: self.joystick_interact[0] = -value + if value > 0: self.joystick_interact[1] = value + + elif "wireless controller" == joystick.device.name.lower(): + if axis == "x": + if math.ceil(value) > 0 and self.joystick_move[0] == 0: self.start_move(self.MoveMode.RIGHT) + elif math.floor(value) < 0 and self.joystick_move[0] == 0: self.start_move(self.MoveMode.LEFT) + elif value == 0 and math.ceil(self.joystick_move[0]) > 0: self.end_move(self.MoveMode.RIGHT) + elif value == 0 and math.floor(self.joystick_move[0]) < 0: self.end_move(self.MoveMode.LEFT) + + self.joystick_move[0] = value + elif axis == "y": + if math.ceil(value) > 0 and self.joystick_move[1] == 0: self.start_move(self.MoveMode.BACKWARD) + elif math.floor(value) < 0 and self.joystick_move[1] == 0: self.start_move(self.MoveMode.FORWARD) + elif value == 0 and math.ceil(self.joystick_move[1]) > 0: self.end_move(self.MoveMode.BACKWARD) + elif value == 0 and math.floor(self.joystick_move[1]) < 0: self.end_move(self.MoveMode.FORWARD) + + self.joystick_move[1] = value + + if axis == "z": self.joystick_look[0] = value + if axis == "rz": self.joystick_look[1] = value + + if axis == "rx": self.joystick_interact[0] = value + if axis == "ry": self.joystick_interact[1] = value + + print(axis) + + def on_joyhat_motion(self, joystick, hat_x, hat_y): + pass \ No newline at end of file diff --git a/community/keyboard_mouse.py b/community/keyboard_mouse.py index 9aca121e..c1146707 100644 --- a/community/keyboard_mouse.py +++ b/community/keyboard_mouse.py @@ -1,9 +1,9 @@ import pyglet.window -import baseinput +import controller import math -class Keyboard_Mouse(baseinput.BaseInput): +class Keyboard_Mouse(controller.Controller): def __init__(self, game): super().__init__(game) diff --git a/community/main.py b/community/main.py index 9aa966d5..aaf34b2c 100644 --- a/community/main.py +++ b/community/main.py @@ -22,7 +22,7 @@ import options import time -import controller +import joystick import keyboard_mouse from collections import deque @@ -125,7 +125,7 @@ def __init__(self, **args): self.controls = [0, 0, 0] # joystick stuff - self.controller = controller.Controller(self) + self.joystick_controller = joystick.Joystick_controller(self) # mouse and keyboard stuff self.keyboard_mouse = keyboard_mouse.Keyboard_Mouse(self) @@ -209,7 +209,7 @@ def update(self, delta_time): if not self.mouse_captured: self.player.input = [0, 0, 0] - self.controller.update_controller(delta_time) + self.joystick_controller.update_controller() self.player.update(delta_time) self.world.tick(delta_time) From 3463830c321d65b76b0515fbc56d16a323d13cbd Mon Sep 17 00:00:00 2001 From: drakeerv Date: Mon, 24 Apr 2023 12:10:32 -0400 Subject: [PATCH 10/14] made requested changes --- community/audio/readme.md | 1 + community/audio/readme.txt | 1 - community/baseinput.py | 3 ++- 3 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 community/audio/readme.md delete mode 100644 community/audio/readme.txt diff --git a/community/audio/readme.md b/community/audio/readme.md new file mode 100644 index 00000000..a25e7240 --- /dev/null +++ b/community/audio/readme.md @@ -0,0 +1 @@ +For music download `.ogg` (or other) from [archive.org](https://archive.org/details/C418-MinecraftSoundtrackVolumeAlpha/) and put them into the music dir \ No newline at end of file diff --git a/community/audio/readme.txt b/community/audio/readme.txt deleted file mode 100644 index cbdccfbe..00000000 --- a/community/audio/readme.txt +++ /dev/null @@ -1 +0,0 @@ -For music download .ogg (or other) from https://archive.org/details/C418-MinecraftSoundtrackVolumeAlpha/ and put them into the music dir \ No newline at end of file diff --git a/community/baseinput.py b/community/baseinput.py index deb343e1..7ee01e40 100644 --- a/community/baseinput.py +++ b/community/baseinput.py @@ -122,4 +122,5 @@ def start_modifier(self, mode): def end_modifier(self, mode): if mode == self.ModifierMode.SPRINT: - self.game.player.target_speed = player.WALKING_SPEED \ No newline at end of file + self.game.player.target_speed = player.WALKING_SPEED + \ No newline at end of file From 7a3c7f8618f4697c2e97b44b0c2f50f3b86e7c49 Mon Sep 17 00:00:00 2001 From: drakeerv Date: Mon, 24 Apr 2023 21:29:44 -0400 Subject: [PATCH 11/14] add newline --- community/baseinput.py | 2 +- community/controller.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/community/baseinput.py b/community/baseinput.py index 7ee01e40..cbcc76c8 100644 --- a/community/baseinput.py +++ b/community/baseinput.py @@ -123,4 +123,4 @@ def start_modifier(self, mode): def end_modifier(self, mode): if mode == self.ModifierMode.SPRINT: self.game.player.target_speed = player.WALKING_SPEED - \ No newline at end of file + diff --git a/community/controller.py b/community/controller.py index 405ff1a6..1239e929 100644 --- a/community/controller.py +++ b/community/controller.py @@ -92,4 +92,5 @@ def update_controller(self, delta_time): def apply_deadzone(self, value): if abs(value[0]) < self.joystick_deadzone: value[0] = 0 if abs(value[1]) < self.joystick_deadzone: value[1] = 0 - return value \ No newline at end of file + return value + From c3400594837fee51a6ceac7c2012b0fc8ea461aa Mon Sep 17 00:00:00 2001 From: obiwac Date: Mon, 15 May 2023 18:41:50 +0200 Subject: [PATCH 12/14] ~: Set `double_buffer` to true in GL config --- episode-10/main.py | 4 ++-- episode-11/main.py | 3 +-- episode-12/main.py | 3 +-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/episode-10/main.py b/episode-10/main.py index 349d0764..5e1cb717 100644 --- a/episode-10/main.py +++ b/episode-10/main.py @@ -145,9 +145,9 @@ def on_key_release(self, key, modifiers): class Game: def __init__(self): - self.config = gl.Config(major_version = 3, minor_version = 3, depth_size = 16) + self.config = gl.Config(double_buffer = True, major_version = 3, minor_version = 3, depth_size = 16) self.window = Window(config = self.config, width = 800, height = 600, caption = "Minecraft clone", resizable = True, vsync = False) - + def run(self): pyglet.app.run() diff --git a/episode-11/main.py b/episode-11/main.py index d25691be..ed1a95cc 100644 --- a/episode-11/main.py +++ b/episode-11/main.py @@ -1,4 +1,3 @@ - import math import ctypes import random @@ -154,7 +153,7 @@ def on_key_release(self, key, modifiers): class Game: def __init__(self): - self.config = gl.Config(major_version = 3, minor_version = 3, depth_size = 16) + self.config = gl.Config(double_buffer = True, major_version = 3, minor_version = 3, depth_size = 16) self.window = Window(config = self.config, width = 800, height = 600, caption = "Minecraft clone", resizable = True, vsync = False) def run(self): diff --git a/episode-12/main.py b/episode-12/main.py index fefb347f..5ea86f1a 100644 --- a/episode-12/main.py +++ b/episode-12/main.py @@ -1,4 +1,3 @@ - import math import ctypes import random @@ -194,7 +193,7 @@ def on_key_release(self, key, modifiers): class Game: def __init__(self): - self.config = gl.Config(major_version = 3, minor_version = 3, depth_size = 16) + self.config = gl.Config(double_buffer = True, major_version = 3, minor_version = 3, depth_size = 16) self.window = Window(config = self.config, width = 800, height = 600, caption = "Minecraft clone", resizable = True, vsync = False) def run(self): From eec9efaaa1effe6c705dd250eb8eda4f573592f6 Mon Sep 17 00:00:00 2001 From: obiwac Date: Mon, 15 May 2023 18:47:59 +0200 Subject: [PATCH 13/14] 13: Set `double_buffer` to true in GL config --- episode-12/.main.py.swp | Bin 16384 -> 0 bytes episode-13/main.py | 3 +-- 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 100644 episode-12/.main.py.swp diff --git a/episode-12/.main.py.swp b/episode-12/.main.py.swp deleted file mode 100644 index db12fdf8f466fd93ce75a91f581f7c298d8c23e2..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16384 zcmeI3Uu+yl9mltA(sCi81vI~?)YO1upZ}<~YuV>GX&NnidvkYN@9l1P z_iUd{T0jCJT2w$qNC;7QX?X&*yfp-gKLt@FcmPD9BE0ZNAb3HP2dW~zznR(fozG_{ zs1G3aq>pcRXXf{t`OR-;=l8p$Qam?1&(0VpHGJ;Sv|Fu8HGEOip1xVr{ED+-*;KIq zXMT2JcA-2peeeA8%md5)>pm(44hQYH;d?_($K!Tr)#4%B^*uh!11IvUd}!q4aMN-; zBWU**m6c=`$SiQf7C5HeeR8Bo%?9%CWN&}l$_-1%8e|s8ERb0svp{Bn%mSGOG7DrD z$Sm-`Yk@epS$hoKN4PIOpHkm9?fSk&-Jez8kMAm1x7k-_fy@G#1u_d{7RW4+Ss=4O zW`WECnFTTnWERLQ@Ly;FtEy@5Lcbc60Ko76N&Nr!+coVE;AQX<_!0OjcobX)HNe3t zm<8_xqd*651$Tg3z(3xiX|IAmgFk^6!Pmg2!4qH`Y=KMQK`;ZR!R_G6F-?0GJOe%t zo&pbn_k%G|04KmJM>Xx&;K$%4@H}`9d=9u^4a|cAI024;9C-E3n)VCubMRg89q=VE z4c-Cn1c$*P@Vg`EJNQ2MGI$bfgDvndaKJgR0*c@W_*bF zgPXv$H)+~$z}LY&;E%Uy+Q-00!H2*km;nE}RnuMvKLgK!XTdWd1n0px_#ik6UOud8 zzXaQ03!DOn!6ERa3|CO)N{ZWyuEV#Dkd8atle{iJhIbTl6v>{&}4r3_Y7Lt)3^_+I41 z&bkw~Va~lfglS9LZ0>3!8>KSP^P*8Ko|01|`qyt-x=bx+W^en+8Or4%xu|Q8ef<$iHTQ&&~H&D z#fs@PHvG`77P_{E#Eb=|oMTJcx=jY^oDJUQE??&^IwJ?Q1r41Q0-wz8dt#=<9wnWm zcXt$aGUjI|C+A9PT{L{RDz`=6v5$l;YyBZSIJ)TVcpO)kS4zHL`B2+rH~MRUVRBTGO5L zt?!c3F=#Qr1FT_rRd|E&9vE#H`?*cV6Sj(k{nd6tRuTrKKJrc5ZAH#HmwSR<4qIG| zTh!=wYRoMaqy6>lN3w0_jwD?~TC-=9==7R(hi{k-?$jGGd91jA)}AUGPBm_zhWMr- z)?K6So^;&+3N6r>!+oJVlEck7y>2e;m)TWC46m`_T zCn~0r$nFD7ys22N<=N!s!p@=-Q;z3E4P9(eqTkXru#0Zsp&EeyY-`OZH=lBibltcuHZTu;xL6DxCb<`mwbB(%49vb0#9G0UapvS>fRr~@pDTeVth zuNBX!`C)U!k^4c^u;7GdWHkd9-&ks5zgFu(s5pxQO&QA$FCf*8a_M||WvOJIoHWOm zmc}nA&sOU}RyCWJXVr1yiPgOoVsA_PJq`=5^F764C1P{~tOC7EwHBuC^oUkawPLuj z6^D+^BQmR+L}7HQ+VK)p#~s;7moJLgX~OrRsms&k43Dvk(w&NzP+>p+7d=gUY-(|N zlx>d~MZDW~l%yV})KlM*7q95V&dYjd4?J~>4Y2o0Lo}Vp-kHS?D_}9Gku8eTW$zX* zr#G1&T0qI3B$k=7v=p*SMbB<9Ww{Y;2kM-Qf8T7c8Ij%K)t1XmtTG<1TdwSb9BP3{ zTbxd&R5snENcPddIcI0B^l?iJSPWk^slh|cmUL*nlJD}g#HDF@s|`@NI*AY$#zQFP|9DL-BvW<-wbj(#7h)SdhXQ$nWUZ}<@{YCdtI>1yDQ9@6mR)9Cx zpkcyDco&=c@#8p+N6KZ>r_wJ}*{Ei_Rur*S5l!gbv4wm{{_Qkr@1vJTdM(we0=v=j z*3zQHKx%0wt>p_gZU;QADLo>oAPtt>i8Z&mQ&omNb(dKYtGk+}`sxQ_kGz<#@=B|2 z*4;cE0i_e|u0jg@4O50r9rY5GvVL+G;^pTzlRKL_9wm2lmeeTzzYQ_v3B;Qe|DRRC z+!e&`FMuxqiuXSZE`a+0#rg&q0(XMf5Uc+a{0jUK`~Z9wdPdxCfjDKSfOb6Yxb)2N%G3Py}~@cZ1gvi(dm*zzg6>@Cc}b8W;qBLG1kv5Q0a* zF!*Z@IRx+&_#}t`#onL5xIYeBpaLu~1t|U|n;Zw(C$m6ify@G#1u_f#A6p<-x0>7t zTOOS~=^AI0X?)=p$9~iFaBZ5Dz{s13HXP41^Wwa&jxbgbB&{j_AVGAF>xez%p$1~r zQO@?gnnM|2agvpHDsIKYGera*X)t`6?kM?(XcL9``Q(2WGV#9)abOi8D@BMXrf|K$ z)}yv(<4&Bk(!+166(QY(=z(7KTWDui5FPR`ZQisV_CphA;z;VeTjc0=KdSN|ZkQsX zM$Y#1UPoD&KptVqoWf*pRMvWReD3}?C}%B_uLq@l_sv<0tTzt%`wtFrp_pzJuE~ry za@NmoukCT3O^?l)&jlwD*)q>l~Z&(Mh6NDx#~teb2MR?LtMwi_(=ksy(M z#WiyBZBdUhfpLT5I*elcmf#IpPAGGOtQPuBQ5gFH^K0TVjZ|}#(UswqL0^r7jC{gS@yNf-@Y~K$d5z%zi-Es6(Wguc-G|mZ~ zP$PaFkowsc92KAAtkYt1rG@G8jObQQ^pG0iR|av7g!UFq-e8|bG#H_ZMwF<+Fxci^ zs#a$wJ2iM{+%JA6$|Hi5*2c zov=wHc3C+hv1^+&ogy(REF`8!&E!r238fP18~zt9?vP&gH<(Mm7%~TQh4~R$K7hjg x*VdBkK}W|TYn>bPhu*ITw0@wY^-d;impfurxLm$u<&PQ0PWmDmq_s1s{TmF8dvgE) diff --git a/episode-13/main.py b/episode-13/main.py index 5c6a28af..67b11c0a 100644 --- a/episode-13/main.py +++ b/episode-13/main.py @@ -1,4 +1,3 @@ - import math import random import pyglet @@ -162,7 +161,7 @@ def on_key_release(self, key, modifiers): class Game: def __init__(self): - self.config = gl.Config(major_version = 3, minor_version = 3, depth_size = 16) + self.config = gl.Config(double_buffer = True, major_version = 3, minor_version = 3, depth_size = 16) self.window = Window(config = self.config, width = 800, height = 600, caption = "Minecraft clone", resizable = True, vsync = False) def run(self): From 7c56b979e5c8c75ac8f72b8aa7dcf8468e5f41d9 Mon Sep 17 00:00:00 2001 From: obiwac Date: Mon, 15 May 2023 18:49:00 +0200 Subject: [PATCH 14/14] gitignore: Ignore VIM swap files --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 27541f05..a645c131 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ *.core *.log .vscode -*.ogg \ No newline at end of file +*.ogg +*.sw[nop]