Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / World.cs
@小李xl 小李xl on 22 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. //TileRoot.YSortEnabled = false;
  76. NormalLayer = GetNode<Node2D>("TileRoot/NormalLayer");
  77. YSortLayer = GetNode<Node2D>("TileRoot/YSortLayer");
  78. TileRoot = GetNode<TileMap>("TileRoot");
  79. StaticSpriteRoot = GetNode<Node2D>("TileRoot/StaticSpriteRoot");
  80. FogMaskRoot = GetNode<Node2D>("TileRoot/FogMaskRoot");
  81. NavigationRoot = GetNode<Node2D>("TileRoot/NavigationRoot");
  82. AffiliationAreaRoot = GetNode<Node2D>("TileRoot/AffiliationAreaRoot");
  83. }
  84.  
  85. public override void _Process(double delta)
  86. {
  87. //协程更新
  88. ProxyCoroutineHandler.ProxyUpdateCoroutine(ref _coroutineList, (float)delta);
  89. }
  90.  
  91. /// <summary>
  92. /// 获取指定层级根节点
  93. /// </summary>
  94. public Node2D GetRoomLayer(RoomLayerEnum layerEnum)
  95. {
  96. switch (layerEnum)
  97. {
  98. case RoomLayerEnum.NormalLayer:
  99. return NormalLayer;
  100. case RoomLayerEnum.YSortLayer:
  101. return YSortLayer;
  102. }
  103.  
  104. return null;
  105. }
  106.  
  107. /// <summary>
  108. /// 通知其他敌人发现目标了
  109. /// </summary>
  110. /// <param name="self">发送通知的角色</param>
  111. /// <param name="target">目标</param>
  112. public void NotifyEnemyTarget(Role self, ActivityObject target)
  113. {
  114. foreach (var role in Enemy_InstanceList)
  115. {
  116. if (role != self && !role.IsDestroyed && role.AffiliationArea == self.AffiliationArea)
  117. {
  118. //将未发现目标的敌人状态置为惊讶状态
  119. var controller = role.StateController;
  120. //延时通知效果
  121. role.CallDelay(Utils.Random.RandomRangeFloat(0.2f, 1f), () =>
  122. {
  123. if (controller.CurrState == AIStateEnum.AiNormal)
  124. {
  125. controller.ChangeState(AIStateEnum.AiLeaveFor, target);
  126. }
  127. });
  128. }
  129. }
  130. }
  131. public long StartCoroutine(IEnumerator able)
  132. {
  133. return ProxyCoroutineHandler.ProxyStartCoroutine(ref _coroutineList, able);
  134. }
  135. public void StopCoroutine(long coroutineId)
  136. {
  137. ProxyCoroutineHandler.ProxyStopCoroutine(ref _coroutineList, coroutineId);
  138. }
  139.  
  140. public bool IsCoroutineOver(long coroutineId)
  141. {
  142. return ProxyCoroutineHandler.ProxyIsCoroutineOver(ref _coroutineList, coroutineId);
  143. }
  144.  
  145. public void StopAllCoroutine()
  146. {
  147. ProxyCoroutineHandler.ProxyStopAllCoroutine(ref _coroutineList);
  148. }
  149.  
  150. /// <summary>
  151. /// 初始化随机池
  152. /// </summary>
  153. public void InitRandomPool(SeedRandom random)
  154. {
  155. Random = random;
  156. RandomPool = new RandomPool(this);
  157. }
  158. }