Skip to content

Commit aaf7e6f

Browse files
Added signals and enhanced many classes (plus added new icon)
1 parent 7fc2f5d commit aaf7e6f

File tree

11 files changed

+240
-62
lines changed

11 files changed

+240
-62
lines changed

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

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package me.stringfromjava.funkin;
22

3-
import aurelienribon.tweenengine.Tween;
43
import com.badlogic.gdx.Game;
5-
import com.badlogic.gdx.graphics.g2d.Sprite;
4+
import com.badlogic.gdx.Gdx;
5+
import com.badlogic.gdx.audio.Music;
6+
import com.badlogic.gdx.audio.Sound;
67
import me.stringfromjava.funkin.backend.display.FunkinScreen;
78
import me.stringfromjava.funkin.tween.FunkinTween;
9+
import me.stringfromjava.funkin.util.FunkinSignal;
10+
import me.stringfromjava.funkin.util.Paths;
11+
12+
import java.util.HashMap;
13+
import java.util.Map;
814

915
/**
1016
* Global manager and utility class for the game.
1117
* <p>
12-
* This is where you want to do the main things, like switching states.
18+
* This is where you want to do the main things, like switching screens, playing sounds/music, etc.
1319
*/
1420
public final class Funkin {
1521

@@ -27,6 +33,19 @@ public final class Funkin {
2733
*/
2834
public static Game game;
2935

36+
/**
37+
* A map containing all sounds that are currently playing.
38+
* <p>
39+
* The key is the sound's ID (created by libGDX), and the value is the sound itself.
40+
* Note that it's not recommended to access this unless you know what you're doing!
41+
*/
42+
public static Map<Long, Sound> soundPool = new HashMap<>();
43+
44+
/**
45+
* The object where the current music being played is stored.
46+
*/
47+
public static Music music = null;
48+
3049
/**
3150
* Has the global manager been initialized yet?
3251
*/
@@ -59,17 +78,95 @@ public static void initialize(Game gameInstance) {
5978
* @param screen The new {@code FunkinScreen} to set as the current screen.
6079
*/
6180
public static void setScreen(FunkinScreen screen) {
81+
Signals.preScreenSwitch.dispatch();
6282
if (!initialized) {
6383
throw new IllegalStateException("FNF:JE has not been initialized yet!");
6484
}
6585
if (screen == null) {
6686
throw new IllegalArgumentException("Screen cannot be null!");
6787
}
6888
if (Funkin.screen != null) {
89+
Funkin.screen.hide();
6990
Funkin.screen.dispose();
7091
}
7192
Funkin.screen = screen;
7293
game.setScreen(screen);
94+
Signals.postScreenSwitch.dispatch();
95+
}
96+
97+
/**
98+
* Plays a sound. (Duh.)
99+
*
100+
* @param path The path to play the sound from.
101+
* @return The sound instance itself.
102+
*/
103+
public static Sound playSound(String path) {
104+
Sound sound = Gdx.audio.newSound(Paths.asset(path));
105+
long id = sound.play();
106+
if (id != -1) { // libGDX will return -1 if the sound fails to play.
107+
soundPool.put(id, sound);
108+
}
109+
return sound;
110+
}
111+
112+
/**
113+
* Plays new music. (Duh.)
114+
*
115+
* @param path The path to play the music from.
116+
* @return The music instance itself.
117+
*/
118+
public static Music playMusic(String path) {
119+
return playMusic(path, 1.0f, true);
120+
}
121+
122+
/**
123+
* Plays new music. (Duh.)
124+
*
125+
* @param path The path to play the music from.
126+
* @param volume The volume to play the music at.
127+
* @return The music instance itself.
128+
*/
129+
public static Music playMusic(String path, float volume) {
130+
return playMusic(path, volume, true);
131+
}
132+
133+
/**
134+
* Plays new music. (Duh.)
135+
*
136+
* @param path The path to play the music from.
137+
* @param volume The volume to play the music at.
138+
* @param looped Should the music loop when it is finished playing?
139+
* @return The music instance itself.
140+
*/
141+
public static Music playMusic(String path, float volume, boolean looped) {
142+
Music music = Gdx.audio.newMusic(Paths.asset(path));
143+
if (Funkin.music != null && Funkin.music.isPlaying()) {
144+
Funkin.music.stop();
145+
}
146+
Funkin.music = music;
147+
music.setVolume(volume);
148+
music.setLooping(looped);
149+
music.play();
150+
return music;
151+
}
152+
153+
/**
154+
* Contains all the global events that get dispatched when something happens in the game.
155+
* <p>
156+
* This includes anything from the screen being switched, the game updating every frame, and
157+
* just about everything you can think of.
158+
*/
159+
public static class Signals {
160+
161+
public static final FunkinSignal preRender = new FunkinSignal();
162+
public static final FunkinSignal postRender = new FunkinSignal();
163+
public static final FunkinSignal preScreenSwitch = new FunkinSignal();
164+
public static final FunkinSignal postScreenSwitch = new FunkinSignal();
165+
public static final FunkinSignal preGameClose = new FunkinSignal();
166+
public static final FunkinSignal postGameClose = new FunkinSignal();
167+
168+
private Signals() {
169+
}
73170
}
74171

75172
private Funkin() {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package me.stringfromjava.funkin;
2+
3+
import com.badlogic.gdx.Game;
4+
import com.badlogic.gdx.Gdx;
5+
import com.badlogic.gdx.audio.Sound;
6+
import me.stringfromjava.funkin.backend.display.cache.TextureCache;
7+
import me.stringfromjava.funkin.game.InitScreen;
8+
import me.stringfromjava.funkin.tween.FunkinTween;
9+
10+
/**
11+
* An enhanced version of libGDX's {@link Game} object.
12+
* <p>
13+
* If you want to change what happens to the pre and window
14+
* configurations, you might want to see {@link me.stringfromjava.funkin.lwjgl3.Lwjgl3Launcher} in the
15+
* {@code lwjgl3} folder.
16+
*/
17+
public class FunkinGame extends Game {
18+
19+
@Override
20+
public void create() {
21+
setScreen(new InitScreen());
22+
}
23+
24+
@Override
25+
public void render() {
26+
super.render();
27+
Funkin.Signals.preRender.dispatch();
28+
29+
float delta = Gdx.graphics.getDeltaTime();
30+
FunkinTween.globalManager.update(delta);
31+
32+
Funkin.Signals.postRender.dispatch();
33+
}
34+
35+
@Override
36+
public void dispose() {
37+
Funkin.Signals.preGameClose.dispatch();
38+
39+
TextureCache.dispose();
40+
41+
// Dispose of all sounds and the music (if there is any playing).
42+
if (Funkin.music != null) {
43+
Funkin.music.stop();
44+
Funkin.music.dispose();
45+
}
46+
47+
var soundPoolKeys = Funkin.soundPool.keySet();
48+
for (long key : soundPoolKeys) {
49+
Sound sound = Funkin.soundPool.get(key);
50+
if (sound == null) {
51+
continue;
52+
}
53+
sound.stop();
54+
sound.dispose();
55+
}
56+
57+
Funkin.Signals.postGameClose.dispatch();
58+
}
59+
}

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

Lines changed: 0 additions & 36 deletions
This file was deleted.

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package me.stringfromjava.funkin.backend.display;
22

3-
import aurelienribon.tweenengine.TweenManager;
3+
import com.badlogic.gdx.Screen;
44
import com.badlogic.gdx.graphics.Color;
55
import com.badlogic.gdx.graphics.OrthographicCamera;
66
import com.badlogic.gdx.graphics.Texture;
@@ -18,13 +18,7 @@
1818
* Base class for creating a better screen display
1919
* with more functionality than the default {@link com.badlogic.gdx.Screen} interface.
2020
*/
21-
public abstract class FunkinScreen implements com.badlogic.gdx.Screen {
22-
23-
/**
24-
* The {@link TweenManager} used for
25-
* adding nice transitions (mainly for sprites).
26-
*/
27-
protected TweenManager tweenManager;
21+
public abstract class FunkinScreen implements Screen {
2822

2923
/**
3024
* The {@link SpriteBatch} used to render
@@ -52,7 +46,6 @@ public abstract class FunkinScreen implements com.badlogic.gdx.Screen {
5246

5347
@Override
5448
public void show() {
55-
tweenManager = new TweenManager();
5649
spriteBatch = new SpriteBatch();
5750

5851
camera = new OrthographicCamera();

core/src/main/java/me/stringfromjava/funkin/InitScreen.java renamed to core/src/main/java/me/stringfromjava/funkin/game/InitScreen.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
package me.stringfromjava.funkin;
1+
package me.stringfromjava.funkin.game;
22

3-
import com.badlogic.gdx.graphics.g2d.Sprite;
3+
import me.stringfromjava.funkin.Funkin;
44
import me.stringfromjava.funkin.backend.display.FunkinScreen;
5+
import me.stringfromjava.funkin.game.menus.TitleScreen;
56

67
public class InitScreen extends FunkinScreen {
78

8-
private Sprite logo;
9-
109
@Override
1110
public void show() {
1211
super.show();
12+
System.out.println("setup complete");
13+
Funkin.setScreen(new TitleScreen());
1314
}
1415

1516
@Override
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package me.stringfromjava.funkin.game.menus;
2+
3+
import com.badlogic.gdx.graphics.g2d.Sprite;
4+
import me.stringfromjava.funkin.Funkin;
5+
import me.stringfromjava.funkin.backend.display.FunkinScreen;
6+
7+
public class TitleScreen extends FunkinScreen {
8+
9+
private Sprite logo;
10+
11+
@Override
12+
public void show() {
13+
super.show();
14+
Funkin.playSound("shared/sounds/tickleFight.ogg");
15+
Funkin.playMusic("preload/music/freakyMenu/freakyMenu.ogg", 0.5f);
16+
}
17+
}

core/src/main/java/me/stringfromjava/funkin/tween/FunkinTween.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import aurelienribon.tweenengine.Tween;
44
import aurelienribon.tweenengine.TweenManager;
5+
import com.badlogic.gdx.Gdx;
56
import com.badlogic.gdx.graphics.g2d.Sprite;
67
import me.stringfromjava.funkin.tween.accessors.SpriteAccessor;
78

@@ -17,8 +18,6 @@ public final class FunkinTween {
1718

1819
/**
1920
* Registers all tween accessors used in the game.
20-
* <p>
21-
* TODO: Make it possible to where you don't need accessors!
2221
*/
2322
public static void registerAccessors() {
2423
Tween.registerAccessor(Sprite.class, new SpriteAccessor());
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package me.stringfromjava.funkin.util;
2+
3+
import java.util.ArrayList;
4+
5+
/**
6+
* Utility class for creating objects that can execute multiple
7+
* runnables when it is dispatched (or triggered).
8+
*/
9+
public class FunkinSignal {
10+
11+
private ArrayList<Runnable> runnables;
12+
13+
public FunkinSignal() {
14+
runnables = new ArrayList<>();
15+
}
16+
17+
/**
18+
* Adds a new {@link Runnable} to {@code this} signal to be executed upon
19+
* {@code dispatch()} being called.
20+
*
21+
* @param runnable The new runnable to add to the signal.
22+
*/
23+
public void add(Runnable runnable) {
24+
if (runnable != null) {
25+
runnables.add(runnable);
26+
}
27+
}
28+
29+
/**
30+
* Removes all runnables from {@code this} signal.
31+
*/
32+
public void clear() {
33+
runnables.clear();
34+
}
35+
36+
/**
37+
* Triggers {@code this} signal and executes all runnables.
38+
*/
39+
public void dispatch() {
40+
for (Runnable runnable : runnables) {
41+
if (runnable != null) {
42+
runnable.run();
43+
}
44+
}
45+
}
46+
}

core/src/main/java/me/stringfromjava/funkin/util/PathUtil.java renamed to core/src/main/java/me/stringfromjava/funkin/util/Paths.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package me.stringfromjava.funkin.util;
22

3+
import com.badlogic.gdx.Gdx;
34
import com.badlogic.gdx.files.FileHandle;
45

56
/**
67
* Utility class for simplifying asset paths.
78
*/
8-
public final class PathUtil {
9+
public final class Paths {
910

1011
public static FileHandle asset(String path) {
11-
return new FileHandle(path);
12+
return Gdx.files.internal(path);
1213
}
1314

1415
public static FileHandle shared(String path) {
@@ -19,6 +20,6 @@ public static FileHandle image(String path) {
1920
return shared(String.format("images/%s.png", path));
2021
}
2122

22-
private PathUtil() {
23+
private Paths() {
2324
}
2425
}

0 commit comments

Comments
 (0)