Skip to content

Commit a7f40e5

Browse files
authored
Merge pull request #97 from neogeek/hotfix/added-missing-godot-method
[hotfix] Added missing method.
2 parents 045a8d8 + 0d418b5 commit a7f40e5

File tree

6 files changed

+172
-122
lines changed

6 files changed

+172
-122
lines changed

Documentation/API/Utilities/FindPositionNearGivenTick.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ extends Node
5656
func _ready() -> void:
5757
var seconds = 5
5858
var resolution = 192
59+
var delta = 50
5960
6061
var notes = [
6162
{"position": 768 }, {"position": 960 }, {"position": 1152 },
@@ -64,7 +65,7 @@ func _ready() -> void:
6465
{"position": 3072 }, {"position": 3264 }
6566
]
6667
67-
var note = rhythm_game_utilities.find_position_near_given_tick(notes, 750);
68+
var note = rhythm_game_utilities.find_position_near_given_tick(notes, 750, delta);
6869
6970
print(note["position"]) # 768
7071
```

GodotPlugin/include/rhythm_game_utilities.cpp

Lines changed: 151 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,15 @@ void rhythm_game_utilities::_bind_methods()
1010
{
1111
// Common
1212

13-
ClassDB::bind_static_method("rhythm_game_utilities",
14-
D_METHOD("lerp", "a", "b", "t"),
15-
&rhythm_game_utilities::lerp);
16-
1713
ClassDB::bind_static_method("rhythm_game_utilities",
1814
D_METHOD("inverse_lerp", "a", "b", "v"),
1915
&rhythm_game_utilities::inverse_lerp);
2016

21-
// Parsers
17+
ClassDB::bind_static_method("rhythm_game_utilities",
18+
D_METHOD("lerp", "a", "b", "t"),
19+
&rhythm_game_utilities::lerp);
2220

23-
ClassDB::bind_static_method(
24-
"rhythm_game_utilities",
25-
D_METHOD("parse_sections_from_chart", "contents"),
26-
&rhythm_game_utilities::parse_sections_from_chart);
21+
// Parsers
2722

2823
ClassDB::bind_static_method(
2924
"rhythm_game_utilities",
@@ -45,13 +40,30 @@ void rhythm_game_utilities::_bind_methods()
4540
D_METHOD("parse_notes_from_chart_section", "section"),
4641
&rhythm_game_utilities::parse_notes_from_chart_section);
4742

43+
ClassDB::bind_static_method(
44+
"rhythm_game_utilities",
45+
D_METHOD("parse_sections_from_chart", "contents"),
46+
&rhythm_game_utilities::parse_sections_from_chart);
47+
4848
ClassDB::bind_static_method(
4949
"rhythm_game_utilities",
5050
D_METHOD("parse_time_signatures_from_chart_section", "section"),
5151
&rhythm_game_utilities::parse_time_signatures_from_chart_section);
5252

5353
// Utilities
5454

55+
ClassDB::bind_static_method(
56+
"rhythm_game_utilities",
57+
D_METHOD("calculate_accuracy_ratio", "position", "current_position",
58+
"delta"),
59+
&rhythm_game_utilities::calculate_accuracy_ratio);
60+
61+
ClassDB::bind_static_method("rhythm_game_utilities",
62+
D_METHOD("calculate_beat_bars", "bpm_changes",
63+
"resolution", "ts",
64+
"include_half_notes"),
65+
&rhythm_game_utilities::calculate_beat_bars);
66+
5567
ClassDB::bind_static_method(
5668
"rhythm_game_utilities",
5769
D_METHOD("convert_seconds_to_ticks", "seconds", "resolution",
@@ -63,6 +75,11 @@ void rhythm_game_utilities::_bind_methods()
6375
D_METHOD("convert_tick_to_position", "tick", "resolution"),
6476
&rhythm_game_utilities::convert_tick_to_position);
6577

78+
ClassDB::bind_static_method(
79+
"rhythm_game_utilities",
80+
D_METHOD("find_position_near_given_tick", "notes", "tick", "delta"),
81+
&rhythm_game_utilities::find_position_near_given_tick);
82+
6683
ClassDB::bind_static_method(
6784
"rhythm_game_utilities",
6885
D_METHOD("is_on_the_beat", "bpm", "current_time", "delta"),
@@ -72,74 +89,22 @@ void rhythm_game_utilities::_bind_methods()
7289
"rhythm_game_utilities",
7390
D_METHOD("round_up_to_the_nearest_multiplier", "value", "multiplier"),
7491
&rhythm_game_utilities::round_up_to_the_nearest_multiplier);
75-
76-
ClassDB::bind_static_method(
77-
"rhythm_game_utilities",
78-
D_METHOD("calculate_accuracy_ratio", "position", "current_position",
79-
"delta"),
80-
&rhythm_game_utilities::calculate_accuracy_ratio);
81-
82-
ClassDB::bind_static_method("rhythm_game_utilities",
83-
D_METHOD("calculate_beat_bars", "bpm_changes",
84-
"resolution", "ts",
85-
"include_half_notes"),
86-
&rhythm_game_utilities::calculate_beat_bars);
8792
}
8893

8994
// Common
9095

91-
float rhythm_game_utilities::lerp(float a, float b, float t)
92-
{
93-
return RhythmGameUtilities::Lerp(a, b, t);
94-
}
95-
9696
float rhythm_game_utilities::inverse_lerp(float a, float b, float v)
9797
{
9898
return RhythmGameUtilities::InverseLerp(a, b, v);
9999
}
100100

101-
// Parsers
102-
103-
Dictionary rhythm_game_utilities::parse_sections_from_chart(String contents)
101+
float rhythm_game_utilities::lerp(float a, float b, float t)
104102
{
105-
Dictionary sections;
106-
107-
auto sections_internal =
108-
RhythmGameUtilities::ParseSectionsFromChart(contents.utf8().get_data());
109-
110-
for (auto section_internal = sections_internal.begin();
111-
section_internal != sections_internal.end(); section_internal++)
112-
{
113-
auto section_key = godot::String(section_internal->first.c_str());
114-
115-
Array section_items;
116-
117-
for (auto i = 0; i < section_internal->second.size(); i += 1)
118-
{
119-
Dictionary section_item;
120-
121-
auto temp = section_internal->second[i];
122-
123-
auto key = godot::Variant(temp.first.c_str());
124-
125-
Array values;
126-
127-
for (auto j = 0; j < temp.second.size(); j += 1)
128-
{
129-
values.append(godot::Variant(temp.second[j].c_str()));
130-
}
131-
132-
section_item[key] = values;
133-
134-
section_items.append(section_item);
135-
}
136-
137-
sections[section_key] = section_items;
138-
}
139-
140-
return sections;
103+
return RhythmGameUtilities::Lerp(a, b, t);
141104
}
142105

106+
// Parsers
107+
143108
Array rhythm_game_utilities::parse_bpm_from_chart_section(Array section)
144109
{
145110
auto bpm_changes_internal = RhythmGameUtilities::ParseBpmFromChartSection(
@@ -214,6 +179,46 @@ Array rhythm_game_utilities::parse_notes_from_chart_section(Array section)
214179
return notes;
215180
}
216181

182+
Dictionary rhythm_game_utilities::parse_sections_from_chart(String contents)
183+
{
184+
Dictionary sections;
185+
186+
auto sections_internal =
187+
RhythmGameUtilities::ParseSectionsFromChart(contents.utf8().get_data());
188+
189+
for (auto section_internal = sections_internal.begin();
190+
section_internal != sections_internal.end(); section_internal++)
191+
{
192+
auto section_key = godot::String(section_internal->first.c_str());
193+
194+
Array section_items;
195+
196+
for (auto i = 0; i < section_internal->second.size(); i += 1)
197+
{
198+
Dictionary section_item;
199+
200+
auto temp = section_internal->second[i];
201+
202+
auto key = godot::Variant(temp.first.c_str());
203+
204+
Array values;
205+
206+
for (auto j = 0; j < temp.second.size(); j += 1)
207+
{
208+
values.append(godot::Variant(temp.second[j].c_str()));
209+
}
210+
211+
section_item[key] = values;
212+
213+
section_items.append(section_item);
214+
}
215+
216+
sections[section_key] = section_items;
217+
}
218+
219+
return sections;
220+
}
221+
217222
Array rhythm_game_utilities::parse_time_signatures_from_chart_section(
218223
Array section)
219224
{
@@ -239,6 +244,54 @@ Array rhythm_game_utilities::parse_time_signatures_from_chart_section(
239244

240245
// Utilities
241246

247+
float rhythm_game_utilities::calculate_accuracy_ratio(int position,
248+
int current_position,
249+
int delta)
250+
{
251+
return RhythmGameUtilities::CalculateAccuracyRatio(position,
252+
current_position, delta);
253+
}
254+
255+
Array rhythm_game_utilities::calculate_beat_bars(Array bpm_changes,
256+
int resolution, int ts,
257+
bool include_half_notes)
258+
{
259+
std::vector<RhythmGameUtilities::Tempo> bpm_changes_internal;
260+
bpm_changes_internal.reserve(bpm_changes.size());
261+
262+
for (auto i = 0; i < bpm_changes.size(); i += 1)
263+
{
264+
RhythmGameUtilities::Tempo bpm_change_internal;
265+
266+
if (bpm_changes[i].get_type() == Variant::DICTIONARY)
267+
{
268+
Dictionary variant = bpm_changes[i];
269+
270+
bpm_change_internal.Position = variant["position"];
271+
bpm_change_internal.BPM = variant["bpm"];
272+
}
273+
274+
bpm_changes_internal.push_back(bpm_change_internal);
275+
}
276+
277+
auto beat_bars_internal = RhythmGameUtilities::CalculateBeatBars(
278+
bpm_changes_internal, resolution, ts, include_half_notes);
279+
280+
Array beat_bars_dictionary_array;
281+
282+
for (auto &beat_bar_internal : beat_bars_internal)
283+
{
284+
Dictionary beat_bar_dictionary;
285+
286+
beat_bar_dictionary["position"] = beat_bar_internal.Position;
287+
beat_bar_dictionary["bpm"] = beat_bar_internal.BPM;
288+
289+
beat_bars_dictionary_array.append(beat_bar_dictionary);
290+
}
291+
292+
return beat_bars_dictionary_array;
293+
}
294+
242295
int rhythm_game_utilities::convert_seconds_to_ticks(
243296
float seconds, int resolution, Array bpm_changes,
244297
Array time_signature_changes)
@@ -291,63 +344,53 @@ float rhythm_game_utilities::convert_tick_to_position(int tick, int resolution)
291344
return RhythmGameUtilities::ConvertTickToPosition(tick, resolution);
292345
}
293346

294-
bool rhythm_game_utilities::is_on_the_beat(int bpm, float current_time,
295-
float delta)
296-
{
297-
return RhythmGameUtilities::IsOnTheBeat(bpm, current_time, delta);
298-
}
299-
300-
int rhythm_game_utilities::round_up_to_the_nearest_multiplier(int value,
301-
int multiplier)
302-
{
303-
return RhythmGameUtilities::RoundUpToTheNearestMultiplier(value,
304-
multiplier);
305-
}
306-
307-
float rhythm_game_utilities::calculate_accuracy_ratio(int position,
308-
int current_position,
309-
int delta)
347+
Dictionary rhythm_game_utilities::find_position_near_given_tick(Array notes,
348+
int tick,
349+
int delta)
310350
{
311-
return RhythmGameUtilities::CalculateAccuracyRatio(position,
312-
current_position, delta);
313-
}
314-
315-
Array rhythm_game_utilities::calculate_beat_bars(Array bpm_changes,
316-
int resolution, int ts,
317-
bool include_half_notes)
318-
{
319-
std::vector<RhythmGameUtilities::Tempo> bpm_changes_internal;
320-
bpm_changes_internal.reserve(bpm_changes.size());
351+
std::vector<RhythmGameUtilities::Note> notes_internal;
352+
notes_internal.reserve(notes.size());
321353

322-
for (auto i = 0; i < bpm_changes.size(); i += 1)
354+
for (auto i = 0; i < notes.size(); i += 1)
323355
{
324-
RhythmGameUtilities::Tempo bpm_change_internal;
356+
RhythmGameUtilities::Note note_internal;
325357

326-
if (bpm_changes[i].get_type() == Variant::DICTIONARY)
358+
if (notes[i].get_type() == Variant::DICTIONARY)
327359
{
328-
Dictionary variant = bpm_changes[i];
360+
Dictionary variant = notes[i];
329361

330-
bpm_change_internal.Position = variant["position"];
331-
bpm_change_internal.BPM = variant["bpm"];
362+
note_internal.Position = variant["position"];
363+
note_internal.HandPosition = variant["hand_position"];
364+
note_internal.Length = variant["length"];
332365
}
333366

334-
bpm_changes_internal.push_back(bpm_change_internal);
367+
notes_internal.push_back(note_internal);
335368
}
336369

337-
auto beat_bars_internal = RhythmGameUtilities::CalculateBeatBars(
338-
bpm_changes_internal, resolution, ts, include_half_notes);
370+
auto note_internal = RhythmGameUtilities::FindPositionNearGivenTick(
371+
notes_internal, tick, delta);
339372

340-
Array beat_bars_dictionary_array;
373+
Dictionary note;
341374

342-
for (auto &beat_bar_internal : beat_bars_internal)
375+
if (note_internal)
343376
{
344-
Dictionary beat_bar_dictionary;
377+
note["position"] = note_internal->Position;
378+
note["hand_position"] = note_internal->HandPosition;
379+
note["length"] = note_internal->Length;
380+
}
345381

346-
beat_bar_dictionary["position"] = beat_bar_internal.Position;
347-
beat_bar_dictionary["bpm"] = beat_bar_internal.BPM;
382+
return note;
383+
}
348384

349-
beat_bars_dictionary_array.append(beat_bar_dictionary);
350-
}
385+
bool rhythm_game_utilities::is_on_the_beat(int bpm, float current_time,
386+
float delta)
387+
{
388+
return RhythmGameUtilities::IsOnTheBeat(bpm, current_time, delta);
389+
}
351390

352-
return beat_bars_dictionary_array;
391+
int rhythm_game_utilities::round_up_to_the_nearest_multiplier(int value,
392+
int multiplier)
393+
{
394+
return RhythmGameUtilities::RoundUpToTheNearestMultiplier(value,
395+
multiplier);
353396
}

0 commit comments

Comments
 (0)