1 using Godot;
2 #if REAL_T_IS_DOUBLE
3 using real_t = System.Double;
4 #else
5 using real_t = System.Single;
6 #endif
7 
8 /// <summary>
9 /// Adds a simple shadow below an object.
10 /// Place this ShadowMath25D node as a child of a Shadow25D, which
11 /// is below the target object in the scene tree (not as a child).
12 /// </summary>
13 [Tool]
14 public class ShadowMath25D : KinematicBody
15 {
16     /// <summary>
17     /// The maximum distance below objects that shadows will appear.
18     /// </summary>
19     public real_t shadowLength = 1000;
20     private Node25D shadowRoot;
21     private Spatial targetMath;
22 
_Ready()23     public override void _Ready()
24     {
25         shadowRoot = GetParent<Node25D>();
26         int index = shadowRoot.GetPositionInParent();
27         targetMath = shadowRoot.GetParent().GetChild<Node25D>(index - 1).GetChild<Spatial>(0);
28     }
29 
_Process(real_t delta)30     public override void _Process(real_t delta)
31     {
32         if (targetMath == null)
33         {
34             if (shadowRoot != null)
35             {
36                 shadowRoot.Visible = false;
37             }
38             return; // Shadow is not in a valid place.
39         }
40 
41         Translation = targetMath.Translation;
42         var k = MoveAndCollide(Vector3.Down * shadowLength);
43         if (k == null)
44         {
45             shadowRoot.Visible = false;
46         }
47         else
48         {
49             shadowRoot.Visible = true;
50             GlobalTransform = Transform;
51         }
52     }
53 
54 }
55