Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / GameApplication.cs
@小李xl 小李xl on 4 Nov 2022 1 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. public Node2D GlobalNodeRoot { get; private set; }
  47.  
  48. public GameApplication()
  49. {
  50. Instance = this;
  51. }
  52.  
  53. public override void _EnterTree()
  54. {
  55. GlobalNodeRoot = GetNode<Node2D>(GlobalNodeRootPath);
  56. // 初始化鼠标
  57. Cursor = CursorPack.Instance<Cursor>();
  58.  
  59. Room = GetNode<RoomManager>(RoomPath);
  60. Viewport = GetNode<Viewport>(ViewportPath);
  61. ViewportContainer = GetNode<ViewportContainer>(ViewportContainerPath);
  62. Ui = GetNode<RoomUI>(UiPath);
  63.  
  64. Ui.AddChild(Cursor);
  65. }
  66.  
  67. public Vector2 GlobalToViewPosition(Vector2 globalPos)
  68. {
  69. return globalPos / GameConfig.WindowScale - (GameConfig.ViewportSize / 2) + GameCamera.Main.GlobalPosition;
  70. }
  71.  
  72. public Vector2 ViewToGlobalPosition(Vector2 viewPos)
  73. {
  74. return (viewPos - GameCamera.Main.GlobalPosition + (GameConfig.ViewportSize / 2)) * GameConfig.WindowScale - GameCamera.Main.SubPixelPosition;
  75. }
  76. }