Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / room / RoomDoor.cs
@小李xl 小李xl on 15 Oct 2023 4 KB 跟换门素材
  1.  
  2. using Godot;
  3.  
  4. /// <summary>
  5. /// 房间的门, 门有两种状态, 打开和关闭
  6. /// </summary>
  7. [Tool]
  8. public partial class RoomDoor : ActivityObject
  9. {
  10. /// <summary>
  11. /// 门的方向
  12. /// </summary>
  13. public DoorDirection Direction => _door.Direction;
  14. /// <summary>
  15. /// 门是否关闭
  16. /// </summary>
  17. public bool IsClose { get; private set; }
  18. private RoomDoorInfo _door;
  19. private bool waitDisabledCollision = false;
  20. private AnimatedSprite2D _animatedDown;
  21.  
  22. public override void OnInit()
  23. {
  24. AnimatedSprite.AnimationFinished += OnAnimationFinished;
  25. }
  26.  
  27. /// <summary>
  28. /// 初始化调用
  29. /// </summary>
  30. public void Init(RoomDoorInfo doorInfo)
  31. {
  32. _door = doorInfo;
  33. IsClose = false;
  34. if (doorInfo.Direction == DoorDirection.E || doorInfo.Direction == DoorDirection.W)
  35. {
  36. _animatedDown = GetNode<AnimatedSprite2D>("AnimatedSpriteDown");
  37. }
  38. OpenDoorHandler();
  39. }
  40.  
  41. /// <summary>
  42. /// 打开当前的门
  43. /// </summary>
  44. public void OpenDoor()
  45. {
  46. IsClose = false;
  47. //Visible = false;
  48. waitDisabledCollision = true;
  49. if (AnimatedSprite.SpriteFrames.HasAnimation(AnimatorNames.OpenDoor))
  50. {
  51. AnimatedSprite.Play(AnimatorNames.OpenDoor);
  52. }
  53. if (_animatedDown != null && _animatedDown.SpriteFrames.HasAnimation(AnimatorNames.OpenDoor))
  54. {
  55. _animatedDown.Play(AnimatorNames.OpenDoor);
  56. }
  57. }
  58.  
  59. /// <summary>
  60. /// 关闭当前的门
  61. /// </summary>
  62. public void CloseDoor()
  63. {
  64. IsClose = true;
  65. //Visible = true;
  66. Collision.Disabled = false;
  67. if (_door.Navigation != null)
  68. {
  69. _door.Navigation.OpenNavigationNode.Enabled = false;
  70. _door.Navigation.OpenNavigationNode.Visible = false;
  71. _door.Navigation.CloseNavigationNode.Enabled = true;
  72. _door.Navigation.CloseNavigationNode.Visible = true;
  73. }
  74.  
  75. if (AnimatedSprite.SpriteFrames.HasAnimation(AnimatorNames.CloseDoor))
  76. {
  77. AnimatedSprite.Play(AnimatorNames.CloseDoor);
  78. }
  79.  
  80. if (_animatedDown != null && _animatedDown.SpriteFrames.HasAnimation(AnimatorNames.CloseDoor))
  81. {
  82. _animatedDown.Play(AnimatorNames.CloseDoor);
  83. }
  84.  
  85. //调整门的层级
  86. switch (Direction)
  87. {
  88. case DoorDirection.E:
  89. ZIndex = GameConfig.TopMapLayer;
  90. if (_animatedDown != null)
  91. {
  92. _animatedDown.ZIndex = GameConfig.TopMapLayer;
  93. }
  94.  
  95. break;
  96. case DoorDirection.W:
  97. ZIndex = GameConfig.TopMapLayer;
  98. if (_animatedDown != null)
  99. {
  100. _animatedDown.ZIndex = GameConfig.TopMapLayer;
  101. }
  102.  
  103. break;
  104. case DoorDirection.S:
  105. ZIndex = GameConfig.TopMapLayer;
  106. break;
  107. case DoorDirection.N:
  108. ZIndex = GameConfig.MiddleMapLayer;
  109. break;
  110. }
  111. }
  112.  
  113. private void OnAnimationFinished()
  114. {
  115. if (!IsClose && waitDisabledCollision) //开门动画播放完成
  116. {
  117. waitDisabledCollision = false;
  118. OpenDoorHandler();
  119. }
  120. }
  121.  
  122. private void OpenDoorHandler()
  123. {
  124. Collision.Disabled = true;
  125. if (_door.Navigation != null)
  126. {
  127. _door.Navigation.OpenNavigationNode.Enabled = true;
  128. _door.Navigation.OpenNavigationNode.Visible = true;
  129. _door.Navigation.CloseNavigationNode.Enabled = false;
  130. _door.Navigation.CloseNavigationNode.Visible = false;
  131. }
  132. //调整门的层级
  133. //ZIndex = GameConfig.FloorMapLayer;
  134. //调整门的层级
  135. switch (Direction)
  136. {
  137. case DoorDirection.E:
  138. ZIndex = GameConfig.MiddleMapLayer;
  139. if (_animatedDown != null)
  140. {
  141. _animatedDown.ZIndex = GameConfig.TopMapLayer;
  142. }
  143.  
  144. break;
  145. case DoorDirection.W:
  146. ZIndex = GameConfig.MiddleMapLayer;
  147. if (_animatedDown != null)
  148. {
  149. _animatedDown.ZIndex = GameConfig.TopMapLayer;
  150. }
  151.  
  152. break;
  153. case DoorDirection.S:
  154. ZIndex = GameConfig.TopMapLayer;
  155. break;
  156. case DoorDirection.N:
  157. ZIndex = GameConfig.MiddleMapLayer;
  158. break;
  159. }
  160. }
  161. }