Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / GameApplication.cs
@小李xl 小李xl on 23 Nov 2023 7 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. /// <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.  
  79. //初始化配置表
  80. ExcelConfig.Init();
  81. //初始化房间配置数据
  82. InitRoomConfig();
  83. //初始化武器数据
  84. Weapon.InitWeaponAttribute();
  85. //初始化敌人数据
  86. Enemy.InitEnemyAttribute();
  87. DungeonConfig = new DungeonConfig();
  88. DungeonConfig.GroupName = RoomConfig.FirstOrDefault().Key;
  89. DungeonConfig.RoomCount = 20;
  90. }
  91. public override void _EnterTree()
  92. {
  93. //背景颜色
  94. RenderingServer.SetDefaultClearColor(new Color(0, 0, 0, 1));
  95. //随机化种子
  96. //GD.Randomize();
  97. //固定帧率
  98. Engine.MaxFps = TargetFps;
  99. //调试绘制开关
  100. ActivityObject.IsDebug = false;
  101. //Engine.TimeScale = 0.2f;
  102. //调整窗口分辨率
  103. OnWindowSizeChanged();
  104. //窗体大小改变
  105. GetWindow().SizeChanged += OnWindowSizeChanged;
  106.  
  107. ImageCanvas.Init(GetTree().CurrentScene);
  108. //初始化ui
  109. UiManager.Init();
  110. //调试Ui
  111. UiManager.Open_Debugger();
  112. // 初始化鼠标
  113. InitCursor();
  114. //地牢管理器
  115. DungeonManager = new DungeonManager();
  116. DungeonManager.Name = "DungeonManager";
  117. SceneRoot.AddChild(DungeonManager);
  118.  
  119. MapProjectManager.Init();
  120. BottomTipsPanel.Init();
  121. //打开主菜单Ui
  122. UiManager.Open_Main();
  123. //UiManager.Open_MapEditorProject();
  124. }
  125.  
  126. public override void _Process(double delta)
  127. {
  128. var newDelta = (float)delta;
  129. InputManager.Update(newDelta);
  130. SoundManager.Update(newDelta);
  131. //协程更新
  132. ProxyCoroutineHandler.ProxyUpdateCoroutine(ref _coroutineList, newDelta);
  133. }
  134.  
  135. /// <summary>
  136. /// 创建新的 World 对象, 相当于清理房间
  137. /// </summary>
  138. public World CreateNewWorld()
  139. {
  140. if (World != null)
  141. {
  142. ClearWorld();
  143. World.QueueFree();
  144. }
  145. World = ResourceManager.LoadAndInstantiate<World>(ResourcePath.scene_World_tscn);
  146. SceneRoot.AddChild(World);
  147. return World;
  148. }
  149.  
  150. /// <summary>
  151. /// 销毁 World 对象, 相当于清理房间
  152. /// </summary>
  153. public void DestroyWorld()
  154. {
  155. //销毁所有物体
  156. if (World != null)
  157. {
  158. ClearWorld();
  159. World.QueueFree();
  160. }
  161. //销毁池中所有物体
  162. ObjectPool.DisposeAllItem();
  163.  
  164. World = null;
  165. }
  166. /// <summary>
  167. /// 将 viewport 以外的全局坐标 转换成 viewport 内的全局坐标
  168. /// </summary>
  169. public Vector2 GlobalToViewPosition(Vector2 globalPos)
  170. {
  171. return globalPos / PixelScale - (ViewportSize / 2) + GameCamera.Main.GlobalPosition - GameCamera.Main.PixelOffset;
  172. }
  173.  
  174. /// <summary>
  175. /// 将 viewport 以内的全局坐标 转换成 viewport 外的全局坐标
  176. /// </summary>
  177. public Vector2 ViewToGlobalPosition(Vector2 viewPos)
  178. {
  179. return (viewPos + GameCamera.Main.PixelOffset - (GameCamera.Main.GlobalPosition + GameCamera.Main.Offset) + (ViewportSize / 2)) * PixelScale;
  180. }
  181. public long StartCoroutine(IEnumerator able)
  182. {
  183. return ProxyCoroutineHandler.ProxyStartCoroutine(ref _coroutineList, able);
  184. }
  185. public void StopCoroutine(long coroutineId)
  186. {
  187. ProxyCoroutineHandler.ProxyStopCoroutine(ref _coroutineList, coroutineId);
  188. }
  189.  
  190. public bool IsCoroutineOver(long coroutineId)
  191. {
  192. return ProxyCoroutineHandler.ProxyIsCoroutineOver(ref _coroutineList, coroutineId);
  193. }
  194.  
  195. public void StopAllCoroutine()
  196. {
  197. ProxyCoroutineHandler.ProxyStopAllCoroutine(ref _coroutineList);
  198. }
  199.  
  200. public void SetRoomConfig(Dictionary<string,DungeonRoomGroup> roomConfig)
  201. {
  202. RoomConfig = roomConfig;
  203. InitReadyRoom();
  204. }
  205.  
  206. //初始化房间配置
  207. private void InitRoomConfig()
  208. {
  209. //加载房间配置信息
  210. var asText = ResourceManager.LoadText("res://" + GameConfig.RoomTileDir + GameConfig.RoomGroupConfigFile);
  211. RoomConfig = JsonSerializer.Deserialize<Dictionary<string, DungeonRoomGroup>>(asText);
  212.  
  213. InitReadyRoom();
  214. }
  215. //初始化房间数据
  216. private void InitReadyRoom()
  217. {
  218. foreach (var dungeonRoomGroup in RoomConfig)
  219. {
  220. RemoveUnreadyRooms(dungeonRoomGroup.Value.BattleList);
  221. RemoveUnreadyRooms(dungeonRoomGroup.Value.InletList);
  222. RemoveUnreadyRooms(dungeonRoomGroup.Value.OutletList);
  223. RemoveUnreadyRooms(dungeonRoomGroup.Value.BossList);
  224. RemoveUnreadyRooms(dungeonRoomGroup.Value.ShopList);
  225. RemoveUnreadyRooms(dungeonRoomGroup.Value.RewardList);
  226. RemoveUnreadyRooms(dungeonRoomGroup.Value.EventList);
  227. }
  228. }
  229. //移除未准备好的房间
  230. private void RemoveUnreadyRooms(List<DungeonRoomSplit> roomInfos)
  231. {
  232. for (var i = 0; i < roomInfos.Count; i++)
  233. {
  234. if (roomInfos[i].ErrorType != RoomErrorType.None) //存在错误
  235. {
  236. roomInfos.RemoveAt(i);
  237. i--;
  238. }
  239. }
  240. }
  241.  
  242. //窗体大小改变
  243. private void OnWindowSizeChanged()
  244. {
  245. var size = GetWindow().Size;
  246. ViewportSize = size / PixelScale;
  247. RefreshSubViewportSize();
  248. }
  249. //刷新视窗大小
  250. private void RefreshSubViewportSize()
  251. {
  252. var s = new Vector2I((int)ViewportSize.X, (int)ViewportSize.Y);
  253. s.X = s.X / 2 * 2 + 2;
  254. s.Y = s.Y / 2 * 2 + 2;
  255. SubViewport.Size = s;
  256. SubViewportContainer.Scale = new Vector2(PixelScale, PixelScale);
  257. SubViewportContainer.Size = s;
  258. SubViewportContainer.Position = new Vector2(-PixelScale, -PixelScale);
  259. }
  260.  
  261. //初始化鼠标
  262. private void InitCursor()
  263. {
  264. Cursor = ResourceManager.LoadAndInstantiate<Cursor>(ResourcePath.prefab_Cursor_tscn);
  265. var cursorLayer = new CanvasLayer();
  266. cursorLayer.Name = "CursorLayer";
  267. cursorLayer.Layer = UiManager.GetUiLayer(UiLayer.Pop).Layer + 10;
  268. AddChild(cursorLayer);
  269. cursorLayer.AddChild(Cursor);
  270. }
  271.  
  272. //清理世界
  273. private void ClearWorld()
  274. {
  275. var childCount = World.NormalLayer.GetChildCount();
  276. for (var i = 0; i < childCount; i++)
  277. {
  278. var c = World.NormalLayer.GetChild(i);
  279. if (c is IDestroy destroy)
  280. {
  281. destroy.Destroy();
  282. }
  283. }
  284. childCount = World.YSortLayer.GetChildCount();
  285. for (var i = 0; i < childCount; i++)
  286. {
  287. var c = World.YSortLayer.GetChild(i);
  288. if (c is IDestroy destroy)
  289. {
  290. destroy.Destroy();
  291. }
  292. }
  293. }
  294. }