Skip to content

Commit 9b9a4fb

Browse files
authored
Merge pull request #29 from Project-Funk-Engine/InputTiming
Input timing and HP bars finished
2 parents 38c5ff1 + 212f95d commit 9b9a4fb

18 files changed

Lines changed: 424 additions & 246 deletions

Funk Engine.sln.DotSettings.user

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AThrowHelper_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003FUsers_003Fgamef_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F6354a7b35d7821629924d3676acd7e67a6f7f94343e0e66ec439aa2bd6ed5_003FThrowHelper_002Ecs/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>

project.godot

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ config_version=5
1111
[application]
1212

1313
config/name="Funk Engine"
14-
run/main_scene="res://scenes/ChartViewport/test_scene.tscn"
14+
run/main_scene="res://scenes/BattleDirector/test_battle_scene.tscn"
1515
config/features=PackedStringArray("4.3", "C#", "Forward Plus")
1616
config/icon="res://icon.svg"
1717

18+
[autoload]
19+
20+
TimeKeeper="*res://scripts/TimeKeeper.cs"
21+
1822
[display]
1923

2024
window/size/viewport_width=640
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using Godot;
6+
7+
/**
8+
* @class BattleDirector
9+
* @brief Higher priority director to manage battle effects. Can directly access managers, which should signal up to Director WIP
10+
*/
11+
public partial class BattleDirector : Node2D
12+
{
13+
[Export]
14+
public ChartManager CM;
15+
16+
[Export]
17+
public NoteManager NM;
18+
19+
private HealthBar Player;
20+
private HealthBar Enemy;
21+
22+
private double _timingInterval = .1; //secs
23+
24+
[Signal]
25+
public delegate void PlayerDamageEventHandler(int damage);
26+
27+
[Signal]
28+
public delegate void EnemyDamageEventHandler(int damage);
29+
30+
public struct SongData
31+
{
32+
public int Bpm;
33+
public double SongLength;
34+
public int NumLoops;
35+
}
36+
37+
private SongData _curSong;
38+
//Assume queue structure for notes in each lane.
39+
private readonly Note[][] _laneNotes = new Note[][]
40+
{
41+
Array.Empty<Note>(),
42+
Array.Empty<Note>(),
43+
Array.Empty<Note>(),
44+
Array.Empty<Note>(),
45+
};
46+
private Note[] _notes = Array.Empty<Note>();
47+
48+
public override void _Ready()
49+
{
50+
AddExampleNote();
51+
CM.PrepChart(_curSong, _notes);
52+
53+
Player = GetNode<HealthBar>("PlayerHP");
54+
Enemy = GetNode<HealthBar>("EnemyHP");
55+
56+
CM.Connect(nameof(NoteManager.NotePressed), new Callable(this, nameof(OnNotePressed)));
57+
CM.Connect(nameof(NoteManager.NoteReleased), new Callable(this, nameof(OnNoteReleased)));
58+
}
59+
60+
public override void _Process(double delta)
61+
{
62+
TimeKeeper.CurrentTime += delta;
63+
//Check beats for each lane for passive misses
64+
double curBeat = TimeKeeper.CurrentTime / (60 / (double)_curSong.Bpm);
65+
for (int i = 0; i < _laneNotes.Length; i++)
66+
{
67+
if (_laneNotes[i].Length <= 0) continue;
68+
double beatDif = (curBeat - _laneNotes[i].First().Beat);
69+
if (beatDif > 1)
70+
{
71+
handleTiming((NoteArrow.ArrowType)i, Math.Abs(beatDif));
72+
}
73+
}
74+
}
75+
76+
//Creeate dummy song data and notes
77+
private void AddExampleNote()
78+
{
79+
_curSong = new SongData
80+
{
81+
Bpm = 120,
82+
SongLength = 100,
83+
NumLoops = 5,
84+
};
85+
for (int i = 0; i < 4; i++)
86+
{
87+
Note exampleNote = new Note(NoteArrow.ArrowType.Up, i + 3);
88+
AddNoteToLane(exampleNote);
89+
}
90+
for (int i = 0; i < 1; i++)
91+
{
92+
Note exampleNote = new Note(NoteArrow.ArrowType.Left, i + 4);
93+
AddNoteToLane(exampleNote);
94+
}
95+
}
96+
97+
private void AddNoteToLane(Note note)
98+
{
99+
_notes = _notes.Append(note).ToArray();
100+
_laneNotes[(int)note.Type] = _laneNotes[(int)note.Type].Append(note).ToArray();
101+
}
102+
103+
private void OnNotePressed(NoteArrow.ArrowType type)
104+
{
105+
CheckNoteTiming(type);
106+
}
107+
108+
private void OnNoteReleased(NoteArrow.ArrowType arrowType)
109+
{
110+
}
111+
112+
private void handleTiming(NoteArrow.ArrowType type, double beatDif)
113+
{
114+
//Cycle note queue
115+
_laneNotes[(int)type].First().Beat += CM.BeatsPerLoop;
116+
_laneNotes[(int)type] = _laneNotes[(int)type] //Credit: Stackoverflow https://stackoverflow.com/questions/49494535/moving-the-first-array-element-to-end-in-c-sharp
117+
.Skip(1)
118+
.Concat(_laneNotes[(int)type].Take(1))
119+
.ToArray(); //TODO: No stackoverflow code
120+
//Do timing stuff
121+
if (beatDif < _timingInterval * 2)
122+
{
123+
GD.Print("Perfect");
124+
Enemy.TakeDamage(10);
125+
}
126+
else if (beatDif < _timingInterval * 4)
127+
{
128+
GD.Print("Good");
129+
Enemy.TakeDamage(5);
130+
}
131+
else if (beatDif < _timingInterval * 6)
132+
{
133+
GD.Print("Okay");
134+
Enemy.TakeDamage(1);
135+
}
136+
else
137+
{
138+
GD.Print("Miss");
139+
Player.TakeDamage(10);
140+
}
141+
}
142+
143+
private void CheckNoteTiming(NoteArrow.ArrowType type)
144+
{
145+
double curBeat = TimeKeeper.CurrentTime / (60 / (double)_curSong.Bpm);
146+
if (_laneNotes[(int)type].Length == 0)
147+
return;
148+
double beatDif = Math.Abs(curBeat - _laneNotes[(int)type].First().Beat);
149+
if (beatDif > 1)
150+
return;
151+
GD.Print("Note Hit. Dif: " + beatDif);
152+
CM.HandleNote(type);
153+
handleTiming(type, beatDif);
154+
}
155+
}

scenes/BattleDirector/HealthBar.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using Godot;
3+
4+
public partial class HealthBar : Control
5+
{
6+
const int MaxHealth = 100;
7+
int _health = MaxHealth;
8+
9+
[Export]
10+
public ProgressBar PlayerHealthBar;
11+
//we can change this to a Texture Progress bar once we have art assets for it
12+
13+
14+
public override void _Ready()
15+
{
16+
if (PlayerHealthBar != null)
17+
{
18+
SetHealth(MaxHealth, MaxHealth);
19+
}
20+
}
21+
22+
public void SetHealth(int max, int current)
23+
{
24+
PlayerHealthBar.MaxValue = max;
25+
PlayerHealthBar.Value = current;
26+
_updateHealthBar();
27+
}
28+
29+
private void _updateHealthBar()
30+
{
31+
PlayerHealthBar.Value = _health;
32+
}
33+
34+
public void TakeDamage(int damage)
35+
{
36+
_health -= damage;
37+
if (_health <= 0)
38+
{
39+
GD.Print("You are dead");
40+
}
41+
_updateHealthBar();
42+
}
43+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[gd_scene load_steps=2 format=3 uid="uid://bgomxovxs7sr8"]
2+
3+
[ext_resource type="Script" path="res://scenes/BattleDirector/HealthBar.cs" id="1_b1t4i"]
4+
5+
[node name="Control" type="Control" node_paths=PackedStringArray("PlayerHealthBar")]
6+
layout_mode = 3
7+
anchors_preset = 0
8+
offset_right = 40.0
9+
offset_bottom = 40.0
10+
script = ExtResource("1_b1t4i")
11+
PlayerHealthBar = NodePath("ProgressBar")
12+
13+
[node name="ProgressBar" type="ProgressBar" parent="."]
14+
layout_mode = 0
15+
offset_left = 29.0
16+
offset_top = 18.0
17+
offset_right = 229.0
18+
offset_bottom = 45.0
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[gd_scene load_steps=4 format=3 uid="uid://b0mrgr7h0ty1y"]
2+
3+
[ext_resource type="Script" path="res://scenes/BattleDirector/BattleDirector.cs" id="1_cwqqr"]
4+
[ext_resource type="PackedScene" uid="uid://dfevfib11kou1" path="res://scenes/ChartViewport/ChartViewport.tscn" id="2_cupb3"]
5+
[ext_resource type="PackedScene" uid="uid://bgomxovxs7sr8" path="res://scenes/BattleDirector/HealthBar.tscn" id="3_pp0u0"]
6+
7+
[node name="ProtoBattleDirector" type="Node2D" node_paths=PackedStringArray("CM", "NM")]
8+
script = ExtResource("1_cwqqr")
9+
CM = NodePath("SubViewport")
10+
NM = NodePath("SubViewport/SubViewport/noteManager")
11+
12+
[node name="SubViewport" parent="." instance=ExtResource("2_cupb3")]
13+
offset_left = 80.0
14+
offset_top = 140.0
15+
offset_right = 560.0
16+
offset_bottom = 360.0
17+
18+
[node name="ColorRect" type="ColorRect" parent="."]
19+
z_index = -1
20+
offset_left = -70.0
21+
offset_top = -34.0
22+
offset_right = 673.0
23+
offset_bottom = 393.0
24+
color = Color(0.147672, 0.147672, 0.147672, 1)
25+
26+
[node name="PlayerHP" parent="." instance=ExtResource("3_pp0u0")]
27+
28+
[node name="EnemyHP" parent="." instance=ExtResource("3_pp0u0")]
29+
offset_left = 403.0
30+
offset_top = -4.0
31+
offset_right = 443.0
32+
offset_bottom = 36.0

scenes/ChartViewport/BattleDirector.cs

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

0 commit comments

Comments
 (0)