47 lines
1.6 KiB
GDScript3
47 lines
1.6 KiB
GDScript3
|
|
extends CharacterBody3D
|
||
|
|
class_name RepairCart
|
||
|
|
|
||
|
|
var pullingPlayer: PlayerCharacter
|
||
|
|
var pullingPlayerDirection: Vector3
|
||
|
|
var pullDirection: Vector3
|
||
|
|
var momentum: Vector3
|
||
|
|
const speed: float = 1.2
|
||
|
|
|
||
|
|
const decelerationMoving: float = 1
|
||
|
|
const turnSpeed: float = PI/2
|
||
|
|
|
||
|
|
func _physics_process(delta: float) -> void:
|
||
|
|
updatePullDirection()
|
||
|
|
if pullDirection.length() > 3:
|
||
|
|
momentum = clampVectorLength(momentum + clampVectorLength(pullDirection,0,1.5) * speed * delta,0,3)
|
||
|
|
if !is_on_floor():
|
||
|
|
momentum += get_gravity()
|
||
|
|
velocity = momentum
|
||
|
|
rotation.y = rotate_toward(rotation.y,atan2(velocity.x,velocity.z) - PI/2, turnSpeed*delta)
|
||
|
|
move_and_slide()
|
||
|
|
|
||
|
|
momentum -= clampVectorLength(momentum.normalized() * decelerationMoving * delta, 0, momentum.length())
|
||
|
|
|
||
|
|
func clampVectorLength(Vector: Vector3, minLength: float, maxLength: float) -> Vector3:
|
||
|
|
#scales Vector up/ down to the max/ min length givin. If the Vector has a length of 0 it will be returned without being scaled.
|
||
|
|
if Vector.length() == 0: return Vector
|
||
|
|
if Vector.length() < minLength:
|
||
|
|
return Vector * minLength / Vector.length()
|
||
|
|
elif Vector.length() > maxLength:
|
||
|
|
return Vector * maxLength / Vector.length()
|
||
|
|
return Vector
|
||
|
|
|
||
|
|
func updatePullDirection() -> void:
|
||
|
|
if pullingPlayer:
|
||
|
|
pullDirection = (pullingPlayer.position - position) * Vector3(1,0,1)
|
||
|
|
|
||
|
|
func _on_interact_box_interacted_with(playerRef: PlayerCharacter) -> void:
|
||
|
|
addPlayer.bind(playerRef.name).rpc()
|
||
|
|
|
||
|
|
@rpc("any_peer","reliable","call_local")
|
||
|
|
func addPlayer(playerRefName: StringName) -> void:
|
||
|
|
if get_node("/root/Main/Players/" + playerRefName) == pullingPlayer:
|
||
|
|
pullingPlayer = null
|
||
|
|
else:
|
||
|
|
pullingPlayer = get_node("/root/Main/Players/" + playerRefName)
|