Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / GameApplication.cs
@小李xl 小李xl on 30 Nov 2022 2 KB ai随机移动(基础)
  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. GlobalNodeRoot = GetNode<Node2D>(GlobalNodeRootPath);
  66. // 初始化鼠标
  67. Cursor = CursorPack.Instance<Cursor>();
  68.  
  69. Room = GetNode<RoomManager>(RoomPath);
  70. Viewport = GetNode<Viewport>(ViewportPath);
  71. ViewportContainer = GetNode<ViewportContainer>(ViewportContainerPath);
  72. Ui = GetNode<RoomUI>(UiPath);
  73.  
  74. Ui.AddChild(Cursor);
  75. }
  76.  
  77. /// <summary>
  78. /// 将 viewport 以外的全局坐标 转换成 viewport 内的全局坐标
  79. /// </summary>
  80. public Vector2 GlobalToViewPosition(Vector2 globalPos)
  81. {
  82. return globalPos / GameConfig.WindowScale - (GameConfig.ViewportSize / 2) + GameCamera.Main.GlobalPosition;
  83. }
  84.  
  85. /// <summary>
  86. /// 将 viewport 以内的全局坐标 转换成 viewport 外的全局坐标
  87. /// </summary>
  88. public Vector2 ViewToGlobalPosition(Vector2 viewPos)
  89. {
  90. return (viewPos - GameCamera.Main.GlobalPosition + (GameConfig.ViewportSize / 2)) * GameConfig.WindowScale - GameCamera.Main.SubPixelPosition;
  91. }
  92. }