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