-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameScene.cs
86 lines (70 loc) · 2.37 KB
/
GameScene.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
using Godot;
using System;
public class GameScene : Node2D
{
enum GameState { Loading, Running, GameOver }
private GameState state;
private PackedScene enemyClass;
private Player player;
private Camera2D cam;
private const int BASE_ENEMIES = 10;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
state = GameState.Loading;
enemyClass = ResourceLoader.Load<PackedScene>("res://Enemy.tscn");
player = (Player)ResourceLoader.Load<PackedScene>("res://Player.tscn").Instance();
player.Position = new Vector2(300, 720/2);
cam = new Camera2D();
cam.Position = new Vector2(360, cam.Position.y);
cam.MakeCurrent();
player.AddChild(cam);
AddChild(player);
((Label)GetNode("HUD/Stage")).Text = "Stage " + globals.currentStage;
SpawnEnemies();
state = GameState.Running;
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(float delta)
{
((Label)GetNode("HUD/Kills")).Text = "Kills: " + globals.kills;
}
public override void _Input(InputEvent @event) {
if (state == GameState.GameOver && @event.IsAction("ui_accept")) {
GetTree().ChangeScene("TitleScene.tscn");
}
}
public void _on_Area2D_area_entered(Area2D area) {
if (area.GetCollisionLayerBit(4) && state == GameState.Running) {
//Trigger next stage
player.vertVel = 0;
globals.currentStage += 1;
GetTree().ReloadCurrentScene();
}
}
public void PlayerDied() {
foreach (Node child in player.GetChildren()) {
child.QueueFree();
}
RemoveChild(player);
((Label)GetNode("HUD/Stage")).Text = "Game Over";
((Control)GetNode("HUD/Stage")).RectPosition = new Vector2(1280/2, 720/2);
}
public void SpawnEnemy(int x, int y) {
Enemy enemy = (Enemy)enemyClass.Instance();
enemy.Position = new Vector2(x, y);
AddChild(enemy);
}
public void SpawnEnemies() {
Random rng = new Random();
for (int i = 0; i < BASE_ENEMIES + globals.currentStage; ++i) {
SpawnEnemy(700 + rng.Next(0,5000), rng.Next((int)GetViewportRect().Size.y));
}
}
public void SpawnBossWave() {
Random rng = new Random();
for (int i = 0; i < BASE_ENEMIES; ++i) {
SpawnEnemy(3800 + rng.Next(0,220), rng.Next((int)GetViewportRect().Size.y));
}
}
}