Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / GameApplication.cs
@小李xl 小李xl on 23 Aug 2023 6 KB 恢复加载地牢
  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.  
  33. /// <summary>
  34. /// 是否开启调试
  35. /// </summary>
  36. [ExportGroup("Debug")]
  37. [Export] public bool Debug = false;
  38.  
  39. /// <summary>
  40. /// 鼠标指针
  41. /// </summary>
  42. public Cursor Cursor { get; private set; }
  43. /// <summary>
  44. /// 游戏世界
  45. /// </summary>
  46. public World World { get; private set; }
  47.  
  48. /// <summary>
  49. /// 地牢管理器
  50. /// </summary>
  51. public DungeonManager DungeonManager { get; private set; }
  52. /// <summary>
  53. /// 房间配置
  54. /// </summary>
  55. public Dictionary<string, DungeonRoomGroup> RoomConfig { get; private set; }
  56. // /// <summary>
  57. // /// 房间配置数据, key: 模板房间资源路径
  58. // /// </summary>
  59. // public Dictionary<string, DungeonRoomSplit> RoomConfigMap { get; private set; }
  60.  
  61. /// <summary>
  62. /// 游戏视图大小
  63. /// </summary>
  64. public Vector2 ViewportSize { get; private set; } = new Vector2(480, 270);
  65. /// <summary>
  66. /// 像素缩放
  67. /// </summary>
  68. public int PixelScale { get; private set; } = 4;
  69. /// <summary>
  70. /// 地牢配置信息
  71. /// </summary>
  72. public DungeonConfig DungeonConfig { get; private set; }
  73.  
  74. //开启的协程
  75. private List<CoroutineData> _coroutineList;
  76. public GameApplication()
  77. {
  78. Instance = this;
  79.  
  80. //初始化配置表
  81. ExcelConfig.Init();
  82. //初始化房间配置数据
  83. InitRoomConfig();
  84. //初始化 ActivityObject
  85. ActivityObject.InitActivity();
  86. //初始化武器数据
  87. Weapon.InitWeaponAttribute();
  88. DungeonConfig = new DungeonConfig();
  89. DungeonConfig.GroupName = RoomConfig.FirstOrDefault().Key;
  90. DungeonConfig.RoomCount = 20;
  91. }
  92. public override void _EnterTree()
  93. {
  94. //随机化种子
  95. //GD.Randomize();
  96. //固定帧率
  97. Engine.MaxFps = 60;
  98. //调试绘制开关
  99. ActivityObject.IsDebug = Debug;
  100. //Engine.TimeScale = 0.2f;
  101.  
  102. ImageCanvas.Init(GetTree().CurrentScene);
  103. //窗体大小改变
  104. GetWindow().SizeChanged += OnWindowSizeChanged;
  105. RefreshSubViewportSize();
  106. //初始化ui
  107. UiManager.Init();
  108. // 初始化鼠标
  109. InitCursor();
  110. //地牢管理器
  111. DungeonManager = new DungeonManager();
  112. DungeonManager.Name = "DungeonManager";
  113. SceneRoot.AddChild(DungeonManager);
  114.  
  115. MapProjectManager.Init();
  116. BottomTipsPanel.Init();
  117. //打开主菜单Ui
  118. UiManager.Open_Main();
  119. //UiManager.Open_MapEditorProject();
  120. }
  121.  
  122. public override void _Process(double delta)
  123. {
  124. var newDelta = (float)delta;
  125. InputManager.Update(newDelta);
  126. SoundManager.Update(newDelta);
  127. //协程更新
  128. if (_coroutineList != null)
  129. {
  130. ProxyCoroutineHandler.ProxyUpdateCoroutine(ref _coroutineList, newDelta);
  131. }
  132. }
  133.  
  134. /// <summary>
  135. /// 创建新的 World 对象, 相当于清理房间
  136. /// </summary>
  137. public World CreateNewWorld()
  138. {
  139. if (World != null)
  140. {
  141. ClearWorld();
  142. World.QueueFree();
  143. }
  144. World = ResourceManager.LoadAndInstantiate<World>(ResourcePath.scene_World_tscn);
  145. SceneRoot.AddChild(World);
  146. return World;
  147. }
  148.  
  149. /// <summary>
  150. /// 销毁 World 对象, 相当于清理房间
  151. /// </summary>
  152. public void DestroyWorld()
  153. {
  154. if (World != null)
  155. {
  156. ClearWorld();
  157. World.QueueFree();
  158. }
  159.  
  160. World = null;
  161. }
  162. /// <summary>
  163. /// 将 viewport 以外的全局坐标 转换成 viewport 内的全局坐标
  164. /// </summary>
  165. public Vector2 GlobalToViewPosition(Vector2 globalPos)
  166. {
  167. //return globalPos;
  168. return globalPos / PixelScale - (ViewportSize / 2) + GameCamera.Main.GlobalPosition;
  169. }
  170.  
  171. /// <summary>
  172. /// 将 viewport 以内的全局坐标 转换成 viewport 外的全局坐标
  173. /// </summary>
  174. public Vector2 ViewToGlobalPosition(Vector2 viewPos)
  175. {
  176. // 3.5写法
  177. //return (viewPos - GameCamera.Main.GlobalPosition + (GameConfig.ViewportSize / 2)) * GameConfig.WindowScale - GameCamera.Main.SubPixelPosition;
  178. return (viewPos - (GameCamera.Main.GlobalPosition + GameCamera.Main.Offset) + (ViewportSize / 2)) * PixelScale;
  179. }
  180. public long StartCoroutine(IEnumerator able)
  181. {
  182. return ProxyCoroutineHandler.ProxyStartCoroutine(ref _coroutineList, able);
  183. }
  184. public void StopCoroutine(long coroutineId)
  185. {
  186. ProxyCoroutineHandler.ProxyStopCoroutine(ref _coroutineList, coroutineId);
  187. }
  188. public void StopAllCoroutine()
  189. {
  190. ProxyCoroutineHandler.ProxyStopAllCoroutine(ref _coroutineList);
  191. }
  192.  
  193. //初始化房间配置
  194. private void InitRoomConfig()
  195. {
  196. //加载房间配置信息
  197. var asText = ResourceManager.LoadText("res://resource/map/tileMaps/GroupConfig.json");
  198. RoomConfig = JsonSerializer.Deserialize<Dictionary<string, DungeonRoomGroup>>(asText);
  199.  
  200. //初始化RoomConfigMap
  201. //RoomConfigMap = new Dictionary<string, DungeonRoomSplit>();
  202. //加载流程更改
  203. // foreach (var dungeonRoomGroup in RoomConfig)
  204. // {
  205. // foreach (var dungeonRoomType in Enum.GetValues<DungeonRoomType>())
  206. // {
  207. // foreach (var dungeonRoomSplit in dungeonRoomGroup.Value.GetRoomList(dungeonRoomType))
  208. // {
  209. // RoomConfigMap.Add(dungeonRoomSplit.ScenePath, dungeonRoomSplit);
  210. // }
  211. // }
  212. // }
  213. }
  214.  
  215. //窗体大小改变
  216. private void OnWindowSizeChanged()
  217. {
  218. var size = GetWindow().Size;
  219. ViewportSize = size / PixelScale;
  220. RefreshSubViewportSize();
  221. }
  222. //刷新视窗大小
  223. private void RefreshSubViewportSize()
  224. {
  225. var s = new Vector2I((int)ViewportSize.X, (int)ViewportSize.Y);
  226. s.X = s.X / 2 * 2;
  227. s.Y = s.Y / 2 * 2;
  228. SubViewport.Size = s;
  229. SubViewportContainer.Scale = new Vector2(PixelScale, PixelScale);
  230. SubViewportContainer.Size = s;
  231. }
  232.  
  233. //初始化鼠标
  234. private void InitCursor()
  235. {
  236. Cursor = ResourceManager.LoadAndInstantiate<Cursor>(ResourcePath.prefab_Cursor_tscn);
  237. var cursorLayer = new CanvasLayer();
  238. cursorLayer.Name = "CursorLayer";
  239. cursorLayer.Layer = UiManager.GetUiLayer(UiLayer.Pop).Layer + 10;
  240. AddChild(cursorLayer);
  241. cursorLayer.AddChild(Cursor);
  242. }
  243.  
  244. //清理世界
  245. private void ClearWorld()
  246. {
  247. var childCount = World.NormalLayer.GetChildCount();
  248. for (var i = 0; i < childCount; i++)
  249. {
  250. var c = World.NormalLayer.GetChild(i);
  251. if (c is IDestroy destroy)
  252. {
  253. destroy.Destroy();
  254. }
  255. }
  256. childCount = World.YSortLayer.GetChildCount();
  257. for (var i = 0; i < childCount; i++)
  258. {
  259. var c = World.YSortLayer.GetChild(i);
  260. if (c is IDestroy destroy)
  261. {
  262. destroy.Destroy();
  263. }
  264. }
  265. }
  266. }