-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunkEngineNameSpace.cs
More file actions
440 lines (383 loc) · 10.6 KB
/
FunkEngineNameSpace.cs
File metadata and controls
440 lines (383 loc) · 10.6 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
using System;
using System.Collections.Generic;
using System.Linq;
using FunkEngine.Classes.MidiMaestro;
using Godot;
namespace FunkEngine;
#region Structs
/**
* <summary>SongData: Basic information defining the statistics of an in-battle song.</summary>
*/
public struct SongData
{
public int Bpm;
public double SongLength;
public int NumLoops;
}
/**
* <summary>ArrowData: Data representing the necessary information for each arrow checker.</summary>
*/
public struct CheckerData
{
public Color Color;
public string Key;
public NoteChecker Node;
public ArrowType Type;
}
/**
* <summary>BattleConfig: Necessary data for a battle.</summary>
*/
public struct BattleConfig
{
public Stages RoomType;
public MapGrid.Room BattleRoom;
public string[] EnemyScenePath;
public SongTemplate CurSong;
}
/**
* <summary>NoteArrowData: Data To be stored and transmitted to represent a NoteArrow.</summary>
*/
public struct ArrowData : IEquatable<ArrowData>, IComparable<ArrowData>
{
public ArrowData(ArrowType type, Beat beat, Note note, double length = 0)
{
Beat = beat;
Type = type;
NoteRef = note;
Length = length;
}
public Beat Beat;
public readonly double Length; //in beats, should never be >= loop
public readonly ArrowType Type;
public readonly Note NoteRef = null;
public static ArrowData Placeholder { get; private set; } =
new(default, default, new Note(-1, ""));
public ArrowData BeatFromLength()
{
Beat += Length;
return this;
}
public bool Equals(ArrowData other)
{
return Beat.Equals(other.Beat) && Type == other.Type;
}
public override bool Equals(object obj)
{
return obj is ArrowData other && Equals(other);
}
public static bool operator ==(ArrowData left, ArrowData right)
{
return left.Equals(right);
}
public static bool operator !=(ArrowData left, ArrowData right)
{
return !(left == right);
}
public override int GetHashCode()
{
return HashCode.Combine(Beat, (int)Type);
}
public int CompareTo(ArrowData data) //Only care about beat for comparison
{
if ((int)Beat.BeatPos == (int)data.Beat.BeatPos && Beat.Loop == data.Beat.Loop)
{
if (Type == data.Type)
{
return Beat.CompareTo(data.Beat);
}
return Type.CompareTo(data.Type);
}
return Beat.CompareTo(data.Beat);
}
}
/**
* <summary>Beat: Data representing a beat and its loop num.</summary>
*/
public struct Beat : IEquatable<Beat>, IComparable<Beat>
{
public int Loop = 0;
public double BeatPos = 0;
public static readonly Beat One = new Beat(1);
public static readonly Beat Zero = new Beat(0);
public Beat(double beat)
{
Loop = (int)(beat / TimeKeeper.BeatsPerLoop);
BeatPos = beat % TimeKeeper.BeatsPerLoop;
}
public Beat(double beat, int loop)
{
Loop = loop;
BeatPos = beat % TimeKeeper.BeatsPerLoop;
}
public double GetBeatInSong()
{
return BeatPos + Loop * TimeKeeper.BeatsPerLoop % TimeKeeper.BeatsPerSong;
}
public Beat IncDecLoop(int amount)
{
Loop += amount;
return this;
}
public Beat RoundBeat()
{
BeatPos = (int)Math.Round(BeatPos); //This can technically overflow, but causes no bugs yet.
return this;
}
public override string ToString()
{
return $"Beat: {BeatPos}, Loop: {Loop}";
}
public static bool operator >(Beat beat1, Beat beat2)
{
return beat1.Loop > beat2.Loop
|| (beat1.Loop == beat2.Loop && beat1.BeatPos > beat2.BeatPos);
}
public static bool operator <(Beat beat1, Beat beat2)
{
return beat1.Loop < beat2.Loop
|| (beat1.Loop == beat2.Loop && beat1.BeatPos < beat2.BeatPos);
}
public static bool operator <=(Beat beat1, Beat beat2)
{
return beat1.Equals(beat2) || beat1 < beat2;
}
public static bool operator >=(Beat beat1, Beat beat2)
{
return beat1.Equals(beat2) || beat1 > beat2;
}
public static Beat operator +(Beat beat1, double beatInc)
{
return new Beat(beat1.BeatPos + beatInc).IncDecLoop(beat1.Loop);
}
public static Beat operator -(Beat beat1, double beatDec)
{
return new Beat(beat1.BeatPos - beatDec).IncDecLoop(beat1.Loop);
}
public static Beat operator -(Beat beat1, Beat beat2)
{
return new Beat(beat1.BeatPos - beat2.BeatPos).IncDecLoop(beat1.Loop - beat2.Loop);
}
public bool Equals(Beat other)
{
return Loop == other.Loop && BeatPos.Equals(other.BeatPos);
}
public override int GetHashCode()
{
return HashCode.Combine(Loop, BeatPos);
}
public int CompareTo(Beat other)
{
var loopComparison = Loop.CompareTo(other.Loop);
return loopComparison != 0 ? loopComparison : BeatPos.CompareTo(other.BeatPos);
}
}
#endregion
#region Enums
public enum ArrowType
{
Up = 0,
Down = 1,
Left = 2,
Right = 3,
}
public enum Timing
{
Miss = 0,
Bad = 1,
Okay = 2,
Good = 3,
Perfect = 4,
}
public enum Targetting
{
Player,
First,
All,
}
public enum BattleEffectTrigger
{
NotePlaced,
NoteHit,
SelfNoteHit,
OnPickup,
OnLoop,
OnBattleStart,
OnBattleEnd,
OnDamageInstance,
OnDamageTaken,
}
public enum Stages
{
Battle = 0,
Chest = 1,
Boss,
Quit,
Map,
Load,
Continue,
Title,
}
public enum Rarity
{
Breakfast = 5,
Common = 4,
Uncommon = 3,
Rare = 2,
Epic = 1,
Legendary = 0,
}
#endregion
/**
* <summary>MapGrid: Map as data.
* Essentially a width by height grid. Valid rooms are determined by choosing a random starting room at height: 0, and makes random walks to height: height.
* Walks go from x,y to {x/x+1/x-1},y+1</summary>
*/
public class MapGrid
{
private int[,] _map;
private Room[] _rooms;
private int _curIdx;
public Room[] GetRooms()
{
return _rooms;
}
public class Room
{
public int Idx { get; private set; }
public int[] Children { get; private set; } = [];
public int X { get; private set; }
public int Y { get; private set; }
public Stages Type { get; private set; }
public Room(int idx, int x, int y)
{
Idx = idx;
X = x;
Y = y;
}
public void SetType(Stages type)
{
Type = type;
}
public void AddChild(int newIdx)
{
if (Children.Contains(newIdx))
return;
Children = Children.Append(newIdx).ToArray();
}
}
/**
* <summary>Initializes the map with max <c>width</c>, max <c>height</c>, and with number of <c>paths</c>.</summary>
*/
public void InitMapGrid(MapLevels.MapConfig curConfig)
{
_curIdx = 0;
_rooms = [];
_map = new int[curConfig.Width, curConfig.Height]; //x,y
int startX = (curConfig.Width / 2);
_rooms = _rooms.Append(new Room(_curIdx, startX, 0)).ToArray();
_rooms[0].SetType(Stages.Battle);
_map[startX, 0] = _curIdx++;
for (int i = 0; i < curConfig.Paths; i++)
{
GeneratePath_r(startX, 0, curConfig);
}
CreateCommonChildren(curConfig.Width, curConfig.Height);
AddBossRoom(curConfig.Width, curConfig.Height);
}
/**Start at x, y, assume prev room exists. Picks new x pos within +/- 1, attaches recursively*/
private void GeneratePath_r(int x, int y, MapLevels.MapConfig curConfig)
{
int nextX = StageProducer.GlobalRng.RandiRange(
Math.Max(x - 1, 0),
Math.Min(x + 1, curConfig.Width - 1)
);
if (_map[nextX, y + 1] == 0)
{
_rooms = _rooms.Append(new Room(_curIdx, nextX, y + 1)).ToArray();
_map[nextX, y + 1] = _curIdx;
_rooms[_map[x, y]].AddChild(_curIdx++);
_rooms[^1].SetType(PickRoomType(x, y, curConfig));
}
else
{
_rooms[_map[x, y]].AddChild(_map[nextX, y + 1]);
}
if (y < curConfig.Height - 2)
{
GeneratePath_r(nextX, y + 1, curConfig);
}
}
private Stages PickRoomType(int x, int y, MapLevels.MapConfig curConfig)
{
//If the y has a set room return it.
if (curConfig.SetRooms.TryGetValue(y, out Stages result))
{
return result;
}
//Random roll for the room type.
float[] validRooms = new float[curConfig.StageOdds.Length];
curConfig.StageOdds.CopyTo(validRooms, 0);
foreach ((Stages stage, int height) in curConfig.MinHeights)
{
if (y < height)
{
validRooms[(int)stage] = 0;
}
}
int idx = (int)StageProducer.GlobalRng.RandWeighted(validRooms);
return MapLevels.MapConfig.StagsToRoll[idx];
}
//Asserts that if there is a room at the same x, but y+1 they are connected
private void CreateCommonChildren(int width, int height)
{
foreach (Room room in _rooms)
{
Vector2I curPos = new Vector2I(room.X, room.Y);
if (room.Y + 1 >= height)
continue;
if (_map[curPos.X, curPos.Y + 1] == 0)
continue;
room.AddChild(_map[curPos.X, curPos.Y + 1]);
}
}
//Adds a boss room at the end of rooms, all max height rooms connect to it.
private void AddBossRoom(int width, int height)
{
_rooms = _rooms.Append(new Room(_curIdx, width / 2, height)).ToArray();
_rooms[_curIdx].SetType(Stages.Boss);
for (int i = 0; i < width; i++) //Attach all last rooms to a boss room
{
if (_map[i, height - 1] != 0)
{
_rooms[_map[i, height - 1]].AddChild(_curIdx);
}
}
}
}
#region Interfaces
public class BattleEventArgs(BattleDirector director) : EventArgs
{
public BattleDirector BD = director;
}
/**
* <summary>A BattleDirector driven battle event. Needs an enum defined trigger.</summary>
*/
public interface IBattleEvent
{
void OnTrigger(BattleEventArgs e);
BattleEffectTrigger GetTrigger();
}
public interface IDisplayable
{
string Name { get; set; }
Texture2D Texture { get; set; }
}
public interface IFocusableMenu
{
IFocusableMenu Prev { get; set; }
void OpenMenu(IFocusableMenu parentMenu);
void PauseFocus();
void ResumeFocus();
void ReturnToPrev();
}
#endregion