diff --git a/source/game/data/scripts/Script.hx b/source/game/data/scripts/Script.hx new file mode 100644 index 000000000..06dbdef0f --- /dev/null +++ b/source/game/data/scripts/Script.hx @@ -0,0 +1,195 @@ +package game.data.scripts; + +#if HSCRIPT_ALLOWED +import hscript.Expr; +import hscript.Interp; +import hscript.Parser; +#end + +#if sys +import sys.FileSystem; +import sys.io.File; +#end + +import openfl.utils.Assets; + +import game.state.PlayState; + +using StringTools; + +/* + Class That Handles Everything Related To Scripts. + + @author Zyflx +*/ + +#if HSCRIPT_ALLOWED +class Script +{ + public var interp:Interp; + public var parser:Parser; + + public var scriptName:String = ''; + + public function new(scriptName:String) + { + // Initialize the interpreter and the script parser + interp = new Interp(); + parser = new Parser(); + + var scriptFile:String = scriptName; + if (!FileSystem.exists(scriptFile)) + scriptFile = null; + else + this.scriptName = scriptName; + + try { + if (scriptFile != null) + { + parser.allowJSON = true; + parser.allowTypes = true; + parser.allowMetadata = true; + + // Predefined imports for scripts. + + // Haxe Stuff + set('Math', Math); + set('Std', Std); + set('Type', Type); + set('Date', Date); + + // Flixel Stuff + set('FlxG', flixel.FlxG); + set('FlxSprite', flixel.FlxSprite); + set('FlxCamera', flixel.FlxCamera); + set('FlxText', flixel.text.FlxText); + set('FlxSound', flixel.system.FlxSound); + set('FlxTween', flixel.tweens.FlxTween); + set('FlxEase', flixel.tweens.FlxEase); + set('FlxTimer', flixel.util.FlxTimer); + + // Engine Stuff + set('Paths', Paths); + set('CoolUtil', CoolUtil); + set('PlayState', game.state.PlayState); + set('game', game.state.PlayState.instance); + set('Conductor', game.data.backend.Conductor); + set('Character', game.objects.Character); + set('Boyfriend', game.objects.Boyfriend); + set('Preferences', game.state.menus.options.PreferencesMenu); // just in case you ever want to check if downscroll if on or somethin in a script + + interp.execute(parser.parseString(Paths.getContent(scriptFile))); + } + } catch(e:haxe.Exception) { + trace('Something went wrong while trying to initialize script $scriptName'); + } + } + + /* + Runs scripts from the desired folder/folders. + + @param scriptArr The array you want to push the scripts to. (NOTE: Make sure that the array you want to push the scripts to is a Array