Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BPM change functionality & implementation #382

Merged
merged 6 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 56 additions & 44 deletions source/ChartingState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package;

import Section.SwagSection;
import Song.SwagSong;
import Conductor.BPMChangeEvent;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.addons.display.FlxGridOverlay;
Expand Down Expand Up @@ -117,11 +118,9 @@ class ChartingState extends MusicBeatState
song: 'Test',
notes: [],
bpm: 150,
sections: 0,
needsVoices: true,
player1: 'bf',
player2: 'dad',
sectionLengths: [],
speed: 1,
validScore: false
};
Expand All @@ -140,6 +139,7 @@ class ChartingState extends MusicBeatState

loadSong(_song.song);
Conductor.changeBPM(_song.bpm);
Conductor.mapBPMChanges(_song);

bpmTxt = new FlxText(1000, 50, 0, "", 16);
bpmTxt.scrollFactor.set();
Expand Down Expand Up @@ -366,6 +366,7 @@ class ChartingState extends MusicBeatState
vocals.time = 0;
FlxG.sound.music.pause();
FlxG.sound.music.time = 0;
changeSection();
};
}

Expand Down Expand Up @@ -425,6 +426,7 @@ class ChartingState extends MusicBeatState
else if (wname == 'song_bpm')
{
tempBpm = Std.int(nums.value);
Conductor.mapBPMChanges(_song);
Conductor.changeBPM(Std.int(nums.value));
}
else if (wname == 'note_susLength')
Expand All @@ -444,12 +446,27 @@ class ChartingState extends MusicBeatState

var updatedSection:Bool = false;

/* this function got owned LOL
function lengthBpmBullshit():Float
{
if (_song.notes[curSection].changeBPM)
return _song.notes[curSection].lengthInSteps * (_song.notes[curSection].bpm / _song.bpm);
else
return _song.notes[curSection].lengthInSteps;
}*/

function sectionStartTime():Float
{
var daBPM:Int = _song.bpm;
var daPos:Float = 0;
for (i in 0...curSection)
{
if (_song.notes[i].changeBPM) {
daBPM = _song.notes[i].bpm;
}
daPos += 4 * (1000 * 60 / daBPM);
}
return daPos;
}

override function update(elapsed:Float)
Expand All @@ -459,23 +476,20 @@ class ChartingState extends MusicBeatState
Conductor.songPosition = FlxG.sound.music.time;
_song.song = typingShit.text;

strumLine.y = getYfromStrum(Conductor.songPosition % (Conductor.stepCrochet * lengthBpmBullshit()));
strumLine.y = getYfromStrum((Conductor.songPosition - sectionStartTime()) % (Conductor.stepCrochet * _song.notes[curSection].lengthInSteps));

if (curBeat % 4 == 0)
if (curBeat % 4 == 0 && curStep >= 16 * (curSection + 1))
{
if (curStep > 16 * (curSection + 1))
{
trace(curStep);
trace((_song.notes[curSection].lengthInSteps) * (curSection + 1));
trace('DUMBSHIT');

if (_song.notes[curSection + 1] == null)
{
addSection();
}
trace(curStep);
trace((_song.notes[curSection].lengthInSteps) * (curSection + 1));
trace('DUMBSHIT');

changeSection(curSection + 1, false);
if (_song.notes[curSection + 1] == null)
{
addSection();
}

changeSection(curSection + 1, false);
}

FlxG.watch.addQuick('daBeat', curBeat);
Expand Down Expand Up @@ -674,21 +688,18 @@ class ChartingState extends MusicBeatState

function recalculateSteps():Int
{
var steps:Int = 0;
var timeShit:Float = 0;

for (i in 0...curSection)
var lastChange:BPMChangeEvent = {
stepTime: 0,
songTime: 0,
bpm: 0
}
for (i in 0...Conductor.bpmChangeMap.length)
{
steps += 16;

if (_song.notes[i].changeBPM)
timeShit += (((60 / _song.notes[i].bpm) * 1000) / 4) * 16;
else
timeShit += (((60 / _song.bpm) * 1000) / 4) * 16;
if (FlxG.sound.music.time > Conductor.bpmChangeMap[i].songTime)
lastChange = Conductor.bpmChangeMap[i];
}

steps += Math.floor((FlxG.sound.music.time - timeShit) / Conductor.stepCrochet);
curStep = steps;
curStep = lastChange.stepTime + Math.floor((FlxG.sound.music.time - lastChange.songTime) / Conductor.stepCrochet);
updateBeat();

return curStep;
Expand All @@ -702,7 +713,7 @@ class ChartingState extends MusicBeatState
vocals.pause();

// Basically old shit from changeSection???
FlxG.sound.music.time = lengthBpmBullshit() * Conductor.stepCrochet * curSection;
FlxG.sound.music.time = sectionStartTime();

if (songBeginning)
{
Expand Down Expand Up @@ -732,15 +743,15 @@ class ChartingState extends MusicBeatState
FlxG.sound.music.pause();
vocals.pause();

var daNum:Int = 0;
/*var daNum:Int = 0;
var daLength:Float = 0;
while (daNum <= sec)
{
daLength += lengthBpmBullshit();
daNum++;
}
}*/

FlxG.sound.music.time = (daLength - lengthBpmBullshit()) * Conductor.stepCrochet;
FlxG.sound.music.time = sectionStartTime();
vocals.time = FlxG.sound.music.time;
updateCurStep();
}
Expand Down Expand Up @@ -815,10 +826,16 @@ class ChartingState extends MusicBeatState
if (_song.notes[curSection].changeBPM && _song.notes[curSection].bpm > 0)
{
Conductor.changeBPM(_song.notes[curSection].bpm);
FlxG.log.add('CHANGED BPM!');
}
else
{
Conductor.changeBPM(tempBpm);
//get last bpm
var daBPM:Int = _song.bpm;
for (i in 0...curSection)
if (_song.notes[i].changeBPM)
daBPM = _song.notes[i].bpm;
Conductor.changeBPM(daBPM);
}

/* // PORT BULLSHIT, INCASE THERE'S NO SUSTAIN DATA FOR A NOTE
Expand Down Expand Up @@ -846,7 +863,7 @@ class ChartingState extends MusicBeatState
note.setGraphicSize(GRID_SIZE, GRID_SIZE);
note.updateHitbox();
note.x = Math.floor(daNoteInfo * GRID_SIZE);
note.y = Math.floor(getYfromStrum(daStrumTime)) % gridBG.height;
note.y = Math.floor(getYfromStrum((daStrumTime - sectionStartTime()) % (Conductor.stepCrochet * _song.notes[curSection].lengthInSteps)));

curRenderedNotes.add(note);

Expand Down Expand Up @@ -925,7 +942,7 @@ class ChartingState extends MusicBeatState

private function addNote():Void
{
var noteStrum = getStrumTime(dummyArrow.y) + (curSection * (Conductor.stepCrochet * 16));
var noteStrum = getStrumTime(dummyArrow.y) + sectionStartTime();
var noteData = Math.floor(FlxG.mouse.x / GRID_SIZE);
var noteSus = 0;

Expand All @@ -938,7 +955,7 @@ class ChartingState extends MusicBeatState
_song.notes[curSection].sectionNotes.push([noteStrum, (noteData + 4) % 8, noteSus]);
}

trace(getStrumTime(dummyArrow.y) + (curSection * (Conductor.stepCrochet * lengthBpmBullshit())));
trace(noteStrum);
trace(curSection);

updateGrid();
Expand All @@ -957,6 +974,7 @@ class ChartingState extends MusicBeatState
return FlxMath.remapToRange(strumTime, 0, 16 * Conductor.stepCrochet, gridBG.y, gridBG.y + gridBG.height);
}

/*
function calculateSectionLengths(?sec:SwagSection):Int
{
var daLength:Int = 0;
Expand All @@ -978,7 +996,7 @@ class ChartingState extends MusicBeatState
}

return daLength;
}
}*/

private var daSpacing:Float = 0.3;

Expand Down Expand Up @@ -1014,21 +1032,15 @@ class ChartingState extends MusicBeatState
function autosaveSong():Void
{
FlxG.save.data.autosave = Json.stringify({
"song": _song,
"bpm": Conductor.bpm,
"sections": _song.notes.length,
'notes': _song.notes
"song": _song
});
FlxG.save.flush();
}

private function saveLevel()
{
var json = {
"song": _song,
"bpm": Conductor.bpm,
"sections": _song.notes.length,
'notes': _song.notes
"song": _song
};

var data:String = Json.stringify(json);
Expand Down
39 changes: 39 additions & 0 deletions source/Conductor.hx
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package;

import Song.SwagSong;

/**
* ...
* @author
*/

typedef BPMChangeEvent =
{
var stepTime:Int;
var songTime:Float;
var bpm:Int;
}

class Conductor
{
public static var bpm:Int = 100;
Expand All @@ -16,10 +26,39 @@ class Conductor
public static var safeFrames:Int = 10;
public static var safeZoneOffset:Float = (safeFrames / 60) * 1000; // is calculated in create(), is safeFrames in milliseconds

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

public function new()
{
}

public static function mapBPMChanges(song:SwagSong)
{
bpmChangeMap = [];

var curBPM:Int = song.bpm;
var totalSteps:Int = 0;
var totalPos:Float = 0;
for (i in 0...song.notes.length)
{
if(song.notes[i].changeBPM && song.notes[i].bpm != curBPM)
{
curBPM = song.notes[i].bpm;
var event:BPMChangeEvent = {
stepTime: totalSteps,
songTime: totalPos,
bpm: curBPM
};
bpmChangeMap.push(event);
}

var deltaSteps:Int = song.notes[i].lengthInSteps;
totalSteps += deltaSteps;
totalPos += ((60 / curBPM) * 1000 / 4) * deltaSteps;
}
trace("new BPM map BUDDY " + bpmChangeMap);
}

public static function changeBPM(newBpm:Int)
{
bpm = newBpm;
Expand Down
Loading