diff options
author | Daniel Weipert <git@mail.dweipert.de> | 2025-02-19 15:00:56 +0100 |
---|---|---|
committer | Daniel Weipert <git@mail.dweipert.de> | 2025-02-19 15:01:22 +0100 |
commit | f0142a29c9a36d2b87b19b763d88c5ca2245c7dc (patch) | |
tree | ed8d45266bd329e1ad33c33bc11dc93741eebee2 /player.gd | |
parent | f448ca4259d7f76fc58bc3b5c12ea8d04ab49a87 (diff) |
next commit
Diffstat (limited to 'player.gd')
-rw-r--r-- | player.gd | 110 |
1 files changed, 63 insertions, 47 deletions
@@ -19,10 +19,14 @@ func _ready() -> void: selection_block.visible = false selection_block.top_level = true selection_block.rotation = Vector3.ZERO - selection_block.scale = Vector3(1.001, 1.001, 1.001) + selection_block.scale = Vector3(1.0001, 1.0001, 1.0001) add_child(selection_block) selection_block_data = Block.new() selection_block_data.type = Block.Type.SELECTION + + #set_physics_process(false) + #await get_tree().create_timer(3.0).timeout + #set_physics_process(true) func _input(event: InputEvent) -> void: @@ -31,66 +35,48 @@ func _input(event: InputEvent) -> void: if event.is_action_pressed("mouse_exit"): Input.mouse_mode = Input.MOUSE_MODE_VISIBLE + if Input.mouse_mode != Input.MOUSE_MODE_CAPTURED: + return + 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) + var point = ( + $CameraAnchor/Camera/RayCast3D.get_collision_point() + + $CameraAnchor/Camera/RayCast3D.get_collision_normal() * (Block.BLOCK_SIZE * 0.5) + ) + + var q := PhysicsShapeQueryParameters3D.new() + q.shape = BoxShape3D.new() + q.transform = Transform3D(Basis(Vector3.ZERO,Vector3.ZERO,Vector3.ZERO), point) + var collision := get_world_3d().direct_space_state.intersect_shape(q) + + if collision.is_empty(): + (get_tree().current_scene as World).add_block( + $CameraAnchor/Camera/RayCast3D.get_collision_point(), + $CameraAnchor/Camera/RayCast3D.get_collision_normal(), + $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) + (get_tree().current_scene as World).remove_block( + $CameraAnchor/Camera/RayCast3D.get_collision_point(), + $CameraAnchor/Camera/RayCast3D.get_collision_normal() + ) -func _process(delta: float) -> void: +func _process(_delta: float) -> void: if $CameraAnchor/Camera/RayCast3D.is_colliding(): var collider = $CameraAnchor/Camera/RayCast3D.get_collider() if collider is not StaticBody3D: return - 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 - ) - #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) - 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] - 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]) + var point = $CameraAnchor/Camera/RayCast3D.get_collision_point() + maybe_update_selection_block(point) else: selection_block.visible = false @@ -114,8 +100,8 @@ func _physics_process(delta: float) -> void: .rotated(Vector3(0, 0, 1), $CameraAnchor.rotation.z) ) if direction: - velocity.x = direction.x * SPEED - velocity.z = direction.z * SPEED + velocity.x = direction.x * SPEED * (2.0 if Input.is_action_pressed("run") else 1.0) * (0.5 if not is_on_floor() else 1.0) + velocity.z = direction.z * SPEED * (2.0 if Input.is_action_pressed("run") else 1.0) * (0.5 if not is_on_floor() else 1.0) else: velocity.x = move_toward(velocity.x, 0, SPEED) velocity.z = move_toward(velocity.z, 0, SPEED) @@ -123,6 +109,27 @@ func _physics_process(delta: float) -> void: move_and_slide() +func maybe_update_selection_block(collision_point: Vector3) -> void: + var chunk_idx := Chunk.global_to_chunk( + collision_point, + $CameraAnchor/Camera/RayCast3D.get_collision_normal() + ) + var block_chunk_idx := Chunk.global_to_chunk_block( + collision_point, + $CameraAnchor/Camera/RayCast3D.get_collision_normal() + ) + + if Chunk.chunk_exists(chunk_idx): + var chunk: Chunk = Global.chunks[chunk_idx] + var block: Block = chunk.blocks[block_chunk_idx.x][block_chunk_idx.y][block_chunk_idx.z] + + selection_block.visible = true + + if previous_looked_at_block_position != block.position: + update_selection_block(block.position) + previous_looked_at_block_position = block.position + + func update_selection_block(new_position: Vector3): var material := preload("res://new_shader_material_selection.tres") var st = SurfaceTool.new() @@ -134,3 +141,12 @@ func update_selection_block(new_position: Vector3): selection_block.global_position = new_position selection_block.mesh = st.commit() + + +func print_debug_block(message: String, block: Block) -> void: + print( + message.rpad(6), " => ", + block, " ", + Block.Type.keys()[block.type].rpad(5), " ", + block.generated_faces + ) |