-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactor.gd
58 lines (42 loc) · 1.08 KB
/
actor.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
class_name Actor
extends CharacterBody2D
enum MotionType
{
DIRECT, LERP, MOVETO
}
@export var motion_type := MotionType.DIRECT
@export var target: PathFollow2D
@export var speed := 600.0
@export var lerp_acceleration := 10
@export var moveto_acceleration := 10000
@export var moving := false
func _physics_process(delta: float) -> void:
if moving:
match motion_type:
MotionType.LERP:
_motion_lerp(delta)
MotionType.MOVETO:
_motion_moveto(delta)
_:
_motion_no_smoothing()
else:
velocity = Vector2.ZERO
move_and_slide()
func _motion_no_smoothing() -> void:
velocity = _target_velocity()
func _motion_lerp(delta: float) -> void:
velocity = velocity.lerp(
_target_velocity(), lerp_acceleration * delta
)
func _motion_moveto(delta: float) -> void:
velocity = velocity.move_toward(
_target_velocity(), moveto_acceleration * delta
)
func _target_velocity() -> Vector2:
var diff := target.global_position - global_position
var result: Vector2
if not diff.is_zero_approx():
result = diff.normalized() * speed
else:
result = Vector2.ZERO
return result