using Godot; using System; public class Main : Node { [Export] public PackedScene mob; private int _score; // We use 'System.Random' as an alternative to GDScript's random methods. private Random _random = new Random(); // We'll use this later because C# doesn't support GDScript's randi(). private float RandRange(float min, float max) { return (float)_random.NextDouble() * (max - min) + min; } public void GameOver() { GetNode("MobTimer").Stop(); GetNode("ScoreTimer").Stop(); GetNode("HUD").ShowGameOver(); GetNode("Music").Stop(); GetNode("DeathSound").Play(); } public void NewGame() { _score = 0; var player = GetNode("Player"); var startPosition = GetNode("StartPosition"); player.Start(startPosition.Position); GetNode("StartTimer").Start(); var hud = GetNode("HUD"); hud.UpdateScore(_score); hud.ShowMessage("Get Ready!"); GetNode("Music").Play(); } public void OnStartTimerTimeout() { GetNode("MobTimer").Start(); GetNode("ScoreTimer").Start(); } public void OnScoreTimerTimeout() { _score++; GetNode("HUD").UpdateScore(_score); } public void OnMobTimerTimeout() { // Note: Normally it is best to use explicit types rather than the var keyword. // However, var is acceptable to use here because the types are obviously // PathFollow2D and RigidBody2D, since they appear later on the line. // Choose a random location on Path2D. var mobSpawnLocation = GetNode("MobPath/MobSpawnLocation"); mobSpawnLocation.SetOffset(_random.Next()); // Create a Mob instance and add it to the scene. var mobInstance = (RigidBody2D)mob.Instance(); AddChild(mobInstance); // Set the mob's direction perpendicular to the path direction. float direction = mobSpawnLocation.Rotation + Mathf.Pi / 2; // Set the mob's position to a random location. mobInstance.Position = mobSpawnLocation.Position; // Add some randomness to the direction. direction += RandRange(-Mathf.Pi / 4, Mathf.Pi / 4); mobInstance.Rotation = direction; // Choose the velocity. mobInstance.SetLinearVelocity(new Vector2(RandRange(150f, 250f), 0).Rotated(direction)); GetNode("HUD").Connect("StartGame", mobInstance, "OnStartGame"); } }