diff options
-rw-r--r-- | Spritesheet_array.png | bin | 6886 -> 6906 bytes | |||
-rw-r--r-- | Spritesheet_array.png~ | bin | 6613 -> 6913 bytes | |||
-rw-r--r-- | block.gd | 24 | ||||
-rw-r--r-- | chunk.gd | 164 | ||||
-rw-r--r-- | inventory_bar.gd | 25 | ||||
-rw-r--r-- | main.gd | 63 | ||||
-rw-r--r-- | main.tscn | 69 | ||||
-rw-r--r-- | new_shader.gdshader | 2 | ||||
-rw-r--r-- | new_shader_material_selection.tres | 10 | ||||
-rw-r--r-- | new_shader_selection.gdshader | 23 | ||||
-rw-r--r-- | player.gd | 98 | ||||
-rw-r--r-- | project.godot | 25 |
12 files changed, 430 insertions, 73 deletions
diff --git a/Spritesheet_array.png b/Spritesheet_array.png Binary files differindex 82159f8..6e3e519 100644 --- a/Spritesheet_array.png +++ b/Spritesheet_array.png diff --git a/Spritesheet_array.png~ b/Spritesheet_array.png~ Binary files differindex 25a41fb..e6f3299 100644 --- a/Spritesheet_array.png~ +++ b/Spritesheet_array.png~ @@ -85,6 +85,8 @@ enum Type { DIRT, LEAVES, STONE, + ICE, + SELECTION, } const BLOCK_TYPES = { @@ -128,6 +130,26 @@ const BLOCK_TYPES = { Vector2(2, 0), # BOTTOM ], }, + Type.ICE: { + "uv2s": [ + Vector2(2, 1), # FRONT + Vector2(2, 1), # BACK + Vector2(2, 1), # LEFT + Vector2(2, 1), # RIGHT + Vector2(2, 1), # TOP + Vector2(2, 1), # BOTTOM + ], + }, + Type.SELECTION: { + "uv2s": [ + Vector2(3, 2), # FRONT + Vector2(3, 2), # BACK + Vector2(3, 2), # LEFT + Vector2(3, 2), # RIGHT + Vector2(3, 2), # TOP + Vector2(3, 2), # BOTTOM + ], + }, } var position: Vector3 @@ -146,7 +168,7 @@ func add_face(surface_tool: SurfaceTool, face: Face): for idx in FACES[face].size(): var triangle = FACES[face][idx] var triangle_positions = triangle.map(func(item): - return item + position + return (item / 2.0) + position + Vector3(0.5, 0.5, 0.5) ) var uvs = FACE_UVS[face][idx] @@ -2,39 +2,31 @@ class_name Chunk extends Node -var chunk_size := Vector3(16, 16, 16) +static var chunks: Array = [] -@export var noise: FastNoiseLite +const CHUNK_SIZE := Vector3i(16, 16, 16) -var blocks: Array = [] +var position := Vector3i.ZERO +var mesh := MeshInstance3D.new() +@export var noise: FastNoiseLite +@export var blocks: Array = [] -func generate_chunk(row, column, depth): - #var material := StandardMaterial3D.new() - #material.albedo_texture = preload("res://new_atlas_texture.tres") - #material.vertex_color_use_as_albedo = true - var material := preload("res://new_shader_material.tres") - var st = SurfaceTool.new() - st.begin(Mesh.PRIMITIVE_TRIANGLES) - st.set_material(material) + +func generate_block_data(row: int, column: int, depth: int, empty: bool = false): + position = Vector3i(row, column, depth) blocks = [] - - for x in chunk_size.x: - #blocks[x] = [] + for x in CHUNK_SIZE.x: blocks.append([]) - for y in chunk_size.y: - #blocks[x][y] = [] + for y in CHUNK_SIZE.y: blocks[x].append([]) - for z in chunk_size.z: - var block_position = Vector3( - row*chunk_size.x + x, - column*chunk_size.y + y, - depth*chunk_size.z + z - #x, - #y, - #z - ) * 2 + for z in CHUNK_SIZE.z: + var block_position = Vector3i( + position.x * CHUNK_SIZE.x + x, + position.y * CHUNK_SIZE.y + y, + position.z * CHUNK_SIZE.z + z + )# * 2 var value = noise.get_noise_3d(block_position.x, block_position.y, block_position.z) * 100 var type = Block.Type.AIR @@ -47,17 +39,63 @@ func generate_chunk(row, column, depth): block.position = block_position block.type = type + if empty: + block.type = Block.Type.AIR + blocks[x][y].append(block) - + + +func generate_mesh() -> MeshInstance3D: + var material := preload("res://new_shader_material.tres") + var st = SurfaceTool.new() + st.begin(Mesh.PRIMITIVE_TRIANGLES) + st.set_material(material) + for x in blocks.size(): for y in blocks[x].size(): for z in blocks[x][y].size(): - var block = blocks[x][y][z] - var block_position = block.position + var block: Block = blocks[x][y][z] + + var block_front: Block + if block_exists(x, y, z + 1): + block_front = blocks[x][y][z + 1] + elif is_chunk_border(x, y, z) and chunk_exists(position.x, position.y, position.z + 1): + block_front = chunks[position.x][position.y][position.z + 1].blocks[x][y][0] + + var block_back: Block + if block_exists(x, y, z - 1): + block_back = blocks[x][y][z - 1] + elif is_chunk_border(x, y, z) and chunk_exists(position.x, position.y, position.z - 1): + block_back = chunks[position.x][position.y][position.z - 1].blocks[x][y][CHUNK_SIZE.z - 1] + + var block_left: Block + if block_exists(x - 1, y, z): + block_left = blocks[x - 1][y][z] + elif is_chunk_border(x, y, z) and chunk_exists(position.x - 1, position.y, position.z): + block_left = chunks[position.x - 1][position.y][position.z].blocks[CHUNK_SIZE.x - 1][y][z] + + var block_right: Block + if block_exists(x + 1, y, z): + block_right = blocks[x + 1][y][z] + elif is_chunk_border(x, y, z) and chunk_exists(position.x + 1, position.y, position.z): + block_right = chunks[position.x + 1][position.y][position.z].blocks[0][y][z] + + var block_top: Block + if block_exists(x, y + 1, z): + block_top = blocks[x][y + 1][z] + elif is_chunk_border(x, y, z) and chunk_exists(position.x, position.y + 1, position.z): + block_top = chunks[position.x][position.y + 1][position.z].blocks[x][0][z] + + var block_bottom: Block + if block_exists(x, y - 1, z): + block_bottom = blocks[x][y - 1][z] + elif is_chunk_border(x, y, z) and chunk_exists(position.x, position.y - 1, position.z): + block_bottom = chunks[position.x][position.y - 1][position.z].blocks[x][CHUNK_SIZE.y - 1][z] if not block.type == Block.Type.AIR: - if block.type == Block.Type.GRASS and blocks[x].size() > y + 1 and not blocks[x][y + 1][z].type == Block.Type.AIR: - block.type = Block.Type.DIRT + if block.type == Block.Type.GRASS: + if block_top and block_top.type != Block.Type.AIR: + block.type = Block.Type.DIRT var rng = RandomNumberGenerator.new() rng.seed = randi() @@ -65,28 +103,78 @@ func generate_chunk(row, column, depth): st.set_uv(Vector2(0, 0)) - if (blocks[x][y].size() > z+1 and blocks[x][y][z + 1].type == Block.Type.AIR) or blocks[x][y].size() == z+1: + if not block_front or block_front.type == Block.Type.AIR: block.add_face(st, Block.Face.FRONT) - if (blocks.size() > x+1 and blocks[x + 1][y][z].type == Block.Type.AIR) or blocks.size() == x+1: + if not block_right or block_right.type == Block.Type.AIR: block.add_face(st, Block.Face.RIGHT) - if blocks[x][y].size() > z-1 and (blocks[x][y][z - 1].type == Block.Type.AIR or z == 0): + if not block_back or block_back.type == Block.Type.AIR: block.add_face(st, Block.Face.BACK) - if blocks.size() > x-1 and (blocks[x - 1][y][z].type == Block.Type.AIR or x == 0): + if not block_left or block_left.type == Block.Type.AIR: block.add_face(st, Block.Face.LEFT) - if (blocks[x].size() > y+1 and blocks[x][y + 1][z].type == Block.Type.AIR) or blocks[x].size() == y+1: + if not block_top or block_top.type == Block.Type.AIR: block.add_face(st, Block.Face.TOP) - if blocks[x].size() > y-1 and (blocks[x][y - 1][z].type == Block.Type.AIR or y == 0): + if not block_bottom or block_bottom.type == Block.Type.AIR: block.add_face(st, Block.Face.BOTTOM) #st.generate_normals() - var mesh = MeshInstance3D.new() + #mesh = MeshInstance3D.new() mesh.mesh = st.commit() - mesh.create_trimesh_collision() + + for child in mesh.get_children(): + child.queue_free() + mesh.create_trimesh_collision.call_deferred() + + mesh.set_meta("chunk", self) return mesh + + +func generate_chunk(row: int, column: int, depth: int): + if blocks.is_empty(): + generate_block_data(row, column, depth) + + generate_mesh() + + +func add_block(block_position: Vector3, block_type: Block.Type = Block.Type.GRASS) -> void: + var block = Block.new() + block.position = Vector3i( + position.x * CHUNK_SIZE.x + block_position.x, + position.y * CHUNK_SIZE.y + block_position.y, + position.z * CHUNK_SIZE.z + block_position.z + ) + block.type = block_type + + blocks[block_position.x][block_position.y][block_position.z] = block + generate_mesh.call_deferred() + + +func remove_block(block_position: Vector3) -> void: + blocks[block_position.x][block_position.y][block_position.z].type = Block.Type.AIR + generate_mesh.call_deferred() + + +static func chunk_exists(x: int, y: int, z: int): + return x >= 0 and chunks.size() - 1 >= x and y >= 0 and chunks[x].size() - 1 >= y and z >= 0 and chunks[x][y].size() - 1 >= z + +func block_exists(x: int, y: int, z: int): + return x >= 0 and blocks.size() - 1 >= x and y >= 0 and blocks[x].size() - 1 >= y and z >= 0 and blocks[x][y].size() - 1 >= z + +func is_chunk_border(x: int, y: int, z: int): + return x == 0 or x == CHUNK_SIZE.x - 1 or y == 0 or y == CHUNK_SIZE.y - 1 or z == 0 or z == CHUNK_SIZE.z - 1 + + +static func global_to_chunk(global_position: Vector3) -> Vector3i: + return floor(global_position / Vector3(CHUNK_SIZE)) + +static func global_to_chunk_block(global_position: Vector3) -> Vector3i: + return global_to_block(global_position) - CHUNK_SIZE * global_to_chunk(global_position) + +static func global_to_block(global_position: Vector3) -> Vector3i: + return floor(global_position) diff --git a/inventory_bar.gd b/inventory_bar.gd new file mode 100644 index 0000000..0077274 --- /dev/null +++ b/inventory_bar.gd @@ -0,0 +1,25 @@ +extends Control + + +var active_type: Block.Type = Block.Type.GRASS + + +func _input(event: InputEvent) -> void: + if event.is_action_pressed("inventory_bar_slot_1"): + _on_texture_rect_pressed() + if event.is_action_pressed("inventory_bar_slot_2"): + _on_texture_rect_2_pressed() + if event.is_action_pressed("inventory_bar_slot_3"): + _on_texture_rect_3_pressed() + + +func _on_texture_rect_pressed() -> void: + active_type = Block.Type.GRASS + + +func _on_texture_rect_2_pressed() -> void: + active_type = Block.Type.STONE + + +func _on_texture_rect_3_pressed() -> void: + active_type = Block.Type.ICE @@ -1,11 +1,9 @@ -class_name Main +class_name World extends Node3D @onready var noise := FastNoiseLite.new() -var chunks := [] - func _ready() -> void: brrr() @@ -21,17 +19,60 @@ func brrr(): var thread = OtherThread.new() var rrrb = func(): - for row in range(0, 8): - chunks.append([]) - for column in range(0, 4): - chunks[row].append([]) - for depth in range(0, 8): - chunks[row][column].append([]) + for row in range(0, 2): + Chunk.chunks.append([]) + for column in range(0, 1): + Chunk.chunks[row].append([]) + for depth in range(0, 2): + Chunk.chunks[row][column].append([]) var chunk = Chunk.new() chunk.noise = noise - var mesh = chunk.generate_chunk(row, column, depth) + chunk.generate_block_data(row, column, depth) + Chunk.chunks[row][column][depth] = chunk + + #var rrrb2 = func(): + for row in Chunk.chunks.size(): + for column in Chunk.chunks[row].size(): + for depth in Chunk.chunks[row][column].size(): + var chunk: Chunk = Chunk.chunks[row][column][depth] + var mesh = chunk.generate_mesh() add_child.call_deferred(mesh) - chunks[row][column][depth] = chunk thread.start(rrrb) #thread.wait_to_finish() + #thread.start(rrrb2) + + +func create_chunk(chunk_position: Vector3) -> Chunk: + var chunk := Chunk.new() + chunk.noise = noise + + if Chunk.chunks.size() == chunk_position.x: + Chunk.chunks.append([]) + if Chunk.chunks[chunk_position.x].size() == chunk_position.y: + Chunk.chunks[chunk_position.x].append([]) + if Chunk.chunks[chunk_position.x][chunk_position.y].size() == chunk_position.z: + Chunk.chunks[chunk_position.y].append([]) + + Chunk.chunks[chunk_position.x][chunk_position.y][chunk_position.z] = chunk + + return chunk + + +func add_block(target_position: Vector3, block_type: Block.Type = Block.Type.AIR): + var chunk_idx := Chunk.global_to_chunk(target_position) + var chunk: Chunk + var created_new_chunk := false + + if Chunk.chunk_exists(chunk_idx.x, chunk_idx.y, chunk_idx.z): + chunk = Chunk.chunks[chunk_idx.x][chunk_idx.y][chunk_idx.z] + else: + created_new_chunk = true + chunk = create_chunk(Vector3(chunk_idx.x, chunk_idx.y, chunk_idx.z)) + chunk.generate_block_data(chunk_idx.x, chunk_idx.y, chunk_idx.z, true) + + var block_chunk_idx := Chunk.global_to_chunk_block(target_position) + chunk.add_block(block_chunk_idx, block_type) + + if created_new_chunk: + add_child.call_deferred(chunk.generate_mesh()) @@ -1,9 +1,11 @@ -[gd_scene load_steps=9 format=3 uid="uid://b7k6l3bm1f0db"] +[gd_scene load_steps=15 format=3 uid="uid://b7k6l3bm1f0db"] [ext_resource type="Script" path="res://main.gd" id="1_g4bmv"] [ext_resource type="Script" path="res://free-look-camera.gd" id="2_4jusq"] [ext_resource type="Texture2D" uid="uid://dv842eajslxyw" path="res://icon.svg" id="3_7tj3h"] [ext_resource type="Script" path="res://player.gd" id="3_tn7lj"] +[ext_resource type="Script" path="res://inventory_bar.gd" id="5_c1toa"] +[ext_resource type="Texture2D" uid="uid://ld8e4g6oef3x" path="res://Spritesheet.png" id="5_dqy55"] [sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_vv2it"] sky_horizon_color = Color(0.64625, 0.65575, 0.67075, 1) @@ -19,6 +21,28 @@ tonemap_mode = 2 glow_enabled = true [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_cggfi"] +radius = 0.45 +height = 1.8 + +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_4b324"] +bg_color = Color(0.18359, 0.18359, 0.18359, 1) +border_width_left = 4 +border_width_top = 4 +border_width_right = 4 +border_width_bottom = 4 +border_color = Color(0.184314, 0.184314, 0.184314, 1) + +[sub_resource type="AtlasTexture" id="AtlasTexture_w53vy"] +atlas = ExtResource("5_dqy55") +region = Rect2(0, 0, 80, 80) + +[sub_resource type="AtlasTexture" id="AtlasTexture_kv0hd"] +atlas = ExtResource("5_dqy55") +region = Rect2(160, 0, 80, 80) + +[sub_resource type="AtlasTexture" id="AtlasTexture_46jgo"] +atlas = ExtResource("5_dqy55") +region = Rect2(160, 80, 80, 80) [node name="Main" type="Node3D"] script = ExtResource("1_g4bmv") @@ -34,14 +58,12 @@ directional_shadow_max_distance = 400.0 [node name="Camera3D" type="Camera3D" parent="."] transform = Transform3D(1, 0, 0, 0, 0.767031, 0.64161, 0, -0.64161, 0.767031, 0.387164, 18.7811, 29.7854) -current = true script = ExtResource("2_4jusq") [node name="MeshInstance3D" type="MeshInstance3D" parent="."] [node name="CharacterBody3D" type="CharacterBody3D" parent="."] -process_mode = 4 -transform = Transform3D(-0.0770845, 0, 0.997025, 0, 1, 0, -0.997025, 0, -0.0770845, 0, 238.895, 0) +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 2, 238.895, 2) script = ExtResource("3_tn7lj") [node name="CollisionShape3D" type="CollisionShape3D" parent="CharacterBody3D"] @@ -51,9 +73,10 @@ shape = SubResource("CapsuleShape3D_cggfi") transform = Transform3D(0.999999, 0, 0, 0, 1, 0, 7.45058e-09, 0, 1, 0, 0.87, 0) [node name="Camera" type="Camera3D" parent="CharacterBody3D/CameraAnchor"] +current = true [node name="RayCast3D" type="RayCast3D" parent="CharacterBody3D/CameraAnchor/Camera"] -target_position = Vector3(0, 0, -5) +target_position = Vector3(0, 0, -8) [node name="CanvasLayer" type="CanvasLayer" parent="CharacterBody3D"] @@ -69,3 +92,39 @@ custom_minimum_size = Vector2(16, 16) layout_mode = 2 texture = ExtResource("3_7tj3h") expand_mode = 1 + +[node name="MarginContainer" type="MarginContainer" parent="CharacterBody3D/CanvasLayer"] +anchors_preset = 12 +anchor_top = 1.0 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 0 +theme_override_constants/margin_bottom = 4 +script = ExtResource("5_c1toa") + +[node name="CenterContainer" type="CenterContainer" parent="CharacterBody3D/CanvasLayer/MarginContainer"] +layout_mode = 2 + +[node name="PanelContainer" type="PanelContainer" parent="CharacterBody3D/CanvasLayer/MarginContainer/CenterContainer"] +layout_mode = 2 +theme_override_styles/panel = SubResource("StyleBoxFlat_4b324") + +[node name="HBoxContainer" type="HBoxContainer" parent="CharacterBody3D/CanvasLayer/MarginContainer/CenterContainer/PanelContainer"] +layout_mode = 2 + +[node name="TextureRect" type="TextureButton" parent="CharacterBody3D/CanvasLayer/MarginContainer/CenterContainer/PanelContainer/HBoxContainer"] +layout_mode = 2 +texture_normal = SubResource("AtlasTexture_w53vy") + +[node name="TextureRect2" type="TextureButton" parent="CharacterBody3D/CanvasLayer/MarginContainer/CenterContainer/PanelContainer/HBoxContainer"] +layout_mode = 2 +texture_normal = SubResource("AtlasTexture_kv0hd") + +[node name="TextureRect3" type="TextureButton" parent="CharacterBody3D/CanvasLayer/MarginContainer/CenterContainer/PanelContainer/HBoxContainer"] +layout_mode = 2 +texture_normal = SubResource("AtlasTexture_46jgo") + +[connection signal="pressed" from="CharacterBody3D/CanvasLayer/MarginContainer/CenterContainer/PanelContainer/HBoxContainer/TextureRect" to="CharacterBody3D/CanvasLayer/MarginContainer" method="_on_texture_rect_pressed"] +[connection signal="pressed" from="CharacterBody3D/CanvasLayer/MarginContainer/CenterContainer/PanelContainer/HBoxContainer/TextureRect2" to="CharacterBody3D/CanvasLayer/MarginContainer" method="_on_texture_rect_2_pressed"] +[connection signal="pressed" from="CharacterBody3D/CanvasLayer/MarginContainer/CenterContainer/PanelContainer/HBoxContainer/TextureRect3" to="CharacterBody3D/CanvasLayer/MarginContainer" method="_on_texture_rect_3_pressed"] diff --git a/new_shader.gdshader b/new_shader.gdshader index e9d2a08..5087a7a 100644 --- a/new_shader.gdshader +++ b/new_shader.gdshader @@ -3,6 +3,8 @@ shader_type spatial; uniform sampler2DArray textures : filter_nearest, source_color; uniform int elements_per_row; +varying float is_block; + void vertex() { // Called for every vertex the material is visible on. } diff --git a/new_shader_material_selection.tres b/new_shader_material_selection.tres new file mode 100644 index 0000000..0aaa4a0 --- /dev/null +++ b/new_shader_material_selection.tres @@ -0,0 +1,10 @@ +[gd_resource type="ShaderMaterial" load_steps=3 format=3 uid="uid://cgvu2qg8kba3m"] + +[ext_resource type="Shader" path="res://new_shader_selection.gdshader" id="1_0lbk8"] +[ext_resource type="CompressedTexture2DArray" uid="uid://demlxku183noo" path="res://Spritesheet_array.png" id="2_xnnyw"] + +[resource] +render_priority = 0 +shader = ExtResource("1_0lbk8") +shader_parameter/elements_per_row = 6 +shader_parameter/textures = ExtResource("2_xnnyw") diff --git a/new_shader_selection.gdshader b/new_shader_selection.gdshader new file mode 100644 index 0000000..513ee92 --- /dev/null +++ b/new_shader_selection.gdshader @@ -0,0 +1,23 @@ +shader_type spatial; + +uniform sampler2DArray textures : filter_nearest, source_color; +uniform int elements_per_row; + +varying float is_block; + +void vertex() { + // Called for every vertex the material is visible on. +} + +void fragment() { + // Called for every pixel the material is visible on. + int index = int(UV2.y) * elements_per_row + int(UV2.x); + vec4 element = texture(textures, vec3(UV, float(index))); + ALBEDO = element.rgb; + ALPHA = element.a; +} + +//void light() { + // Called for every pixel for every light affecting the material. + // Uncomment to replace the default light processing function with this one. +//} @@ -1,14 +1,28 @@ +class_name Player extends CharacterBody3D -const SPEED = 20.0 -const JUMP_VELOCITY = 20.0 +var selection_block: MeshInstance3D +var selection_block_data: Block +var previous_looked_at_block_position: Vector3 = Vector3.ZERO + +const SPEED = 5.0#10.0#20.0 +const JUMP_VELOCITY = 5.0#10.0#20.0 var acc = 0 func _ready() -> void: Input.mouse_mode = Input.MOUSE_MODE_CAPTURED + + selection_block = MeshInstance3D.new() + selection_block.visible = false + selection_block.top_level = true + selection_block.rotation = Vector3.ZERO + selection_block.scale = Vector3(1.001, 1.001, 1.001) + add_child(selection_block) + selection_block_data = Block.new() + selection_block_data.type = Block.Type.SELECTION func _input(event: InputEvent) -> void: @@ -20,36 +34,71 @@ func _input(event: InputEvent) -> void: if event is InputEventMouseMotion: $CameraAnchor.rotate_y(deg_to_rad(clamp(-event.relative.x * 0.25, -45, 45))) $CameraAnchor.rotate_object_local(Vector3(1,0,0), deg_to_rad(-event.relative.y * 0.25)) + + if event.is_action_pressed("right_click"): + if selection_block.visible: + var point = selection_block.global_position + $CameraAnchor/Camera/RayCast3D.get_collision_normal() + (get_tree().current_scene as World).add_block(point, $CanvasLayer/MarginContainer.active_type) + + if event.is_action_pressed("left_click"): + if selection_block.visible: + var point = selection_block.global_position + var chunk_idx := Chunk.global_to_chunk(point) + if Chunk.chunk_exists(chunk_idx.x, chunk_idx.y, chunk_idx.z): + var chunk: Chunk = Chunk.chunks[chunk_idx.x][chunk_idx.y][chunk_idx.z] + var block_chunk_idx := Chunk.global_to_chunk_block(point) + chunk.remove_block(block_chunk_idx) func _process(delta: float) -> void: if $CameraAnchor/Camera/RayCast3D.is_colliding(): var collider = $CameraAnchor/Camera/RayCast3D.get_collider() - var point = $CameraAnchor/Camera/RayCast3D.get_collision_point() + if collider is not StaticBody3D: + return - var block_idx = floor(point / 2) - var block_chunk_idx = Vector3( - int(floor(point.x / 2)) % 16, - int(floor(point.y / 2)) % 16, - int(floor(point.z / 2)) % 16 + var p = $CameraAnchor/Camera/RayCast3D.get_collision_point() + var n = $CameraAnchor/Camera/RayCast3D.get_collision_normal() + #var point = $CameraAnchor/Camera/RayCast3D.get_collision_point() - Vector3(1, 1, 1) - $CameraAnchor/Camera/RayCast3D.get_collision_normal() + #point = $CameraAnchor/Camera/RayCast3D.get_collision_point() - Vector3(0.5, 0.5, 0.5) - $CameraAnchor/Camera/RayCast3D.get_collision_normal() + var point = $CameraAnchor/Camera/RayCast3D.get_collision_point() - ( + $CameraAnchor/Camera/RayCast3D.get_collision_normal() if $CameraAnchor/Camera/RayCast3D.get_collision_normal() > Vector3.ZERO else Vector3.ZERO ) - var chunk_idx = floor(point / (16*2)) + #point = $CameraAnchor/Camera/RayCast3D.get_collision_point().snapped(Vector3(0.5, 0.5, 0.5)) + + #var block_idx = floor(point)#floor(point / 2) + #var chunk_idx = floor(point / 16)#floor(point / (16*2)) + #var block_chunk_idx = Vector3( + #int(floor(point.x / 2)) % 16, + #int(floor(point.y / 2)) % 16, + #int(floor(point.z / 2)) % 16 + #) + #var block_chunk_idx = block_idx - 16 * chunk_idx + + var chunk_idx := Chunk.global_to_chunk(point) + var block_chunk_idx := Chunk.global_to_chunk_block(point) - acc += delta - if acc / 5 > 1.0: - acc = 0 - print(block_chunk_idx) - print(chunk_idx) - var chunk: Chunk = get_tree().current_scene.chunks[chunk_idx.x][chunk_idx.y][chunk_idx.z] + #acc += delta + #if acc / 5 > 1.0: + #acc = 0 + #print(block_chunk_idx) + #print(chunk_idx) + if Chunk.chunk_exists(chunk_idx.x, chunk_idx.y, chunk_idx.z): + var chunk: Chunk = Chunk.chunks[chunk_idx.x][chunk_idx.y][chunk_idx.z] var block: Block = chunk.blocks[block_chunk_idx.x][block_chunk_idx.y][block_chunk_idx.z] - print(point, "==", block.position) #?? - print(Block.Type.keys()[block.type]) + selection_block.visible = true + if previous_looked_at_block_position != block.position: + update_selection_block(block.position) + previous_looked_at_block_position = block.position + #print(point, "==", block.position) #?? + #print(Block.Type.keys()[block.type]) + else: + selection_block.visible = false func _physics_process(delta: float) -> void: # Add the gravity. if not is_on_floor(): - velocity += get_gravity() * delta + velocity += get_gravity() * delta * 1.5 # Handle jump. if Input.is_action_just_pressed("ui_accept") and is_on_floor(): @@ -72,3 +121,16 @@ func _physics_process(delta: float) -> void: velocity.z = move_toward(velocity.z, 0, SPEED) move_and_slide() + + +func update_selection_block(new_position: Vector3): + var material := preload("res://new_shader_material_selection.tres") + var st = SurfaceTool.new() + st.begin(Mesh.PRIMITIVE_TRIANGLES) + st.set_material(material) + + for face in Block.Face.values(): + selection_block_data.add_face(st, face) + + selection_block.global_position = new_position + selection_block.mesh = st.commit() diff --git a/project.godot b/project.godot index e6bea42..6321864 100644 --- a/project.godot +++ b/project.godot @@ -69,6 +69,31 @@ mouse_exit={ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194305,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) ] } +left_click={ +"deadzone": 0.5, +"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":1,"position":Vector2(282, 22),"global_position":Vector2(291, 68),"factor":1.0,"button_index":1,"canceled":false,"pressed":true,"double_click":false,"script":null) +] +} +right_click={ +"deadzone": 0.5, +"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":2,"position":Vector2(324, 20),"global_position":Vector2(333, 66),"factor":1.0,"button_index":2,"canceled":false,"pressed":true,"double_click":false,"script":null) +] +} +inventory_bar_slot_1={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":49,"key_label":0,"unicode":49,"location":0,"echo":false,"script":null) +] +} +inventory_bar_slot_2={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":50,"key_label":0,"unicode":50,"location":0,"echo":false,"script":null) +] +} +inventory_bar_slot_3={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":51,"key_label":0,"unicode":51,"location":0,"echo":false,"script":null) +] +} [rendering] |