-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.gd
81 lines (65 loc) · 2.5 KB
/
player.gd
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
75
76
77
78
79
80
81
extends CharacterBody3D
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
const MOUSE_SENSITIVY = 0.01
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
@onready var camera := $Neck/PlayerCamera
@onready var neck := $Neck
@onready var player := $"."
var tree: AnimationTree
var camera_sprite: Sprite3D
var capture_viewport: SubViewport
var is_capturing: bool
var capture_camera: Camera3D
var capture_camera_ref: Node3D
func _ready():
tree = get_node("Neck/all2/AnimationTree")
capture_viewport = get_node("CaptureViewport")
camera_sprite = get_node("Neck/all2/Armature/Skeleton3D/BoneAttachment3D/CameraSprite")
capture_camera = get_node("CaptureViewport/Camera3D")
capture_camera_ref = get_node("Neck/CameraPosRef")
func _input(event):
if event is InputEventMouseButton:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
elif event.is_action_pressed("ui_cancel"):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
if (is_capturing):
return
if event is InputEventMouseMotion:
neck.rotate_x( - event.relative.y * MOUSE_SENSITIVY)
player.rotate_y( - event.relative.x * MOUSE_SENSITIVY)
neck.rotation.x = clamp(neck.rotation.x, deg_to_rad( - 30), deg_to_rad(60))
if (Input.is_action_pressed("capture")):
_capture()
func _exit_tree():
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y -= 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.
if (is_capturing):
return
var input_dir = Input.get_vector("left", "right", "forward", "back")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
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()
func _capture():
is_capturing = true
tree.set("parameters/capture/request", AnimationNodeOneShot.ONE_SHOT_REQUEST_FIRE)
await get_tree().create_timer(1).timeout
capture_camera.global_transform = capture_camera_ref.global_transform
camera_sprite.texture = capture_viewport.get_texture()
camera_sprite.show()
is_capturing = false