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