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(); public override void _Ready() { } // 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() { // 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)); } }