1+ package states .freeplay ;
2+
3+ import flixel .FlxState ;
4+ import flixel .FlxG ;
5+ import hscript .Parser ;
6+ import hscript .Interp ;
7+ import sys .io .File ;
8+ import managers .FreeplayManager ;
9+
10+ import crowplexus .iris .Iris ;
11+ import crowplexus .iris .IrisConfig ;
12+
13+ class CustomFreeplayState extends MusicBeatState {
14+ public var scriptInterp : psychlua. HScript . CustomInterp ;
15+ public var scriptEnv : Dynamic ;
16+ public var scriptPath : String ;
17+ public var themeName : String ;
18+
19+ public function new (scriptPath : String ) {
20+ super ();
21+ this .scriptPath = scriptPath ;
22+ this .themeName = scriptPath .split (' /' ).pop ().split (' .' ).shift (); // Extract theme name from script path
23+ }
24+
25+
26+ // ...existing code...
27+
28+ override public function create (): Void {
29+ super .create ();
30+
31+ // Prepare Iris interpreter
32+ var scriptCode = File .getContent (scriptPath );
33+ var iris = new Iris (scriptCode , new IrisConfig (null , false , false ));
34+ var customInterp : psychlua. HScript . CustomInterp = new psychlua. HScript . CustomInterp ();
35+ customInterp .parentInstance = FlxG .state ;
36+ customInterp .showPosOnLog = false ;
37+ this .scriptInterp = customInterp ;
38+
39+ // Only expose Freeplay-relevant variables
40+ iris .set (' FreeplayManager' , FreeplayManager );
41+ iris .set (' FlxG' , FlxG );
42+ iris .set (' FlxState' , FlxState );
43+ iris .set (' FlxSprite' , flixel. FlxSprite );
44+ iris .set (' ${Type .getClassName (Type .getClass (FlxG .state ))}' , this );
45+ iris .set (' state' , this );
46+
47+ // Parse and execute the script, expecting it to return an object with lifecycle methods
48+ iris .parse (true );
49+ scriptEnv = iris .funcAndReturn (iris .execute );
50+
51+ // Call script's create if it exists
52+ if (scriptEnv != null && Reflect .hasField (scriptEnv , " create" )) {
53+ Reflect .callMethod (scriptEnv , Reflect .field (scriptEnv , " create" ), []);
54+ }
55+ this .scriptInterp = iris ;
56+ }
57+
58+ override public function update (elapsed : Float ): Void {
59+ super .update (elapsed );
60+ if (scriptEnv != null && Reflect .hasField (scriptEnv , " update" )) {
61+ Reflect .callMethod (scriptEnv , Reflect .field (scriptEnv , " update" ), [elapsed ]);
62+ }
63+ }
64+
65+ override public function destroy (): Void {
66+ if (scriptEnv != null && Reflect .hasField (scriptEnv , " destroy" )) {
67+ Reflect .callMethod (scriptEnv , Reflect .field (scriptEnv , " destroy" ), []);
68+ }
69+ super .destroy ();
70+ }
71+ }
0 commit comments