Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions community/block_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import models.cube # default model

class Block_type:
# new optional model argument (cube model by default)
def __init__(self, texture_manager, name = "unknown", block_face_textures = {"all": "cobblestone"}, model = models.cube):
self.name = name
self.block_face_textures = block_face_textures
self.model = model

# create members based on model attributes

self.transparent = model.transparent
self.is_cube = model.is_cube
self.glass = model.glass
self.translucent = model.translucent

# replace data contained in numbers.py with model specific data

self.vertex_positions = model.vertex_positions
self.tex_coords = model.tex_coords # to deprecate
self.tex_indices = [0] * len(self.tex_coords)
self.shading_values = model.shading_values

def set_block_face(face, texture):
# make sure we don't add inexistent face
if face > len(self.tex_coords) - 1:
return
self.tex_indices[face] = texture

for face in block_face_textures:
texture = block_face_textures[face]
texture_manager.add_texture(texture)

texture_index = texture_manager.textures.index(texture)

if face == "all":
for i in range(len(self.tex_coords)):
set_block_face(i, texture_index)

elif face == "sides":
set_block_face(0, texture_index)
set_block_face(1, texture_index)
set_block_face(4, texture_index)
set_block_face(5, texture_index)

elif face == "x":
set_block_face(0, texture_index)
set_block_face(1, texture_index)

elif face == "y":
set_block_face(2, texture_index)
set_block_face(3, texture_index)

elif face == "z":
set_block_face(4, texture_index)
set_block_face(5, texture_index)

else:
set_block_face(["right", "left", "top", "bottom", "front", "back"].index(face), texture_index)
63 changes: 63 additions & 0 deletions community/camera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import math
import matrix

WALKING_SPEED = 7
SPRINTING_SPEED = 21

class Camera:
def __init__(self, shader, width, height):
self.width = width
self.height = height

# create matrices

self.mv_matrix = matrix.Matrix()
self.p_matrix = matrix.Matrix()
self.mvp_matrix = matrix.Matrix()

# shaders

self.shader = shader
self.shader_matrix_location = self.shader.find_uniform(b"u_ModelViewProjMatrix")

# camera variables

self.input = [0, 0, 0]

self.position = [0, 80, 0]
self.rotation = [-math.tau / 4, 0]

self.target_speed = WALKING_SPEED
self.speed = self.target_speed

def update_camera(self, delta_time):
self.speed += (self.target_speed - self.speed) * delta_time * 20
multiplier = self.speed * delta_time

self.position[1] += self.input[1] * multiplier

if self.input[0] or self.input[2]:
angle = self.rotation[0] - math.atan2(self.input[2], self.input[0]) + math.tau / 4

self.position[0] += math.cos(angle) * multiplier
self.position[2] += math.sin(angle) * multiplier

def update_matrices(self):
# create projection matrix

self.p_matrix.load_identity()

self.p_matrix.perspective(
90 + 20 * (self.speed - WALKING_SPEED) / (SPRINTING_SPEED - WALKING_SPEED),
float(self.width) / self.height, 0.1, 500)

# create modelview matrix

self.mv_matrix.load_identity()
self.mv_matrix.rotate_2d(self.rotation[0] + math.tau / 4, self.rotation[1])
self.mv_matrix.translate(-self.position[0], -self.position[1], -self.position[2])

# modelviewprojection matrix

self.mvp_matrix = self.p_matrix * self.mv_matrix
self.shader.uniform_matrix(self.shader_matrix_location, self.mvp_matrix)
164 changes: 164 additions & 0 deletions community/chunk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
import ctypes

import pyglet.gl as gl

import subchunk

CHUNK_WIDTH = 16
CHUNK_HEIGHT = 128
CHUNK_LENGTH = 16

class Chunk:
def __init__(self, world, chunk_position):
self.world = world

self.modified = False
self.chunk_position = chunk_position

self.position = (
self.chunk_position[0] * CHUNK_WIDTH,
self.chunk_position[1] * CHUNK_HEIGHT,
self.chunk_position[2] * CHUNK_LENGTH)

self.blocks = [[[0
for z in range(CHUNK_LENGTH)]
for y in range(CHUNK_HEIGHT)]
for x in range(CHUNK_WIDTH )]

self.subchunks = {}

for x in range(int(CHUNK_WIDTH / subchunk.SUBCHUNK_WIDTH)):
for y in range(int(CHUNK_HEIGHT / subchunk.SUBCHUNK_HEIGHT)):
for z in range(int(CHUNK_LENGTH / subchunk.SUBCHUNK_LENGTH)):
self.subchunks[(x, y, z)] = subchunk.Subchunk(self, (x, y, z))

# mesh variables

self.mesh = []
self.translucent_mesh = []

self.mesh_quad_count = 0
self.translucent_quad_count = 0

# create VAO and VBO's

self.vao = gl.GLuint(0)
gl.glGenVertexArrays(1, ctypes.byref(self.vao))
gl.glBindVertexArray(self.vao)

self.vbo = gl.GLuint(0)
gl.glGenBuffers(1, ctypes.byref(self.vbo))
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.vbo)
gl.glBufferData(gl.GL_ARRAY_BUFFER, ctypes.sizeof(gl.GLfloat * CHUNK_WIDTH * CHUNK_HEIGHT * CHUNK_LENGTH * 8), None, gl.GL_DYNAMIC_DRAW)

gl.glVertexAttribPointer(0, 3, gl.GL_FLOAT,
gl.GL_FALSE, 6 * ctypes.sizeof(gl.GLfloat), 0)
gl.glEnableVertexAttribArray(0)
gl.glVertexAttribPointer(1, 2, gl.GL_FLOAT,
gl.GL_FALSE, 6 * ctypes.sizeof(gl.GLfloat), 3 * ctypes.sizeof(gl.GLfloat))
gl.glEnableVertexAttribArray(1)
gl.glVertexAttribPointer(2, 1, gl.GL_FLOAT,
gl.GL_FALSE, 6 * ctypes.sizeof(gl.GLfloat), 5 * ctypes.sizeof(gl.GLfloat))
gl.glEnableVertexAttribArray(2)

gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, world.ibo)

def __del__(self):
gl.glDeleteBuffers(1, ctypes.byref(self.vbo))
gl.glDeleteVertexArrays(1, ctypes.byref(self.vao))

def update_subchunk_meshes(self):
for subchunk_position in self.subchunks:
subchunk = self.subchunks[subchunk_position]
subchunk.update_mesh()

def update_at_position(self, position):
x, y, z = position

lx = int(x % subchunk.SUBCHUNK_WIDTH )
ly = int(y % subchunk.SUBCHUNK_HEIGHT)
lz = int(z % subchunk.SUBCHUNK_LENGTH)

clx, cly, clz = self.world.get_local_position(position)

sx = clx // subchunk.SUBCHUNK_WIDTH
sy = cly // subchunk.SUBCHUNK_HEIGHT
sz = clz // subchunk.SUBCHUNK_LENGTH

self.subchunks[(sx, sy, sz)].update_mesh()

def try_update_subchunk_mesh(subchunk_position):
if subchunk_position in self.subchunks:
self.subchunks[subchunk_position].update_mesh()

if lx == subchunk.SUBCHUNK_WIDTH - 1: try_update_subchunk_mesh((sx + 1, sy, sz))
if lx == 0: try_update_subchunk_mesh((sx - 1, sy, sz))

if ly == subchunk.SUBCHUNK_HEIGHT - 1: try_update_subchunk_mesh((sx, sy + 1, sz))
if ly == 0: try_update_subchunk_mesh((sx, sy - 1, sz))

if lz == subchunk.SUBCHUNK_LENGTH - 1: try_update_subchunk_mesh((sx, sy, sz + 1))
if lz == 0: try_update_subchunk_mesh((sx, sy, sz - 1))

def update_mesh(self):
# combine all the small subchunk meshes into one big chunk mesh

for subchunk_position in self.subchunks:
subchunk = self.subchunks[subchunk_position]

self.mesh.extend(subchunk.mesh)
self.translucent_mesh.extend(subchunk.translucent_mesh)

# send the full mesh data to the GPU and free the memory used client-side (we don't need it anymore)
# don't forget to save the length of 'self.mesh_indices' before freeing

self.mesh_quad_count = len(self.mesh) // 24
self.translucent_quad_count = len(self.translucent_mesh) // 24

self.send_mesh_data_to_gpu()

self.mesh = []
self.translucent_mesh = []

def send_mesh_data_to_gpu(self): # pass mesh data to gpu
if not self.mesh_quad_count:
return

self.mesh.extend(self.translucent_mesh)

gl.glBindVertexArray(self.vao)

gl.glBindBuffer(gl.GL_ARRAY_BUFFER, self.vbo)
gl.glBufferSubData(
gl.GL_ARRAY_BUFFER,
0,
ctypes.sizeof(gl.GLfloat * len(self.mesh)),
(gl.GLfloat * len(self.mesh)) (*self.mesh))


def draw(self):
if not self.mesh_quad_count:
return

gl.glBindVertexArray(self.vao)

gl.glDrawElementsBaseVertex(
gl.GL_TRIANGLES,
self.mesh_quad_count * 6,
gl.GL_UNSIGNED_INT,
None,
0)

def draw_translucent(self):
if not self.translucent_quad_count:
return

gl.glBindVertexArray(self.vao)

gl.glDrawElementsBaseVertex(
gl.GL_TRIANGLES,
self.translucent_quad_count * 6,
gl.GL_UNSIGNED_INT,
None,
self.mesh_quad_count * 4
)
91 changes: 91 additions & 0 deletions community/data/blocks.mcpy
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# block ID's from:
# https://www.minecraftforum.net/forums/minecraft-java-edition/discussion/114963-all-item-block-ids-in-one-place
# (with some slight modifications)

1: name "Stone", texture.all stone
2: name "Grass", texture.top grass, texture.bottom dirt, texture.sides grass_side
3: name "Dirt", texture.all dirt
4: name "Cobblestone", texture.all cobblestone
5: name "Planks", texture.all planks
6: name "Sapling", model models.plant, texture.all sapling
7: name "Bedrock", texture.all bedrock
8: name "Water", model models.liquid, texture.all water
9: sameas 8, name "Stationary Water"
10: name "Lava", model models.liquid, texture.all lava
11: sameas 10, name "Stationary Lava"
12: name "Sand", texture.all sand
13: name "Gravel", texture.all gravel
14: name "Gold Ore", texture.all gold_ore
15: name "Iron Ore", texture.all iron_ore
16: name "Coal Ore", texture.all coal_ore
17: name "Log", texture.y log_y, texture.sides log_side
18: name "Leaves", model models.leaves, texture.all leaves
19: name "Sponge", texture.all sponge
20: name "Glass", model models.glass, texture.all glass
21: name "Red Cloth", texture.all red_cloth
22: name "Orange Cloth", texture.all orange_cloth
23: name "Yellow Cloth", texture.all yellow_cloth
24: name "Lime Cloth", texture.all lime_cloth
25: name "Green Cloth", texture.all green_cloth
26: name "Aqua Cloth", texture.all aqua_cloth
27: name "Cyan Cloth", texture.all cyan_cloth
28: name "Blue Cloth", texture.all blue_cloth
29: name "Purple Cloth", texture.all purple_cloth
30: name "Indigo Cloth", texture.all indigo_cloth
31: name "Violet Cloth", texture.all violet_cloth
32: name "Magenta Cloth", texture.all magenta_cloth
33: name "Pink Cloth", texture.all pink_cloth
34: name "Black Cloth", texture.all black_cloth
35: name "Grey Cloth", texture.all grey_cloth
36: name "White Cloth", texture.all white_cloth
37: name "Yellow Flower", model models.plant, texture.all yellow_flower
38: name "Red Rose", model models.plant, texture.all red_rose
39: name "Brown Mushroom", model models.plant, texture.all brown_mushroom
40: name "Red Mushroom", model models.plant, texture.all red_mushroom
41: name "Gold Block", texture.all gold_block
42: name "Iron Block", texture.all iron_block
43: name "Double Slab", texture.sides slab_side, texture.y slab_y
44: name "Slab", model models.slab, texture.sides slab_side, texture.y slab_y
45: name "Bricks", texture.all bricks
46: name "TNT", texture.top tnt_top, texture.bottom tnt_bottom, texture.sides tnt_side
47: name "Bookshelf", texture.y planks, texture.sides bookshelf
48: name "Mossy Cobblestone", texture.all mossy_cobblestone
49: name "Obsidian", texture.all obsidian
50: name "Torch", model models.torch, texture.top torch_top, texture.bottom torch, texture.sides torch
51: name "Fire", model models.fire, texture.all fire
# I know, the model name isn't great, but it's got the same graphical properties
52: name "Mob Spawner", model models.leaves, texture.all mob_spawner
53: name "Wooden Stairs", model models.stairs, texture.all planks
54: name "Chest", texture.y chest_top, texture.sides chest_side, texture.front chest_front
55: name "Redstone Wire", model models.flat, texture.all redstone_wire
56: name "Diamond Ore", texture.all diamond_ore
57: name "Diamond Block", texture.all diamond_block
58: name "Crafting Table", texture.top crafting_table_top, texture.bottom planks, texture.x crafting_table_x, texture.z crafting_table_z
59: name "Crops", model models.crop, texture.all crops
60: name "Soil", model models.soil, texture.all dirt, texture.top soil
61: name "Furnace", texture.y furnace_y, texture.sides furnace_side, texture.front furnace_front
62: name "Lit Furnace", texture.y furnace_y, texture.sides furnace_side, texture.front lit_furnace_front
63: name "Sign Post", model models.sign_post, texture.all planks
64: name "Wooden Door", model models.door, texture.all wooden_door
65: name "Ladder", model models.ladder, texture.all ladder
66: name "Rails", model models.flat, texture.all rails
67: name "Cobblestone Stairs", model models.stairs, texture.all cobblestone
68: name "Sign", model models.sign, texture.all planks
69: name "Lever", model models.lever, texture.all lever
70: name "Stone Pressure Plate", model models.pressure_plate, texture.all stone
71: name "Iron Door", model models.door, texture.all iron_door_bottom_half
72: name "Wooden Pressure Plate", model models.pressure_plate, texture.all planks
73: name "Redstone Ore", texture.all redstone_ore
# when we implement a lighting system, this will have some kind of "emissive" property
74: name "Lit Redstone Ore", texture.all redstone_ore
75: name "Redstone Torch", model models.torch, texture.top redstone_torch_top, texture.bottom redstone_torch, texture.sides redstone_torch
76: name "Redstone Torch (Off)", model models.torch, texture.top off_redstone_torch_top, texture.bottom off_redstone_torch, texture.sides off_redstone_torch
77: name "Stone Button", model models.button, texture.all stone
78: name "Snow", model models.snow, texture.all snow
# ditto as for mob spawners (52)
79: name "Ice", model models.glass, texture.all ice
80: name "Snow Block", texture.all snow
81: name "Cactus", model models.cactus, texture.top cactus_top, texture.bottom cactus_bottom, texture.sides cactus_side
82: name "Clay", texture.all clay
83: name "Sugar Cane", model models.plant, texture.all sugar_cane
84: name "Jukebox", texture.all jukebox, texture.top jukebox_top
Loading