Newer
Older
DungeonShooting / DungeonShooting_Godot / src / test / TestNavigation2.cs
@小李xl 小李xl on 1 Mar 2023 1 KB 测试房间门遮挡效果
  1. using Godot;
  2.  
  3. public partial class TestNavigation2 : Node2D
  4. {
  5. private Node2D _navigation2D;
  6.  
  7.  
  8. private Sprite2D _enemy;
  9. private NavigationAgent2D _navigationAgent2D;
  10.  
  11. public override void _Ready()
  12. {
  13. _navigation2D = GetNode<Node2D>("Node2D");
  14. _enemy = _navigation2D.GetNode<Sprite2D>("Enemy");
  15. _navigationAgent2D = _enemy.GetNode<NavigationAgent2D>("NavigationAgent2D");
  16.  
  17. _navigationAgent2D.TargetPosition = GetGlobalMousePosition();
  18.  
  19. }
  20.  
  21. public override void _PhysicsProcess(double delta)
  22. {
  23. if (_navigationAgent2D.IsNavigationFinished())
  24. {
  25. return;
  26. }
  27. var pos = _navigationAgent2D.GetNextPathPosition();
  28. _enemy.GlobalPosition = _enemy.GlobalPosition.MoveToward(pos, 400 * (float)delta);
  29.  
  30. }
  31.  
  32. public override void _Process(double delta)
  33. {
  34. QueueRedraw();
  35. }
  36.  
  37. public override void _Draw()
  38. {
  39. var points = _navigationAgent2D.GetCurrentNavigationPath();
  40. if (points != null && points.Length >= 2)
  41. {
  42. DrawPolyline(points, Colors.Red);
  43. //DrawMultiline(points, Colors.Red);
  44. }
  45. }
  46.  
  47. private void _on_Timer_timeout()
  48. {
  49. var target = GetGlobalMousePosition();
  50. _navigationAgent2D.TargetPosition = target;
  51. }
  52. }