Skip to content

Commit

Permalink
Transition crash fix again + changeable transition script (#493)
Browse files Browse the repository at this point in the history
* fix transition crash when skipping

* transition crash fix again

* rename transition script var

* better transition scripting

* add more transition script functions

* move post create to actually be after

* change this

* actually destroy the script

* add a variable to disable skipping the transition

* opa

idk felt better like this

---------

Co-authored-by: ⍚~Nex <87421482+NexIsDumb@users.noreply.github.com>
  • Loading branch information
TheZoroForce240 and NexIsDumb authored Dec 14, 2024
1 parent 6e2877c commit e2a3951
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 15 deletions.
69 changes: 55 additions & 14 deletions source/funkin/backend/MusicBeatTransition.hx
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
package funkin.backend;

import funkin.backend.scripting.events.CancellableEvent;
import funkin.backend.scripting.events.TransitionCreationEvent;
import funkin.backend.scripting.Script;
import flixel.tweens.FlxTween;
import flixel.FlxState;
import funkin.backend.utils.FunkinParentDisabler;

class MusicBeatTransition extends MusicBeatSubstate {
public static var script:String = "";
public var transitionScript:Script;

var nextFrameSkip:Bool = false;

public var transitionTween:FlxTween = null;
public var transitionCamera:FlxCamera;
public var newState:FlxState;
public var transOut:Bool = false;

public var allowSkip:Bool = true;

public var blackSpr:FlxSprite;
public var transitionSprite:FunkinSprite;
Expand All @@ -20,16 +29,30 @@ class MusicBeatTransition extends MusicBeatSubstate {

public override function create() {
if (newState != null)
add(new FunkinParentDisabler(true));
add(new FunkinParentDisabler(true, false));

transitionCamera = new FlxCamera();
transitionCamera.bgColor = 0;
FlxG.cameras.add(transitionCamera, false);

cameras = [transitionCamera];
var out = newState != null;

blackSpr = new FlxSprite(0, out ? -transitionCamera.height : transitionCamera.height).makeGraphic(1, 1, -1);
transitionScript = Script.create(Paths.script(script));
transitionScript.setParent(this);
transitionScript.load();

var event = EventManager.get(TransitionCreationEvent).recycle(newState != null, newState);
transitionScript.call('create', [event]);

transOut = event.transOut;
newState = event.newState;

if (event.cancelled) {
super.create();
return;
}

blackSpr = new FlxSprite(0, transOut ? -transitionCamera.height : transitionCamera.height).makeGraphic(1, 1, -1);
blackSpr.scale.set(transitionCamera.width, transitionCamera.height);
blackSpr.color = 0xFF000000;
blackSpr.updateHitbox();
Expand All @@ -43,7 +66,7 @@ class MusicBeatTransition extends MusicBeatSubstate {
} else {
transitionSprite.screenCenter();
}
transitionCamera.flipY = !out;
transitionCamera.flipY = !transOut;
add(transitionSprite);

transitionCamera.scroll.y = transitionCamera.height;
Expand All @@ -55,41 +78,59 @@ class MusicBeatTransition extends MusicBeatSubstate {
});

super.create();
transitionScript.call('postCreate', [event]);
}

public override function update(elapsed:Float) {
transitionScript.call('update', [elapsed]);
super.update(elapsed);

if (nextFrameSkip) {
finish();
return;
var event = new CancellableEvent();
transitionScript.call('onSkip', [event]);
if (!event.cancelled) {
finish();
return;
}
}

if (!parent.persistentUpdate && FlxG.keys.pressed.SHIFT) {
if (allowSkip && !parent.persistentUpdate && FlxG.keys.pressed.SHIFT) {
// skip
if (newState != null) {
nextFrameSkip = true;
parent.persistentDraw = false;
} else {
finish();
var event = new CancellableEvent();
transitionScript.call('onSkip', [event]);
if (!event.cancelled) {
finish();
}
}
}
transitionScript.call('postUpdate', [elapsed]);
}

public function finish() {
var event = new CancellableEvent();
transitionScript.call('onFinish', [event]);
if (event.cancelled) return;

if (newState != null)
FlxG.switchState(newState);
close();

transitionScript.call('onPostFinish');
}

public override function destroy() {
if (transitionTween != null)
transitionTween.cancel();
transitionScript.call('destroy');

if (transitionTween != null) transitionTween.cancel();
transitionTween = FlxDestroyUtil.destroy(transitionTween);
if (newState == null && FlxG.cameras.list.contains(transitionCamera))
FlxG.cameras.remove(transitionCamera);
else
transitionCamera.bgColor = 0xFF000000;
if (newState == null && FlxG.cameras.list.contains(transitionCamera)) FlxG.cameras.remove(transitionCamera);
else transitionCamera.bgColor = 0xFF000000;

transitionScript.destroy();
super.destroy();
}
}
18 changes: 18 additions & 0 deletions source/funkin/backend/scripting/events/TransitionCreationEvent.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package funkin.backend.scripting.events;

import flixel.FlxState;

/**
* CANCEL this event to prevent default behaviour!
*/
final class TransitionCreationEvent extends CancellableEvent {
/**
* If the transition is going out into another state
*/
public var transOut:Bool;

/**
* The state that is about to be loaded (only on trans out)
*/
public var newState:FlxState;
}
1 change: 1 addition & 0 deletions source/funkin/backend/system/MainState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class MainState extends FlxState {
}
#end

MusicBeatTransition.script = "";
Main.refreshAssets();
ModsFolder.onModSwitch.dispatch(ModsFolder.currentModFolder);
DiscordUtil.init();
Expand Down
9 changes: 8 additions & 1 deletion source/funkin/backend/utils/FunkinParentDisabler.hx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ class FunkinParentDisabler extends FlxBasic {
var __timers:Array<FlxTimer>;
var __sounds:Array<FlxSound>;
var __replaceUponDestroy:Bool;
public function new(replaceUponDestroy:Bool = false) {
var __restoreUponDestroy:Bool;
public function new(replaceUponDestroy:Bool = false, restoreUponDestroy:Bool = true) {
super();
__replaceUponDestroy = replaceUponDestroy;
__restoreUponDestroy = restoreUponDestroy;
@:privateAccess {
// tweens
__tweens = FlxTween.globalManager._tweens.copy();
Expand Down Expand Up @@ -53,6 +55,11 @@ class FunkinParentDisabler extends FlxBasic {
public override function destroy() {
super.destroy();
@:privateAccess {
if (!__restoreUponDestroy) {
for(t in __tweens) { t.cancel(); t.destroy(); };
for(t in __timers) { t.cancel(); t.destroy(); };
return;
}
if (__replaceUponDestroy) {
FlxTween.globalManager._tweens = __tweens;
FlxTimer.globalManager._timers = __timers;
Expand Down

0 comments on commit e2a3951

Please sign in to comment.