Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / GameApplication.cs
  1.  
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text.Json;
  7. using Config;
  8. using Godot;
  9. using UI.BottomTips;
  10.  
  11. public partial class GameApplication : Node2D, ICoroutine
  12. {
  13. public static GameApplication Instance { get; private set; }
  14. /// <summary>
  15. /// 游戏渲染视口
  16. /// </summary>
  17. [Export] public SubViewport SubViewport;
  18.  
  19. /// <summary>
  20. /// SubViewportContainer 组件
  21. /// </summary>
  22. [Export] public SubViewportContainer SubViewportContainer;
  23.  
  24. /// <summary>
  25. /// 场景根节点
  26. /// </summary>
  27. [Export] public Node2D SceneRoot;
  28. /// <summary>
  29. /// 全局根节点
  30. /// </summary>
  31. [Export] public Node2D GlobalNodeRoot;
  32. /// <summary>
  33. /// 游戏目标帧率
  34. /// </summary>
  35. public int TargetFps { get; private set; }
  36. /// <summary>
  37. /// 鼠标指针
  38. /// </summary>
  39. public Cursor Cursor { get; private set; }
  40.  
  41. /// <summary>
  42. /// 游戏世界
  43. /// </summary>
  44. public World World { get; private set; }
  45.  
  46. /// <summary>
  47. /// 地牢管理器
  48. /// </summary>
  49. public DungeonManager DungeonManager { get; private set; }
  50. /// <summary>
  51. /// 房间配置
  52. /// </summary>
  53. public Dictionary<string, DungeonRoomGroup> RoomConfig { get; private set; }
  54. // /// <summary>
  55. // /// 房间配置数据, key: 模板房间资源路径
  56. // /// </summary>
  57. // public Dictionary<string, DungeonRoomSplit> RoomConfigMap { get; private set; }
  58.  
  59. /// <summary>
  60. /// 游戏视图大小
  61. /// </summary>
  62. public Vector2 ViewportSize { get; private set; } = new Vector2(480, 270);
  63. /// <summary>
  64. /// 像素缩放
  65. /// </summary>
  66. public int PixelScale { get; private set; } = 4;
  67. /// <summary>
  68. /// 地牢配置信息
  69. /// </summary>
  70. public DungeonConfig DungeonConfig { get; private set; }
  71.  
  72. //开启的协程
  73. private List<CoroutineData> _coroutineList;
  74. public GameApplication()
  75. {
  76. Instance = this;
  77. TargetFps = (int)DisplayServer.ScreenGetRefreshRate();
  78. Utils.InitRandom();
  79.  
  80. //初始化配置表
  81. ExcelConfig.Init();
  82. //初始化房间配置数据
  83. InitRoomConfig();
  84. //初始化武器数据
  85. Weapon.InitWeaponAttribute();
  86. //初始化敌人数据
  87. Enemy.InitEnemyAttribute();
  88. DungeonConfig = new DungeonConfig();
  89. DungeonConfig.GroupName = RoomConfig.FirstOrDefault().Key;
  90. DungeonConfig.RoomCount = 20;
  91. }
  92. public override void _EnterTree()
  93. {
  94. //背景颜色
  95. RenderingServer.SetDefaultClearColor(new Color(0, 0, 0, 1));
  96. //随机化种子
  97. //GD.Randomize();
  98. //固定帧率
  99. Engine.MaxFps = TargetFps;
  100. //调试绘制开关
  101. ActivityObject.IsDebug = false;
  102. //Engine.TimeScale = 0.2f;
  103. //调整窗口分辨率
  104. OnWindowSizeChanged();
  105. //窗体大小改变
  106. GetWindow().SizeChanged += OnWindowSizeChanged;
  107.  
  108. ImageCanvas.Init(GetTree().CurrentScene);
  109. //初始化ui
  110. UiManager.Init();
  111. //调试Ui
  112. UiManager.Open_Debugger();
  113. // 初始化鼠标
  114. InitCursor();
  115. //地牢管理器
  116. DungeonManager = new DungeonManager();
  117. DungeonManager.Name = "DungeonManager";
  118. SceneRoot.AddChild(DungeonManager);
  119.  
  120. MapProjectManager.Init();
  121. BottomTipsPanel.Init();
  122. //打开主菜单Ui
  123. UiManager.Open_Main();
  124. //UiManager.Open_MapEditorProject();
  125. }
  126.  
  127. public override void _Process(double delta)
  128. {
  129. var newDelta = (float)delta;
  130. InputManager.Update(newDelta);
  131. SoundManager.Update(newDelta);
  132. //协程更新
  133. ProxyCoroutineHandler.ProxyUpdateCoroutine(ref _coroutineList, newDelta);
  134. }
  135.  
  136. /// <summary>
  137. /// 创建新的 World 对象, 相当于清理房间
  138. /// </summary>
  139. public World CreateNewWorld()
  140. {
  141. if (World != null)
  142. {
  143. ClearWorld();
  144. World.QueueFree();
  145. }
  146. World = ResourceManager.LoadAndInstantiate<World>(ResourcePath.scene_World_tscn);
  147. SceneRoot.AddChild(World);
  148. return World;
  149. }
  150.  
  151. /// <summary>
  152. /// 销毁 World 对象, 相当于清理房间
  153. /// </summary>
  154. public void DestroyWorld()
  155. {
  156. //销毁所有物体
  157. if (World != null)
  158. {
  159. ClearWorld();
  160. World.QueueFree();
  161. }
  162. //销毁池中所有物体
  163. ObjectPool.DisposeAllItem();
  164.  
  165. World = null;
  166. }
  167. /// <summary>
  168. /// 将 viewport 以外的全局坐标 转换成 viewport 内的全局坐标
  169. /// </summary>
  170. public Vector2 GlobalToViewPosition(Vector2 globalPos)
  171. {
  172. return globalPos / PixelScale - (ViewportSize / 2) + GameCamera.Main.GlobalPosition - GameCamera.Main.PixelOffset;
  173. }
  174.  
  175. /// <summary>
  176. /// 将 viewport 以内的全局坐标 转换成 viewport 外的全局坐标
  177. /// </summary>
  178. public Vector2 ViewToGlobalPosition(Vector2 viewPos)
  179. {
  180. return (viewPos + GameCamera.Main.PixelOffset - (GameCamera.Main.GlobalPosition + GameCamera.Main.Offset) + (ViewportSize / 2)) * PixelScale;
  181. }
  182. public long StartCoroutine(IEnumerator able)
  183. {
  184. return ProxyCoroutineHandler.ProxyStartCoroutine(ref _coroutineList, able);
  185. }
  186. public void StopCoroutine(long coroutineId)
  187. {
  188. ProxyCoroutineHandler.ProxyStopCoroutine(ref _coroutineList, coroutineId);
  189. }
  190.  
  191. public bool IsCoroutineOver(long coroutineId)
  192. {
  193. return ProxyCoroutineHandler.ProxyIsCoroutineOver(ref _coroutineList, coroutineId);
  194. }
  195.  
  196. public void StopAllCoroutine()
  197. {
  198. ProxyCoroutineHandler.ProxyStopAllCoroutine(ref _coroutineList);
  199. }
  200.  
  201. public void SetRoomConfig(Dictionary<string,DungeonRoomGroup> roomConfig)
  202. {
  203. RoomConfig = roomConfig;
  204. InitReadyRoom();
  205. }
  206.  
  207. //初始化房间配置
  208. private void InitRoomConfig()
  209. {
  210. //加载房间配置信息
  211. var asText = ResourceManager.LoadText("res://" + GameConfig.RoomTileDir + GameConfig.RoomGroupConfigFile);
  212. RoomConfig = JsonSerializer.Deserialize<Dictionary<string, DungeonRoomGroup>>(asText);
  213.  
  214. InitReadyRoom();
  215. }
  216. //初始化房间数据
  217. private void InitReadyRoom()
  218. {
  219. foreach (var dungeonRoomGroup in RoomConfig)
  220. {
  221. RemoveUnreadyRooms(dungeonRoomGroup.Value.BattleList);
  222. RemoveUnreadyRooms(dungeonRoomGroup.Value.InletList);
  223. RemoveUnreadyRooms(dungeonRoomGroup.Value.OutletList);
  224. RemoveUnreadyRooms(dungeonRoomGroup.Value.BossList);
  225. RemoveUnreadyRooms(dungeonRoomGroup.Value.ShopList);
  226. RemoveUnreadyRooms(dungeonRoomGroup.Value.RewardList);
  227. RemoveUnreadyRooms(dungeonRoomGroup.Value.EventList);
  228. }
  229. }
  230. //移除未准备好的房间
  231. private void RemoveUnreadyRooms(List<DungeonRoomSplit> roomInfos)
  232. {
  233. for (var i = 0; i < roomInfos.Count; i++)
  234. {
  235. if (roomInfos[i].ErrorType != RoomErrorType.None) //存在错误
  236. {
  237. roomInfos.RemoveAt(i);
  238. i--;
  239. }
  240. }
  241. }
  242.  
  243. //窗体大小改变
  244. private void OnWindowSizeChanged()
  245. {
  246. var size = GetWindow().Size;
  247. ViewportSize = size / PixelScale;
  248. RefreshSubViewportSize();
  249. }
  250. //刷新视窗大小
  251. private void RefreshSubViewportSize()
  252. {
  253. var s = new Vector2I((int)ViewportSize.X, (int)ViewportSize.Y);
  254. s.X = s.X / 2 * 2 + 2;
  255. s.Y = s.Y / 2 * 2 + 2;
  256. SubViewport.Size = s;
  257. SubViewportContainer.Scale = new Vector2(PixelScale, PixelScale);
  258. SubViewportContainer.Size = s;
  259. SubViewportContainer.Position = new Vector2(-PixelScale, -PixelScale);
  260. }
  261.  
  262. //初始化鼠标
  263. private void InitCursor()
  264. {
  265. Cursor = ResourceManager.LoadAndInstantiate<Cursor>(ResourcePath.prefab_Cursor_tscn);
  266. var cursorLayer = new CanvasLayer();
  267. cursorLayer.Name = "CursorLayer";
  268. cursorLayer.Layer = UiManager.GetUiLayer(UiLayer.Pop).Layer + 10;
  269. AddChild(cursorLayer);
  270. cursorLayer.AddChild(Cursor);
  271. }
  272.  
  273. //清理世界
  274. private void ClearWorld()
  275. {
  276. var childCount = World.NormalLayer.GetChildCount();
  277. for (var i = 0; i < childCount; i++)
  278. {
  279. var c = World.NormalLayer.GetChild(i);
  280. if (c is IDestroy destroy)
  281. {
  282. destroy.Destroy();
  283. }
  284. }
  285. childCount = World.YSortLayer.GetChildCount();
  286. for (var i = 0; i < childCount; i++)
  287. {
  288. var c = World.YSortLayer.GetChild(i);
  289. if (c is IDestroy destroy)
  290. {
  291. destroy.Destroy();
  292. }
  293. }
  294. }
  295. }