Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / GameApplication.cs
@小李xl 小李xl on 12 Nov 2022 2 KB 添加敌人视野
  1.  
  2. using Godot;
  3.  
  4. public class GameApplication : Node2D
  5. {
  6. public static GameApplication Instance { get; private set; }
  7.  
  8. [Export] public PackedScene CursorPack;
  9.  
  10. [Export] public NodePath RoomPath;
  11.  
  12. [Export] public NodePath ViewportPath;
  13.  
  14. [Export] public NodePath ViewportContainerPath;
  15.  
  16. [Export] public NodePath UiPath;
  17.  
  18. [Export] public NodePath GlobalNodeRootPath;
  19.  
  20. [Export] public Font Font;
  21. /// <summary>
  22. /// 鼠标指针
  23. /// </summary>
  24. public Cursor Cursor { get; private set; }
  25.  
  26. /// <summary>
  27. /// 游戏房间
  28. /// </summary>
  29. public RoomManager Room { get; private set; }
  30.  
  31. /// <summary>
  32. /// 游戏渲染视口
  33. /// </summary>
  34. public Viewport Viewport { get; private set; }
  35.  
  36. /// <summary>
  37. /// ViewportContainer 组件
  38. /// </summary>
  39. public ViewportContainer ViewportContainer { get; private set; }
  40.  
  41. /// <summary>
  42. /// 游戏ui对象
  43. /// </summary>
  44. public RoomUI Ui { get; private set; }
  45.  
  46. /// <summary>
  47. /// 全局根节点
  48. /// </summary>
  49. public Node2D GlobalNodeRoot { get; private set; }
  50.  
  51. public GameApplication()
  52. {
  53. Instance = this;
  54. }
  55.  
  56. public override void _EnterTree()
  57. {
  58. GlobalNodeRoot = GetNode<Node2D>(GlobalNodeRootPath);
  59. // 初始化鼠标
  60. Cursor = CursorPack.Instance<Cursor>();
  61.  
  62. Room = GetNode<RoomManager>(RoomPath);
  63. Viewport = GetNode<Viewport>(ViewportPath);
  64. ViewportContainer = GetNode<ViewportContainer>(ViewportContainerPath);
  65. Ui = GetNode<RoomUI>(UiPath);
  66.  
  67. Ui.AddChild(Cursor);
  68. }
  69.  
  70. /// <summary>
  71. /// 将 viewport 以外的全局坐标 转换成 viewport 内的全局坐标
  72. /// </summary>
  73. public Vector2 GlobalToViewPosition(Vector2 globalPos)
  74. {
  75. return globalPos / GameConfig.WindowScale - (GameConfig.ViewportSize / 2) + GameCamera.Main.GlobalPosition;
  76. }
  77.  
  78. /// <summary>
  79. /// 将 viewport 以内的全局坐标 转换成 viewport 外的全局坐标
  80. /// </summary>
  81. public Vector2 ViewToGlobalPosition(Vector2 viewPos)
  82. {
  83. return (viewPos - GameCamera.Main.GlobalPosition + (GameConfig.ViewportSize / 2)) * GameConfig.WindowScale - GameCamera.Main.SubPixelPosition;
  84. }
  85. }