Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / World.cs
@小李xl 小李xl on 21 Feb 2024 4 KB 游戏大厅, 开发中
  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.DungeonManager.CurrWorld;
  15. /// <summary>
  16. /// //对象根节点
  17. /// </summary>
  18. public Node2D NormalLayer;
  19. /// <summary>
  20. /// 对象根节点, 带y轴排序功能
  21. /// </summary>
  22. public Node2D YSortLayer;
  23. /// <summary>
  24. /// 地图根节点
  25. /// </summary>
  26. public TileMap TileRoot;
  27.  
  28. public Node2D StaticSpriteRoot;
  29. public Node2D AffiliationAreaRoot;
  30. public Node2D FogMaskRoot;
  31. 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. /// <summary>
  63. /// 随机数对象
  64. /// </summary>
  65. public SeedRandom Random { get; private set; }
  66. /// <summary>
  67. /// 随机对象池
  68. /// </summary>
  69. public RandomPool RandomPool { get; private set; }
  70. private bool _pause = false;
  71. private List<CoroutineData> _coroutineList;
  72.  
  73. public override void _Ready()
  74. {
  75. Color = Colors.Black;
  76. //TileRoot.YSortEnabled = false;
  77. NormalLayer = GetNode<Node2D>("TileRoot/NormalLayer");
  78. YSortLayer = GetNode<Node2D>("TileRoot/YSortLayer");
  79. TileRoot = GetNode<TileMap>("TileRoot");
  80. StaticSpriteRoot = GetNode<Node2D>("TileRoot/StaticSpriteRoot");
  81. FogMaskRoot = GetNode<Node2D>("TileRoot/FogMaskRoot");
  82. NavigationRoot = GetNode<Node2D>("TileRoot/NavigationRoot");
  83. AffiliationAreaRoot = GetNode<Node2D>("TileRoot/AffiliationAreaRoot");
  84. }
  85.  
  86. public override void _Process(double delta)
  87. {
  88. //协程更新
  89. ProxyCoroutineHandler.ProxyUpdateCoroutine(ref _coroutineList, (float)delta);
  90. }
  91.  
  92. /// <summary>
  93. /// 初始化 TileMap 中的层级
  94. /// </summary>
  95. public void InitLayer()
  96. {
  97. MapLayerManager.InitMapLayer(TileRoot);
  98. }
  99.  
  100. /// <summary>
  101. /// 获取指定层级根节点
  102. /// </summary>
  103. public Node2D GetRoomLayer(RoomLayerEnum layerEnum)
  104. {
  105. switch (layerEnum)
  106. {
  107. case RoomLayerEnum.NormalLayer:
  108. return NormalLayer;
  109. case RoomLayerEnum.YSortLayer:
  110. return YSortLayer;
  111. }
  112.  
  113. return null;
  114. }
  115.  
  116. /// <summary>
  117. /// 通知其他敌人发现目标了
  118. /// </summary>
  119. /// <param name="self">发送通知的角色</param>
  120. /// <param name="target">目标</param>
  121. public void NotifyEnemyTarget(Role self, ActivityObject target)
  122. {
  123. foreach (var role in Enemy_InstanceList)
  124. {
  125. if (role != self && !role.IsDestroyed && role.AffiliationArea == self.AffiliationArea)
  126. {
  127. //将未发现目标的敌人状态置为惊讶状态
  128. var controller = role.StateController;
  129. //延时通知效果
  130. role.CallDelay(Utils.Random.RandomRangeFloat(0.2f, 1f), () =>
  131. {
  132. if (controller.CurrState == AIStateEnum.AiNormal)
  133. {
  134. controller.ChangeState(AIStateEnum.AiLeaveFor, target);
  135. }
  136. });
  137. }
  138. }
  139. }
  140. public long StartCoroutine(IEnumerator able)
  141. {
  142. return ProxyCoroutineHandler.ProxyStartCoroutine(ref _coroutineList, able);
  143. }
  144. public void StopCoroutine(long coroutineId)
  145. {
  146. ProxyCoroutineHandler.ProxyStopCoroutine(ref _coroutineList, coroutineId);
  147. }
  148.  
  149. public bool IsCoroutineOver(long coroutineId)
  150. {
  151. return ProxyCoroutineHandler.ProxyIsCoroutineOver(ref _coroutineList, coroutineId);
  152. }
  153.  
  154. public void StopAllCoroutine()
  155. {
  156. ProxyCoroutineHandler.ProxyStopAllCoroutine(ref _coroutineList);
  157. }
  158.  
  159. /// <summary>
  160. /// 初始化随机池
  161. /// </summary>
  162. public void InitRandomPool(SeedRandom random)
  163. {
  164. Random = random;
  165. RandomPool = new RandomPool(this);
  166. }
  167. }