characters xml events + script extensions too!

Co-Authored-By: Ne_Eo <NeeEoo@users.noreply.github.com>
This commit is contained in:
⍚~Nex
2024-10-18 01:17:09 +02:00
co-authored by Ne_Eo
parent 0db00222ca
commit 682358c93a
6 changed files with 158 additions and 69 deletions
@@ -0,0 +1,21 @@
package funkin.backend.scripting.events;
import funkin.game.Character;
import haxe.xml.Access;
final class CharacterNodeEvent extends CancellableEvent {
/**
* The character instance
*/
public var character:Character;
/**
* The node which is currently being parsed
*/
public var node:Access;
/**
* The name of the node, quicker access than e.node.name
*/
public var name:String;
}
@@ -0,0 +1,16 @@
package funkin.backend.scripting.events;
import funkin.game.Character;
import haxe.xml.Access;
final class CharacterXMLEvent extends CancellableEvent {
/**
* The character instance
*/
public var character:Character;
/**
* The xml
*/
public var xml:Access;
}
@@ -62,6 +62,7 @@ class AudioSwitchFix {
}
}
@:dox(hide)
typedef PlayingSound = {
var sound:FlxSound;
var time:Float;
+48
View File
@@ -6,6 +6,9 @@ import funkin.backend.system.ErrorCode;
import funkin.backend.FunkinSprite.XMLAnimType;
import flixel.util.FlxColor;
import haxe.xml.Access;
import funkin.backend.scripting.Script;
import funkin.backend.scripting.DummyScript;
import funkin.backend.scripting.ScriptPack;
import flixel.util.typeLimit.OneOfTwo;
import funkin.backend.system.interfaces.IOffsetCompatible;
@@ -326,6 +329,51 @@ class XMLUtil {
}
}
class XMLImportedScriptInfo {
public var path:String;
public var shortLived:Bool = false;
public var loadBefore:Bool = true;
public var importStageSprites:Bool = false; // maybe will change later?? - Nex
public var parentScriptPack:ScriptPack = null;
public function new(path:String, parentScriptPack:ScriptPack) {
this.parentScriptPack = parentScriptPack;
this.path = path;
}
public function getScript():Script
return parentScriptPack == null ? null : parentScriptPack.getByPath(path);
public static function prepareInfos(node:Access, parentScriptPack:ScriptPack, ?onScriptPreLoad:XMLImportedScriptInfo->Void):XMLImportedScriptInfo {
if (!node.has.script || parentScriptPack == null) return null;
var folder = node.getAtt("folder").getDefault("data/scripts/");
if (!folder.endsWith("/")) folder += "/";
var path = Paths.script(folder + node.getAtt("script"));
var daScript = Script.create(path);
if (daScript is DummyScript) {
Logs.trace('Script Extension at ${path} does not exist.', ERROR);
return null;
}
var infos = new XMLImportedScriptInfo(daScript.path, parentScriptPack);
infos.shortLived = node.getAtt("isShortLived") == "true" || node.getAtt("shortLived") == "true";
infos.importStageSprites = node.getAtt("importStageSprites") == "true";
@:privateAccess infos.loadBefore = shouldLoadBefore(node);
if (onScriptPreLoad != null) onScriptPreLoad(infos);
parentScriptPack.add(daScript);
daScript.set("scriptInfo", infos);
daScript.load();
return infos;
}
@:dox(hide) public static inline function shouldLoadBefore(node:Access):Bool
return node.getAtt("loadBefore") != "false";
}
typedef AnimData = {
var name:String;
var anim:String;
+65 -22
View File
@@ -8,6 +8,9 @@ import flixel.util.FlxColor;
import funkin.backend.FunkinSprite;
import funkin.backend.scripting.DummyScript;
import funkin.backend.scripting.Script;
import funkin.backend.scripting.ScriptPack;
import funkin.backend.scripting.events.CharacterNodeEvent;
import funkin.backend.scripting.events.CharacterXMLEvent;
import funkin.backend.scripting.events.DanceEvent;
import funkin.backend.scripting.events.DirectionAnimEvent;
import funkin.backend.scripting.events.PlayAnimEvent;
@@ -44,8 +47,22 @@ class Character extends FunkinSprite implements IBeatReceiver implements IOffset
public var cameraOffset:FlxPoint = FlxPoint.get(0, 0);
public var globalOffset:FlxPoint = FlxPoint.get(0, 0);
public var script:Script;
public var xml:Access;
public var scripts:ScriptPack;
public var xmlImportedScripts:Array<XMLImportedScriptInfo> = [];
public var script(default, set):Script;
public function prepareInfos(node:Access)
return XMLImportedScriptInfo.prepareInfos(node, scripts, (infos) -> xmlImportedScripts.push(infos));
// backward compat - Nex
private function set_script(script:Script):Script {
if (scripts == null) (scripts = new ScriptPack("Character")).setParent(this);
else scripts.remove(scripts.scripts[0]);
if(script != null) scripts.insert(0, script);
this.script = script;
return script;
}
public var idleSuffix:String = "";
public var stunned(default, set):Bool = false;
@@ -71,16 +88,15 @@ class Character extends FunkinSprite implements IBeatReceiver implements IOffset
script = Script.create(Paths.script(Path.withoutExtension(Paths.xml('characters/$curCharacter')), null, true));
else
script = new DummyScript(curCharacter);
script.setParent(this);
script.load();
buildCharacter(xml);
script.call("create");
scripts.call("create");
if (script == null)
script = new DummyScript(curCharacter);
script.call("postCreate");
scripts.call("postCreate");
}
@:noCompletion var __swappedLeftRightAnims:Bool = false;
@@ -92,9 +108,9 @@ class Character extends FunkinSprite implements IBeatReceiver implements IOffset
__autoInterval = autoInterval;
// character is flipped
if (isPlayer != playerOffsets && switchAnims)
if (isPlayer != playerOffsets && switchAnims)
swapLeftRightAnimations();
frameOffset.set(getAnimOffset(getAnimName()).x, getAnimOffset(getAnimName()).y);
if (isPlayer) flipX = !flipX;
__baseFlipped = flipX;
@@ -115,7 +131,7 @@ class Character extends FunkinSprite implements IBeatReceiver implements IOffset
override function update(elapsed:Float) {
super.update(elapsed);
script.call("update", [elapsed]);
scripts.call("update", [elapsed]);
if (stunned) {
__stunnedTime += elapsed;
if (__stunnedTime > 5 / 60)
@@ -134,7 +150,7 @@ class Character extends FunkinSprite implements IBeatReceiver implements IOffset
if(debugMode) return;
var event = EventManager.get(DanceEvent).recycle(danced);
script.call("onDance", [event]);
scripts.call("onDance", [event]);
if (event.cancelled) return;
if (isDanceLeftDanceRight)
@@ -164,14 +180,14 @@ class Character extends FunkinSprite implements IBeatReceiver implements IOffset
*/
public var danceOnBeat:Bool = true;
public override function beatHit(curBeat:Int) {
script.call("beatHit", [curBeat]);
scripts.call("beatHit", [curBeat]);
if (danceOnBeat && (curBeat + beatOffset) % beatInterval == 0 && !__lockAnimThisFrame)
tryDance();
}
public override function stepHit(curStep:Int)
script.call("stepHit", [curStep]);
scripts.call("stepHit", [curStep]);
@:noCompletion var __reverseDrawProcedure:Bool = false;
public override function getScreenBounds(?newRect:FlxRect, ?camera:FlxCamera):FlxRect {
@@ -209,14 +225,14 @@ class Character extends FunkinSprite implements IBeatReceiver implements IOffset
public var singAnims = ["singLEFT", "singDOWN", "singUP", "singRIGHT"];
public function playSingAnim(direction:Int, suffix:String = "", Context:PlayAnimContext = SING, ?Force:Null<Bool> = null, Reversed:Bool = false, Frame:Int = 0) {
var event = EventManager.get(DirectionAnimEvent).recycle(singAnims[direction % singAnims.length] + suffix, direction, suffix, Context, Reversed, Frame, Force);
script.call("onPlaySingAnim", [event]);
scripts.call("onPlaySingAnim", [event]);
if (!event.cancelled)
playAnim(event.animName, event.force, event.context, event.reversed, event.frame);
}
public override function playAnim(AnimName:String, ?Force:Bool, Context:PlayAnimContext = NONE, Reversed:Bool = false, Frame:Int = 0) {
var event = EventManager.get(PlayAnimEvent).recycle(AnimName, Force, Reversed, Frame, Context);
script.call("onPlayAnim", [event]);
scripts.call("onPlayAnim", [event]);
if (event.cancelled) return;
super.playAnim(event.animName, event.force, event.context, event.reverse, event.startingFrame);
@@ -231,15 +247,15 @@ class Character extends FunkinSprite implements IBeatReceiver implements IOffset
var event = EventManager.get(PointEvent).recycle(
midpoint.x + (isPlayer ? -100 : 150) + globalOffset.x + cameraOffset.x,
midpoint.y - 100 + globalOffset.y + cameraOffset.y);
script.call("onGetCamPos", [event]);
scripts.call("onGetCamPos", [event]);
midpoint.put();
return new FlxPoint(event.x, event.y);
}
public override function destroy() {
script.call('destroy');
script.destroy();
scripts.call('destroy');
scripts.destroy();
super.destroy();
cameraOffset.put();
@@ -281,9 +297,18 @@ class Character extends FunkinSprite implements IBeatReceiver implements IOffset
}
public inline function buildCharacter(xml:Access) {
this.xml = xml; // Modders wassup :D
for(node in xml.elements)
switch(node.name) {
case "use-extension" | "extension" | "ext":
if (!XMLImportedScriptInfo.shouldLoadBefore(node)) continue;
prepareInfos(node);
}
xml = scripts.event("onCharacterXMLParsed", EventManager.get(CharacterXMLEvent).recycle(this, xml)).xml;
sprite = curCharacter;
spriteAnimType = BEAT;
this.xml = xml; // Modders wassup :D
if (xml.x.exists("isPlayer")) playerOffsets = (xml.x.get("isPlayer") == "true");
if (xml.x.exists("x")) globalOffset.x = Std.parseFloat(xml.x.get("x"));
@@ -307,9 +332,19 @@ class Character extends FunkinSprite implements IBeatReceiver implements IOffset
if (hasInterval) beatInterval = Std.parseInt(xml.x.get("interval"));
loadSprite(Paths.image('characters/$sprite'));
for(node in xml.elements) {
switch(node.name) {
case "anim":
XMLUtil.addXMLAnimation(this, node);
case "use-extension" | "extension" | "ext":
if (XMLImportedScriptInfo.shouldLoadBefore(node)) continue;
prepareInfos(node);
default:
// nothing
}
for (anim in xml.nodes.anim)
XMLUtil.addXMLAnimation(this, anim);
scripts.event("onCharacterNodeParsed", EventManager.get(CharacterNodeEvent).recycle(this, node, node.name));
}
for (attribute in xml.x.attributes())
if (!characterProperties.contains(attribute))
@@ -317,11 +352,19 @@ class Character extends FunkinSprite implements IBeatReceiver implements IOffset
fixChar(__switchAnims, !hasInterval);
dance();
for (info in xmlImportedScripts) if (info.shortLived) {
var script = info.getScript();
if (script == null) continue;
scripts.remove(script);
script.destroy();
}
}
public static var characterProperties:Array<String> = [
"x", "y", "sprite", "scale", "antialiasing",
"flipX", "camx", "camy", "isPlayer", "icon",
"x", "y", "sprite", "scale", "antialiasing",
"flipX", "camx", "camy", "isPlayer", "icon",
"color", "gameOverChar", "holdTime"
];
public static var characterAnimProperties:Array<String> = [
@@ -331,7 +374,7 @@ class Character extends FunkinSprite implements IBeatReceiver implements IOffset
public inline function buildXML(?animsOrder:Array<String>):Xml {
var xml:Xml = Xml.createElement("character");
xml.attributeOrder = characterProperties.copy();
if (globalOffset.x != 0) xml.set("x", Std.string(FlxMath.roundDecimal(globalOffset.x, 2)));
if (globalOffset.y != 0) xml.set("y", Std.string(FlxMath.roundDecimal(globalOffset.y, 2)));
@@ -436,7 +479,7 @@ class Character extends FunkinSprite implements IBeatReceiver implements IOffset
Logs.trace('Error while loading character ${character}: ${e}', ERROR);
character = FALLBACK_CHARACTER;
if (char != null)
if (char != null)
char.curCharacter = character;
continue;
}
+7 -47
View File
@@ -5,6 +5,7 @@ import funkin.backend.scripting.events.StageNodeEvent;
import flixel.math.FlxPoint;
import flixel.FlxState;
import haxe.xml.Access;
import funkin.backend.utils.XMLUtil.XMLImportedScriptInfo;
import funkin.backend.system.interfaces.IBeatReceiver;
import funkin.backend.scripting.DummyScript;
import funkin.backend.scripting.Script;
@@ -30,6 +31,9 @@ class Stage extends FlxBasic implements IBeatReceiver {
public function setStagesSprites(script:Script)
for (k=>e in stageSprites) script.set(k, e);
public function prepareInfos(node:Access)
return XMLImportedScriptInfo.prepareInfos(node, PlayState.instance.scripts, (infos) -> xmlImportedScripts.push(infos));
public function new(stage:String, ?state:FlxState) {
super();
@@ -136,7 +140,7 @@ class Stage extends FlxBasic implements IBeatReceiver {
PlayState.instance.add(PlayState.instance.comboGroup);
PlayState.instance.comboGroup;
case "use-extension" | "extension" | "ext":
if (__shouldLoadBefore(node) || XMLImportedScriptInfo.prepareInfos(node, this) == null) continue;
if (XMLImportedScriptInfo.shouldLoadBefore(node) || prepareInfos(node) == null) continue;
null;
default: null;
}
@@ -201,13 +205,10 @@ class Stage extends FlxBasic implements IBeatReceiver {
@:dox(hide) private function __pushNcheckNode(array:Array<Access>, node:Access) {
array.push(node);
if ((node.name == "use-extension" || node.name == "extension" || node.name == "ext") && __shouldLoadBefore(node))
XMLImportedScriptInfo.prepareInfos(node, this);
if ((node.name == "use-extension" || node.name == "extension" || node.name == "ext") && XMLImportedScriptInfo.shouldLoadBefore(node))
prepareInfos(node);
}
@:dox(hide) private static inline function __shouldLoadBefore(node:Access):Bool
return node.getAtt("loadBefore") != "false";
public function addCharPos(name:String, node:Access, ?nonXMLInfo:StageCharPosInfo):StageCharPos {
var charPos = new StageCharPos();
charPos.visible = charPos.active = false;
@@ -317,45 +318,4 @@ typedef StageCharPosInfo = {
var y:Float;
var flip:Bool;
var scroll:Float;
}
class XMLImportedScriptInfo {
public var path:String;
public var shortLived:Bool = false;
public var loadBefore:Bool = true;
public var importStageSprites:Bool = false;
public function new(path:String)
this.path = path;
public function getScript():Script
return PlayState.instance == null ? null : PlayState.instance.scripts.getByPath(path);
public static function prepareInfos(node:Access, ?stage:Stage):XMLImportedScriptInfo {
if (!node.has.script || PlayState.instance == null) return null;
var folder = node.getAtt("folder").getDefault("data/scripts/");
if (!folder.endsWith("/")) folder += "/";
var path = Paths.script(folder + node.getAtt("script"));
var daScript = Script.create(path);
if (daScript is DummyScript) {
var msg = 'Script Extension at ${path} does not exist';
if (stage != null) msg += ' through stage "${stage.stageName}"';
Logs.trace(msg + ".", ERROR);
return null;
}
var infos = new XMLImportedScriptInfo(daScript.path);
infos.shortLived = node.getAtt("isShortLived") == "true";
infos.importStageSprites = node.getAtt("importStageSprites") == "true";
@:privateAccess infos.loadBefore = Stage.__shouldLoadBefore(node);
if (stage != null) stage.xmlImportedScripts.push(infos);
PlayState.instance.scripts.add(daScript);
daScript.set("scriptInfo", infos);
daScript.load();
return infos;
}
}