Skip to content

Commit 47ed22b

Browse files
Add Lua scripting with a test script + code optimizations
1 parent aaf7e6f commit 47ed22b

File tree

8 files changed

+178
-10
lines changed

8 files changed

+178
-10
lines changed

assets/test.lua

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
local Gdx = luajava.bindClass("com.badlogic.gdx.Gdx")
2+
local Input = luajava.bindClass("com.badlogic.gdx.Input")
3+
local Funkin = luajava.bindClass("me.stringfromjava.funkin.Funkin")
4+
local Paths = luajava.bindClass("me.stringfromjava.funkin.util.Paths")
5+
local Sprite = luajava.bindClass("com.badlogic.gdx.graphics.g2d.Sprite")
6+
local Texture = luajava.bindClass("com.badlogic.gdx.graphics.Texture")
7+
8+
local TitleScreen = Funkin.screen
9+
10+
local path = Paths:image('transitionSwag/stickers-set-1/bfSticker1')
11+
12+
local new_sprite = Sprite.new(Texture.new(path))
13+
new_sprite:setPosition(100, 100)
14+
15+
TitleScreen:add(new_sprite)
16+
17+
local function onPostRender()
18+
if Gdx.input:isKeyPressed(Input.Keys.W) then
19+
new_sprite:setY(new_sprite:getY() + 10)
20+
end
21+
if Gdx.input:isKeyPressed(Input.Keys.A) then
22+
new_sprite:setX(new_sprite:getX() - 10)
23+
end
24+
if Gdx.input:isKeyPressed(Input.Keys.S) then
25+
new_sprite:setY(new_sprite:getY() - 10)
26+
end
27+
if Gdx.input:isKeyPressed(Input.Keys.D) then
28+
new_sprite:setX(new_sprite:getX() + 10)
29+
end
30+
end
31+
32+
local runnable = luajava.createProxy('java.lang.Runnable', {
33+
run = onPostRender
34+
})
35+
36+
Funkin.Signals.postRender:add(runnable)
37+
38+
Funkin:playSound('shared/sounds/ANGRY_TEXT_BOX.ogg')

core/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ dependencies {
99
api "io.github.libktx:ktx-freetype:$ktxVersion"
1010
api "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
1111
api "org.mini2Dx:universal-tween-engine:$universalTweenVersion"
12+
implementation 'org.luaj:luaj-jse:3.0.1'
1213

1314
if(enableGraalNative == 'true') {
1415
implementation "io.github.berstanio:gdx-svmhelper-annotations:$graalHelperVersion"

core/src/main/java/me/stringfromjava/funkin/Funkin.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package me.stringfromjava.funkin;
22

3-
import com.badlogic.gdx.Game;
43
import com.badlogic.gdx.Gdx;
54
import com.badlogic.gdx.audio.Music;
65
import com.badlogic.gdx.audio.Sound;
76
import me.stringfromjava.funkin.backend.display.FunkinScreen;
7+
import me.stringfromjava.funkin.lua.FunkinLua;
88
import me.stringfromjava.funkin.tween.FunkinTween;
99
import me.stringfromjava.funkin.util.FunkinSignal;
1010
import me.stringfromjava.funkin.util.Paths;
@@ -27,12 +27,6 @@ public final class Funkin {
2727
*/
2828
public static FunkinScreen screen = null;
2929

30-
/**
31-
* The static instance used to access the core elements of the game.
32-
* This includes the loop, setting the current screen, and more.
33-
*/
34-
public static Game game;
35-
3630
/**
3731
* A map containing all sounds that are currently playing.
3832
* <p>
@@ -46,6 +40,17 @@ public final class Funkin {
4640
*/
4741
public static Music music = null;
4842

43+
/**
44+
* The global volume multiplier for all sounds and music.
45+
*/
46+
public static float masterVolume = 1.0f;
47+
48+
/**
49+
* The static instance used to access the core elements of the game.
50+
* This includes the loop, setting the current screen, and more.
51+
*/
52+
private static FunkinGame game;
53+
4954
/**
5055
* Has the global manager been initialized yet?
5156
*/
@@ -59,13 +64,14 @@ public final class Funkin {
5964
*
6065
* @param gameInstance The instance of the game to use.
6166
*/
62-
public static void initialize(Game gameInstance) {
67+
public static void initialize(FunkinGame gameInstance) {
6368
if (initialized) {
6469
throw new IllegalStateException("FNF:JE has already been initialized!");
6570
}
6671
game = gameInstance;
6772

6873
FunkinTween.registerAccessors();
74+
FunkinLua.initialize();
6975

7076
initialized = true;
7177
}
@@ -150,6 +156,15 @@ public static Music playMusic(String path, float volume, boolean looped) {
150156
return music;
151157
}
152158

159+
/**
160+
* Gets the {@link FunkinGame} instance being used.
161+
*
162+
* @return The current {@code FunkinGame} instance.
163+
*/
164+
public static FunkinGame getGame() {
165+
return game;
166+
}
167+
153168
/**
154169
* Contains all the global events that get dispatched when something happens in the game.
155170
* <p>

core/src/main/java/me/stringfromjava/funkin/FunkinGame.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ public void render() {
3232
Funkin.Signals.postRender.dispatch();
3333
}
3434

35+
@Override
36+
public void resume() {
37+
super.resume();
38+
Funkin.masterVolume = 1.0f;
39+
Funkin.music.setVolume(1);
40+
}
41+
42+
@Override
43+
public void pause() {
44+
super.pause();
45+
Funkin.masterVolume = 0.008f;
46+
Funkin.music.setVolume(0.008f);
47+
}
48+
3549
@Override
3650
public void dispose() {
3751
Funkin.Signals.preGameClose.dispatch();

core/src/main/java/me/stringfromjava/funkin/backend/display/FunkinScreen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public abstract class FunkinScreen implements Screen {
4242
*/
4343
protected Color bgColor;
4444

45-
private final ArrayList<Sprite> sprites = new ArrayList<>();
45+
public final ArrayList<Sprite> sprites = new ArrayList<>();
4646

4747
@Override
4848
public void show() {
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package me.stringfromjava.funkin.game.menus;
22

3+
import com.badlogic.gdx.Gdx;
4+
import com.badlogic.gdx.Input;
5+
import com.badlogic.gdx.graphics.Texture;
36
import com.badlogic.gdx.graphics.g2d.Sprite;
47
import me.stringfromjava.funkin.Funkin;
58
import me.stringfromjava.funkin.backend.display.FunkinScreen;
9+
import me.stringfromjava.funkin.lua.FunkinLua;
10+
import me.stringfromjava.funkin.util.Paths;
611

712
public class TitleScreen extends FunkinScreen {
813

@@ -11,7 +16,16 @@ public class TitleScreen extends FunkinScreen {
1116
@Override
1217
public void show() {
1318
super.show();
14-
Funkin.playSound("shared/sounds/tickleFight.ogg");
19+
// Funkin.playSound("shared/sounds/tickleFight.ogg");
1520
Funkin.playMusic("preload/music/freakyMenu/freakyMenu.ogg", 0.5f);
1621
}
22+
23+
@Override
24+
public void render(float delta) {
25+
super.render(delta);
26+
27+
if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) {
28+
FunkinLua.executeScript("test.lua");
29+
}
30+
}
1731
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package me.stringfromjava.funkin.lua;
2+
3+
import me.stringfromjava.funkin.lua.resource.FunkinLuaResourceFinder;
4+
import org.luaj.vm2.Globals;
5+
import org.luaj.vm2.LuaValue;
6+
import org.luaj.vm2.Varargs;
7+
import org.luaj.vm2.lib.jse.JsePlatform;
8+
9+
/**
10+
* Global manager and utility class for executing, manipulating and handling Lua scripts.
11+
*/
12+
public final class FunkinLua {
13+
14+
private static Globals globals;
15+
16+
/**
17+
* Initializes the Lua global manager.
18+
* <p>
19+
* This must be called before any Lua scripts can be executed.
20+
*/
21+
public static void initialize() {
22+
globals = JsePlatform.standardGlobals();
23+
globals.finder = new FunkinLuaResourceFinder();
24+
}
25+
26+
/**
27+
* Attempts to execute a Lua script located at the given path.
28+
* If the script fails to run, then {@code LuaValue.NIL} is returned.
29+
*
30+
* @param path The path to the Lua script to execute.
31+
* @return The result of the script execution, or {@code LuaValue.NIL} if it failed.
32+
*/
33+
public static Varargs executeScript(String path) {
34+
try {
35+
LuaValue chunk = loadScript(path);
36+
37+
if (chunk.isfunction()) {
38+
return chunk.invoke();
39+
}
40+
} catch (Exception e) {
41+
e.printStackTrace();
42+
}
43+
return LuaValue.NIL;
44+
}
45+
46+
public static LuaValue loadScript(String path) {
47+
try {
48+
LuaValue chunk = globals.loadfile(path);
49+
return chunk;
50+
} catch (Exception e) {
51+
e.printStackTrace();
52+
return LuaValue.NIL;
53+
}
54+
}
55+
56+
private FunkinLua() {
57+
}
58+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package me.stringfromjava.funkin.lua.resource;
2+
3+
import com.badlogic.gdx.Gdx;
4+
import com.badlogic.gdx.files.FileHandle;
5+
import org.luaj.vm2.lib.ResourceFinder;
6+
7+
import java.io.InputStream;
8+
9+
/**
10+
* A resource finder for Lua scripts that looks for files
11+
* in the internal assets of the game.
12+
*/
13+
public class FunkinLuaResourceFinder implements ResourceFinder {
14+
15+
@Override
16+
public InputStream findResource(String filename) {
17+
if (!filename.endsWith(".lua") && Gdx.files.internal(filename + ".lua").exists()) {
18+
filename += ".lua";
19+
}
20+
21+
FileHandle file = Gdx.files.internal(filename);
22+
if (file.exists()) {
23+
return file.read();
24+
}
25+
26+
return null;
27+
}
28+
}

0 commit comments

Comments
 (0)