-
-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathmarble.gd
More file actions
90 lines (67 loc) · 2.38 KB
/
marble.gd
File metadata and controls
90 lines (67 loc) · 2.38 KB
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
82
83
84
85
86
87
88
89
90
extends CharacterBody3D
@export var max_speed: float = 16.
@export var acceleration: float = 16.
@export var turn_degrees: float = 360.
@export var jump_strength: float = 8.
var is_reversing := false
@onready var _rollback_synchronizer := $RollbackSynchronizer as RollbackSynchronizer
@onready var input := $Input as Node
var gravity: float = ProjectSettings.get_setting(&"physics/3d/default_gravity")
func _ready():
position = Vector3(0, 4, 0)
var player_id := input.get_multiplayer_authority()
var mesh := $MeshInstance3D as MeshInstance3D
var material := mesh.get_active_material(0).duplicate() as StandardMaterial3D
material.albedo_color = Color.from_hsv((player_id % 256) / 256.0, 1.0, 1.0)
mesh.set_surface_override_material(0, material)
func _rollback_tick(dt, _t, _if):
if is_zero_approx(input.confidence):
# Can't predict, not enough confidence in input
_rollback_synchronizer.ignore_prediction(self)
return
_force_update()
if is_on_floor():
velocity.y = input.movement.y * jump_strength
else:
velocity.y -= gravity * dt
var movement := input.movement as Vector3
movement.y = 0.
var reverse_factor = 1.
if is_on_floor():
var accel := 0.
var steer := 0.
var brake := 0.
var speed := velocity.length()
if movement.is_zero_approx():
# Brake
brake = acceleration * 1. * dt
else:
if is_zero_approx(speed) and not is_zero_approx(movement.z):
is_reversing = not is_reversing
if is_reversing:
movement.z *= -1.
if movement.z > 0:
accel = abs(movement.z) * acceleration * dt
else:
brake = abs(movement.z) * 2. * acceleration * dt
steer = movement.x * turn_degrees
steer *= pow(clampf(speed / max_speed, 0., 1.), .5)
if is_reversing:
reverse_factor = -1.
brake = minf(brake, speed)
velocity += accel * reverse_factor * transform.basis.z - velocity.normalized() * brake
velocity = velocity.rotated(transform.basis.y, deg_to_rad(steer) * dt)
if velocity.length() > max_speed:
velocity = velocity.normalized() * max_speed
velocity *= NetworkTime.physics_factor
move_and_slide()
velocity /= NetworkTime.physics_factor
# Face velocity
var look = velocity.normalized() * reverse_factor
if not look.is_zero_approx() and abs(look.y) < .95:
look_at_from_position(position, position + look, transform.basis.y, true)
func _force_update():
var old_velocity = velocity
velocity *= 0
move_and_slide()
velocity = old_velocity