Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / room / RoomManager.cs
  1. using System.Collections.Generic;
  2. using Godot;
  3.  
  4. /// <summary>
  5. /// 房间管理器
  6. /// </summary>
  7. public class RoomManager : Navigation2D
  8. {
  9. /// <summary>
  10. /// 玩家对象
  11. /// </summary>
  12. public Role Player { get; private set; }
  13.  
  14. //对象根节点
  15. private Node2D _objectRoot;
  16.  
  17. //对象根节点, 带y轴排序功能
  18. private YSort _sortRoot;
  19.  
  20. private Node2D _mapRoot;
  21.  
  22. private NavigationPolygonInstance _navigationPolygon;
  23. private Enemy _enemy;
  24.  
  25. private List<int> _wayIds = new List<int>(new[] { 129 });
  26. private List<Vector2> _points = new List<Vector2>();
  27.  
  28. public override void _EnterTree()
  29. {
  30. Input.MouseMode = Input.MouseModeEnum.Hidden;
  31.  
  32. _sortRoot = GetNode<YSort>("SortRoot");
  33. _objectRoot = GetNode<Node2D>("ObjectRoot");
  34.  
  35. _navigationPolygon = GetNode<NavigationPolygonInstance>("NavigationPolygonInstance");
  36.  
  37. //初始化地图
  38. _mapRoot = GetNode<Node2D>("MapRoot");
  39. var node = _mapRoot.GetChild(0).GetNode("Config");
  40. Color color = (Color)node.GetMeta("ClearColor");
  41. VisualServer.SetDefaultClearColor(color);
  42.  
  43. //创建玩家
  44. Player = new Player();
  45. Player.Position = new Vector2(100, 100);
  46. Player.Name = "Player";
  47. Player.PutDown();
  48.  
  49. _enemy = new Enemy();
  50. _enemy.Name = "Enemy";
  51. _enemy.PutDown(new Vector2(150, 150));
  52. }
  53.  
  54. public override void _Ready()
  55. {
  56. GenerateNavigationPolygon();
  57.  
  58. //播放bgm
  59. SoundManager.PlayMusic(ResourcePath.resource_sound_bgm_Intro_ogg, -17f);
  60. _enemy.PickUpWeapon(WeaponManager.GetGun("1001"));
  61.  
  62. WeaponManager.GetGun("1001").PutDown(new Vector2(80, 100));
  63. WeaponManager.GetGun("1001").PutDown(new Vector2(80, 80));
  64. WeaponManager.GetGun("1002").PutDown(new Vector2(80, 120));
  65. WeaponManager.GetGun("1003").PutDown(new Vector2(120, 80));
  66.  
  67. WeaponManager.GetGun("1003").PutDown(new Vector2(180, 80));
  68. WeaponManager.GetGun("1003").PutDown(new Vector2(180, 180));
  69. WeaponManager.GetGun("1002").PutDown(new Vector2(180, 120));
  70.  
  71. WeaponManager.GetGun("1004").PutDown(new Vector2(220, 120));
  72. }
  73.  
  74. public override void _Process(float delta)
  75. {
  76. if (GameApplication.Instance.Debug)
  77. {
  78. Update();
  79. }
  80. }
  81.  
  82. public override void _Draw()
  83. {
  84. if (GameApplication.Instance.Debug)
  85. {
  86. if (_points != null && _points.Count >= 2)
  87. {
  88. DrawPolyline(_points.ToArray(), Colors.Red);
  89. }
  90. }
  91. }
  92.  
  93. /// <summary>
  94. /// 获取房间根节点
  95. /// </summary>
  96. /// <param name="useYSort">是否获取 YSort 节点</param>
  97. /// <returns></returns>
  98. public Node2D GetRoot(bool useYSort = false)
  99. {
  100. return useYSort ? _sortRoot : _objectRoot;
  101. }
  102.  
  103. /// <summary>
  104. /// 自动生成导航区域
  105. /// </summary>
  106. private void GenerateNavigationPolygon()
  107. {
  108. //129
  109. var tileMap = _mapRoot.GetChild(0).GetNode<TileMap>("Wall");
  110. var size = tileMap.CellSize;
  111.  
  112. var rect = tileMap.GetUsedRect();
  113.  
  114. var x = (int)rect.Position.x;
  115. var y = (int)rect.Position.y;
  116. var w = (int)rect.Size.x;
  117. var h = (int)rect.Size.y;
  118.  
  119. for (int i = x; i < w; i++)
  120. {
  121. for (int j = y; j < h; j++)
  122. {
  123. var tileId = tileMap.GetCell(i, j);
  124. if (tileId != -1 && _wayIds.Contains(tileId))
  125. {
  126. //---------------------------------------
  127.  
  128. // 0:右, 1:下, 2:左, 3:上
  129. var dir = 0;
  130. _points.Clear();
  131. //找到路, 向右开始找边界
  132. _points.Add(new Vector2(i * size.x + size.x * 0.5f, j * size.y + size.y * 0.5f));
  133.  
  134. var tempI = i;
  135. var tempJ = j;
  136.  
  137. var prevI = i;
  138. var prevJ = j;
  139.  
  140. var len = 1;
  141. while (true)
  142. {
  143. switch (dir)
  144. {
  145. case 0:
  146. tempI++;
  147. break;
  148. case 1:
  149. tempJ++;
  150. break;
  151. case 2:
  152. tempI--;
  153. break;
  154. case 3:
  155. tempJ--;
  156. break;
  157. }
  158.  
  159. bool flag = true;
  160. int nextCellId = -1;
  161.  
  162. switch (dir)
  163. {
  164. case 0: //右
  165. {
  166. //向右找
  167. nextCellId = tileMap.GetCell(tempI, tempJ);
  168. flag = isWayCell(nextCellId);
  169. if (!flag)
  170. {
  171. if (isWayCell(tileMap.GetCell(tempI, tempJ - 1))) //向上找
  172. {
  173. dir = 3 - 1;
  174. }
  175. }
  176. }
  177. break;
  178. case 1: //下
  179. {
  180. nextCellId = tileMap.GetCell(tempI, tempJ);
  181. flag = isWayCell(nextCellId);
  182. }
  183. break;
  184. case 2: //左
  185. {
  186. // nextCellId = tileMap.GetCell(tempI, tempJ + 1);//向下找
  187. // flag = isWayCell(nextCellId);
  188. // if (flag)
  189. // {
  190. // dir = 1 - 1;
  191. // }
  192. // else
  193. // {
  194. nextCellId = tileMap.GetCell(tempI, tempJ); //向左找
  195. flag = isWayCell(nextCellId);
  196. //}
  197. }
  198. break;
  199. case 3: //上
  200. {
  201. nextCellId = tileMap.GetCell(tempI, tempJ);
  202. flag = isWayCell(nextCellId);
  203. }
  204. break;
  205. }
  206. if (!flag) //下一个不是可行走区域
  207. {
  208.  
  209. if (len <= 1)
  210. {
  211. //有问题, 不支持单格道路!
  212. GD.PrintErr("不支持单格道路: " + tempI + ", " + tempJ);
  213. }
  214. tempI = prevI;
  215. tempJ = prevJ;
  216. //转向
  217. dir++;
  218. //记录当前点
  219. _points.Add(new Vector2(tempI * size.x + size.x * 0.5f, tempJ * size.y + size.y * 0.5f));
  220. if (dir == 4)
  221. {
  222. goto a;
  223. }
  224. }
  225. else
  226. {
  227. len++;
  228. }
  229.  
  230. prevI = tempI;
  231. prevJ = tempJ;
  232. }
  233. //---------------------------------------
  234. }
  235. }
  236. }
  237.  
  238. a: ;
  239.  
  240. }
  241.  
  242. private bool isWayCell(int cellId)
  243. {
  244. return cellId != -1 && _wayIds.Contains(cellId);
  245. }
  246. }