123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using Godot;
- public partial class Main : Node
- {
- #pragma warning disable 649
- // We assign this in the editor, so we don't need the warning about not being assigned.
- [Export]
- public PackedScene mobScene;
- #pragma warning restore 649
- public int score;
- public override void _Ready()
- {
- GD.Randomize();
- }
- public void GameOver()
- {
- GetNode<Timer>("MobTimer").Stop();
- GetNode<Timer>("ScoreTimer").Stop();
- GetNode<HUD>("HUD").ShowGameOver();
- GetNode<AudioStreamPlayer>("Music").Stop();
- GetNode<AudioStreamPlayer>("DeathSound").Play();
- }
- public void NewGame()
- {
- // Note that for calling Godot-provided methods with strings,
- // we have to use the original Godot snake_case name.
- GetTree().CallGroup("mobs", "queue_free");
- score = 0;
- var player = GetNode<Player>("Player");
- var startPosition = GetNode<Position2D>("StartPosition");
- player.Start(startPosition.Position);
- GetNode<Timer>("StartTimer").Start();
- var hud = GetNode<HUD>("HUD");
- hud.UpdateScore(score);
- hud.ShowMessage("Get Ready!");
- GetNode<AudioStreamPlayer>("Music").Play();
- }
- public void OnStartTimerTimeout()
- {
- GetNode<Timer>("MobTimer").Start();
- GetNode<Timer>("ScoreTimer").Start();
- }
- public void OnScoreTimerTimeout()
- {
- score++;
- GetNode<HUD>("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 Mob, since they appear later on the line.
- // Choose a random location on Path2D.
- var mobSpawnLocation = GetNode<PathFollow2D>("MobPath/MobSpawnLocation");
- mobSpawnLocation.Offset = GD.Randi();
- // Create a Mob instance and add it to the scene.
- var mob = (Mob)mobScene.Instantiate();
- AddChild(mob);
- // 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.
- mob.Position = mobSpawnLocation.Position;
- // Add some randomness to the direction.
- direction += (float)GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
- mob.Rotation = direction;
- // Choose the velocity for the mob.
- var velocity = new Vector2((float)GD.RandRange(150.0, 250.0), 0);
- mob.LinearVelocity = velocity.Rotated(direction);
- }
- }
|