-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCharacter.gd
More file actions
59 lines (45 loc) · 1.55 KB
/
Copy pathCharacter.gd
File metadata and controls
59 lines (45 loc) · 1.55 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
extends Spatial
export(Color) var color = Color(0.25, 0.25, 1.0) setget _set_color
const GRAVITY = 20.0
const JUMP_SPEED = 6.0
const WALK_RATIO = 0.1
const WALK_SPEED = 5.0
var controlling_peer_id = null
var movement = Vector3()
var jump = false
var speed = Vector3()
func _set_color(new_color):
color = new_color
if $Mesh != null:
var mat = $Mesh.get("material/0").duplicate()
mat.albedo_color = color
$Mesh.set("material/0", mat)
func _ready():
var mat = $Mesh.get("material/0").duplicate()
mat.albedo_color = color
$Mesh.set("material/0", mat)
func _physics_process(delta):
if not get_tree().has_network_peer() or is_network_master():
movement.x = clamp(movement.x, -1.0, 1.0)
movement.z = clamp(movement.z, -1.0, 1.0)
if translation.y <= 0.5 and speed.y < 0.5 * JUMP_SPEED:
if jump:
speed.y = JUMP_SPEED
jump = false
if translation.y <= 0.5:
speed.x = lerp(speed.x, movement.x * WALK_SPEED, WALK_RATIO)
speed.z = lerp(speed.z, movement.z * WALK_SPEED, WALK_RATIO)
else:
speed.y -= GRAVITY * delta
translation += speed * delta
translation.y = max(0.0, translation.y)
if get_tree().has_network_peer() and is_network_master():
rpc("network_update", translation, speed, movement)
puppet func network_update(r_position, r_speed, r_movement):
translation = r_position
speed = r_speed
movement = r_movement
master func set_input(new_movement, new_jump):
if not get_tree().has_network_peer() or get_tree().get_rpc_sender_id() == controlling_peer_id:
movement = new_movement
jump = jump or new_jump # Make sure we jump at least once