From 5626cd17802e79b7098fdbd7cbea38e775fe21cc Mon Sep 17 00:00:00 2001 From: khuonghoanghuy Date: Wed, 25 Dec 2024 20:27:05 +0700 Subject: [PATCH] lua init --- Project.xml | 1 + TODO.md | 4 +++ haxelibs.json | 5 ++++ source/GameHandler.hx | 6 ++++- source/LuaScript.hx | 51 ++++++++++++++++++++++++++++++++++++++ source/PlayState.hx | 10 ++++++++ source/PolyHandler.hx | 4 ++- source/ScriptedSubClass.hx | 3 +-- 8 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 source/LuaScript.hx diff --git a/Project.xml b/Project.xml index 022fd48..71308e0 100644 --- a/Project.xml +++ b/Project.xml @@ -44,6 +44,7 @@ + diff --git a/TODO.md b/TODO.md index 17ac2d9..a1214b9 100644 --- a/TODO.md +++ b/TODO.md @@ -1,4 +1,8 @@ # List TODO for Cruese Engine +## v2.0 (Lua update) +- [ ] Added Lua script +- [ ] Make `GameSelectionState` can be editable + ## v1.3.1 (Little Fixed) - DONE - [X] Make Splashes can be skipping by press Enter - [X] Fix `engineCredits` From `Mad by JoaTH Team` to `Made by JoaTH Team` diff --git a/haxelibs.json b/haxelibs.json index 3cfdbc0..c5ebe15 100644 --- a/haxelibs.json +++ b/haxelibs.json @@ -78,6 +78,11 @@ { "name": "jsonpatch", "type": "haxelib" + }, + { + "name": "hxluajit", + "type": "haxelib", + "version": "1.0.3" } ] } \ No newline at end of file diff --git a/source/GameHandler.hx b/source/GameHandler.hx index 3848d0c..b1718f7 100644 --- a/source/GameHandler.hx +++ b/source/GameHandler.hx @@ -1,6 +1,7 @@ package; import flixel.FlxG; +import flixel.text.FlxText; import flixel.util.FlxColor; import haxe.Http; import haxe.Json; @@ -10,7 +11,10 @@ import openfl.Lib; class GameHandler { // public static var version:String = Lib.application.meta.get("version"); - public static var versionA_M:String = "1.0.0"; + public static var versionA_M:String = "2.0.0"; + + // Bunch Variable for Lua + public static var gameText:Map = new Map(); public static function exitGame(?exitActually:Bool = false) { diff --git a/source/LuaScript.hx b/source/LuaScript.hx new file mode 100644 index 0000000..87aace5 --- /dev/null +++ b/source/LuaScript.hx @@ -0,0 +1,51 @@ +package; + +import cpp.RawPointer; +import flixel.FlxBasic; +import flixel.FlxG; +import flixel.text.FlxText; +import hxluajit.*; +import hxluajit.Types.Lua_State; + +class LuaScript extends FlxBasic +{ + var vm:RawPointer; + + public function new(file:String) + { + super(); + Sys.println(Lua.VERSION); + Sys.println(LuaJIT.VERSION); + + vm = LuaL.newstate(); + LuaL.openlibs(vm); + LuaL.dofile(vm, file); + + // register the function + setFuction("setFuction", setFuction); + setFuction("createText", function(tag:String, x:Float = 0, y:Float = 0, width:Int = 0, text:String) + { + var text:FlxText = new FlxText(x, y, width, text); + text.active = true; + GameHandler.gameText.set(tag, text); + }); + setFuction("addText", function(tag:String, ?part:Int = 0) + { + FlxG.state.insert(part, GameHandler.gameText.get(tag)); + }); + + Lua.close(vm); + vm = null; + } + + // it different than hscript + public function setFuction(name:String, func:Dynamic) + { + Lua.register(vm, name, func); + } + + public function setVariable(name:String, value:Dynamic) + { + Lua.setglobal(vm, name); + } +} \ No newline at end of file diff --git a/source/PlayState.hx b/source/PlayState.hx index 1d9c244..6cf832e 100644 --- a/source/PlayState.hx +++ b/source/PlayState.hx @@ -8,6 +8,7 @@ using StringTools; class PlayState extends FlxState { public var scriptArray:Array = []; + public var luaArray:Array = []; public static var trackerFolder:Int = 0; public static var instance:PlayState = null; @@ -28,6 +29,10 @@ class PlayState extends FlxState { scriptArray.push(new HScript(folder + file)); } + if (file.endsWith('.lua')) + { + luaArray.push(new LuaScript(folder + file)); + } } } } @@ -50,6 +55,11 @@ class PlayState extends FlxState final script:HScript = scriptArray[i]; script.call(funcName, funcArgs); } + for (i in 0...luaArray.length) + { + final script:LuaScript = luaArray[i]; + script.setFuction(funcName, funcArgs); + } } override public function update(elapsed:Float) diff --git a/source/PolyHandler.hx b/source/PolyHandler.hx index bdca7a1..fd28dd7 100644 --- a/source/PolyHandler.hx +++ b/source/PolyHandler.hx @@ -9,7 +9,7 @@ class PolyHandler { static final MOD_DIR:String = 'mods'; static final CORE_DIR:String = 'assets'; - static final API_VERSION:String = '1.0.0'; + static final API_VERSION:String = '2.0.0'; public static var trackedMods:Array = []; @@ -21,6 +21,7 @@ class PolyHandler 'json' => TEXT, 'txt' => TEXT, 'hxs' => TEXT, + 'lua' => TEXT, // omg lua support??? 'ttf' => FONT, 'otf' => FONT ]; @@ -89,6 +90,7 @@ class PolyHandler final output:ParseRules = ParseRules.getDefault(); output.addType("txt", TextFileFormat.LINES); output.addType("hxs", TextFileFormat.PLAINTEXT); + output.addType("lua", TextFileFormat.PLAINTEXT); return output != null ? output : null; } diff --git a/source/ScriptedSubClass.hx b/source/ScriptedSubClass.hx index 917814d..5752094 100644 --- a/source/ScriptedSubClass.hx +++ b/source/ScriptedSubClass.hx @@ -28,8 +28,7 @@ class ScriptedSubClass extends FlxUISubState script = new HScript(path, false); script.execute(path, false); - - + scriptExecute("new", args); } catch (e:Dynamic)