Skip to content

Commit fc51749

Browse files
committed
Refactoring
Moved enums and structs to common namespace Removed note resource class needing beat and type (moving forward note data shouldn't hold that.) Added IsNoteActive() Removed unused code and parameters Removed unnecessary textparticle packedscene
1 parent 0d77e31 commit fc51749

9 files changed

Lines changed: 106 additions & 129 deletions

File tree

Classes/Note.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using FunkEngine;
23
using Godot;
34

45
/**
@@ -7,12 +8,15 @@
78
*/
89
public partial class Note : Resource
910
{
10-
public int Beat;
11-
public NoteArrow.ArrowType Type;
11+
private string _effect;
1212

13-
public Note(NoteArrow.ArrowType type = NoteArrow.ArrowType.Up, int beat = 0)
13+
public Note(string effect = "")
1414
{
15-
Beat = beat;
16-
Type = type;
15+
_effect = effect;
16+
}
17+
18+
public string GetEffect()
19+
{
20+
return _effect;
1721
}
1822
}

Globals/FunkEngineNameSpace.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Godot;
2+
3+
namespace FunkEngine;
4+
5+
public enum ArrowType
6+
{
7+
Up = 0,
8+
Down = 1,
9+
Left = 2,
10+
Right = 3,
11+
}
12+
13+
public struct ArrowData
14+
{
15+
public Color Color;
16+
public string Key;
17+
public NoteChecker Node;
18+
public ArrowType Type;
19+
}

scenes/BattleDirector/TextParticle.tscn

Lines changed: 0 additions & 9 deletions
This file was deleted.

scenes/BattleDirector/scripts/BattleDirector.cs

Lines changed: 49 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Linq;
5+
using FunkEngine;
56
using Godot;
67

78
/**
@@ -26,13 +27,7 @@ public partial class BattleDirector : Node2D
2627
[Export]
2728
private AudioStreamPlayer Audio;
2829

29-
private double _timingInterval = .1; //secs
30-
31-
[Signal]
32-
public delegate void PlayerDamageEventHandler(int damage);
33-
34-
[Signal]
35-
public delegate void EnemyDamageEventHandler(int damage);
30+
private double _timingInterval = .1; //secs, maybe make somewhat note dependent
3631

3732
private SongData _curSong;
3833

@@ -58,7 +53,7 @@ public struct SongData
5853
private Note[] _notes = Array.Empty<Note>();
5954

6055
//Returns first note of lane without modifying lane data
61-
private Note GetNoteAt(NoteArrow.ArrowType dir, int beat)
56+
private Note GetNoteAt(ArrowType dir, int beat)
6257
{
6358
return GetNote(_laneData[(int)dir][beat]);
6459
}
@@ -69,19 +64,23 @@ private Note GetNote(NoteArrow arrow)
6964
return _notes[arrow.NoteIdx];
7065
}
7166

72-
private bool AddNoteToLane(Note note, bool isActive = true)
67+
private bool IsNoteActive(ArrowType type, int beat)
68+
{
69+
return _laneData[(int)type][beat] != null && _laneData[(int)type][beat].IsActive;
70+
}
71+
72+
private bool AddNoteToLane(ArrowType type, int beat, bool isActive = true)
7373
{
74-
note.Beat %= CM.BeatsPerLoop;
75-
//Don't add dupe notes
76-
if (note.Beat == 0 || _notes.Any(nt => nt.Type == note.Type && nt.Beat == note.Beat))
74+
beat %= CM.BeatsPerLoop;
75+
//Don't add dupe notes //Beat at 0 is too messy.
76+
if (beat == 0 || _laneData[(int)type][beat] != null)
7777
{
78-
return false; //Beat at 0 is too messy.
78+
return false;
7979
}
80-
_notes = _notes.Append(note).ToArray();
8180
//Get noteArrow from CM
82-
var arrow = CM.AddArrowToLane(note, _notes.Length - 1);
81+
var arrow = CM.AddArrowToLane(type, beat, _notes.Length - 1);
8382
arrow.IsActive = isActive;
84-
_laneData[(int)note.Type][note.Beat] = arrow;
83+
_laneData[(int)type][beat] = arrow;
8584
return true;
8685
}
8786
#endregion
@@ -92,23 +91,19 @@ private void AddExampleNotes()
9291
GD.Print(CM.BeatsPerLoop);
9392
for (int i = 1; i < 15; i++)
9493
{
95-
Note exampleNote = new Note(NoteArrow.ArrowType.Up, i * 4);
96-
AddNoteToLane(exampleNote);
94+
AddNoteToLane(ArrowType.Up, i * 4);
9795
}
9896
for (int i = 1; i < 15; i++)
9997
{
100-
Note exampleNote = new Note(NoteArrow.ArrowType.Left, 4 * i + 1);
101-
AddNoteToLane(exampleNote);
98+
AddNoteToLane(ArrowType.Left, 4 * i + 1);
10299
}
103100
for (int i = 0; i < 10; i++)
104101
{
105-
Note exampleNote = new Note(NoteArrow.ArrowType.Right, 3 * i + 32);
106-
AddNoteToLane(exampleNote);
102+
AddNoteToLane(ArrowType.Right, 3 * i + 32);
107103
}
108104
for (int i = 0; i < 3; i++)
109105
{
110-
Note exampleNote = new Note(NoteArrow.ArrowType.Down, 8 * i + 16);
111-
AddNoteToLane(exampleNote);
106+
AddNoteToLane(ArrowType.Down, 8 * i + 16);
112107
}
113108
}
114109

@@ -120,13 +115,16 @@ public override void _Ready()
120115
SongLength = Audio.Stream.GetLength(),
121116
NumLoops = 5,
122117
};
123-
118+
124119
Player = new Puppet_Template();
125120
AddChild(Player);
126-
Player.Init(GD.Load<Texture2D>("res://scenes/BattleDirector/assets/Character1.png"), "Player");
121+
Player.Init(
122+
GD.Load<Texture2D>("res://scenes/BattleDirector/assets/Character1.png"),
123+
"Player"
124+
);
127125
Player.SetPosition(new Vector2(80, 0));
128126
Player.Sprite.Position += Vector2.Down * 30; //TEMP
129-
127+
130128
Enemy = new Puppet_Template();
131129
Enemy.SetPosition(new Vector2(400, 0));
132130
AddChild(Enemy);
@@ -149,15 +147,11 @@ private void Begin()
149147
new NoteArrow[CM.BeatsPerLoop],
150148
};
151149
AddExampleNotes();
152-
150+
153151
//TEMP
154152
var enemTween = CreateTween();
155-
enemTween
156-
.TweenProperty(Enemy.Sprite, "position", Vector2.Down * 5, 1f)
157-
.AsRelative();
158-
enemTween
159-
.TweenProperty(Enemy.Sprite, "position", Vector2.Up * 5, 1f)
160-
.AsRelative();
153+
enemTween.TweenProperty(Enemy.Sprite, "position", Vector2.Down * 5, 1f).AsRelative();
154+
enemTween.TweenProperty(Enemy.Sprite, "position", Vector2.Up * 5, 1f).AsRelative();
161155
enemTween.SetTrans(Tween.TransitionType.Spring);
162156
enemTween.SetEase(Tween.EaseType.In);
163157
enemTween.SetLoops();
@@ -176,12 +170,12 @@ public override void _Process(double delta)
176170
}
177171

178172
#region Input&Timing
179-
private void OnNotePressed(NoteArrow.ArrowType type)
173+
private void OnNotePressed(ArrowType type)
180174
{
181175
CheckNoteTiming(type);
182176
}
183177

184-
private void OnNoteReleased(NoteArrow.ArrowType arrowType) { }
178+
private void OnNoteReleased(ArrowType arrowType) { }
185179

186180
//Check all lanes for misses from missed inputs
187181
private void CheckMiss()
@@ -190,49 +184,39 @@ private void CheckMiss()
190184
double realBeat = TimeKeeper.CurrentTime / (60 / (double)_curSong.Bpm) % CM.BeatsPerLoop;
191185
for (int i = 0; i < _laneData.Length; i++)
192186
{
193-
if (
194-
_laneLastBeat[i] < Math.Floor(realBeat)
195-
|| (_laneLastBeat[i] == CM.BeatsPerLoop - 1 && Math.Floor(realBeat) == 0)
196-
)
197-
{ //If above, a note has been missed
198-
//GD.Print("Last beat " + _laneLastBeat[i]);
199-
if (
200-
_laneData[i][_laneLastBeat[i]] == null
201-
|| !_laneData[i][_laneLastBeat[i]].IsActive
202-
)
203-
{
204-
_laneLastBeat[i] = (_laneLastBeat[i] + 1) % CM.BeatsPerLoop;
205-
continue;
206-
}
207-
//Note exists and has been missed
208-
_laneData[i][_laneLastBeat[i]].NoteHit();
209-
HandleTiming((NoteArrow.ArrowType)i, 1);
187+
if (!(_laneLastBeat[i] < Math.Floor(realBeat)))
188+
continue;
189+
if (!IsNoteActive((ArrowType)i, _laneLastBeat[i]))
190+
{
210191
_laneLastBeat[i] = (_laneLastBeat[i] + 1) % CM.BeatsPerLoop;
192+
continue;
211193
}
194+
//Note exists and has been missed
195+
_laneData[i][_laneLastBeat[i]].NoteHit();
196+
HandleTiming(1);
197+
_laneLastBeat[i] = (_laneLastBeat[i] + 1) % CM.BeatsPerLoop;
212198
}
213199
}
214200

215-
private void CheckNoteTiming(NoteArrow.ArrowType type)
201+
private void CheckNoteTiming(ArrowType type)
216202
{
217203
double realBeat = TimeKeeper.CurrentTime / (60 / (double)_curSong.Bpm) % CM.BeatsPerLoop;
218204
int curBeat = (int)Math.Round(realBeat);
219205
GD.Print("Cur beat " + curBeat + "Real: " + realBeat.ToString("#.###"));
220-
if (
221-
_laneData[(int)type][curBeat % CM.BeatsPerLoop] == null
222-
|| !_laneData[(int)type][curBeat % CM.BeatsPerLoop].IsActive
223-
)
206+
if (_laneData[(int)type][curBeat % CM.BeatsPerLoop] == null)
224207
{
225-
_laneLastBeat[(int)type] = (curBeat) % CM.BeatsPerLoop;
226208
PlayerAddNote(type, curBeat);
227209
return;
228210
}
211+
if (!_laneData[(int)type][curBeat % CM.BeatsPerLoop].IsActive)
212+
return;
229213
double beatDif = Math.Abs(realBeat - curBeat);
230214
_laneData[(int)type][curBeat % CM.BeatsPerLoop].NoteHit();
231215
_laneLastBeat[(int)type] = (curBeat) % CM.BeatsPerLoop;
232-
HandleTiming(type, beatDif);
216+
HandleTiming(beatDif);
233217
}
234218

235-
private void HandleTiming(NoteArrow.ArrowType type, double beatDif)
219+
private void HandleTiming(double beatDif)
236220
{
237221
if (beatDif < _timingInterval * 1)
238222
{
@@ -265,21 +249,16 @@ private void HandleTiming(NoteArrow.ArrowType type, double beatDif)
265249
}
266250
#endregion
267251

268-
private void PlayerAddNote(NoteArrow.ArrowType type, int beat)
252+
private void PlayerAddNote(ArrowType type, int beat)
269253
{
270254
// can also add some sort of keybind here to also have pressed
271255
// in case the user just presses the note too early and spawns a note
272-
GD.Print(
273-
$"Player trying to place {type} typed note at beat: "
274-
+ beat
275-
+ " Verdict: "
276-
+ NotePlacementBar.CanPlaceNote()
277-
);
256+
GD.Print($"Player trying to place {type} typed note at beat: " + beat);
278257
if (NotePlacementBar.CanPlaceNote())
279258
{
280-
Note exampleNote = new Note(type, beat % CM.BeatsPerLoop);
281-
if (AddNoteToLane(exampleNote, false))
259+
if (AddNoteToLane(type, beat % CM.BeatsPerLoop, false))
282260
NotePlacementBar.PlacedNote();
261+
GD.Print("Note Placed.");
283262
}
284263
}
285264
}

scenes/BattleDirector/scripts/NotePlacementBar.cs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
public partial class NotePlacementBar : Node
55
{
66
const int MaxValue = 80;
7-
int currentBarValue;
8-
int currentCombo;
7+
private int _currentBarValue;
8+
private int _currentCombo;
99
int comboMult;
1010
int notesToIncreaseCombo;
1111

@@ -19,63 +19,60 @@ public partial class NotePlacementBar : Node
1919
public override void _Ready()
2020
{
2121
notePlacementBar.MaxValue = MaxValue;
22-
currentBarValue = 0;
23-
currentCombo = 0;
22+
_currentBarValue = 0;
23+
_currentCombo = 0;
2424
comboMult = 1;
2525
notesToIncreaseCombo = 4;
2626
}
2727

2828
public void ComboText(string text)
2929
{
30-
var feedbackScene = ResourceLoader.Load<PackedScene>(
31-
"res://scenes/BattleDirector/TextParticle.tscn"
32-
);
33-
TextParticle newText = feedbackScene.Instantiate<TextParticle>();
30+
TextParticle newText = new TextParticle();
3431
AddChild(newText);
35-
newText.Text = text + $" {currentCombo}";
32+
newText.Text = text + $" {_currentCombo}";
3633
}
3734

3835
// Hitting a note increases combo, combo mult, and note placement bar
3936
public void HitNote()
4037
{
41-
currentCombo++;
38+
_currentCombo++;
4239
DetermineComboMult();
43-
currentBarValue += comboMult;
44-
UpdateNotePlacementBar(currentBarValue);
40+
_currentBarValue += comboMult;
41+
UpdateNotePlacementBar(_currentBarValue);
4542
UpdateComboMultText();
4643
}
4744

4845
// Missing a note resets combo
4946
public void MissNote()
5047
{
51-
currentCombo = 0;
48+
_currentCombo = 0;
5249
DetermineComboMult();
5350
UpdateComboMultText();
5451
}
5552

5653
// Placing a note resets the note placement bar
5754
public void PlacedNote()
5855
{
59-
currentBarValue = 0;
60-
UpdateNotePlacementBar(currentBarValue);
56+
_currentBarValue = 0;
57+
UpdateNotePlacementBar(_currentBarValue);
6158
}
6259

6360
public bool CanPlaceNote()
6461
{
65-
return currentBarValue >= MaxValue;
62+
return _currentBarValue >= MaxValue;
6663
}
6764

6865
private void DetermineComboMult()
6966
{
70-
comboMult = currentCombo / notesToIncreaseCombo + 1;
67+
comboMult = _currentCombo / notesToIncreaseCombo + 1;
7168
}
7269

73-
public void UpdateNotePlacementBar(int newValue)
70+
private void UpdateNotePlacementBar(int newValue)
7471
{
7572
notePlacementBar.Value = newValue;
7673
}
7774

78-
public void UpdateComboMultText()
75+
private void UpdateComboMultText()
7976
{
8077
currentComboMultText.Text = $"x{comboMult.ToString()}";
8178
}

scenes/BattleDirector/scripts/TextParticle.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,4 @@ public override void _Ready()
1717
tween.SetParallel(false);
1818
tween.TweenCallback(Callable.From(QueueFree));
1919
}
20-
21-
// Called every frame. 'delta' is the elapsed time since the previous frame.
22-
public override void _Process(double delta) { }
2320
}

0 commit comments

Comments
 (0)