-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChartManager.cs
More file actions
292 lines (254 loc) · 9.43 KB
/
ChartManager.cs
File metadata and controls
292 lines (254 loc) · 9.43 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
using System;
using System.Collections.Generic;
using System.Linq;
using FunkEngine;
using Godot;
/**<summary>Manager for handling the visual aspects of a battle. Placing visual NoteArrows, looping visuals, and combo text.</summary>
*/
public partial class ChartManager : SubViewportContainer
{
[Export]
public InputHandler IH;
[Export]
public CanvasGroup ChartLoopables;
private Node _arrowGroup;
private readonly List<NoteArrow> _arrowPool = new();
private readonly List<HoldArrow> _holdPool = new();
private readonly HoldArrow[] _currentHolds = new HoldArrow[4];
private readonly List<NoteArrow>[] _queuedArrows = { new(), new(), new(), new() };
private double _chartLength = 2500; //Play with this
#region Initialization
public override void _Ready()
{
_arrowGroup = ChartLoopables.GetNode<Node>("ArrowGroup");
IH.Connect(nameof(InputHandler.NotePressed), new Callable(this, nameof(OnNotePressed)));
IH.Connect(nameof(InputHandler.NoteReleased), new Callable(this, nameof(OnNoteReleased)));
}
public override void _Process(double delta)
{
if (!BattleDirector.AutoPlay)
return;
for (int dir = 0; dir < 4; dir++)
{
if (
_currentHolds[dir] != null
&& _currentHolds[dir].EndBeat - TimeKeeper.LastBeat < new Beat(0.1)
)
{
OnNoteReleased((ArrowType)dir);
IH.Arrows[dir].Node.SetPressed(false);
continue;
}
foreach (NoteArrow note in _queuedArrows[dir])
{
if (!note.IsHit && note.Beat - TimeKeeper.LastBeat < new Beat(0.1))
{
OnNotePressed((ArrowType)dir);
IH.Arrows[dir].Node.SetPressed(true);
if (note is not HoldArrow)
{
int capDir = dir;
Callable
.From(() =>
{
OnNoteReleased((ArrowType)capDir);
IH.Arrows[capDir].Node.SetPressed(false);
})
.CallDeferred();
}
}
}
}
}
private bool _initialized;
public void Initialize(NoteChart songData, double songLen)
{
if (_initialized)
return;
TimeKeeper.LoopsPerSong = songData.NumLoops;
TimeKeeper.SongLength = songLen;
double loopLen = songLen / songData.NumLoops;
if (songData.SongSpeed > 0)
_chartLength =
songData.SongSpeed * loopLen * StageProducer.PlayerStats.ChartSpeedMultiplier;
//99% sure chart length can never be less than (chart viewport width) * 2,
//otherwise there isn't room for things to loop from off and on screen
_chartLength = Math.Max(
loopLen * Math.Ceiling(Size.X * 2 / loopLen),
//Also minimize rounding point imprecision, improvement is qualitative
loopLen * Math.Floor(_chartLength / loopLen)
);
TimeKeeper.ChartWidth = _chartLength;
TimeKeeper.Bpm = songData.Bpm;
_initialized = true;
}
public Tween ArrowTween;
public void BeginTweens()
{
if (ArrowTween != null)
ArrowTween.Kill();
//This could be good as a function to call on something, to have many things animated to the beat.
ArrowTween = CreateTween();
ArrowTween
.TweenMethod(
Callable.From((Vector2 scale) => TweenArrows(scale)),
Vector2.One * .8f,
Vector2.One,
60f / TimeKeeper.Bpm / 2
)
.SetEase(Tween.EaseType.Out)
.SetTrans(Tween.TransitionType.Elastic);
ArrowTween.TweenMethod(
Callable.From((Vector2 scale) => TweenArrows(scale)),
Vector2.One,
Vector2.One * .8f,
60f / TimeKeeper.Bpm / 2
);
ArrowTween.SetLoops().Play();
}
private void TweenArrows(Vector2 scale)
{
foreach (var node in _arrowGroup.GetChildren())
{
NoteArrow arrow = (NoteArrow)node;
arrow.Scale = scale;
}
}
#endregion
#region NoteArrow Creation
public void AddNoteArrow(ArrowData arrowData, bool preHit = false)
{
bool isHold = arrowData.Length > 0;
NoteArrow noteArrow;
if (isHold)
noteArrow = _holdPool.Count == 0 ? InstantiateNewArrow(true) : DePoolArrow(true);
else
noteArrow = _arrowPool.Count == 0 ? InstantiateNewArrow() : DePoolArrow();
noteArrow.Init(
IH.Arrows[(int)arrowData.Type],
arrowData,
TimeKeeper.GetTimeOfBeat(arrowData.Beat)
);
if (arrowData.NoteRef.IsPlayerNote())
noteArrow.SelfModulate = PlayerPuppet.NoteColor;
if (preHit)
noteArrow.NoteHit();
}
private NoteArrow InstantiateNewArrow(bool isHold = false)
{
string path = isHold ? HoldArrow.LoadPath : NoteArrow.LoadPath;
NoteArrow result = ResourceLoader.Load<PackedScene>(path).Instantiate<NoteArrow>();
result.Missed += OnArrowMissed;
result.QueueForHit += OnArrowHittable;
result.QueueForPool += PoolArrow;
_arrowGroup.AddChild(result);
return result;
}
private NoteArrow DePoolArrow(bool isHold = false)
{
NoteArrow res;
if (isHold)
{
res = _holdPool[0];
_holdPool.RemoveAt(0);
}
else
{
res = _arrowPool[0];
_arrowPool.RemoveAt(0);
}
res.Recycle();
res.SelfModulate = Colors.White;
return res;
}
private void PoolArrow(NoteArrow noteArrow)
{
int index = _queuedArrows[(int)noteArrow.Type].IndexOf(noteArrow);
if (index != -1)
_queuedArrows[(int)noteArrow.Type].RemoveAt(index);
noteArrow.ProcessMode = ProcessModeEnum.Disabled;
noteArrow.Visible = false;
if (noteArrow is HoldArrow holdArrow)
_holdPool.Add(holdArrow);
else
_arrowPool.Add(noteArrow);
}
#endregion
#region Input Handling
public delegate void InputEventHandler(ArrowData data);
public event InputEventHandler ArrowFromInput;
public void OnNotePressed(ArrowType type)
{
//No beat zero, also if there is a current hold, don't handle a re input
if (TimeKeeper.LastBeat.CompareTo(Beat.Zero) == 0 || _currentHolds[(int)type] != null)
return;
ArrowFromInput?.Invoke(GetArrowFromInput(type));
}
public void OnNoteReleased(ArrowType type)
{
if (_currentHolds[(int)type] == null)
return;
HandleRelease(type);
}
private void HandleRelease(ArrowType type)
{
HoldArrow hold = _currentHolds[(int)type];
hold.NoteRelease();
_currentHolds[(int)type] = null;
ArrowData incrData = hold.Data;
ArrowFromInput?.Invoke(incrData.BeatFromLength());
}
private void OnArrowHittable(NoteArrow noteArrow)
{
_queuedArrows[(int)noteArrow.Type].Add(noteArrow);
}
private void OnArrowMissed(NoteArrow noteArrow)
{
noteArrow.NoteHit();
if (noteArrow is HoldArrow)
_currentHolds[(int)noteArrow.Type] = null;
ArrowFromInput?.Invoke(noteArrow.Data);
}
#endregion
#region Determine Arrow From Input
//TODO: Breakup and simplify where possible
private ArrowData GetArrowFromInput(ArrowType type)
{
ArrowData placeable = new ArrowData(type, TimeKeeper.LastBeat.RoundBeat(), null);
if (_queuedArrows[(int)type].Count == 0)
return placeable; //Empty return null, place note action
List<NoteArrow> activeArrows = _queuedArrows[(int)type]
.Where(arrow =>
!arrow.IsHit
&& Math.Abs((arrow.Beat - TimeKeeper.LastBeat).BeatPos) <= Note.TimingMax
)
.OrderBy(arrow => Math.Abs((arrow.Beat - TimeKeeper.LastBeat).BeatPos)) //Sort by closest to cur beat
.ToList();
if (activeArrows.Count != 0) //There is an active note in hittable range activate it and pass it
{
activeArrows[0].NoteHit();
if (activeArrows[0] is HoldArrow holdArrow) //Best active arrow is hold
_currentHolds[(int)type] = holdArrow;
return activeArrows[0].Data;
}
int index = _queuedArrows[(int)type]
.FindIndex(arrow => arrow.IsInRange(TimeKeeper.LastBeat));
if (index != -1) //There is an inactive note in the whole beat, pass it something so no new note is placed
return ArrowData.Placeholder;
return placeable; //No truly hittable notes, and no notes in current beat
}
#endregion
public void ComboText(Timing timed, ArrowType arrow, int currentCombo)
{
TextParticle newText = new TextParticle();
AddChild(newText);
newText.Position = IH.Arrows[(int)arrow].Node.Position - newText.Size / 2;
if (BattleDirector.VerticalScroll)
{
newText.RotationDegrees += 90f;
newText.Position += Vector2.Right * 70;
}
IH.FeedbackEffect(arrow, timed);
newText.Text = Tr("BATTLE_ROOM_" + timed.ToString().ToUpper()) + $" {currentCombo}";
}
}