Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / GameApplication.cs
@lijincheng lijincheng on 13 Feb 2023 2 KB 更改工程结构
  1.  
  2. using Godot;
  3.  
  4. public partial class GameApplication : Node2D
  5. {
  6. public static GameApplication Instance { get; private set; }
  7.  
  8. /// <summary>
  9. /// 是否开启调试
  10. /// </summary>
  11. [Export] public bool Debug = false;
  12.  
  13. [Export] public PackedScene CursorPack;
  14.  
  15. [Export] public NodePath RoomPath;
  16.  
  17. [Export] public NodePath ViewportPath;
  18.  
  19. [Export] public NodePath ViewportContainerPath;
  20.  
  21. [Export] public NodePath UiPath;
  22.  
  23. [Export] public NodePath GlobalNodeRootPath;
  24.  
  25. [Export] public Font Font;
  26. /// <summary>
  27. /// 鼠标指针
  28. /// </summary>
  29. public Cursor Cursor { get; private set; }
  30.  
  31. /// <summary>
  32. /// 游戏房间
  33. /// </summary>
  34. public RoomManager RoomManager { get; private set; }
  35.  
  36. /// <summary>
  37. /// 游戏渲染视口
  38. /// </summary>
  39. public SubViewport SubViewport { get; private set; }
  40.  
  41. /// <summary>
  42. /// SubViewportContainer 组件
  43. /// </summary>
  44. public SubViewportContainer SubViewportContainer { get; private set; }
  45.  
  46. /// <summary>
  47. /// 游戏ui对象
  48. /// </summary>
  49. public RoomUI Ui { get; private set; }
  50.  
  51. /// <summary>
  52. /// 全局根节点
  53. /// </summary>
  54. public Node2D GlobalNodeRoot { get; private set; }
  55.  
  56. public GameApplication()
  57. {
  58. Instance = this;
  59. //扫描并注册当前程序集下的武器
  60. WeaponManager.RegisterWeaponFromAssembly(GetType().Assembly);
  61. }
  62. public override void _EnterTree()
  63. {
  64. //随机化种子
  65. GD.Randomize();
  66. //固定帧率
  67. Engine.MaxFps = 60;
  68. //调试绘制开关
  69. ActivityObject.IsDebug = Debug;
  70. //Engine.TimeScale = 0.3f;
  71.  
  72. GlobalNodeRoot = GetNode<Node2D>(GlobalNodeRootPath);
  73. // 初始化鼠标
  74. Input.MouseMode = Input.MouseModeEnum.Hidden;
  75. Cursor = CursorPack.Instantiate<Cursor>();
  76.  
  77. RoomManager = GetNode<RoomManager>(RoomPath);
  78. SubViewport = GetNode<SubViewport>(ViewportPath);
  79. SubViewportContainer = GetNode<SubViewportContainer>(ViewportContainerPath);
  80. Ui = GetNode<RoomUI>(UiPath);
  81.  
  82. Ui.AddChild(Cursor);
  83. }
  84.  
  85. public override void _Process(double delta)
  86. {
  87. InputManager.Update((float)delta);
  88. }
  89.  
  90. /// <summary>
  91. /// 将 viewport 以外的全局坐标 转换成 viewport 内的全局坐标
  92. /// </summary>
  93. public Vector2 GlobalToViewPosition(Vector2 globalPos)
  94. {
  95. //return globalPos;
  96. return globalPos / GameConfig.WindowScale - (GameConfig.ViewportSize / 2) + GameCamera.Main.GlobalPosition;
  97. }
  98.  
  99. /// <summary>
  100. /// 将 viewport 以内的全局坐标 转换成 viewport 外的全局坐标
  101. /// </summary>
  102. public Vector2 ViewToGlobalPosition(Vector2 viewPos)
  103. {
  104. //return viewPos;
  105. return (viewPos - GameCamera.Main.GlobalPosition + (GameConfig.ViewportSize / 2)) * GameConfig.WindowScale - GameCamera.Main.SubPixelPosition;
  106. }
  107. }