Skip to content

Commit

Permalink
ep13: Reorganize code and fix issues reported by LSP
Browse files Browse the repository at this point in the history
  • Loading branch information
obiwac committed Jul 26, 2024
1 parent 4b6364e commit a60f5dd
Show file tree
Hide file tree
Showing 21 changed files with 1,426 additions and 47 deletions.
92 changes: 45 additions & 47 deletions episode-13/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
pyglet.options["debug_gl"] = False

import pyglet.gl as gl
import pyglet.window.mouse

import shader
import player


import chunk
import world

import hit
from src.entity.player import SPRINTING_SPEED, WALKING_SPEED, Player
from src.physics.hit import HIT_RANGE, HitRay
from src.renderer.shader import Shader
from src.world import World
from src.chunk.chunk import CHUNK_HEIGHT, CHUNK_WIDTH, CHUNK_LENGTH


class Window(pyglet.window.Window):
Expand All @@ -23,11 +21,11 @@ def __init__(self, **args):

# create world

self.world = world.World()
self.world = World()

# create shader

self.shader = shader.Shader("vert.glsl", "frag.glsl")
self.shader = Shader("shaders/vert.glsl", "shaders/frag.glsl")
self.shader_sampler_location = self.shader.find_uniform(b"texture_array_sampler")
self.shader.use()

Expand All @@ -38,7 +36,7 @@ def __init__(self, **args):

# player stuff

self.player = player.Player(self.world, self.shader, self.width, self.height)
self.player = Player(self.world, self.shader, self.width, self.height)

# misc stuff

Expand Down Expand Up @@ -101,54 +99,54 @@ def hit_callback(current_block, next_block):
x, y, z = self.player.position
y += self.player.eyelevel

hit_ray = hit.Hit_ray(self.world, self.player.rotation, (x, y, z))
hit_ray = HitRay(self.world, self.player.rotation, (x, y, z))

while hit_ray.distance < hit.HIT_RANGE:
while hit_ray.distance < HIT_RANGE:
if hit_ray.step(hit_callback):
break

def on_mouse_motion(self, x, y, delta_x, delta_y):
def on_mouse_motion(self, x, y, dx, dy):
if self.mouse_captured:
sensitivity = 0.004

self.player.rotation[0] += delta_x * sensitivity
self.player.rotation[1] += delta_y * sensitivity
self.player.rotation[0] += dx * sensitivity
self.player.rotation[1] += dy * sensitivity

self.player.rotation[1] = max(-math.tau / 4, min(math.tau / 4, self.player.rotation[1]))

def on_mouse_drag(self, x, y, delta_x, delta_y, buttons, modifiers):
self.on_mouse_motion(x, y, delta_x, delta_y)
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
self.on_mouse_motion(x, y, dx, dy)

def on_key_press(self, key, modifiers):
def on_key_press(self, symbol, modifiers):
if not self.mouse_captured:
return

if key == pyglet.window.key.D:
if symbol == pyglet.window.key.D:
self.player.input[0] += 1
elif key == pyglet.window.key.A:
elif symbol == pyglet.window.key.A:
self.player.input[0] -= 1
elif key == pyglet.window.key.W:
elif symbol == pyglet.window.key.W:
self.player.input[2] += 1
elif key == pyglet.window.key.S:
elif symbol == pyglet.window.key.S:
self.player.input[2] -= 1

elif key == pyglet.window.key.SPACE:
elif symbol == pyglet.window.key.SPACE:
self.player.input[1] += 1
elif key == pyglet.window.key.LSHIFT:
elif symbol == pyglet.window.key.LSHIFT:
self.player.input[1] -= 1
elif key == pyglet.window.key.LCTRL:
self.player.target_speed = player.SPRINTING_SPEED
elif symbol == pyglet.window.key.LCTRL:
self.player.target_speed = SPRINTING_SPEED

elif key == pyglet.window.key.F:
elif symbol == pyglet.window.key.F:
self.player.flying = not self.player.flying

elif key == pyglet.window.key.G:
elif symbol == pyglet.window.key.G:
self.holding = random.randint(1, len(self.world.block_types) - 1)

elif key == pyglet.window.key.O:
elif symbol == pyglet.window.key.O:
self.world.save.save()

elif key == pyglet.window.key.R:
elif symbol == pyglet.window.key.R:
# how large is the world?

max_y = 0
Expand All @@ -159,13 +157,13 @@ def on_key_press(self, key, modifiers):
for pos in self.world.chunks:
x, y, z = pos

max_y = max(max_y, (y + 1) * chunk.CHUNK_HEIGHT)
max_y = max(max_y, (y + 1) * CHUNK_HEIGHT)

max_x = max(max_x, (x + 1) * chunk.CHUNK_WIDTH)
min_x = min(min_x, x * chunk.CHUNK_WIDTH)
max_x = max(max_x, (x + 1) * CHUNK_WIDTH)
min_x = min(min_x, x * CHUNK_WIDTH)

max_z = max(max_z, (z + 1) * chunk.CHUNK_LENGTH)
min_z = min(min_z, z * chunk.CHUNK_LENGTH)
max_z = max(max_z, (z + 1) * CHUNK_LENGTH)
min_z = min(min_z, z * CHUNK_LENGTH)

# get random X & Z coordinates to teleport the player to

Expand All @@ -174,36 +172,36 @@ def on_key_press(self, key, modifiers):

# 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):
for y in range(CHUNK_HEIGHT - 1, -1, -1):
if not self.world.get_block_number((x, y, z)):
continue

self.player.teleport((x, y + 1, z))
break

elif key == pyglet.window.key.ESCAPE:
elif symbol == pyglet.window.key.ESCAPE:
self.mouse_captured = False
self.set_exclusive_mouse(False)

def on_key_release(self, key, modifiers):
def on_key_release(self, symbol, modifiers):
if not self.mouse_captured:
return

if key == pyglet.window.key.D:
if symbol == pyglet.window.key.D:
self.player.input[0] -= 1
elif key == pyglet.window.key.A:
elif symbol == pyglet.window.key.A:
self.player.input[0] += 1
elif key == pyglet.window.key.W:
elif symbol == pyglet.window.key.W:
self.player.input[2] -= 1
elif key == pyglet.window.key.S:
elif symbol == pyglet.window.key.S:
self.player.input[2] += 1

elif key == pyglet.window.key.SPACE:
elif symbol == pyglet.window.key.SPACE:
self.player.input[1] -= 1
elif key == pyglet.window.key.LSHIFT:
elif symbol == pyglet.window.key.LSHIFT:
self.player.input[1] += 1
elif key == pyglet.window.key.LCTRL:
self.player.target_speed = player.WALKING_SPEED
elif symbol == pyglet.window.key.LCTRL:
self.player.target_speed = WALKING_SPEED


class Game:
Expand Down
9 changes: 9 additions & 0 deletions episode-13/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ ruff = "^0.5.5"
exclude = [".venv"]
venvPath = "."
venv = ".venv"

# For some reason, pyright thinks '__setitem__' is not defined on 'pyglet.options' when it totally is.

reportIndexIssue = false

# From https://github.com/obiwac/python-minecraft-clone/pull/107:
# F405 * may be undefined, or defined from star imports: These are indeed defined from star imports. I guess we could import all the symbols in '__all__' explicitly, but if there's a mistake here and it causes a runtime error, that's not the end of the world.

ignore = ["models/__init__.py"]
18 changes: 18 additions & 0 deletions episode-13/shaders/frag.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#version 330

out vec4 fragment_colour;

uniform sampler2DArray texture_array_sampler;

in vec3 local_position;
in vec3 interpolated_tex_coords;
in float interpolated_shading_value;

void main(void) {
vec4 texture_colour = texture(texture_array_sampler, interpolated_tex_coords);
fragment_colour = texture_colour * interpolated_shading_value;

if (texture_colour.a == 0.0) { // discard if texel's alpha component is 0 (texel is transparent)
discard;
}
}
18 changes: 18 additions & 0 deletions episode-13/shaders/vert.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#version 330

layout(location = 0) in vec3 vertex_position;
layout(location = 1) in vec3 tex_coords;
layout(location = 2) in float shading_value;

out vec3 local_position;
out vec3 interpolated_tex_coords;
out float interpolated_shading_value;

uniform mat4 matrix;

void main(void) {
local_position = vertex_position;
interpolated_tex_coords = tex_coords;
interpolated_shading_value = shading_value;
gl_Position = matrix * vec4(vertex_position, 1.0);
}
Empty file added episode-13/src/__init__.py
Empty file.
Empty file.
Loading

0 comments on commit a60f5dd

Please sign in to comment.