Skip to content

Commit

Permalink
Build 0.0.802
Browse files Browse the repository at this point in the history
 Build 0.0.802
  • Loading branch information
HackusatePvP committed Mar 14, 2024
2 parents b6eb943 + 780612d commit 258a41a
Show file tree
Hide file tree
Showing 47 changed files with 1,710 additions and 889 deletions.
8 changes: 1 addition & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.piitex</groupId>
<artifactId>RenJava</artifactId>
<version>0.0.757-SNAPSHOT</version>
<version>0.0.802-SNAPSHOT</version>
<name>RenJava</name>

<properties>
Expand Down Expand Up @@ -102,12 +102,6 @@
<version>9.0.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.15.1</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/me/piitex/renjava/Game.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package me.piitex.renjava;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Used to set default information about the game. Use this annotation to tag the ReJava class constructor.
*/
@Target(ElementType.CONSTRUCTOR)
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Game {
String name() default ("Name");
String author() default("Author");
String version() default("1.0");
}
57 changes: 29 additions & 28 deletions src/main/java/me/piitex/renjava/Launch.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,50 @@
import org.reflections.util.ConfigurationBuilder;

import java.util.HashSet;
import java.util.stream.Collectors;

public class Launch extends Application {

public static void main(String[] args) {
// Scans for all classes in all packages. (We need to do all packages because this allows the author the freedom to do their own package scheme.)
Collection<URL> allPackagePrefixes = Arrays.stream(Package.getPackages()).map(Package::getName)
.map(s -> s.split("\\.")[0]).distinct().map(ClasspathHelper::forPackage).reduce((c1, c2) -> {
Collection<URL> c3 = new HashSet<>();
c3.addAll(c1);
c3.addAll(c2);
return c3;
}).get();
Collection<URL> allPackagePrefixes = Arrays.stream(Package.getPackages())
.map(Package::getName)
.map(s -> s.split("\\.")[0])
.distinct()
.map(ClasspathHelper::forPackage)
.flatMap(Collection::stream)
.collect(Collectors.toSet());
ConfigurationBuilder config = new ConfigurationBuilder().addUrls(allPackagePrefixes)
.addScanners(Scanners.SubTypes);
Reflections reflections = new Reflections(config);

// Detect any classes that extend RenJava
Class<?> renJavaClass = null;
for (Class<?> c : reflections.getSubTypesOf(RenJava.class)) {

// Checks for default RenJava class: This was removed but could be re-added at a later date.
if (c.getName().contains("Example")) {
renJavaClass = c;
} else {
try {
c.getDeclaredConstructor().newInstance();
launch(args);
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException |
InvocationTargetException e) {
e.printStackTrace();
try {
Object o = c.getDeclaredConstructor().newInstance();
RenJava renJava = (RenJava) o;
if (c.getDeclaredConstructor().isAnnotationPresent(Game.class)) {
Game game = c.getDeclaredConstructor().getAnnotation(Game.class);
renJava.name = game.name();
renJava.author = game.author();
renJava.version = game.version();
} else {
System.err.println("Please annotate your main constructor with Game.\n\t\t@Game\n\t\tpublic void " + c.getDeclaredConstructor().getName() + "() { }");
renJava.name = "Error";
renJava.author = "Error";
renJava.version = "Error";
}
return;
}
}

try {
if (renJavaClass != null) {
renJavaClass.getDeclaredConstructor().newInstance();
renJava.init(); // Initialize game
launch(args);
//c.getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException |
InvocationTargetException e) {
e.printStackTrace();
}
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
return;
}

System.err.println("Could not initialize RenJava. Please make a class which extends 'RenJava'.");
}

@Override
Expand Down
100 changes: 75 additions & 25 deletions src/main/java/me/piitex/renjava/RenJava.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import javafx.stage.StageStyle;
import me.piitex.renjava.addons.Addon;
import me.piitex.renjava.addons.AddonLoader;
import me.piitex.renjava.api.builders.ButtonBuilder;
import me.piitex.renjava.api.builders.ImageLoader;
import me.piitex.renjava.api.exceptions.InvalidCharacterException;
import me.piitex.renjava.api.music.Tracks;
import me.piitex.renjava.api.saves.Save;
import me.piitex.renjava.api.saves.data.Data;
import me.piitex.renjava.api.saves.data.PersistentData;
import me.piitex.renjava.api.characters.Character;
Expand All @@ -27,6 +28,8 @@

import me.piitex.renjava.gui.exceptions.ImageNotFoundException;
import me.piitex.renjava.gui.Menu;
import me.piitex.renjava.gui.layouts.Layout;
import me.piitex.renjava.gui.layouts.impl.HorizontalLayout;
import me.piitex.renjava.gui.layouts.impl.VerticalLayout;
import me.piitex.renjava.gui.overlay.ButtonOverlay;
import me.piitex.renjava.gui.StageType;
Expand Down Expand Up @@ -63,14 +66,14 @@
* Note: Do not call the `RenJava` constructor directly. The framework creates a new instance of your class automatically using reflections.
*/
public abstract class RenJava {
private final String name;
private final String author;
private final String version;
private final Logger logger;
private final Player player;
protected String name;
protected String author;
protected String version;
private Logger logger;
private Player player;
// Audio Tracking
private final Tracks tracks;
private final AddonLoader addonLoader;
private Tracks tracks;
private AddonLoader addonLoader;

private Stage stage; // Move this somewhere else.
private StageType stageType;
Expand All @@ -94,16 +97,28 @@ public abstract class RenJava {
* Entry point for the RenJava framework. This class is designed to be extended by your own class, which will serve as the entry point for your game.
* <p>
* Note: Do not call this constructor directly. The RenJava framework creates a new instance of your class automatically using reflection.
* <p>
* Make sure to use {@link Game} to specify the information of the game.
* <pre>{@code
* public class YourGame extends RenJava {
*
* @param name The name of the game, used for displaying the game in the window and other various places.
* @param author The author of the game.
* @param version The current version of the game, used to display specific information about the game.
* @Game(name = "Your Game", author = "You", version = "1.0")
* public YourGame() {
*
* }
* }
* }</pre>
* If you do not specify the game information it will assume default values.
*
* @see Game
*/
public RenJava(String name, String author, String version) {
public RenJava() {
// Super is ran first than the superior method is ran.
instance = this;
this.name = name;
this.author = author;
this.version = version;
}

protected void init() {
// Run after super
this.player = new Player();
this.tracks = new Tracks();
// Load logger
Expand Down Expand Up @@ -228,6 +243,10 @@ public Collection<Character> getCharacters() {
* @see RenJava#registerCharacter(Character)
*/
public Character getCharacter(String id) {
if (!registeredCharacters.containsKey(id)) {
getLogger().severe(new InvalidCharacterException(id).getMessage());
return null;
}
return registeredCharacters.get(id.toLowerCase());
}

Expand Down Expand Up @@ -304,7 +323,7 @@ public void buildStage(Stage stage) {
} else {
stage.setMaximized(true);
}
stage.setTitle(getName());
stage.setTitle(getConfiguration().getGameTitle());

stage.setOnHiding(windowEvent -> {
getAddonLoader().disable();
Expand Down Expand Up @@ -394,20 +413,19 @@ public void buildStage(Stage stage) {
public abstract Menu buildTitleScreen();

public Menu buildSideMenu() {
// Don't build background image
Menu menu = new Menu(350, 500, new ImageLoader("gui/overlay/main_menu.png"));

Font uiFont = RenJava.getInstance().getConfiguration().getUiFont().getFont();

ButtonOverlay startButton = new ButtonOverlay(new ButtonBuilder("menu-start-button", "Start", uiFont, Color.BLACK, 1, 1));
ButtonOverlay loadButton = new ButtonOverlay(new ButtonBuilder("menu-load-button", "Load", uiFont, Color.BLACK, 1, 1));
ButtonOverlay optionsButton = new ButtonOverlay(new ButtonBuilder("menu-preference-button", "Preferences", uiFont, Color.BLACK, 1, 1));
ButtonOverlay aboutButton = new ButtonOverlay(new ButtonBuilder("menu-about-button", "About", uiFont, Color.BLACK, 1, 1));
ButtonOverlay startButton = new ButtonOverlay("menu-start-button", "Start", uiFont, Color.BLACK, Color.TRANSPARENT, Color.TRANSPARENT, Color.BLUE, 1, 1);
ButtonOverlay loadButton = new ButtonOverlay("menu-load-button", "Load", uiFont, Color.BLACK, Color.TRANSPARENT, Color.TRANSPARENT, Color.BLUE, 1, 1);
ButtonOverlay optionsButton = new ButtonOverlay("menu-preference-button", "Preferences", uiFont, Color.BLACK, Color.TRANSPARENT, Color.TRANSPARENT, Color.BLUE, 1, 1);
ButtonOverlay aboutButton = new ButtonOverlay("menu-about-button", "About", uiFont, Color.BLACK, Color.TRANSPARENT, Color.TRANSPARENT, Color.BLUE, 1, 1);

// Create vbox for the buttons. You can also do an HBox
VerticalLayout layout = new VerticalLayout(200, 500);
layout.setXPosition(50);
layout.setYPosition(250);
layout.setX(50);
layout.setY(250);
layout.setSpacing(20);
layout.addOverlays(startButton, loadButton, optionsButton, aboutButton);

Expand All @@ -417,8 +435,40 @@ public Menu buildSideMenu() {
return menu;
}

public Menu buildLoadMenu() {
public Menu buildLoadMenu(int page) {
Menu menu = new Menu(1920, 1080, new ImageLoader("gui/main_menu.png"));
// Setup pagination.
// 6 save slots per page
// 2 Rows
// 3 Columns
// Make this customizable

int maxSavesPerPage = 6;

int index = 1;
VerticalLayout rootLayout = new VerticalLayout(1000, 400); // The root is a vertical which stacks the two horizontal layouts.
HorizontalLayout topLayout = new HorizontalLayout(1000, 200);
HorizontalLayout bottomLayout = new HorizontalLayout(1000, 200);
while (index <= maxSavesPerPage) {
// Create a button overlay of the scene that the save file would have been on.,
// This will be a little challenging, it should be possible given the api of the Save object.
Save save = new Save(index);
if (save.getFile() != null) {
if (index <= 3) {
// Top layout

} else {
// Bottom layout
}
} else {
// Process empty slot
}
index++;
}

rootLayout.addChildLayout(topLayout);
rootLayout.addChildLayout(bottomLayout);


return menu;
}
Expand Down Expand Up @@ -489,7 +539,7 @@ public Menu buildAboutScreen() {
* Example usage:
* <pre>{@code
* // Get the current story from the StoryManager
* Story myStory = this.getStoryManager().getStory("my-story");
* Story myStory = this.getPlayer().getStory("my-story");
* // Start the story
* myStory.start();
* }</pre>
Expand Down
39 changes: 25 additions & 14 deletions src/main/java/me/piitex/renjava/RenLoader.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,50 @@
package me.piitex.renjava;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Properties;
import java.util.stream.Stream;

import javafx.application.Platform;
import me.piitex.renjava.api.builders.FontLoader;
import me.piitex.renjava.api.music.Track;
import me.piitex.renjava.configuration.SettingsProperties;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.commons.io.filefilter.RegexFileFilter;

public class RenLoader {
private final RenJava renJava;

boolean shutdown = false;
public RenLoader(RenJava renJava) {
this.renJava = renJava;
renJava.getLogger().info("Starting processes...");
renJava.buildVersion = getVersion();
setupMain();
setupGame();
if (shutdown) {
// Shutdown application.
renJava.getLogger().severe("Game assets do not exist. Please download default assets and place them inside the 'game' folder.");
Platform.exit();
System.exit(0);
return;
}
startPreProcess();
}

private void setupMain() {
renJava.getLogger().info("Checking game environment...");


File gameDirectory = new File(System.getProperty("user.dir") + "/game/");
if (gameDirectory.mkdir()) {
renJava.getLogger().severe("Game directory does not exist. The game will not work properly, please move all assets into the newly created game directory.");
shutdown = true;
}
File renJavaDirectory = new File(System.getProperty("user.dir") + "/renjava/");
renJavaDirectory.mkdir();
if (renJavaDirectory.mkdir()) {
renJava.getLogger().warning("RenJava folder does not exist. User settings will be reset to defaults.");
}
File logFile = new File(System.getProperty("user.dir"), "log.txt");
try {
logFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
renJava.getLogger().warning("Could not create log file. Ensure the application has read and write permissions.");
}
for (File file : new File(System.getProperty("user.dir")).listFiles()) {
if (file.getName().endsWith(".txt.lck")) {
Expand All @@ -60,11 +65,17 @@ private void setupGame() {
}
renJava.getLogger().info("Loaded " + audioLoaded + " audio file(s)");
File imageDirectory = new File(directory, "/images/");
imageDirectory.mkdir();
if (imageDirectory.mkdir()) {
renJava.getLogger().warning("Images folder does not exist, creating...");
}
File savesDirectory = new File(directory, "/saves/");
savesDirectory.mkdir();
if (savesDirectory.mkdir()) {
renJava.getLogger().warning("Saves folder does not exist, creating...");
}
File fontsDirectory = new File(directory, "/fonts/");
fontsDirectory.mkdir();
if (fontsDirectory.mkdir()) {
renJava.getLogger().warning("Fonts folder does not exist, creating...");
}

renJava.getLogger().info("Loading fonts...");
int fonts = 0;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/me/piitex/renjava/addons/AddonLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ private void initAddon(File file, @Nullable Collection<Addon> dependencies) thro
addon.getDependencies().addAll(dependencies);
}
addons.add(addon);
clazz.getMethod("onLoad").invoke(object, null);
//clazz.getMethod("onLoad").invoke(object, null);
addon.onLoad(); // Loads addon
logger.info("Loaded: " + addon.getName());
}
}
Expand Down
Loading

0 comments on commit 258a41a

Please sign in to comment.