Skip to content

Commit 64922c3

Browse files
author
ryfax
committed
Big improvements and modifications
Improvement: - Add SoundManager (not 100% finished yet) - Add init(Engine engine) in GameObject - Add ButtonBuilder (to create Button) - Add onClickExit() in the ButtonListener Modifications: - SceneManager.scenes is now in static - FontLoader.getDefaultFont // FontLoader.getLoadedFonts is now in static - Entity don't need Engine to be declared - Reorganised InformationsPanel and not need Engine - Reorganised SplashScreen and not need Engine Bugs fixed: - MouseListener bug with buttonsPressed fixed
1 parent bf4fcee commit 64922c3

20 files changed

Lines changed: 288 additions & 128 deletions
19.8 KB
Binary file not shown.
Binary file not shown.

src/fr/ryfax/rge/engine/global/listeners/MouseEvents.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@ public MouseEvents(Engine engine) {
1616
this.engine = engine;
1717
}
1818

19-
public void mouseClicked(MouseEvent mouseEvent) {
20-
engine.getButtonsPressed().remove((Object) mouseEvent.getButton());
21-
engine.getMouseListeners().forEach(list -> list.onButtonReleased(mouseEvent.getButton()));
22-
}
19+
public void mouseClicked(MouseEvent mouseEvent) {}
2320

2421
public void mousePressed(MouseEvent mouseEvent) {
25-
engine.getButtonsPressed().add(mouseEvent.getButton());
22+
if(!engine.getButtonsPressed().contains(mouseEvent.getButton()))
23+
engine.getButtonsPressed().add(mouseEvent.getButton());
2624
engine.getMouseListeners().forEach(list -> list.onButtonPressed(mouseEvent.getButton()));
2725
}
2826

29-
public void mouseReleased(MouseEvent mouseEvent) {}
27+
public void mouseReleased(MouseEvent mouseEvent) {
28+
engine.getButtonsPressed().remove((Object) mouseEvent.getButton());
29+
engine.getMouseListeners().forEach(list -> list.onButtonReleased(mouseEvent.getButton()));
30+
}
3031
public void mouseEntered(MouseEvent mouseEvent) {}
3132
public void mouseExited(MouseEvent mouseEvent) {}
3233
}

src/fr/ryfax/rge/engine/global/scenes/Scene.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public void draw(Drawer d) {
5959
public void addGameObject(GameObject gameObject, int zindex) {
6060
boolean isVisualGO = gameObject instanceof VisualGameObject;
6161

62+
gameObject.init(engine);
63+
6264
if(gameObjs.containsKey(zindex)) {
6365
gameObjs.get(zindex).add(gameObject);
6466
if(isVisualGO) visualGameObjs.get(zindex).add((VisualGameObject) gameObject);

src/fr/ryfax/rge/engine/global/scenes/SceneManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class SceneManager {
1212
private Engine engine;
1313
private Scene currentScene;
1414

15-
private ArrayList<Scene> scenes = new ArrayList<>();
15+
private final static ArrayList<Scene> scenes = new ArrayList<>();
1616

1717
public SceneManager(Engine engine) {
1818
this.engine = engine;
@@ -30,7 +30,7 @@ public void setScene(Scene scene) {
3030
/*
3131
* Getters
3232
*/
33-
public Scene getSceneById(int id) {
33+
public static Scene getSceneById(int id) {
3434
Scene out = null;
3535

3636
for(Scene scene : scenes) {
@@ -43,7 +43,7 @@ public Scene getSceneById(int id) {
4343
return out;
4444
}
4545

46-
public Scene getSceneByName(String name) {
46+
public static Scene getSceneByName(String name) {
4747
Scene out = null;
4848

4949
for(Scene scene : scenes) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package fr.ryfax.rge.engine.global.sounds;
2+
3+
import javax.sound.sampled.*;
4+
5+
class Sound {
6+
7+
private Clip clip;
8+
9+
public Sound(AudioInputStream stream) {
10+
try {
11+
AudioFormat format = stream.getFormat();
12+
DataLine.Info info = new DataLine.Info(Clip.class, format);
13+
clip = (Clip) AudioSystem.getLine(info);
14+
clip.open(stream);
15+
}catch (Exception e) {
16+
e.printStackTrace();
17+
}
18+
}
19+
20+
21+
public void play(boolean loop) {
22+
if(loop) clip.loop(Clip.LOOP_CONTINUOUSLY);
23+
clip.setFramePosition(0);
24+
clip.start();
25+
}
26+
27+
public void stop() {
28+
clip.stop();
29+
}
30+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package fr.ryfax.rge.engine.global.sounds;
2+
3+
import javax.sound.sampled.*;
4+
import java.util.HashMap;
5+
6+
public class SoundManager {
7+
8+
9+
public static HashMap<String, Sound> sounds = new HashMap<>();
10+
11+
public static void load(String name, String path) {
12+
try {
13+
ClassLoader cl = Thread.currentThread().getContextClassLoader();
14+
AudioInputStream stream = AudioSystem.getAudioInputStream(cl.getResourceAsStream(path));
15+
sounds.put(name, new Sound(stream));
16+
}catch (Exception e) {
17+
System.err.println("Can't load the sound!");
18+
}
19+
}
20+
21+
22+
public static void play(String name) {
23+
Sound sound = sounds.get(name);
24+
sound.play(false);
25+
}
26+
27+
public static void play(String name, boolean loop) {
28+
Sound sound = sounds.get(name);
29+
sound.play(loop);
30+
}
31+
32+
/*public static void stop(String name) {
33+
sounds.get(name).stop();
34+
}*/
35+
36+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package fr.ryfax.rge.engine.object;
22

33

4+
import fr.ryfax.rge.engine.global.Engine;
5+
46
public interface GameObject {
57

8+
void init(Engine engine);
69
void update(int tick);
710

811
}

src/fr/ryfax/rge/engine/object/elements/entity/Entity.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,10 @@ public class Entity implements VisualGameObject {
1818

1919
private double height, width;
2020
private Vector2D position;
21-
2221
private Engine engine;
23-
public Entity(Engine engine) { this.engine = engine; }
2422

25-
/*
26-
* Methods
27-
*/
23+
public void init(Engine engine) { this.engine = engine; }
24+
2825
public void update(int tick) {
2926
modules.forEach((z, modules) ->
3027
modules.forEach(module -> module.update(tick)));
@@ -35,6 +32,7 @@ public void draw(Drawer drawer) {
3532
visualModules.forEach(visualModule -> visualModule.draw(drawer)));
3633
}
3734

35+
3836
/*
3937
* Setters
4038
*/
@@ -63,6 +61,7 @@ public void addModule(EntityModule module, int zindex) {
6361
public void setHeight(double height) { this.height = height; }
6462
public void setWidth(double width) { this.width = width; }
6563

64+
6665
/*
6766
* Getters
6867
*/

src/fr/ryfax/rge/engine/object/modules/InformationsPanel.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,16 @@
1010
import fr.ryfax.rge.engine.utils.drawing.font.FontLoader;
1111
import fr.ryfax.rge.engine.utils.drawing.font.FontRenderer;
1212

13-
import java.awt.*;
14-
import java.awt.image.BufferedImage;
15-
1613
public class InformationsPanel implements VisualGameObject {
1714

18-
/*
19-
* Vars
20-
*/
21-
private final Statistics statistics;
22-
private final FontRenderer fontRenderer;
15+
// Variables
16+
private Statistics statistics;
17+
private FontRenderer fontRenderer;
18+
private Image version = null, fpsAndTick = null, ticks = null, time = null, cam = null, ram = null;
2319
private int tick = 0;
2420

25-
private final Image version;
26-
private Image fpsAndTick = null, ticks = null, time = null, cam = null, ram = null;
27-
28-
/*
29-
* Methods
30-
*/
31-
public InformationsPanel(Engine engine) {
32-
Font font = engine.getFontLoader().getLoadedFonts().get(FontLoader.RGE_SHADOW_BACKGROUND);
21+
public void init(Engine engine) {
22+
Font font = FontLoader.getLoadedFonts().get(FontLoader.RGE_SHADOW_BACKGROUND);
3323
statistics = engine.getStatistics();
3424
fontRenderer = new FontRenderer(font);
3525

0 commit comments

Comments
 (0)