Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / room / World.cs
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Godot;
  4.  
  5. /// <summary>
  6. /// 游戏世界
  7. /// </summary>
  8. public partial class World : CanvasModulate, ICoroutine
  9. {
  10. /// <summary>
  11. /// 当前的游戏世界对象
  12. /// </summary>
  13. public static World Current => GameApplication.Instance.World;
  14. /// <summary>
  15. /// //对象根节点
  16. /// </summary>
  17. [Export] public Node2D NormalLayer;
  18. /// <summary>
  19. /// 对象根节点, 带y轴排序功能
  20. /// </summary>
  21. [Export] public Node2D YSortLayer;
  22. /// <summary>
  23. /// 地图根节点
  24. /// </summary>
  25. [Export] public TileMap TileRoot;
  26.  
  27. [Export] public Node2D StaticSpriteRoot;
  28. [Export] public Node2D AffiliationAreaRoot;
  29. [Export] public Node2D FogMaskRoot;
  30. /// <summary>
  31. /// 是否暂停
  32. /// </summary>
  33. public bool Pause
  34. {
  35. get => _pause;
  36. set
  37. {
  38. if (_pause != value)
  39. {
  40. _pause = value;
  41. if (value)
  42. {
  43. ProcessMode = ProcessModeEnum.WhenPaused;
  44. }
  45. else
  46. {
  47. ProcessMode = ProcessModeEnum.Inherit;
  48. }
  49. }
  50. }
  51. }
  52. /// <summary>
  53. /// 所有被扔在地上的武器
  54. /// </summary>
  55. public HashSet<Weapon> Weapon_UnclaimedWeapons { get; } = new HashSet<Weapon>();
  56. /// <summary>
  57. /// 记录所有存活的敌人
  58. /// </summary>
  59. public List<Enemy> Enemy_InstanceList { get; } = new List<Enemy>();
  60. private bool _pause = false;
  61. private List<CoroutineData> _coroutineList;
  62.  
  63. public override void _Ready()
  64. {
  65. Color = Colors.Black;
  66. //临时处理, 加载TileSet
  67. var tileSet = ResourceManager.Load<TileSet>(ResourcePath.resource_tileSet_map1_TileSet1_tres);
  68. var tileSetAtlasSource = (TileSetAtlasSource)tileSet.GetSource(0);
  69. tileSetAtlasSource.Texture = ImageTexture.CreateFromImage(Image.LoadFromFile("resource/tileSprite/map1/16x16 dungeon ii wall reconfig v04 spritesheet.png"));
  70. TileRoot.TileSet = tileSet;
  71. TileRoot.YSortEnabled = false;
  72. }
  73.  
  74. public override void _Process(double delta)
  75. {
  76. //协程更新
  77. ProxyCoroutineHandler.ProxyUpdateCoroutine(ref _coroutineList, (float)delta);
  78. }
  79.  
  80. /// <summary>
  81. /// 获取指定层级根节点
  82. /// </summary>
  83. public Node2D GetRoomLayer(RoomLayerEnum layerEnum)
  84. {
  85. switch (layerEnum)
  86. {
  87. case RoomLayerEnum.NormalLayer:
  88. return NormalLayer;
  89. case RoomLayerEnum.YSortLayer:
  90. return YSortLayer;
  91. }
  92.  
  93. return null;
  94. }
  95.  
  96. /// <summary>
  97. /// 通知其他敌人发现目标了
  98. /// </summary>
  99. /// <param name="self">发送通知的角色</param>
  100. /// <param name="target">目标</param>
  101. public void NotifyEnemyTarget(Role self, ActivityObject target)
  102. {
  103. foreach (var role in Enemy_InstanceList)
  104. {
  105. if (role != self && !role.IsDestroyed && role.AffiliationArea == self.AffiliationArea)
  106. {
  107. //将未发现目标的敌人状态置为惊讶状态
  108. var controller = role.StateController;
  109. //延时通知效果
  110. role.CallDelay(Utils.Random.RandomRangeFloat(0.2f, 1f), () =>
  111. {
  112. if (controller.CurrState == AIStateEnum.AiNormal)
  113. {
  114. controller.ChangeState(AIStateEnum.AiLeaveFor, target);
  115. }
  116. });
  117. }
  118. }
  119. }
  120. public long StartCoroutine(IEnumerator able)
  121. {
  122. return ProxyCoroutineHandler.ProxyStartCoroutine(ref _coroutineList, able);
  123. }
  124. public void StopCoroutine(long coroutineId)
  125. {
  126. ProxyCoroutineHandler.ProxyStopCoroutine(ref _coroutineList, coroutineId);
  127. }
  128.  
  129. public bool IsCoroutineOver(long coroutineId)
  130. {
  131. return ProxyCoroutineHandler.ProxyIsCoroutineOver(ref _coroutineList, coroutineId);
  132. }
  133.  
  134. public void StopAllCoroutine()
  135. {
  136. ProxyCoroutineHandler.ProxyStopAllCoroutine(ref _coroutineList);
  137. }
  138. }