Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / GameApplication.cs
  1.  
  2. using Godot;
  3.  
  4. public 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 Room { get; private set; }
  35.  
  36. /// <summary>
  37. /// 游戏渲染视口
  38. /// </summary>
  39. public Viewport Viewport { get; private set; }
  40.  
  41. /// <summary>
  42. /// ViewportContainer 组件
  43. /// </summary>
  44. public ViewportContainer ViewportContainer { 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.  
  61. public override void _EnterTree()
  62. {
  63. GD.Randomize();
  64. ActivityObject.IsDebug = Debug;
  65.  
  66. GlobalNodeRoot = GetNode<Node2D>(GlobalNodeRootPath);
  67. // 初始化鼠标
  68. Cursor = CursorPack.Instance<Cursor>();
  69.  
  70. Room = GetNode<RoomManager>(RoomPath);
  71. Viewport = GetNode<Viewport>(ViewportPath);
  72. ViewportContainer = GetNode<ViewportContainer>(ViewportContainerPath);
  73. Ui = GetNode<RoomUI>(UiPath);
  74.  
  75. Ui.AddChild(Cursor);
  76. }
  77.  
  78. /// <summary>
  79. /// 将 viewport 以外的全局坐标 转换成 viewport 内的全局坐标
  80. /// </summary>
  81. public Vector2 GlobalToViewPosition(Vector2 globalPos)
  82. {
  83. return globalPos / GameConfig.WindowScale - (GameConfig.ViewportSize / 2) + GameCamera.Main.GlobalPosition;
  84. }
  85.  
  86. /// <summary>
  87. /// 将 viewport 以内的全局坐标 转换成 viewport 外的全局坐标
  88. /// </summary>
  89. public Vector2 ViewToGlobalPosition(Vector2 viewPos)
  90. {
  91. return (viewPos - GameCamera.Main.GlobalPosition + (GameConfig.ViewportSize / 2)) * GameConfig.WindowScale - GameCamera.Main.SubPixelPosition;
  92. }
  93. }