state scripting

This commit is contained in:
Yoshman29
2022-11-10 14:11:28 +01:00
parent 94d2a3c30d
commit 949b6dc9e5
6 changed files with 120 additions and 9 deletions
+1 -1
View File
@@ -1 +1 @@
1252
1256
+2 -2
View File
@@ -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)
@@ -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<Int>;
/**
* Old height (may be null)
*/
public var oldHeight:Null<Int>;
public function new(width:Int, height:Int, ?oldWidth:Int, ?oldHeight:Int) {
super();
this.width = width;
this.height = height;
this.oldWidth = oldWidth;
this.oldHeight = oldHeight;
}
}
@@ -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;
}
+82
View File
@@ -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<Dynamic>, ?defaultVal:Dynamic):Dynamic {
// calls the function on the assigned script
if (stateScript == null) return defaultVal;
return stateScript.call(name, args);
}
public function event<T:CancellableEvent>(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");
}
}
+2 -2
View File
@@ -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;
}