Skip to content

Commit

Permalink
use less dynamics for bpm change shit
Browse files Browse the repository at this point in the history
  • Loading branch information
MaybeMaru committed Apr 17, 2024
1 parent 52cf320 commit 9cc2fe0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
52 changes: 25 additions & 27 deletions source/funkin/util/backend/MusicBeat.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package funkin.util.backend;

import flixel.FlxBasic;

class MusicBeat extends FlxBasic {
class MusicBeat extends FlxBasic
{
public var curStep(default, null):Int = 0;
public var curBeat(default, null):Int = 0;
public var curSection(default, null):Int = 0;
Expand All @@ -11,14 +12,8 @@ class MusicBeat extends FlxBasic {
public var curBeatDecimal(default, null):Float = 0;
public var curSectionDecimal(default, null):Float = 0;

public var targetSound:FlxSound = null;

public var hasParent(default, null):Bool = false;
public var parent(default, set):IMusicGetter = null;
inline function set_parent(value:IMusicGetter):IMusicGetter {
hasParent = value != null;
return parent = value;
}
public var targetSound:FlxSound;
public var parent:IMusicGetter;

public function new(?parent:IMusicGetter) {
this.parent = parent;
Expand All @@ -27,7 +22,8 @@ class MusicBeat extends FlxBasic {

var lastStep(default, null):Int = -1;

override function update(elapsed:Float):Void {
override function update(elapsed:Float):Void
{
if (targetSound != null) if (targetSound.playing)
Conductor.songPosition = targetSound.time - Conductor.latency;

Expand All @@ -37,9 +33,8 @@ class MusicBeat extends FlxBasic {
updateBeat();
updateSection();

if (lastStep != curStep && curStep >= 0) {
if (lastStep != curStep) if (curStep > -1)
stepHit();
}

#if FLX_DEBUG
FlxG.watch.addQuick("curSection", curSection);
Expand All @@ -58,38 +53,41 @@ class MusicBeat extends FlxBasic {
curBeat = Math.floor(curBeatDecimal);
}

private inline function updateStep():Void {
var lastChange:BPMChangeEvent = {
stepTime: 0,
songTime: 0,
bpm: 0
}

for (i in 0...Conductor.bpmChangeMap.length) {
if (Conductor.songPosition >= Conductor.bpmChangeMap[i].songTime) {
lastChange = Conductor.bpmChangeMap[i];
}
private inline function updateStep():Void
{
var lastStep:Int = 0;
var lastTime:Float = 0;

if (Conductor.bpmChangeMap.length > 0)
{
Conductor.bpmChangeMap.fastForEach((event, i) -> {
if (Conductor.songPosition >= event.songTime) {
lastTime = event.songTime;
lastStep = event.stepTime;
}
else break; // No need to loop through the rest
});
}

curStepDecimal = lastChange.stepTime + (Conductor.songPosition - lastChange.songTime) / Conductor.stepCrochet;
curStepDecimal = lastStep + (Conductor.songPosition - lastTime) / Conductor.stepCrochet;
curStep = Math.floor(curStepDecimal);
}

public inline function stepHit():Void {
if (hasParent) parent.stepHit(curStep);
if (parent != null) parent.stepHit(curStep);
if (curStep % Conductor.STEPS_PER_BEAT == 0) {
beatHit();
}
}

public inline function beatHit():Void {
if (hasParent) parent.beatHit(curBeat);
if (parent != null) parent.beatHit(curBeat);
if (curBeat % Conductor.BEATS_PER_MEASURE == 0) {
sectionHit();
}
}

public inline function sectionHit():Void {
if (hasParent) parent.sectionHit(curSection);
if (parent != null) parent.sectionHit(curSection);
}
}
5 changes: 3 additions & 2 deletions source/funkin/util/song/Conductor.hx
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ class Conductor

public static var bpmChangeMap:Array<BPMChangeEvent> = [];

public static function mapBPMChanges(song:SwagSong):Void {
bpmChangeMap = [];
public static function mapBPMChanges(song:SwagSong):Void
{
bpmChangeMap.splice(0, bpmChangeMap.length);

var curBPM:Float = song.bpm;
var totalSteps:Int = 0;
Expand Down

0 comments on commit 9cc2fe0

Please sign in to comment.