diff options
Diffstat (limited to 'block.gd')
-rw-r--r-- | block.gd | 160 |
1 files changed, 160 insertions, 0 deletions
diff --git a/block.gd b/block.gd new file mode 100644 index 0000000..bfa9928 --- /dev/null +++ b/block.gd @@ -0,0 +1,160 @@ +class_name Block +extends Resource + + +const FACE_CORNERS = [ + Vector2(0, 0), Vector2(1, 0), + Vector2(0, 1), Vector2(1, 1), +] + +enum Face { + FRONT, + BACK, + LEFT, + RIGHT, + TOP, + BOTTOM, +} + +const FACES = [ + [ # FRONT + [Vector3(-1.0, 1.0, 1.0), Vector3(1.0, -1.0, 1.0), Vector3(-1.0, -1.0, 1.0)], + [Vector3(-1.0, 1.0, 1.0), Vector3(1.0, 1.0, 1.0), Vector3(1.0, -1.0, 1.0)], + ], + [ # BACK + [Vector3(1.0, 1.0, -1.0), Vector3(-1.0, -1.0, -1.0), Vector3(1.0, -1.0, -1.0)], + [Vector3(1.0, 1.0, -1.0), Vector3(-1.0, 1.0, -1.0), Vector3(-1.0, -1.0, -1.0)], + ], + [ # LEFT + [Vector3(-1.0, 1.0, -1.0), Vector3(-1.0, -1.0, 1.0), Vector3(-1.0, -1.0, -1.0)], + [Vector3(-1.0, 1.0, -1.0), Vector3(-1.0, 1.0, 1.0), Vector3(-1.0, -1.0, 1.0)], + ], + [ # RIGHT + [Vector3(1.0, 1.0, 1.0), Vector3(1.0, -1.0, -1.0), Vector3(1.0, -1.0, 1.0)], + [Vector3(1.0, 1.0, 1.0), Vector3(1.0, 1.0, -1.0), Vector3(1.0, -1.0, -1.0)], + ], + [ # TOP + [Vector3(-1.0, 1.0, -1.0), Vector3(1.0, 1.0, 1.0), Vector3(-1.0, 1.0, 1.0)], + [Vector3(-1.0, 1.0, -1.0), Vector3(1.0, 1.0, -1.0), Vector3(1.0, 1.0, 1.0)], + ], + [ # BOTTOM + [Vector3(-1.0, -1.0, 1.0), Vector3(1.0, -1.0, -1.0), Vector3(-1.0, -1.0, -1.0)], + [Vector3(-1.0, -1.0, 1.0), Vector3(1.0, -1.0, 1.0), Vector3(1.0, -1.0, -1.0)], + ], +] + +const FACE_UVS = [ + [ # FRONT + [Vector2(0.0, 0.0), Vector2(1.0, 1.0), Vector2(0.0, 1.0)], + [Vector2(0.0, 0.0), Vector2(1.0, 0.0), Vector2(1.0, 1.0)], + ], + [ # BACK + [Vector2(0.0, 0.0), Vector2(1.0, 1.0), Vector2(0.0, 1.0)], + [Vector2(0.0, 0.0), Vector2(1.0, 0.0), Vector2(1.0, 1.0)], + ], + [ # LEFT + [Vector2(0.0, 0.0), Vector2(1.0, 1.0), Vector2(0.0, 1.0)], + [Vector2(0.0, 0.0), Vector2(1.0, 0.0), Vector2(1.0, 1.0)], + ], + [ # RIGHT + [Vector2(0.0, 0.0), Vector2(1.0, 1.0), Vector2(0.0, 1.0)], + [Vector2(0.0, 0.0), Vector2(1.0, 0.0), Vector2(1.0, 1.0)], + ], + [ # TOP + [Vector2(0.0, 1.0), Vector2(1.0, 2.0), Vector2(0.0, 2.0)], + [Vector2(0.0, 1.0), Vector2(1.0, 1.0), Vector2(1.0, 2.0)], + ], + [ # BOTTOM + [Vector2(1.0, 0.0), Vector2(2.0, 1.0), Vector2(1.0, 1.0)], + [Vector2(1.0, 0.0), Vector2(2.0, 0.0), Vector2(2.0, 1.0)], + ], +] + +const FACE_NORMALS = [ + Vector3(0, 0, 1), # FRONT + Vector3(0, 0, -1), # BACK + Vector3(-1, 0, 0), # LEFT + Vector3(1, 0, 0), # RIGHT + Vector3(0, 1, 0), # TOP + Vector3(0, -1, 0), # BOTTOM +] + +enum Type { + AIR, + GRASS, + DIRT, + LEAVES, + STONE, +} + +const BLOCK_TYPES = { + Type.GRASS: { + "uv2s": [ + Vector2(0, 0), # FRONT + Vector2(0, 0), # BACK + Vector2(0, 0), # LEFT + Vector2(0, 0), # RIGHT + Vector2(0, 1), # TOP + Vector2(1, 0), # BOTTOM + ], + }, + Type.DIRT: { + "uv2s": [ + Vector2(1, 0), # FRONT + Vector2(1, 0), # BACK + Vector2(1, 0), # LEFT + Vector2(1, 0), # RIGHT + Vector2(1, 0), # TOP + Vector2(1, 0), # BOTTOM + ], + }, + Type.LEAVES: { + "uv2s": [ + Vector2(2, 2), # FRONT + Vector2(2, 2), # BACK + Vector2(2, 2), # LEFT + Vector2(2, 2), # RIGHT + Vector2(2, 2), # TOP + Vector2(2, 2), # BOTTOM + ], + }, + Type.STONE: { + "uv2s": [ + Vector2(2, 0), # FRONT + Vector2(2, 0), # BACK + Vector2(2, 0), # LEFT + Vector2(2, 0), # RIGHT + Vector2(2, 0), # TOP + Vector2(2, 0), # BOTTOM + ], + }, +} + +var position: Vector3 +var type: Type + + +func add_face(surface_tool: SurfaceTool, face: Face): + var uv2s = [] + uv2s.resize(3) + uv2s.fill(BLOCK_TYPES[type].uv2s[face]) + + var normals = [] + normals.resize(3) + normals.fill(FACE_NORMALS[face]) + + for idx in FACES[face].size(): + var triangle = FACES[face][idx] + var triangle_positions = triangle.map(func(item): + return item + position + ) + + var uvs = FACE_UVS[face][idx] + + surface_tool.add_triangle_fan( + PackedVector3Array(triangle_positions), + PackedVector2Array(uvs), + PackedColorArray(), + PackedVector2Array(uv2s), + PackedVector3Array(normals) + ) |