Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / room / World.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Godot;
  5.  
  6. /// <summary>
  7. /// 游戏世界
  8. /// </summary>
  9. public partial class World : CanvasModulate, ICoroutine
  10. {
  11. /// <summary>
  12. /// 当前的游戏世界对象
  13. /// </summary>
  14. public static World Current => GameApplication.Instance.World;
  15. /// <summary>
  16. /// //对象根节点
  17. /// </summary>
  18. [Export] public Node2D NormalLayer;
  19. /// <summary>
  20. /// 对象根节点, 带y轴排序功能
  21. /// </summary>
  22. [Export] public Node2D YSortLayer;
  23. /// <summary>
  24. /// 地图根节点
  25. /// </summary>
  26. [Export] public TileMap TileRoot;
  27.  
  28. [Export] public Node2D StaticSpriteRoot;
  29. [Export] public Node2D AffiliationAreaRoot;
  30. [Export] public Node2D FogMaskRoot;
  31. [Export] public Node2D NavigationRoot;
  32. /// <summary>
  33. /// 是否暂停
  34. /// </summary>
  35. public bool Pause
  36. {
  37. get => _pause;
  38. set
  39. {
  40. if (_pause != value)
  41. {
  42. _pause = value;
  43. if (value)
  44. {
  45. ProcessMode = ProcessModeEnum.WhenPaused;
  46. }
  47. else
  48. {
  49. ProcessMode = ProcessModeEnum.Inherit;
  50. }
  51. }
  52. }
  53. }
  54. /// <summary>
  55. /// 所有被扔在地上的武器
  56. /// </summary>
  57. public HashSet<Weapon> Weapon_UnclaimedWeapons { get; } = new HashSet<Weapon>();
  58. /// <summary>
  59. /// 记录所有存活的敌人
  60. /// </summary>
  61. public List<Enemy> Enemy_InstanceList { get; } = new List<Enemy>();
  62. private bool _pause = false;
  63. private List<CoroutineData> _coroutineList;
  64.  
  65. public override void _Ready()
  66. {
  67. Color = Colors.Black;
  68. //TileRoot.YSortEnabled = false;
  69. }
  70.  
  71. public override void _Process(double delta)
  72. {
  73. //协程更新
  74. ProxyCoroutineHandler.ProxyUpdateCoroutine(ref _coroutineList, (float)delta);
  75. }
  76.  
  77. /// <summary>
  78. /// 初始化 TileMap 中的层级
  79. /// </summary>
  80. public void InitLayer()
  81. {
  82. TileRoot.AddLayer(MapLayer.AutoFloorLayer);
  83. TileRoot.SetLayerZIndex(MapLayer.AutoFloorLayer, -10);
  84. TileRoot.SetLayerNavigationEnabled(MapLayer.AutoFloorLayer, false);
  85. TileRoot.AddLayer(MapLayer.CustomFloorLayer1);
  86. TileRoot.SetLayerZIndex(MapLayer.CustomFloorLayer1, -10);
  87. TileRoot.SetLayerNavigationEnabled(MapLayer.CustomFloorLayer1, false);
  88. TileRoot.AddLayer(MapLayer.CustomFloorLayer2);
  89. TileRoot.SetLayerZIndex(MapLayer.CustomFloorLayer2, -10);
  90. TileRoot.SetLayerNavigationEnabled(MapLayer.CustomFloorLayer2, false);
  91. TileRoot.AddLayer(MapLayer.CustomFloorLayer3);
  92. TileRoot.SetLayerZIndex(MapLayer.CustomFloorLayer3, -10);
  93. TileRoot.SetLayerNavigationEnabled(MapLayer.CustomFloorLayer3, false);
  94. TileRoot.AddLayer(MapLayer.AutoMiddleLayer);
  95. TileRoot.SetLayerZIndex(MapLayer.AutoMiddleLayer, 0);
  96. TileRoot.SetLayerNavigationEnabled(MapLayer.AutoMiddleLayer, false);
  97. TileRoot.SetLayerYSortEnabled(MapLayer.AutoMiddleLayer, true);
  98. TileRoot.AddLayer(MapLayer.CustomMiddleLayer1);
  99. TileRoot.SetLayerZIndex(MapLayer.CustomMiddleLayer1, 0);
  100. TileRoot.SetLayerNavigationEnabled(MapLayer.CustomMiddleLayer1, false);
  101. TileRoot.SetLayerYSortEnabled(MapLayer.CustomMiddleLayer1, true);
  102. TileRoot.AddLayer(MapLayer.CustomMiddleLayer2);
  103. TileRoot.SetLayerZIndex(MapLayer.CustomMiddleLayer2, 0);
  104. TileRoot.SetLayerNavigationEnabled(MapLayer.CustomMiddleLayer2, false);
  105. TileRoot.SetLayerYSortEnabled(MapLayer.CustomMiddleLayer2, true);
  106. TileRoot.AddLayer(MapLayer.AutoTopLayer);
  107. TileRoot.SetLayerZIndex(MapLayer.AutoTopLayer, 10);
  108. TileRoot.SetLayerNavigationEnabled(MapLayer.AutoTopLayer, false);
  109. TileRoot.AddLayer(MapLayer.CustomTopLayer);
  110. TileRoot.SetLayerZIndex(MapLayer.CustomTopLayer, 10);
  111. TileRoot.SetLayerNavigationEnabled(MapLayer.CustomTopLayer, false);
  112. TileRoot.AddLayer(MapLayer.AutoAisleFloorLayer);
  113. TileRoot.SetLayerZIndex(MapLayer.AutoAisleFloorLayer, -10);
  114. TileRoot.SetLayerNavigationEnabled(MapLayer.AutoAisleFloorLayer, false);
  115. }
  116.  
  117. /// <summary>
  118. /// 获取指定层级根节点
  119. /// </summary>
  120. public Node2D GetRoomLayer(RoomLayerEnum layerEnum)
  121. {
  122. switch (layerEnum)
  123. {
  124. case RoomLayerEnum.NormalLayer:
  125. return NormalLayer;
  126. case RoomLayerEnum.YSortLayer:
  127. return YSortLayer;
  128. }
  129.  
  130. return null;
  131. }
  132.  
  133. /// <summary>
  134. /// 通知其他敌人发现目标了
  135. /// </summary>
  136. /// <param name="self">发送通知的角色</param>
  137. /// <param name="target">目标</param>
  138. public void NotifyEnemyTarget(Role self, ActivityObject target)
  139. {
  140. foreach (var role in Enemy_InstanceList)
  141. {
  142. if (role != self && !role.IsDestroyed && role.AffiliationArea == self.AffiliationArea)
  143. {
  144. //将未发现目标的敌人状态置为惊讶状态
  145. var controller = role.StateController;
  146. //延时通知效果
  147. role.CallDelay(Utils.Random.RandomRangeFloat(0.2f, 1f), () =>
  148. {
  149. if (controller.CurrState == AIStateEnum.AiNormal)
  150. {
  151. controller.ChangeState(AIStateEnum.AiLeaveFor, target);
  152. }
  153. });
  154. }
  155. }
  156. }
  157. public long StartCoroutine(IEnumerator able)
  158. {
  159. return ProxyCoroutineHandler.ProxyStartCoroutine(ref _coroutineList, able);
  160. }
  161. public void StopCoroutine(long coroutineId)
  162. {
  163. ProxyCoroutineHandler.ProxyStopCoroutine(ref _coroutineList, coroutineId);
  164. }
  165.  
  166. public bool IsCoroutineOver(long coroutineId)
  167. {
  168. return ProxyCoroutineHandler.ProxyIsCoroutineOver(ref _coroutineList, coroutineId);
  169. }
  170.  
  171. public void StopAllCoroutine()
  172. {
  173. ProxyCoroutineHandler.ProxyStopAllCoroutine(ref _coroutineList);
  174. }
  175. }