ShadowMath25D.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. /// <summary>
  8. /// Adds a simple shadow below an object.
  9. /// Place this ShadowMath25D node as a child of a Shadow25D, which
  10. /// is below the target object in the scene tree (not as a child).
  11. /// </summary>
  12. [Tool]
  13. public partial class ShadowMath25D : CharacterBody3D
  14. {
  15. /// <summary>
  16. /// The maximum distance below objects that shadows will appear.
  17. /// </summary>
  18. public real_t shadowLength = 1000;
  19. private Node25D shadowRoot;
  20. private Node3D targetMath;
  21. public override void _Ready()
  22. {
  23. shadowRoot = GetParent<Node25D>();
  24. int index = shadowRoot.GetIndex();
  25. targetMath = shadowRoot.GetParent().GetChild<Node25D>(index - 1).GetChild<Node3D>(0);
  26. }
  27. public override void _Process(real_t delta)
  28. {
  29. if (targetMath == null)
  30. {
  31. if (shadowRoot != null)
  32. {
  33. shadowRoot.Visible = false;
  34. }
  35. return; // Shadow is not in a valid place.
  36. }
  37. Position = targetMath.Position;
  38. var k = MoveAndCollide(Vector3.Down * shadowLength);
  39. if (k == null)
  40. {
  41. shadowRoot.Visible = false;
  42. }
  43. else
  44. {
  45. shadowRoot.Visible = true;
  46. GlobalTransform = Transform;
  47. }
  48. }
  49. }