Main.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using Godot;
  2. public partial class Main : Node
  3. {
  4. #pragma warning disable 649
  5. // We assign this in the editor, so we don't need the warning about not being assigned.
  6. [Export]
  7. public PackedScene MobScene { get; set; }
  8. #pragma warning restore 649
  9. private int _score;
  10. public void GameOver()
  11. {
  12. GetNode<Timer>("MobTimer").Stop();
  13. GetNode<Timer>("ScoreTimer").Stop();
  14. GetNode<HUD>("HUD").ShowGameOver();
  15. GetNode<AudioStreamPlayer>("Music").Stop();
  16. GetNode<AudioStreamPlayer>("DeathSound").Play();
  17. }
  18. public void NewGame()
  19. {
  20. // Note that for calling Godot-provided methods with strings,
  21. // we have to use the original Godot snake_case name.
  22. GetTree().CallGroup("mobs", Node.MethodName.QueueFree);
  23. _score = 0;
  24. var player = GetNode<Player>("Player");
  25. var startPosition = GetNode<Marker2D>("StartPosition");
  26. player.Start(startPosition.Position);
  27. GetNode<Timer>("StartTimer").Start();
  28. var hud = GetNode<HUD>("HUD");
  29. hud.UpdateScore(_score);
  30. hud.ShowMessage("Get Ready!");
  31. GetNode<AudioStreamPlayer>("Music").Play();
  32. }
  33. private void OnStartTimerTimeout()
  34. {
  35. GetNode<Timer>("MobTimer").Start();
  36. GetNode<Timer>("ScoreTimer").Start();
  37. }
  38. private void OnScoreTimerTimeout()
  39. {
  40. _score++;
  41. GetNode<HUD>("HUD").UpdateScore(_score);
  42. }
  43. // We also specified this function name in PascalCase in the editor's connection window.
  44. private void OnMobTimerTimeout()
  45. {
  46. // Note: Normally it is best to use explicit types rather than the `var`
  47. // keyword. However, var is acceptable to use here because the types are
  48. // obviously Mob and PathFollow2D, since they appear later on the line.
  49. if (MobScene is null)
  50. {
  51. GD.PrintErr("Mob scene is not set. You need to set it in the inspector.");
  52. return;
  53. }
  54. // Create a new instance of the Mob scene.
  55. Mob mob = MobScene.Instantiate<Mob>();
  56. // Choose a random location on Path2D.
  57. var mobSpawnLocation = GetNode<PathFollow2D>("MobPath/MobSpawnLocation");
  58. mobSpawnLocation.ProgressRatio = GD.Randf();
  59. // Set the mob's direction perpendicular to the path direction.
  60. float direction = mobSpawnLocation.Rotation + Mathf.Pi / 2;
  61. // Set the mob's position to a random location.
  62. mob.Position = mobSpawnLocation.Position;
  63. // Add some randomness to the direction.
  64. direction += (float)GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
  65. mob.Rotation = direction;
  66. // Choose the velocity for the mob.
  67. var velocity = new Vector2((float)GD.RandRange(150.0, 250.0), 0);
  68. mob.LinearVelocity = velocity.Rotated(direction);
  69. // Spawn the mob by adding it to the Main scene.
  70. AddChild(mob);
  71. }
  72. }