summaryrefslogtreecommitdiff
path: root/player.gd
diff options
context:
space:
mode:
Diffstat (limited to 'player.gd')
-rw-r--r--player.gd74
1 files changed, 74 insertions, 0 deletions
diff --git a/player.gd b/player.gd
new file mode 100644
index 0000000..cf8e90e
--- /dev/null
+++ b/player.gd
@@ -0,0 +1,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 = get_tree().current_scene.chunks[chunk_idx.x][chunk_idx.y][chunk_idx.z]
+ var block = chunk[block_chunk_idx.x][block_chunk_idx.y][block_chunk_idx.z]
+ print(block)
+ print(Main.BlockType.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()