Refactor Scene swapping logic, refactor Log class
This commit is contained in:
@@ -1,7 +1,15 @@
|
||||
package org.hirw.game;
|
||||
|
||||
public abstract class Scene {
|
||||
public final SceneType SCENE_TYPE = SceneType.UNDEFINED;
|
||||
|
||||
public Scene() {}
|
||||
|
||||
abstract void update();
|
||||
public abstract void init();
|
||||
|
||||
public abstract void update();
|
||||
|
||||
public String toString() {
|
||||
return SCENE_TYPE.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.hirw.game;
|
||||
import java.util.EnumMap;
|
||||
import java.util.Map;
|
||||
import lombok.Getter;
|
||||
import org.hirw.game.util.Log;
|
||||
|
||||
public final class SceneManager {
|
||||
private static final SceneType DEFAULT_SCENE_TYPE = SceneType.SPLASH;
|
||||
@@ -16,11 +17,30 @@ public final class SceneManager {
|
||||
|
||||
@Getter private static Scene scene = SCENES.get(DEFAULT_SCENE_TYPE);
|
||||
|
||||
public static void setScene(SceneType sType) {
|
||||
scene = SCENES.get(sType);
|
||||
public static void init() {
|
||||
setScene(DEFAULT_SCENE_TYPE);
|
||||
}
|
||||
|
||||
public static void setScene(SceneType sceneType) {
|
||||
scene = SCENES.get(sceneType);
|
||||
scene.init();
|
||||
logSceneChange();
|
||||
}
|
||||
|
||||
public static void update() {
|
||||
scene.update();
|
||||
if (scene.SCENE_TYPE == SceneType.UNDEFINED) return;
|
||||
|
||||
getScene().update();
|
||||
}
|
||||
|
||||
private static void logSceneChange() {
|
||||
SceneType newSceneType = getScene().SCENE_TYPE;
|
||||
String loadedSceneMessage = String.format("Changed to scene '%s'", newSceneType.toString());
|
||||
|
||||
if (newSceneType == SceneType.UNDEFINED) {
|
||||
Log.warning("SceneManager", loadedSceneMessage);
|
||||
} else {
|
||||
Log.success("SceneManager", loadedSceneMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
package org.hirw.game;
|
||||
|
||||
public enum SceneType {
|
||||
UNDEFINED,
|
||||
SPLASH,
|
||||
MENU,
|
||||
GAME
|
||||
GAME,
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ public class Shader {
|
||||
if (glGetShaderi(shaderID, GL_COMPILE_STATUS) == GL_FALSE) {
|
||||
int len = glGetShaderi(shaderID, GL_INFO_LOG_LENGTH);
|
||||
Log.error(
|
||||
"Shader initialisation",
|
||||
"Shader",
|
||||
String.format(
|
||||
"Failed to compile %s shader: %s",
|
||||
shaderType.toString(), glGetShaderInfoLog(shaderID, len)));
|
||||
@@ -95,10 +95,10 @@ public class Shader {
|
||||
int len = glGetProgrami(id, GL_INFO_LOG_LENGTH);
|
||||
|
||||
Log.error(
|
||||
"Shader initialisation",
|
||||
"Shader",
|
||||
String.format("Failed to create Shader Program: %s", glGetShaderInfoLog(id, len)));
|
||||
} else {
|
||||
Log.success("Shader initialisation", "Shader Program created.");
|
||||
Log.success("Shader", "Shader Program created.");
|
||||
}
|
||||
|
||||
setShaderProgramID(id);
|
||||
@@ -112,12 +112,9 @@ public class Shader {
|
||||
source = Files.readString(filePath);
|
||||
|
||||
} catch (NoSuchFileException | InvalidPathException e) {
|
||||
Log.error(
|
||||
"Shader initialisation", "Couldn't open file (probably a bad path): " + stringFilePath);
|
||||
Log.error("Shader", "Couldn't open file (probably a bad path): " + stringFilePath);
|
||||
} catch (IOException e) {
|
||||
Log.error(
|
||||
"Shader initialisation",
|
||||
"An IO Exception occured while reading from file: " + stringFilePath);
|
||||
Log.error("Shader", "An IO Exception occured while reading from file: " + stringFilePath);
|
||||
}
|
||||
|
||||
return source;
|
||||
|
||||
@@ -3,23 +3,38 @@ package org.hirw.game;
|
||||
import static org.lwjgl.opengl.GL11.*;
|
||||
|
||||
import java.util.Objects;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.hirw.game.util.Time;
|
||||
|
||||
public class SplashScene extends Scene {
|
||||
@Getter public final SceneType SCENE_TYPE = SceneType.SPLASH;
|
||||
private final float INITIAL_BRIGHTNESS = 0.0f;
|
||||
private final float FADE_RATE = 0.5f;
|
||||
|
||||
private float brightness;
|
||||
|
||||
@Getter @Setter private Mesh screenCover;
|
||||
|
||||
public SplashScene() {
|
||||
this.brightness = INITIAL_BRIGHTNESS;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
Shader faderShader = new Shader();
|
||||
faderShader.init();
|
||||
Mesh screenCover = new Mesh(faderShader);
|
||||
screenCover.init();
|
||||
setScreenCover(screenCover);
|
||||
}
|
||||
|
||||
public void update() {
|
||||
if (Objects.isNull(Window.get().getGlfwWindow())) {
|
||||
return;
|
||||
}
|
||||
|
||||
screenCover.draw();
|
||||
|
||||
fadeIn();
|
||||
}
|
||||
|
||||
@@ -28,4 +43,11 @@ public class SplashScene extends Scene {
|
||||
final float fadeAmount = FADE_RATE * Time.deltaTime();
|
||||
this.brightness += fadeAmount;
|
||||
}
|
||||
|
||||
private static final float[] screenCoverRect = {
|
||||
1.0f, -0.0f, 0.0f, /* */ 0.0f, 0.0f, 0.0f, 0.0f,
|
||||
-0.0f, 1.0f, 0.0f, /* */ 0.0f, 0.0f, 0.0f, 0.0f,
|
||||
1.0f, 1.0f, 0.0f, /* */ 0.0f, 0.0f, 0.0f, 0.0f,
|
||||
-1.0f, -1.0f, 0.0f, /* */ 0.0f, 0.0f, 0.0f, 0.0f,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ public class Window {
|
||||
createWindow();
|
||||
createShader();
|
||||
createMesh();
|
||||
SceneManager.init();
|
||||
}
|
||||
|
||||
private void logVersion() {
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package org.hirw.game.util;
|
||||
|
||||
import java.util.EnumMap;
|
||||
import java.util.Map;
|
||||
|
||||
public final class Log {
|
||||
private interface Colours {
|
||||
final String BLACK = "\u001B[30m";
|
||||
@@ -11,6 +14,8 @@ public final class Log {
|
||||
final String YELLOW = "\u001B[33m";
|
||||
final String YELLOW_BG = "\u001B[43m";
|
||||
final String BLUE = "\u001B[34m";
|
||||
final String BLUE_BG = "\u001B[44m";
|
||||
final String PURPLE = "\u001B[35m";
|
||||
final String PURPLE_BG = "\u001B[45m";
|
||||
final String CYAN = "\u001B[36m";
|
||||
final String CYAN_BG = "\u001B[46m";
|
||||
@@ -19,24 +24,40 @@ public final class Log {
|
||||
final String ANSI_RESET = "\u001B[0m";
|
||||
}
|
||||
|
||||
private static final EnumMap<LogType, String> COLOUR_FOR_LOG_TYPE =
|
||||
new EnumMap<>(
|
||||
Map.of(
|
||||
LogType.SUCCESS, Colours.GREEN,
|
||||
LogType.ERROR, Colours.RED,
|
||||
LogType.WARNING, Colours.PURPLE));
|
||||
|
||||
public static void error(String errorStage, String errorDescription) {
|
||||
String fancyError = String.format("[%s] ", colourisedString(Colours.RED, "ERROR"));
|
||||
String fancyErrorStage = colourisedString(Colours.YELLOW, String.format("<%s> ", errorStage));
|
||||
System.err.println(fancyError + fancyErrorStage + errorDescription);
|
||||
System.out.println(formatString(LogType.ERROR, errorStage, errorDescription));
|
||||
}
|
||||
|
||||
public static void success(String successStage, String successDescription) {
|
||||
String fancySuccess = String.format("[%s] ", colourisedString(Colours.GREEN, "SUCCESS"));
|
||||
String fancySucessStage =
|
||||
colourisedString(Colours.YELLOW, String.format("<%s> ", successStage));
|
||||
System.out.println(fancySuccess + fancySucessStage + successDescription);
|
||||
System.out.println(formatString(LogType.SUCCESS, successStage, successDescription));
|
||||
}
|
||||
|
||||
private static String colourisedString(String colour, String string) {
|
||||
public static void warning(String warningStage, String warningDescription) {
|
||||
System.out.println(formatString(LogType.WARNING, warningStage, warningDescription));
|
||||
}
|
||||
|
||||
private static String formatString(LogType logType, String stage, String description) {
|
||||
String formattedType = String.format("[%s]", colouriseString(logType, logType.toString()));
|
||||
String formattedStage = colouriseString(Colours.YELLOW, String.format("<%s>", stage));
|
||||
return String.format("%s %s %s", formattedType, formattedStage, description);
|
||||
}
|
||||
|
||||
private static String colouriseString(LogType logType, String string) {
|
||||
return COLOUR_FOR_LOG_TYPE.get(logType) + string + Colours.ANSI_RESET;
|
||||
}
|
||||
|
||||
private static String colouriseString(String colour, String string) {
|
||||
return colour + string + Colours.ANSI_RESET;
|
||||
}
|
||||
|
||||
private static String colourisedString(String colour, String otherColour, String string) {
|
||||
private static String colouriseString(String colour, String otherColour, String string) {
|
||||
return colour + otherColour + string + Colours.ANSI_RESET;
|
||||
}
|
||||
}
|
||||
|
||||
7
src/main/java/org/hirw/game/util/LogType.java
Normal file
7
src/main/java/org/hirw/game/util/LogType.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package org.hirw.game.util;
|
||||
|
||||
public enum LogType {
|
||||
SUCCESS,
|
||||
WARNING,
|
||||
ERROR,
|
||||
}
|
||||
Reference in New Issue
Block a user