Funkin/source/ChartingState.hx

343 lines
7.8 KiB
Haxe
Raw Normal View History

package;
2020-10-09 23:22:07 -04:00
import flixel.FlxG;
2020-10-10 05:28:44 -04:00
import flixel.FlxSprite;
import flixel.FlxState;
2020-10-13 01:18:50 -04:00
import flixel.addons.display.FlxGridOverlay;
2020-10-11 23:52:21 -04:00
import flixel.addons.ui.FlxUI9SliceSprite;
import flixel.addons.ui.FlxUICheckBox;
2020-10-12 23:08:08 -04:00
import flixel.addons.ui.FlxUITooltip.FlxUITooltipStyle;
2020-10-10 00:22:26 -04:00
import flixel.group.FlxGroup.FlxTypedGroup;
2020-10-11 23:52:21 -04:00
import flixel.group.FlxGroup;
2020-10-13 01:18:50 -04:00
import flixel.math.FlxMath;
2020-10-12 23:08:08 -04:00
import flixel.math.FlxPoint;
2020-10-10 05:28:44 -04:00
import flixel.text.FlxText;
2020-10-09 23:22:07 -04:00
import flixel.ui.FlxButton;
2020-10-10 00:22:26 -04:00
import flixel.ui.FlxSpriteButton;
import flixel.util.FlxColor;
2020-10-09 23:22:07 -04:00
import haxe.Json;
import openfl.events.Event;
import openfl.events.IOErrorEvent;
import openfl.events.IOErrorEvent;
import openfl.events.IOErrorEvent;
2020-10-11 23:52:21 -04:00
import openfl.geom.Rectangle;
2020-10-09 23:22:07 -04:00
import openfl.net.FileReference;
2020-10-13 04:07:04 -04:00
using StringTools;
2020-10-09 23:22:07 -04:00
class ChartingState extends MusicBeatState
{
2020-10-09 23:22:07 -04:00
var _file:FileReference;
2020-10-10 00:22:26 -04:00
var notes:Array<Dynamic> = [];
2020-10-11 20:24:34 -04:00
2020-10-11 23:52:21 -04:00
var UI_box:FlxUI9SliceSprite;
2020-10-11 20:24:34 -04:00
/**
2020-10-11 22:05:26 -04:00
* Array of notes showing when each section STARTS in STEPS
* Usually rounded up??
2020-10-11 20:24:34 -04:00
*/
2020-10-13 04:07:04 -04:00
var curSection:Int = 0;
var sectionInfo:Array<Dynamic>;
2020-10-13 01:18:50 -04:00
2020-10-10 05:28:44 -04:00
var bpmTxt:FlxText;
var strumLine:FlxSprite;
2020-10-13 04:07:04 -04:00
var curSong:String = 'Tutorial';
2020-10-11 20:24:34 -04:00
var amountSteps:Int = 0;
2020-10-11 23:52:21 -04:00
var bullshitUI:FlxGroup;
var highlight:FlxSprite;
2020-10-13 01:18:50 -04:00
2020-10-13 04:07:04 -04:00
var GRID_SIZE:Int = 40;
2020-10-13 01:18:50 -04:00
var dummyArrow:FlxSprite;
2020-10-13 04:07:04 -04:00
var curRenderedNotes:FlxTypedGroup<Note>;
var sections:Array<Section> = [];
2020-10-13 01:18:50 -04:00
var gridBG:FlxSprite;
2020-10-09 23:22:07 -04:00
override function create()
{
2020-10-13 01:18:50 -04:00
gridBG = FlxGridOverlay.create(GRID_SIZE, GRID_SIZE, GRID_SIZE * 8, GRID_SIZE * 16);
add(gridBG);
2020-10-13 04:07:04 -04:00
curRenderedNotes = new FlxTypedGroup<Note>();
addSection();
2020-10-11 20:24:34 -04:00
FlxG.sound.playMusic('assets/music/' + curSong + '.mp3', 0.6);
2020-10-10 05:28:44 -04:00
FlxG.sound.music.pause();
2020-10-11 20:24:34 -04:00
FlxG.sound.music.onComplete = function()
{
FlxG.sound.music.pause();
FlxG.sound.music.time = 0;
};
2020-10-10 05:28:44 -04:00
Conductor.changeBPM(120);
2020-10-09 23:22:07 -04:00
var saveButton:FlxButton = new FlxButton(0, 0, "Save", function()
{
2020-10-10 00:22:26 -04:00
saveLevel();
});
saveButton.screenCenter();
add(saveButton);
2020-10-09 23:22:07 -04:00
2020-10-10 05:28:44 -04:00
bpmTxt = new FlxText(20, 20);
add(bpmTxt);
strumLine = new FlxSprite(0, 50).makeGraphic(Std.int(FlxG.width / 2), 4);
add(strumLine);
2020-10-11 23:52:21 -04:00
UI_box = new FlxUI9SliceSprite(FlxG.width / 2, 20, null, new Rectangle(0, 0, FlxG.width * 0.46, 400));
add(UI_box);
2020-10-13 01:18:50 -04:00
dummyArrow = new FlxSprite().makeGraphic(GRID_SIZE, GRID_SIZE);
add(dummyArrow);
2020-10-13 04:07:04 -04:00
add(curRenderedNotes);
2020-10-10 00:22:26 -04:00
super.create();
}
2020-10-09 23:22:07 -04:00
2020-10-11 23:52:21 -04:00
function generateUI():Void
{
while (bullshitUI.members.length > 0)
{
bullshitUI.remove(bullshitUI.members[0], true);
}
// general shit
var title:FlxText = new FlxText(UI_box.x + 20, UI_box.y + 20, 0);
bullshitUI.add(title);
2020-10-13 01:18:50 -04:00
/*
var loopCheck = new FlxUICheckBox(UI_box.x + 10, UI_box.y + 50, null, null, "Loops", 100, ['loop check']);
loopCheck.checked = curNoteSelected.doesLoop;
tooltips.add(loopCheck, {title: 'Section looping', body: "Whether or not it's a simon says style section", style: tooltipType});
bullshitUI.add(loopCheck);
2020-10-11 23:52:21 -04:00
2020-10-13 01:18:50 -04:00
*/
2020-10-11 23:52:21 -04:00
}
override function getEvent(id:String, sender:Dynamic, data:Dynamic, ?params:Array<Dynamic>)
{
if (id == FlxUICheckBox.CLICK_EVENT)
{
var check:FlxUICheckBox = cast sender;
var label = check.getLabel().text;
switch (label)
{
case 'Loops':
2020-10-13 01:18:50 -04:00
// curNoteSelected.doesLoop = check.checked;
2020-10-11 23:52:21 -04:00
}
}
// FlxG.log.add(id + " WEED " + sender + " WEED " + data + " WEED " + params);
}
2020-10-10 00:22:26 -04:00
override function update(elapsed:Float)
{
2020-10-10 05:28:44 -04:00
Conductor.songPosition = FlxG.sound.music.time;
2020-10-13 04:07:04 -04:00
strumLine.y = getYfromStrum(Conductor.songPosition % (Conductor.stepCrochet * 16));
if (curBeat % 4 == 0)
{
if (curStep > (sections[curSection].lengthInSteps * 2) * (curSection + 1))
{
if (sections[curSection + 1] == null)
{
addSection();
}
2020-10-13 01:18:50 -04:00
2020-10-13 04:07:04 -04:00
changeSection(curSection + 1, false);
}
}
if (FlxG.mouse.overlaps(gridBG))
2020-10-13 01:18:50 -04:00
{
2020-10-13 04:07:04 -04:00
dummyArrow.x = Math.floor(FlxG.mouse.x / GRID_SIZE) * GRID_SIZE;
if (FlxG.keys.pressed.SHIFT)
dummyArrow.y = FlxG.mouse.y;
else
dummyArrow.y = Math.floor(FlxG.mouse.y / GRID_SIZE) * GRID_SIZE;
if (FlxG.mouse.justPressed)
{
addNote();
}
2020-10-13 01:18:50 -04:00
}
2020-10-10 05:28:44 -04:00
if (FlxG.keys.justPressed.SPACE)
2020-10-10 00:22:26 -04:00
{
2020-10-10 05:28:44 -04:00
if (FlxG.sound.music.playing)
{
FlxG.sound.music.pause();
}
else
FlxG.sound.music.play();
}
2020-10-13 04:07:04 -04:00
if (FlxG.keys.justPressed.R)
{
changeSection();
}
2020-10-10 05:28:44 -04:00
if (FlxG.keys.justPressed.UP)
Conductor.changeBPM(Conductor.bpm + 1);
if (FlxG.keys.justPressed.DOWN)
Conductor.changeBPM(Conductor.bpm - 1);
2020-10-13 04:07:04 -04:00
if (FlxG.keys.justPressed.RIGHT)
changeSection(curSection + 1);
if (FlxG.keys.justPressed.LEFT)
changeSection(curSection - 1);
bpmTxt.text = "BPM: " + Conductor.bpm + "\nSection: " + curSection;
2020-10-10 00:22:26 -04:00
super.update(elapsed);
}
2020-10-13 04:07:04 -04:00
function changeSection(sec:Int = 0, ?updateMusic:Bool = true):Void
{
if (sections[sec] != null)
{
curSection = sec;
updateGrid();
if (updateMusic)
{
FlxG.sound.music.pause();
var daNum:Int = 0;
var daLength:Int = 0;
while (daNum <= sec)
{
daLength += sections[daNum].lengthInSteps * 2;
daNum++;
}
FlxG.sound.music.time = (daLength - (sections[sec].lengthInSteps * 2)) * Conductor.stepCrochet;
}
}
}
function updateGrid():Void
{
while (curRenderedNotes.members.length > 0)
{
curRenderedNotes.remove(curRenderedNotes.members[0], true);
}
var sectionInfo:Array<Dynamic> = sections[curSection].notes;
for (i in sectionInfo)
{
var daNoteInfo = i[1];
switch (daNoteInfo)
{
case 0:
daNoteInfo = 4;
case 1:
daNoteInfo = 3;
case 2:
daNoteInfo = 1;
case 3:
daNoteInfo = 2;
}
var note:Note = new Note(i[0], daNoteInfo);
note.setGraphicSize(GRID_SIZE, GRID_SIZE);
note.updateHitbox();
note.x = Math.floor(i[1] * GRID_SIZE);
note.y = getYfromStrum(note.strumTime);
curRenderedNotes.add(note);
}
}
private function addSection(lengthInSteps:Int = 16):Void
{
sections.push(new Section(lengthInSteps));
}
2020-10-13 01:18:50 -04:00
private function addNote():Void
{
2020-10-13 04:07:04 -04:00
sections[curSection].notes.push([getStrumTime(dummyArrow.y), Math.floor(FlxG.mouse.x / GRID_SIZE)]);
updateGrid();
2020-10-13 01:18:50 -04:00
}
function getStrumTime(yPos:Float):Float
{
2020-10-13 04:07:04 -04:00
return FlxMath.remapToRange(yPos, gridBG.y, gridBG.y + gridBG.height, 0, 16 * Conductor.stepCrochet);
}
function getYfromStrum(strumTime:Float):Float
{
return FlxMath.remapToRange(strumTime, 0, Conductor.stepCrochet * 16, gridBG.y, gridBG.y + gridBG.height);
2020-10-13 01:18:50 -04:00
}
2020-10-10 05:28:44 -04:00
private var daSpacing:Float = 0.3;
2020-10-10 00:22:26 -04:00
private function saveLevel()
{
2020-10-13 04:07:04 -04:00
var noteData:Array<Dynamic> = [];
for (i in sections)
{
noteData.push(i.notes);
}
2020-10-10 00:22:26 -04:00
var json = {
2020-10-13 04:07:04 -04:00
"song": curSong,
"bpm": Conductor.bpm,
"sections": sections.length,
'notes': noteData
2020-10-10 00:22:26 -04:00
};
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);
2020-10-13 04:07:04 -04:00
// _file.save(data.trim(), json.song + ".json");
_file.browse();
2020-10-10 00:22:26 -04:00
}
}
2020-10-09 23:22:07 -04: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");
}
}