Newer
Older
DungeonShooting / DungeonShooting_Godot / addons / dungeonShooting_plugin / ActivityObjectTemplate.cs
  1. using Godot;
  2.  
  3. namespace Plugin
  4. {
  5. /// <summary>
  6. /// ActivityObject 节点模板对象
  7. /// </summary>
  8. [Tool]
  9. public class ActivityObjectTemplate : Node
  10. {
  11. /// <summary>
  12. /// 当前物体所属物理层
  13. /// </summary>
  14. [Export(PropertyHint.Layers2dPhysics)]
  15. public uint CollisionLayer;
  16. /// <summary>
  17. /// 当前物体扫描的物理层
  18. /// </summary>
  19. [Export(PropertyHint.Layers2dPhysics)]
  20. public uint CollisionMask;
  21.  
  22. public override void _Ready()
  23. {
  24. // 在工具模式下创建的 template 节点自动创建对应的必要子节点
  25. if (Engine.EditorHint)
  26. {
  27. var parent = GetParent();
  28. if (parent != null)
  29. {
  30. //寻找 owner
  31. Node owner;
  32. if (parent.Owner != null)
  33. {
  34. owner = parent.Owner;
  35. }
  36. else if (Plugin.Instance.GetEditorInterface().GetEditedSceneRoot() == this)
  37. {
  38. owner = this;
  39. }
  40. else
  41. {
  42. owner = parent;
  43. }
  44.  
  45. //创建 Sprite
  46. if (GetNodeOrNull("AnimatedSprite") == null)
  47. {
  48. var sp = new AnimatedSprite();
  49. sp.Name = "AnimatedSprite";
  50. AddChild(sp);
  51. sp.Owner = owner;
  52. }
  53.  
  54. //创建Shadow
  55. if (GetNodeOrNull("ShadowSprite") == null)
  56. {
  57. var sd = new Sprite();
  58. sd.Name = "ShadowSprite";
  59. sd.Material = ResourceManager.ShadowMaterial;
  60. AddChild(sd);
  61. sd.Owner = owner;
  62. }
  63.  
  64. //创建Collision
  65. if (GetNodeOrNull("Collision") == null)
  66. {
  67. var co = new CollisionShape2D();
  68. co.Name = "Collision";
  69. AddChild(co);
  70. co.Owner = owner;
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }