-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.cs
121 lines (104 loc) · 2.83 KB
/
Main.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using Godot;
using System;
public partial class Main : Node
{
[Export] public PackedScene MobScene { get; set; }
private int _score;
private AudioStreamPlayer _music;
private AudioStreamPlayer _deathSound;
private HUD _hud;
private Timer _mobTimer;
private Timer _scoreTimer;
private Timer _startTimer;
private Player _player;
private Marker2D _startPosition;
private PathFollow2D _mobSpawnLocation;
public override void _Ready()
{
_music = GetNode<AudioStreamPlayer>("Music");
_deathSound = GetNode<AudioStreamPlayer>("DeathSound");
_hud = GetNode<HUD>("HUD");
_mobTimer = GetNode<Timer>("MobTimer");
_scoreTimer = GetNode<Timer>("ScoreTimer");
_startTimer = GetNode<Timer>("StartTimer");
_player = GetNode<Player>("Player");
_startPosition = GetNode<Marker2D>("StartPosition");
_mobSpawnLocation = GetNode<PathFollow2D>("MobPath/MobSpawnLocation");
}
public void NewGame()
{
ResetGame();
StartMusic();
DisplayStartMessage();
PreparePlayer();
_startTimer.Start();
}
private void ResetGame()
{
_score = 0;
GetTree().CallGroup("mobs", Node.MethodName.QueueFree);
}
private void StartMusic()
{
_music.Play();
}
private void DisplayStartMessage()
{
_hud.UpdateScore(_score);
_hud.ShowMessage("Get Ready!");
}
private void PreparePlayer()
{
_player.Start(_startPosition.Position);
}
public void GameOver()
{
StopGame();
_hud.ShowGameOver();
}
private void StopGame()
{
_music.Stop();
_deathSound.Play();
_mobTimer.Stop();
_scoreTimer.Stop();
}
private void OnStartTimerTimeout()
{
_mobTimer.Start();
_scoreTimer.Start();
}
private void OnScoreTimerTimeout()
{
_score++;
_hud.UpdateScore(_score);
}
private void OnMobTimerTimeout()
{
SpawnMob();
}
private void SpawnMob()
{
Mob mob = MobScene.Instantiate<Mob>();
SetMobSpawnLocation(mob);
SetMobDirection(mob);
SetMobVelocity(mob);
AddChild(mob);
}
private void SetMobSpawnLocation(Mob mob)
{
_mobSpawnLocation.ProgressRatio = GD.Randf();
mob.Position = _mobSpawnLocation.Position;
}
private void SetMobDirection(Mob mob)
{
float direction = _mobSpawnLocation.Rotation + Mathf.Pi / 2;
direction += (float)GD.RandRange(-Mathf.Pi / 4, Mathf.Pi / 4);
mob.Rotation = direction;
}
private void SetMobVelocity(Mob mob)
{
var velocity = new Vector2((float)GD.RandRange(150.0, 250.0), 0);
mob.LinearVelocity = velocity.Rotated(mob.Rotation);
}
}