1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2024-11-10 00:34:40 +00:00
Funkin/source/ChartingState.hx

261 lines
6.1 KiB
Haxe
Raw Normal View History

package;
2020-10-10 03:22:07 +00:00
import flixel.FlxG;
2020-10-10 09:28:44 +00:00
import flixel.FlxSprite;
import flixel.FlxState;
2020-10-10 04:22:26 +00:00
import flixel.group.FlxGroup.FlxTypedGroup;
2020-10-10 09:28:44 +00:00
import flixel.text.FlxText;
2020-10-10 03:22:07 +00:00
import flixel.ui.FlxButton;
2020-10-10 04:22:26 +00:00
import flixel.ui.FlxSpriteButton;
import flixel.util.FlxColor;
2020-10-10 03:22:07 +00:00
import haxe.Json;
import openfl.events.Event;
import openfl.events.IOErrorEvent;
import openfl.events.IOErrorEvent;
import openfl.events.IOErrorEvent;
import openfl.net.FileReference;
2020-10-10 03:22:07 +00:00
class ChartingState extends MusicBeatState
{
2020-10-10 03:22:07 +00:00
var _file:FileReference;
2020-10-10 09:28:44 +00:00
var sequencer:FlxTypedGroup<DisplayNote>;
2020-10-12 02:05:26 +00:00
var sectionShit:FlxTypedGroup<DisplayNote>;
2020-10-10 04:22:26 +00:00
var notes:Array<Dynamic> = [];
2020-10-12 00:24:34 +00:00
/**
2020-10-12 02:05:26 +00:00
* Array of notes showing when each section STARTS in STEPS
* Usually rounded up??
2020-10-12 00:24:34 +00:00
*/
2020-10-12 02:05:26 +00:00
var sectionData:Map<Int, Int>;
2020-10-12 00:24:34 +00:00
var section:Int = 0;
2020-10-10 09:28:44 +00:00
var bpmTxt:FlxText;
var strumLine:FlxSprite;
2020-10-12 00:24:34 +00:00
var curSong:String = 'Fresh';
var amountSteps:Int = 0;
2020-10-10 03:22:07 +00:00
override function create()
{
2020-10-12 00:24:34 +00:00
FlxG.sound.playMusic('assets/music/' + curSong + '.mp3', 0.6);
2020-10-10 09:28:44 +00:00
FlxG.sound.music.pause();
2020-10-12 00:24:34 +00:00
FlxG.sound.music.onComplete = function()
{
FlxG.sound.music.pause();
FlxG.sound.music.time = 0;
};
2020-10-10 09:28:44 +00:00
Conductor.changeBPM(120);
2020-10-10 03:22:07 +00:00
var saveButton:FlxButton = new FlxButton(0, 0, "Save", function()
{
2020-10-10 04:22:26 +00:00
saveLevel();
});
saveButton.screenCenter();
add(saveButton);
2020-10-10 03:22:07 +00:00
2020-10-10 09:28:44 +00:00
bpmTxt = new FlxText(20, 20);
add(bpmTxt);
strumLine = new FlxSprite(0, 50).makeGraphic(Std.int(FlxG.width / 2), 4);
add(strumLine);
2020-10-12 02:05:26 +00:00
sectionShit = new FlxTypedGroup<DisplayNote>();
2020-10-10 04:22:26 +00:00
createStepChart();
2020-10-12 02:05:26 +00:00
sectionData = new Map<Int, Int>();
sectionData.set(0, 0);
updateSectionColors();
2020-10-10 03:22:07 +00:00
2020-10-10 04:22:26 +00:00
super.create();
}
2020-10-10 03:22:07 +00:00
2020-10-12 02:05:26 +00:00
function updateSectionColors():Void
{
sectionShit.forEach(function(note:DisplayNote)
{
sequencer.remove(note, true);
sectionShit.remove(note, true);
note.destroy();
});
for (i in sectionData.keys())
{
var sec:FlxText = new FlxText(strumLine.width, 0, 0, "Section " + i);
var sectionTex:DisplayNote = createDisplayNote(5, i - 1, sec);
sectionTex.strumTime = sectionData.get(i) * Conductor.stepCrochet;
sequencer.add(sectionTex);
sectionShit.add(sectionTex);
trace(i);
}
}
function createDisplayNote(row:Float, column:Float, ?spr:FlxSprite, ?func:Void->Void):DisplayNote
{
return new DisplayNote((35 * row) + 10, (35 * column) + 50, spr, func);
}
2020-10-10 04:22:26 +00:00
function createStepChart()
{
2020-10-10 09:28:44 +00:00
sequencer = new FlxTypedGroup<DisplayNote>();
2020-10-10 04:22:26 +00:00
add(sequencer);
2020-10-12 00:24:34 +00:00
amountSteps = Math.floor(FlxG.sound.music.length / Conductor.stepCrochet);
2020-10-10 09:28:44 +00:00
for (r in 0...4)
2020-10-10 04:22:26 +00:00
{
notes.push([]);
2020-10-12 00:24:34 +00:00
for (i in 0...amountSteps)
2020-10-10 03:22:07 +00:00
{
2020-10-10 04:22:26 +00:00
notes[r].push(false);
2020-10-12 02:05:26 +00:00
var seqBtn:DisplayNote = createDisplayNote(r, i, null, function()
2020-10-10 04:22:26 +00:00
{
2020-10-10 09:28:44 +00:00
if (notes[r][i] == 0)
notes[r][i] = 1;
else
notes[r][i] = 0;
2020-10-10 04:22:26 +00:00
});
2020-10-10 09:28:44 +00:00
seqBtn.strumTime = Conductor.stepCrochet * i;
2020-10-10 04:22:26 +00:00
seqBtn.makeGraphic(30, 30, FlxColor.WHITE);
2020-10-12 00:24:34 +00:00
seqBtn.ID = i + (amountSteps * r);
2020-10-10 04:22:26 +00:00
sequencer.add(seqBtn);
2020-10-10 03:22:07 +00:00
}
2020-10-10 04:22:26 +00:00
}
}
override function update(elapsed:Float)
{
2020-10-10 09:28:44 +00:00
Conductor.songPosition = FlxG.sound.music.time;
2020-10-12 00:24:34 +00:00
if (FlxG.mouse.justPressedMiddle && section > 0)
{
2020-10-12 02:05:26 +00:00
var pushSection:Int = Math.round(Conductor.songPosition / Conductor.crochet) * 4;
2020-10-12 00:24:34 +00:00
2020-10-12 02:05:26 +00:00
sectionData.set(section, pushSection);
updateSectionColors();
2020-10-12 00:24:34 +00:00
}
if (FlxG.keys.justPressed.LEFT || FlxG.keys.justPressed.RIGHT)
{
FlxG.sound.music.pause();
if (FlxG.keys.justPressed.RIGHT)
{
2020-10-12 02:05:26 +00:00
if (section + 1 <= Lambda.count(sectionData))
2020-10-12 00:24:34 +00:00
section += 1;
else
section = 0;
}
if (FlxG.keys.justPressed.LEFT)
{
if (section > 0)
section -= 1;
else
2020-10-12 02:05:26 +00:00
section = Lambda.count(sectionData);
2020-10-12 00:24:34 +00:00
}
2020-10-12 02:05:26 +00:00
if (sectionData.exists(section))
FlxG.sound.music.time = sectionData.get(section) * Conductor.stepCrochet;
2020-10-12 00:24:34 +00:00
}
2020-10-12 02:05:26 +00:00
if (FlxG.keys.justPressed.R && sectionData.exists(section))
FlxG.sound.music.time = sectionData.get(section) * Conductor.stepCrochet;
2020-10-12 00:24:34 +00:00
2020-10-10 09:28:44 +00:00
if (FlxG.sound.music.playing)
{
}
else
{
if (FlxG.keys.pressed.W)
FlxG.sound.music.time -= 900 * FlxG.elapsed;
if (FlxG.keys.pressed.S)
FlxG.sound.music.time += 900 * FlxG.elapsed;
}
if (FlxG.keys.justPressed.SPACE)
2020-10-10 04:22:26 +00:00
{
2020-10-10 09:28:44 +00:00
if (FlxG.sound.music.playing)
{
FlxG.sound.music.pause();
}
else
FlxG.sound.music.play();
}
if (FlxG.keys.justPressed.UP)
Conductor.changeBPM(Conductor.bpm + 1);
if (FlxG.keys.justPressed.DOWN)
Conductor.changeBPM(Conductor.bpm - 1);
2020-10-12 00:24:34 +00:00
bpmTxt.text = "BPM: " + Conductor.bpm + "\nSection: " + section;
2020-10-10 09:28:44 +00:00
sequencer.forEach(function(spr:DisplayNote)
{
2020-10-12 00:24:34 +00:00
if (notes[Std.int(spr.ID / amountSteps)][spr.ID % amountSteps] != 0)
2020-10-10 04:22:26 +00:00
spr.alpha = 1;
else
spr.alpha = 0.5;
2020-10-10 09:28:44 +00:00
spr.y = (strumLine.y - (Conductor.songPosition - spr.strumTime) * daSpacing);
2020-10-10 03:22:07 +00:00
});
2020-10-10 04:22:26 +00:00
super.update(elapsed);
}
2020-10-10 09:28:44 +00:00
private var daSpacing:Float = 0.3;
2020-10-10 04:22:26 +00:00
private function saveLevel()
{
var json = {
"song": "Bopeebo",
"bpm": 100,
"sections": 15
};
var data:String = Json.stringify(json);
if ((data != null) && (data.length > 0))
{
_file = new FileReference();
_file.addEventListener(Event.COMPLETE, onSaveComplete);
_file.addEventListener(Event.CANCEL, onSaveCancel);
_file.addEventListener(IOErrorEvent.IO_ERROR, onSaveError);
_file.save(data, json.song + ".json");
}
}
2020-10-10 03:22:07 +00:00
function onSaveComplete(_):Void
{
_file.removeEventListener(Event.COMPLETE, onSaveComplete);
_file.removeEventListener(Event.CANCEL, onSaveCancel);
_file.removeEventListener(IOErrorEvent.IO_ERROR, onSaveError);
_file = null;
FlxG.log.notice("Successfully saved LEVEL DATA.");
}
/**
* Called when the save file dialog is cancelled.
*/
function onSaveCancel(_):Void
{
_file.removeEventListener(Event.COMPLETE, onSaveComplete);
_file.removeEventListener(Event.CANCEL, onSaveCancel);
_file.removeEventListener(IOErrorEvent.IO_ERROR, onSaveError);
_file = null;
}
/**
* Called if there is an error while saving the gameplay recording.
*/
function onSaveError(_):Void
{
_file.removeEventListener(Event.COMPLETE, onSaveComplete);
_file.removeEventListener(Event.CANCEL, onSaveCancel);
_file.removeEventListener(IOErrorEvent.IO_ERROR, onSaveError);
_file = null;
FlxG.log.error("Problem saving Level data");
}
}