-
Notifications
You must be signed in to change notification settings - Fork 60
/
Trail2D.cs
156 lines (144 loc) · 4.96 KB
/
Trail2D.cs
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
using Godot;
public class Trail2D : Line2D
{
public enum Persistence
{
Off, // Do not persist. Remove all points after the trailLength.
Always, // Always persist. Do not remove any points.
Conditional, // Sometimes persist. Choose an algorithm for when to add and remove points.
}
public enum PersistWhen
{
OnMovement, // Add points during movement and remove points when not moving.
Custom, // Override ShouldGrow() and ShouldShrink() to define when to add/remove points.
}
// The target node to track
private Node2D _target;
// The NodePath to the target
[Export] public NodePath targetPath = "..";
// if not persisting, the number of points that should be allowed in the trail
[Export] public int trailLength = 10;
// To what degree the trail should remain in existence before automatically removing points.
[Export] public Persistence persistence = Persistence.Off;
// During conditional persistence, which persistence algorithm to use
[Export] public PersistWhen persistWhen = PersistWhen.OnMovement;
// During conditional persistence, how many points to remove per frame
[Export] public int degenRate = 1;
// if true, automatically set ZIndex to be one less than the 'target'
[Export] public bool autoZIndex = true;
// if true, will automatically setup a gradient for a gradually transparent trail
[Export] public bool autoAlphaGradient = true;
public override void _EnterTree()
{
SetAsToplevel(true);
GlobalPosition = Vector2.Zero;
GlobalRotation = 0;
if (autoAlphaGradient && Gradient == null)
{
Gradient = new Gradient();
Color first = DefaultColor;
first.a = 0; // TODO: Use https://github.com/godotengine/godot/pull/31658 instead.
Gradient.SetColor(0, first);
Gradient.SetColor(1, DefaultColor);
}
}
public override void _Notification(int p_what)
{
switch (p_what)
{
case Node.NotificationParented:
if (autoZIndex)
{
_target = GetNodeOrNull<Node2D>(targetPath);
ZIndex = _target != null ? _target.ZIndex - 1 : 0;
}
break;
case Node.NotificationUnparented:
targetPath = @"";
trailLength = 0;
break;
}
}
public override void _Process(float delta)
{
if (_target == null)
{
_target = GetNodeOrNull<Node2D>(targetPath);
return;
}
switch (persistence)
{
case Persistence.Off:
AddPoint(_target.GlobalPosition);
while (GetPointCount() > trailLength)
{
RemovePoint(0);
}
break;
case Persistence.Always:
AddPoint(_target.GlobalPosition);
break;
case Persistence.Conditional:
switch (persistWhen)
{
case PersistWhen.OnMovement:
bool moved = GetPointCount() > 0 ? GetPointPosition(GetPointCount() - 1) != _target.GlobalPosition : false;
if (GetPointCount() == 0 || moved)
{
AddPoint(_target.GlobalPosition);
}
else
{
for (int i = 0; i < degenRate; i++)
{
RemovePoint(0);
}
}
break;
case PersistWhen.Custom:
if (ShouldGrow())
{
AddPoint(_target.GlobalPosition);
if (ShouldShrink())
{
for (int i = 0; i < degenRate; i++)
{
RemovePoint(0);
}
}
break;
}
break;
}
break;
}
}
protected bool ShouldGrow() { return true; }
protected bool ShouldShrink() { return true; }
public void EraseTrail()
{
for (int i = 0; i < GetPointCount(); i++)
{
RemovePoint(0);
}
}
public void SetTarget(Node2D value)
{
if (value != null)
{
if (GetPathTo(value) != targetPath)
{
targetPath = GetPathTo(value);
}
}
else
{
targetPath = @"";
}
}
public void SetTargetPath(NodePath value)
{
targetPath = value;
_target = GetNode(value) as Node2D;
}
}