-
Notifications
You must be signed in to change notification settings - Fork 1
/
Formations.cs
103 lines (85 loc) · 2.85 KB
/
Formations.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
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Godot;
using static Godot.GD;
using File = Godot.File;
using Newtonsoft.Json;
namespace SpaceShooter.Application.Core
{
public class Formations
{
// formations are groups of spawn positions. Each spawn has a 1 or 0 where 1 means to spawn said enemy and 0
// is to ignore. Gives us the opportunity to make cool formations if we wanted to.
private List<List<List<int>>> _formationList = new List<List<List<int>>>();
public int CurrentColumn = 0;
public int CurrentFormation = 0;
public int XPosition = 1364;
// y positions for each enemy for each column
public int[] Positions =
{
160, 320, 480, 640
};
private string _formationDataPath = "res://Data/formations.json";
// todo: make a note talking about converting formation list into json data
public Formations()
{
LoadJson();
SelectRandomFormation();
}
public int[] Spawns()
{
var spawnCount = 0;
int[] spawns = {
0, 0, 0, 0
};
var formation = _formationList[CurrentFormation];
if(formation != null)
{
foreach(var spawn in formation)
{
spawns[spawnCount] = spawn[CurrentColumn];
++spawnCount;
}
}
return spawns;
}
public void SelectRandomFormation()
{
CurrentFormation = GameScene.Rand.Next(0, _formationList.Count);
}
public void NextColumn()
{
if(_formationList.Count == 0) return;
// we select a random column if we hit the end of the line or we append +1 and move to the
// next column
if(IsEndOfColumn(out var nextColumn))
{
CurrentColumn = 0;
SelectRandomFormation();
}
else
{
CurrentColumn = nextColumn;
}
}
public bool IsEndOfColumn(out int outColumn)
{
outColumn = CurrentColumn + 1;
var currentFormation = _formationList[CurrentFormation];
return outColumn == currentFormation.Count;
}
private void LoadJson()
{
var file = new File();
if(file.Open(_formationDataPath, (int) File.ModeFlags.Read) != Error.Ok)
{
file.Close();
throw new FileLoadException($"There was a problem loading: {_formationDataPath}");
}
var data = file.GetAsText();
file.Close();
_formationList = JsonConvert.DeserializeObject<List<List<List<int>>>>(data);
}
}
}