Skip to content
This repository has been archived by the owner on Jan 26, 2025. It is now read-only.

Commit

Permalink
custom states overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
MaybeMaru committed Nov 29, 2023
1 parent ebde3d5 commit 47a785d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 49 deletions.
21 changes: 4 additions & 17 deletions example_mods/CustomStateTemplate.hx
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,13 @@ function update(elapsed) {
}

function stepHit(curStep) {
super_stepHit();
super_stepHit(curStep);
}

function beatHit(curBeat) {
super_beatHit();
super_beatHit(curBeat);
}

function sectionHit(curSection) {
super_sectionHit();
}

function destroy() {
super_destroy();
}

/*
CUSTOM STATE VARIABLES
*/

Parent // FlxState instance
add();
insert();
remove();
super_sectionHit(curSection);
}
17 changes: 13 additions & 4 deletions source/funkin/Transition.hx
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,17 @@ class Transition extends ResizableSprite {
}

function setupTrans(start:Float = 0, end:Float = 0, time:Float = 1, ?callback:Dynamic) {
y = startPosition = (skipTrans ? end : start);
y = startPosition = start;
visible = !skipTrans;
endPosition = end;
transDuration = time;
timeElapsed = 0;
finishCallback = callback;
transitioning = true;

if (skipTrans) {
__finishTrans();
}
}

var timeElapsed:Float = 0;
Expand All @@ -123,10 +127,15 @@ class Transition extends ResizableSprite {
y = FlxMath.lerp(startPosition, endPosition, lerpValue);

if (timeElapsed >= transDuration) {
if (finishCallback != null) Reflect.callMethod(null, finishCallback, []);
if (inExit) visible = false;
transitioning = false;
__finishTrans();
}

}

@:noCompletion
function __finishTrans() {
if (finishCallback != null) Reflect.callMethod(null, finishCallback, []);
if (inExit) visible = false;
transitioning = false;
}
}
25 changes: 17 additions & 8 deletions source/funkin/states/MusicBeatState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,25 @@ class MusicBeatState extends FlxUIState implements IMusicGetter {
curState = CoolUtil.formatClass(this, false);
super.create();
add(musicBeat = new MusicBeat(this));

Main.transition.exitTrans();

//State Scripts
if (curState == "funkin.states.PlayState") return;
ModdingUtil.clearScripts();
var globalStateScripts:Array<String> = ModdingUtil.getScriptList('data/scripts/state');
var curStateScripts:Array<String> = ModdingUtil.getScriptList('data/scripts/state/${CoolUtil.formatClass(this).split('funkin/states/')[1]}');
for (script in globalStateScripts.concat(curStateScripts)) ModdingUtil.addScript(script);
ModdingUtil.addCall('stateCreate', []);
if (!curState.endsWith("PlayState")) {
ModdingUtil.clearScripts();
final globalStateScripts:Array<String> = ModdingUtil.getScriptList('data/scripts/state');
final curStateScripts:Array<String> = ModdingUtil.getScriptList('data/scripts/state/${CoolUtil.formatClass(this).split('funkin/states/')[1]}');
for (script in globalStateScripts.concat(curStateScripts)) ModdingUtil.addScript(script);
ModdingUtil.addCall('stateCreate', []);
}

Main.transition.exitTrans();
}

override function draw() {
if (ScriptUtil.stateQueue != null) {
CoolUtil.switchState(ScriptUtil.stateQueue.state, ScriptUtil.stateQueue.skipTrans);
ScriptUtil.stateQueue = null;
}
super.draw();
}

override function update(elapsed:Float):Void {
Expand Down
52 changes: 34 additions & 18 deletions source/funkin/util/modding/FunkScript.hx
Original file line number Diff line number Diff line change
Expand Up @@ -394,38 +394,54 @@ class CustomState extends MusicBeatState {

// This method sucks, but it works, sooooooo yeah... sorry
for (i in super_methods) {
script.set('super_' + i, function(?value:Dynamic) {
super_map.set(i, {
callback: CONTINUE_FUNCTION,
value: value,
});
script.set('super_' + i, function(?v:Dynamic) {
callDynamicSuper(i);
});
}

return this;
}

function superCallback(method:String, ?args:Dynamic) {
super_map.set(method, {
callback: STOP_FUNCTION,
value: null,
});
script.callback(method, args);
return super_map.get(method).callback == CONTINUE_FUNCTION;
final function super_create() super.create();
final function super_update(?e:Float) super.update(e ?? FlxG.elapsed);
final function super_stepHit(s:Int) super.stepHit(s);
final function super_beatHit(b:Int) super.beatHit(b);
final function super_sectionHit(s:Int) super.sectionHit(s);

function callDynamicSuper(f:String, ?v:Dynamic) {
switch(f) {
case "create": super_create();
case "update": super_update(v);
case "stepHit": super_stepHit(v);
case "beatHit": super_beatHit(v);
case "sectionHit": super_sectionHit(v);
}
}

function checkSuper(f:String, ?v:Array<Dynamic>) {
if (script.exists(f)) {
script.callback(f, v);
} else {
callDynamicSuper(f, v[0]); // Gets called if function doesnt have an override
}
}

override public function create() {
ModdingUtil.addPrint(_scriptKey + " / Custom State");
if (superCallback('create')) super.create();
checkSuper("create");
}

override public function update(elapsed:Float) {
if (FlxG.keys.justPressed.F4) switchState(new StoryMenuState()); // emergency exit
if (FlxG.keys.justPressed.F5) ScriptUtil.switchCustomState(_scriptKey, Transition.skipTrans);
if (superCallback('update', [elapsed])) super.update(super_map.get('update').value);
checkSuper("update", [elapsed]);
}

override public function stepHit(curStep) if (superCallback('stepHit', [curStep])) super.stepHit(curStep);
override public function beatHit(curBeat) if (superCallback('beatHit', [curBeat])) super.beatHit(curBeat);
override public function sectionHit(curSection) if (superCallback('sectionHit', [curSection])) super.sectionHit(curSection);
override public function destroy() if (superCallback('destroy')) super.destroy();
override public function stepHit(curStep) checkSuper("stepHit", [curStep]);
override public function beatHit(curBeat) checkSuper("beatHit", [curBeat]);
override public function sectionHit(curSection) checkSuper("sectionHit", [curSection]);
override public function destroy() {
script.callback("destroy");
super.destroy();
}
}
8 changes: 6 additions & 2 deletions source/funkin/util/modding/ScriptUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class ScriptUtil {
inline public static function getGroup(key:String) {
return key == 'fg' ? PlayState.instance.fgSpr : PlayState.instance.bgSpr;
}

public static var stateQueue:{state:MusicBeatState, skipTrans:Bool} = null;

inline public static function switchCustomState(key:String, skipTrans:Bool) {
final scriptCode = CoolUtil.getFileContent(Paths.script('scripts/customStates/$key'));
Expand All @@ -57,7 +59,9 @@ class ScriptUtil {
return;
}

final state = new CustomState().initScript(scriptCode, key);
CoolUtil.switchState(state, skipTrans);
stateQueue = {
state: new CustomState().initScript(scriptCode, key),
skipTrans: skipTrans
}
}
}

0 comments on commit 47a785d

Please sign in to comment.