-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharacterBody3D.gd
68 lines (56 loc) · 2.08 KB
/
CharacterBody3D.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
extends CharacterBody3D
@export_category("Movement")
@export var SPEED : float = 3.75
@export var JUMP_VELOCITY : float = 1.25
var points = 0
var oxygen = 100
# Onready variables
@onready var head = $Head
@onready var camera = $Head/Camera
func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func _unhandled_input(event):
if event is InputEventMouseMotion:
head.rotate_y(-event.relative.x * 0.004)
camera.rotate_x(-event.relative.y * 0.004)
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-75), deg_to_rad(60))
func _process(delta):
$Label.text = "maintenance " + str(global.maintenance)
$Label2.text = "oxygen " + str(oxygen)
$Label3.text = str(points) + " rocks"
oxygen -= 1.5 * delta
if oxygen <= 0:
get_tree().change_scene("res://node_3d.tscn")
if Input.is_action_just_pressed("ui_cancel"):
if !OS.has_feature("web"):
get_tree().quit()
if Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
else:
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity += (get_gravity() * 2) * delta
# Handle jump.
if Input.is_action_just_pressed("jump") 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("left", "right", "up", "down")
var direction = (head.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
if is_on_floor():
velocity.x = lerp(velocity.x, direction.x * SPEED, delta * 15)
velocity.z = lerp(velocity.z, direction.z * SPEED, delta * 15)
else:
velocity.x = lerp(velocity.x, direction.x * SPEED * 1.5, delta)
velocity.z = lerp(velocity.z, direction.z * SPEED * 1.5, delta)
else:
if is_on_floor():
velocity.x = lerp(velocity.x, 0.0, delta * 10)
velocity.z = lerp(velocity.z, 0.0, delta * 10)
else:
velocity.x = lerp(velocity.x, 0.0, delta / 2)
velocity.z = lerp(velocity.z, 0.0, delta / 2)
move_and_slide()