1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
extends CharacterBody3D
const SPEED = 20.0
const JUMP_VELOCITY = 20.0
var acc = 0
func _ready() -> void:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func _input(event: InputEvent) -> void:
if event is InputEventMouse and event.is_pressed():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
if event.is_action_pressed("mouse_exit"):
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
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))
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()
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 chunk_idx = floor(point / (16*2))
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]
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])
func _physics_process(delta: float) -> void:
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
var direction := (
(transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
.rotated(Vector3(1, 0, 0), $CameraAnchor.rotation.x)
.rotated(Vector3(0, 1, 0), $CameraAnchor.rotation.y)
.rotated(Vector3(0, 0, 1), $CameraAnchor.rotation.z)
)
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
move_and_slide()
|