diff --git a/buildnumber.txt b/buildnumber.txt index 03ce6dfa..832d4caf 100644 --- a/buildnumber.txt +++ b/buildnumber.txt @@ -1 +1 @@ -1252 \ No newline at end of file +1256 \ No newline at end of file diff --git a/source/funkin/game/PlayState.hx b/source/funkin/game/PlayState.hx index 9c8ef4a8..a0f8ca53 100644 --- a/source/funkin/game/PlayState.hx +++ b/source/funkin/game/PlayState.hx @@ -721,7 +721,7 @@ class PlayState extends MusicBeatState override function openSubState(SubState:FlxSubState) { - var event = scripts.event("onSubstateOpen", new SubstateEvent(SubState)); + var event = scripts.event("onSubstateOpen", new StateEvent(SubState)); if (!postCreated) FlxTransitionableState.skipNextTransIn = true; @@ -745,7 +745,7 @@ class PlayState extends MusicBeatState override function closeSubState() { - var event = scripts.event("onSubstateClose", new SubstateEvent(subState)); + var event = scripts.event("onSubstateClose", new StateEvent(subState)); if (event.cancelled) return; if (paused) diff --git a/source/funkin/scripting/events/ResizeEvent.hx b/source/funkin/scripting/events/ResizeEvent.hx new file mode 100644 index 00000000..8abaa0ea --- /dev/null +++ b/source/funkin/scripting/events/ResizeEvent.hx @@ -0,0 +1,29 @@ +package funkin.scripting.events; + +class ResizeEvent extends CancellableEvent { + /** + * New width + */ + public var width:Int; + /** + * New height + */ + public var height:Int; + + /** + * Old width (may be null) + */ + public var oldWidth:Null; + + /** + * Old height (may be null) + */ + public var oldHeight:Null; + public function new(width:Int, height:Int, ?oldWidth:Int, ?oldHeight:Int) { + super(); + this.width = width; + this.height = height; + this.oldWidth = oldWidth; + this.oldHeight = oldHeight; + } +} \ No newline at end of file diff --git a/source/funkin/scripting/events/SubstateEvent.hx b/source/funkin/scripting/events/StateEvent.hx similarity index 50% rename from source/funkin/scripting/events/SubstateEvent.hx rename to source/funkin/scripting/events/StateEvent.hx index 541db4fe..6b5e625e 100644 --- a/source/funkin/scripting/events/SubstateEvent.hx +++ b/source/funkin/scripting/events/StateEvent.hx @@ -1,14 +1,14 @@ package funkin.scripting.events; -import flixel.FlxSubState; +import flixel.FlxState; -class SubstateEvent extends CancellableEvent { +class StateEvent extends CancellableEvent { /** * Substate that is about to be opened/closed */ - public var substate:FlxSubState; + public var substate:FlxState; - public function new(substate:FlxSubState) { + public function new(substate:FlxState) { super(); this.substate = substate; } diff --git a/source/funkin/system/MusicBeatState.hx b/source/funkin/system/MusicBeatState.hx index 98c2ecef..2259a8c3 100644 --- a/source/funkin/system/MusicBeatState.hx +++ b/source/funkin/system/MusicBeatState.hx @@ -1,5 +1,9 @@ package funkin.system; +import flixel.FlxState; +import flixel.FlxSubState; +import funkin.scripting.events.*; +import funkin.scripting.Script; import funkin.interfaces.IBeatReceiver; import funkin.system.Conductor.BPMChangeEvent; import funkin.system.Conductor; @@ -58,13 +62,51 @@ class MusicBeatState extends FlxUIState implements IBeatReceiver */ public var controls(get, never):Controls; + /** + * Current injected script attached to the state. To add one, create a file at path "scripts/stateName" (ex: "scripts/") + */ + public var stateScript:Script; + + public var scriptAllowed(get, null):Bool; + + // OVERRIDE THIS TO DISABLE SCRIPTS!! + public function get_scriptAllowed() {return true;} inline function get_controls():Controls return PlayerSettings.player1.controls; + public function new(scriptsAllowed:Bool = true) { + super(); + + if (scriptsAllowed) { + var className = Type.getClassName(Type.getClass(this)); + var scriptName = className.substr(className.lastIndexOf(".")+1); + + stateScript = Script.create(Paths.script('data/states/${scriptName}')); + stateScript.setParent(this); + stateScript.load(); + } + } override function create() { super.create(); + call("create"); + } + + public override function createPost() { + super.createPost(); + call("createPost"); + } + public function call(name:String, ?args:Array, ?defaultVal:Dynamic):Dynamic { + // calls the function on the assigned script + if (stateScript == null) return defaultVal; + return stateScript.call(name, args); + } + + public function event(name:String, event:T):T { + if (stateScript == null) return event; + stateScript.call(name, [event]); + return event; } override function update(elapsed:Float) @@ -73,17 +115,20 @@ class MusicBeatState extends FlxUIState implements IBeatReceiver var oldStep:Int = curStep; + call("update"); super.update(elapsed); } @:dox(hide) public function stepHit(curStep:Int):Void { for(e in members) if (e is IBeatReceiver) cast(e, IBeatReceiver).stepHit(curStep); + call("stepHit", [curStep]); } @:dox(hide) public function beatHit(curBeat:Int):Void { for(e in members) if (e is IBeatReceiver) cast(e, IBeatReceiver).beatHit(curBeat); + call("beatHit", [curBeat]); } /** @@ -99,4 +144,41 @@ class MusicBeatState extends FlxUIState implements IBeatReceiver else return CoolUtil.fpsLerp(v1, v2, ratio); } + + /** + * SCRIPTING STUFF + */ + public override function openSubState(subState:FlxSubState) { + var e = event("onOpenSubState", new StateEvent(subState)); + if (!e.cancelled) + super.openSubState(subState); + } + + public override function onResize(w:Int, h:Int) { + super.onResize(w, h); + event("onResize", new ResizeEvent(w, h)); + } + + public override function destroy() { + super.destroy(); + call("onDestroy"); + stateScript.destroy(); + } + + public override function switchTo(nextState:FlxState) { + var e = event("onStateSwitch", new StateEvent(nextState)); + if (e.cancelled) + return false; + return super.switchTo(nextState); + } + + public override function onFocus() { + super.onFocus(); + call("onFocus"); + } + + public override function onFocusLost() { + super.onFocusLost(); + call("onFocusLost"); + } } diff --git a/source/scripting/PlayStateScript.hx b/source/scripting/PlayStateScript.hx index 18ffdacd..b459274e 100644 --- a/source/scripting/PlayStateScript.hx +++ b/source/scripting/PlayStateScript.hx @@ -98,11 +98,11 @@ interface PlayStateScript { * Triggered on substate open. * @param event Event object containing the substate, which is cancellable. */ - public function onSubstateOpen(event:SubstateEvent):Void; + public function onSubstateOpen(event:StateEvent):Void; /** * Triggered on substate close. * @param event Event object containing the substate, which is cancellable. */ - public function onSubstateClose(event:SubstateEvent):Void; + public function onSubstateClose(event:StateEvent):Void; } \ No newline at end of file