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