Skip to content

Commit

Permalink
clean up code a lot in playstate for scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Vortex2Oblivion committed Mar 24, 2024
1 parent 3521a15 commit 2f8d3a6
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 286 deletions.
18 changes: 9 additions & 9 deletions source/modding/custom/CustomState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import states.TitleState;
import flixel.FlxObject;


class CustomState extends MusicBeatState {
class CustomState extends MusicBeatState implements IHScriptable{
public var script:HScript;
public static var instance:CustomState = null;
override function new(script:String){
Expand All @@ -30,24 +30,24 @@ class CustomState extends MusicBeatState {
}
override function create(){
super.create();
allScriptCall("createPost");
call("createPost");
}
override function update(elapsed:Float){
allScriptCall("update", [elapsed]);
call("update", [elapsed]);
super.update(elapsed);
allScriptCall("updatePost", [elapsed]);
call("updatePost", [elapsed]);
}
override function beatHit(){
allScriptCall("beatHit");
call("beatHit");
super.beatHit();
allScriptCall("beatHitPost");
call("beatHitPost");
}
override function stepHit(){
allScriptCall("stepHit");
call("stepHit");
super.stepHit();
allScriptCall("stepHitPost");
call("stepHitPost");
}
private inline function allScriptCall(func:String, ?args:Array<Dynamic>) {
public inline function call(func:String, ?args:Array<Dynamic>) {
script.call(func, args);
}
}
18 changes: 9 additions & 9 deletions source/modding/custom/CustomSubstate.hx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import modding.scripts.languages.HScript;
import substates.MusicBeatSubstate;
import flixel.FlxObject;

class CustomSubstate extends MusicBeatSubstate {
class CustomSubstate extends MusicBeatSubstate implements IHScriptable{
public var script:HScript;
public static var instance:CustomSubstate = null;
override function new(script:String){
Expand All @@ -24,24 +24,24 @@ class CustomSubstate extends MusicBeatSubstate {
}
override function create(){
super.create();
allScriptCall("createPost");
call("createPost");
}
override function update(elapsed:Float){
allScriptCall("update", [elapsed]);
call("update", [elapsed]);
super.update(elapsed);
allScriptCall("updatePost", [elapsed]);
call("updatePost", [elapsed]);
}
override function beatHit(){
allScriptCall("beatHit");
call("beatHit");
super.beatHit();
allScriptCall("beatHitPost");
call("beatHitPost");
}
override function stepHit(){
allScriptCall("stepHit");
call("stepHit");
super.stepHit();
allScriptCall("stepHitPost");
call("stepHitPost");
}
private inline function allScriptCall(func:String, ?args:Array<Dynamic>) {
public inline function call(func:String, ?args:Array<Dynamic>) {
script.call(func, args);
}
}
6 changes: 5 additions & 1 deletion source/modding/scripts/languages/HScript.hx
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ class HScript

public function new(hscript_path:String, ?global:Bool = false)
{
trace('Loading script at path \'${hscript_path}\'', DEBUG);
// parser settings
parser.allowJSON = true;
parser.allowTypes = true;
parser.allowMetadata = true;

// load text
#if sys
//Shoutout to @sword_352 on discord for helping my dumbass brain with this
if(global)
program = parser.parseString(sys.io.File.getContent(hscript_path));
else
Expand Down Expand Up @@ -239,3 +239,7 @@ class HScript
});
}
}
interface IHScriptable{
function call(func:String, ?args:Array<Dynamic>):Void;
var script:HScript;
}
2 changes: 1 addition & 1 deletion source/modding/scripts/languages/LuaScript.hx
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ class LuaScript extends Script
else
return NORMAL;
}
}
}
53 changes: 38 additions & 15 deletions source/states/FreeplayState.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package states;

import modding.scripts.languages.HScript;
import modding.ModList;
import game.Conductor;
#if sys
Expand Down Expand Up @@ -27,10 +28,11 @@ import flixel.text.FlxText;
import flixel.util.FlxColor;
import lime.utils.Assets;
import flixel.tweens.FlxEase;
import modding.scripts.languages.HScript.IHScriptable;

using StringTools;

class FreeplayState extends MusicBeatState {
class FreeplayState extends MusicBeatState implements IHScriptable{
var songs:Array<SongMetadata> = [];

var selector:FlxText;
Expand Down Expand Up @@ -86,8 +88,30 @@ class FreeplayState extends MusicBeatState {

var ui_Skin:Null<String>;
var lastSelectedSong:Int = -1;
public var script:HScript;

/**
Current instance of `FreeplayState`.
**/
public static var instance:FreeplayState = null;
public inline function call(func:String, ?args:Array<Dynamic>) {
if (script != null ) script.call(func, args);
}


override function create() {
instance = this;
#if sys
if (sys.FileSystem.exists("mods/" + Options.getData("curMod") + "/classes/states/FreeplayState.hx")){
script = new HScript("mods/" + Options.getData("curMod") + "/classes/states/FreeplayState.hx", true);
script.start();
}
#else
if (Assets.exists("assets/classes/states/FreeplayState.hx")){
script = new HScript("assets/classes/states/FreeplayState.hx");
script.start();
}
#end
if (ui_Skin == null || ui_Skin == "default")
ui_Skin = Options.getData("uiSkin");

Expand Down Expand Up @@ -283,28 +307,18 @@ class FreeplayState extends MusicBeatState {
add(text);

super.create();
call("createPost");
}

public function addSong(songName:String, weekNum:Int, songCharacter:String) {
call("addSong", [songName, weekNum, songCharacter]);
songs.push(new SongMetadata(songName, weekNum, songCharacter));
call("addSongPost", [songName, weekNum, songCharacter]);
}

public function addWeek(songs:Array<String>, weekNum:Int, ?songCharacters:Array<String>) {
if (songCharacters == null)
songCharacters = ['bf'];

var num:Int = 0;

for (song in songs) {
addSong(song, weekNum, songCharacters[num]);

if (songCharacters.length != 1)
num++;
}
}

override function update(elapsed:Float) {

call("update", [elapsed]);
#if sys
if(FlxG.keys.justPressed.TAB){
openSubState(new modding.SwitchModSubstate());
Expand Down Expand Up @@ -502,6 +516,7 @@ class FreeplayState extends MusicBeatState {
"Leather Engine's No Crash, We Help Fix Stuff Tool");
}
}
call("updatePost", [elapsed]);
}

override function closeSubState() {
Expand All @@ -511,6 +526,7 @@ class FreeplayState extends MusicBeatState {
}

function changeDiff(change:Int = 0) {
call("changeDiff", [change]);
curDifficulty = FlxMath.wrap(curDifficulty + change, 0, curDiffArray.length - 1);
curDiffString = curDiffArray[curDifficulty].toUpperCase();

Expand All @@ -525,9 +541,11 @@ class FreeplayState extends MusicBeatState {
diffText.text = "< " + curDiffString + " ~ " + curRank + " >";
else
diffText.text = curDiffString + " ~ " + curRank + " ";
call("changeDiffPost", [change]);
}

function changeSelection(change:Int = 0) {
call("changeSelection", [change]);
if(grpSongs.length != 0 || curSelected != 0)
curSelected = FlxMath.wrap(curSelected + change, 0, grpSongs.length - 1);

Expand Down Expand Up @@ -610,9 +628,11 @@ class FreeplayState extends MusicBeatState {
bg.color = songs[curSelected].color;
}
}
call("changeSelectionPost", [change]);
}

public function destroyFreeplayVocals(?destroyInst:Bool = true) {
call("destroyFreeplayVocals", [destroyInst]);
if (vocals != null) {
vocals.stop();
vocals.destroy();
Expand All @@ -629,13 +649,16 @@ class FreeplayState extends MusicBeatState {
}

FlxG.sound.music = null;
call("destroyFreeplayVocalsPost", [destroyInst]);
}

override function beatHit() {
call("beatHit");
super.beatHit();

if (lastSelectedSong != -1 && iconArray[lastSelectedSong] != null)
iconArray[lastSelectedSong].scale.add(0.2, 0.2);
call("beatHitPost");
}
}

Expand Down
39 changes: 15 additions & 24 deletions source/states/MainMenuState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ import flixel.tweens.FlxTween;
import flixel.util.FlxColor;
import lime.app.Application;
import modding.PolymodHandler;
import modding.scripts.languages.HScript.IHScriptable;

using StringTools;

class MainMenuState extends MusicBeatState
class MainMenuState extends MusicBeatState implements IHScriptable
{
/**
Current instance of `MainMenuState`.
Expand All @@ -42,31 +43,21 @@ class MainMenuState extends MusicBeatState
var magenta:FlxSprite;
var camFollow:FlxObject;
var ui_Skin:Null<String>;
public var scripts:Array<HScript> = [];
var script:HScript;
public var script:HScript;

function allScriptCall(func:String, ?args:Array<Dynamic>) {
for (cool_script in scripts) {
cool_script.call(func, args);
}
public inline function call(func:String, ?args:Array<Dynamic>) {
if(script != null) script.call(func, args);
}

function loadScripts(){
override function create()
{
instance = this;
#if sys
if (sys.FileSystem.exists("mods/" + Options.getData("curMod") + "/classes/states/MainMenuState.hx")){
script = new HScript("mods/" + Options.getData("curMod") + "/classes/states/MainMenuState.hx", true);
script.start();
scripts.push(script);
}
#end
}


override function create()
{
loadScripts();
instance = this;

#end
if (ui_Skin == null || ui_Skin == "default")
ui_Skin = Options.getData("uiSkin");

Expand All @@ -79,7 +70,7 @@ class MainMenuState extends MusicBeatState
if(Options.getData("developer"))
optionShit.push('toolbox');

allScriptCall("buttonsAdded");
call("buttonsAdded");

#if !web
//optionShit.push('multiplayer');
Expand Down Expand Up @@ -181,7 +172,7 @@ class MainMenuState extends MusicBeatState

super.create();

allScriptCall("createPost");
call("createPost");
}

var selectedSomethin:Bool = false;
Expand Down Expand Up @@ -256,11 +247,11 @@ class MainMenuState extends MusicBeatState
}
}

allScriptCall("update", [elapsed]);
call("update", [elapsed]);

super.update(elapsed);

allScriptCall("updatePost", [elapsed]);
call("updatePost", [elapsed]);

menuItems.forEach(function(spr:FlxSprite)
{
Expand Down Expand Up @@ -298,7 +289,7 @@ class MainMenuState extends MusicBeatState
case 'toolbox':
FlxG.switchState(new toolbox.ToolboxPlaceholder());
}
allScriptCall("changeState");
call("changeState");
}

function changeItem(itemChange:Int = 0)
Expand All @@ -322,6 +313,6 @@ class MainMenuState extends MusicBeatState

spr.updateHitbox();
});
allScriptCall("changeItem", [itemChange]);
call("changeItem", [itemChange]);
}
}
Loading

0 comments on commit 2f8d3a6

Please sign in to comment.