Skip to content

Commit

Permalink
lua init
Browse files Browse the repository at this point in the history
  • Loading branch information
khuonghoanghuy committed Dec 25, 2024
1 parent 6a7019a commit 5626cd1
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 4 deletions.
1 change: 1 addition & 0 deletions Project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<haxelib name="flixel-ui" version="2.6.1" />
<haxelib name="hscript" version="2.5.0" />
<haxelib name="polymod" version="1.8.0" />
<haxelib name="hxluajit" version="1.0.3" />

<!-- ______________________________ Haxedefines _____________________________ -->

Expand Down
4 changes: 4 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
5 changes: 5 additions & 0 deletions haxelibs.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@
{
"name": "jsonpatch",
"type": "haxelib"
},
{
"name": "hxluajit",
"type": "haxelib",
"version": "1.0.3"
}
]
}
6 changes: 5 additions & 1 deletion source/GameHandler.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package;

import flixel.FlxG;
import flixel.text.FlxText;
import flixel.util.FlxColor;
import haxe.Http;
import haxe.Json;
Expand All @@ -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<String, FlxText> = new Map<String, FlxText>();

public static function exitGame(?exitActually:Bool = false)
{
Expand Down
51 changes: 51 additions & 0 deletions source/LuaScript.hx
Original file line number Diff line number Diff line change
@@ -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<Lua_State>;

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);
}
}
10 changes: 10 additions & 0 deletions source/PlayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using StringTools;
class PlayState extends FlxState
{
public var scriptArray:Array<HScript> = [];
public var luaArray:Array<LuaScript> = [];

public static var trackerFolder:Int = 0;
public static var instance:PlayState = null;
Expand All @@ -28,6 +29,10 @@ class PlayState extends FlxState
{
scriptArray.push(new HScript(folder + file));
}
if (file.endsWith('.lua'))
{
luaArray.push(new LuaScript(folder + file));
}
}
}
}
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion source/PolyHandler.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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<ModMetadata> = [];

Expand All @@ -21,6 +21,7 @@ class PolyHandler
'json' => TEXT,
'txt' => TEXT,
'hxs' => TEXT,
'lua' => TEXT, // omg lua support???
'ttf' => FONT,
'otf' => FONT
];
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 1 addition & 2 deletions source/ScriptedSubClass.hx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class ScriptedSubClass extends FlxUISubState

script = new HScript(path, false);
script.execute(path, false);



scriptExecute("new", args);
}
catch (e:Dynamic)
Expand Down

0 comments on commit 5626cd1

Please sign in to comment.