Create Input model
This commit is contained in:
@@ -4,6 +4,7 @@ import com.bartlomiejpluta.base.api.game.camera.Camera;
|
|||||||
import com.bartlomiejpluta.base.api.game.entity.Entity;
|
import com.bartlomiejpluta.base.api.game.entity.Entity;
|
||||||
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
||||||
import com.bartlomiejpluta.base.api.game.image.Image;
|
import com.bartlomiejpluta.base.api.game.image.Image;
|
||||||
|
import com.bartlomiejpluta.base.api.game.input.Input;
|
||||||
import com.bartlomiejpluta.base.api.game.runner.GameRunner;
|
import com.bartlomiejpluta.base.api.game.runner.GameRunner;
|
||||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||||
import com.bartlomiejpluta.base.api.internal.gc.Disposable;
|
import com.bartlomiejpluta.base.api.internal.gc.Disposable;
|
||||||
@@ -17,6 +18,8 @@ public interface Context extends Updatable, Renderable, Disposable {
|
|||||||
|
|
||||||
Camera getCamera();
|
Camera getCamera();
|
||||||
|
|
||||||
|
Input getInput();
|
||||||
|
|
||||||
String getProjectName();
|
String getProjectName();
|
||||||
|
|
||||||
void openMap(String mapUid);
|
void openMap(String mapUid);
|
||||||
@@ -39,7 +42,7 @@ public interface Context extends Updatable, Renderable, Disposable {
|
|||||||
|
|
||||||
boolean togglePause();
|
boolean togglePause();
|
||||||
|
|
||||||
void init(Screen screen, Camera camera);
|
void init(Screen screen, Input input, Camera camera);
|
||||||
|
|
||||||
void input(Screen screen);
|
void input(Input input);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.bartlomiejpluta.base.api.game.input;
|
||||||
|
|
||||||
|
public interface Input {
|
||||||
|
boolean isKeyPressed(Key key);
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.bartlomiejpluta.base.api.game.map.handler;
|
package com.bartlomiejpluta.base.api.game.map.handler;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.game.context.Context;
|
import com.bartlomiejpluta.base.api.game.context.Context;
|
||||||
|
import com.bartlomiejpluta.base.api.game.input.Input;
|
||||||
import com.bartlomiejpluta.base.api.game.map.model.GameMap;
|
import com.bartlomiejpluta.base.api.game.map.model.GameMap;
|
||||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||||
|
|
||||||
@@ -9,7 +10,7 @@ public interface MapHandler {
|
|||||||
|
|
||||||
void onOpen(Context context, GameMap map);
|
void onOpen(Context context, GameMap map);
|
||||||
|
|
||||||
void input(Screen screen);
|
void input(Input input);
|
||||||
|
|
||||||
void update(Context context, GameMap map, float dt);
|
void update(Context context, GameMap map, float dt);
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
package com.bartlomiejpluta.base.api.game.runner;
|
package com.bartlomiejpluta.base.api.game.runner;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.game.context.Context;
|
import com.bartlomiejpluta.base.api.game.context.Context;
|
||||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
import com.bartlomiejpluta.base.api.game.input.Input;
|
||||||
import com.bartlomiejpluta.base.api.internal.gc.Disposable;
|
import com.bartlomiejpluta.base.api.internal.gc.Disposable;
|
||||||
|
|
||||||
public interface GameRunner extends Disposable {
|
public interface GameRunner extends Disposable {
|
||||||
void init(Context context);
|
void init(Context context);
|
||||||
|
|
||||||
void input(Screen screen);
|
void input(Input input);
|
||||||
|
|
||||||
void update(float dt);
|
void update(float dt);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
package com.bartlomiejpluta.base.api.game.screen;
|
package com.bartlomiejpluta.base.api.game.screen;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.game.input.Key;
|
|
||||||
import org.joml.Vector2fc;
|
import org.joml.Vector2fc;
|
||||||
|
|
||||||
public interface Screen {
|
public interface Screen {
|
||||||
@@ -18,8 +17,6 @@ public interface Screen {
|
|||||||
|
|
||||||
boolean shouldClose();
|
boolean shouldClose();
|
||||||
|
|
||||||
boolean isKeyPressed(Key key);
|
|
||||||
|
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
void clear(float r, float g, float b, float alpha);
|
void clear(float r, float g, float b, float alpha);
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import com.bartlomiejpluta.base.api.game.context.Context;
|
|||||||
import com.bartlomiejpluta.base.api.game.entity.Entity;
|
import com.bartlomiejpluta.base.api.game.entity.Entity;
|
||||||
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
import com.bartlomiejpluta.base.api.game.gui.base.GUI;
|
||||||
import com.bartlomiejpluta.base.api.game.image.Image;
|
import com.bartlomiejpluta.base.api.game.image.Image;
|
||||||
|
import com.bartlomiejpluta.base.api.game.input.Input;
|
||||||
import com.bartlomiejpluta.base.api.game.map.handler.MapHandler;
|
import com.bartlomiejpluta.base.api.game.map.handler.MapHandler;
|
||||||
import com.bartlomiejpluta.base.api.game.runner.GameRunner;
|
import com.bartlomiejpluta.base.api.game.runner.GameRunner;
|
||||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||||
@@ -52,6 +53,9 @@ public class DefaultContext implements Context {
|
|||||||
@NonNull
|
@NonNull
|
||||||
private final String projectName;
|
private final String projectName;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
private Input input;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private Screen screen;
|
private Screen screen;
|
||||||
|
|
||||||
@@ -65,8 +69,9 @@ public class DefaultContext implements Context {
|
|||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
@Override
|
@Override
|
||||||
public void init(@NonNull Screen screen, @NonNull Camera camera) {
|
public void init(@NonNull Screen screen, @NonNull Input input, @NonNull Camera camera) {
|
||||||
this.screen = screen;
|
this.screen = screen;
|
||||||
|
this.input = input;
|
||||||
this.camera = camera;
|
this.camera = camera;
|
||||||
|
|
||||||
gameRunner.init(this);
|
gameRunner.init(this);
|
||||||
@@ -132,11 +137,11 @@ public class DefaultContext implements Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void input(Screen screen) {
|
public void input(Input input) {
|
||||||
gameRunner.input(screen);
|
gameRunner.input(input);
|
||||||
|
|
||||||
if (mapHandler != null) {
|
if (mapHandler != null) {
|
||||||
mapHandler.input(screen);
|
mapHandler.input(input);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
package com.bartlomiejpluta.base.engine.core.engine;
|
package com.bartlomiejpluta.base.engine.core.engine;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.game.context.Context;
|
import com.bartlomiejpluta.base.api.game.context.Context;
|
||||||
|
import com.bartlomiejpluta.base.api.game.input.Input;
|
||||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||||
import com.bartlomiejpluta.base.engine.gc.OffHeapGarbageCollector;
|
import com.bartlomiejpluta.base.engine.gc.OffHeapGarbageCollector;
|
||||||
import com.bartlomiejpluta.base.engine.logic.GameLogic;
|
import com.bartlomiejpluta.base.engine.logic.GameLogic;
|
||||||
import com.bartlomiejpluta.base.engine.thread.ThreadManager;
|
import com.bartlomiejpluta.base.engine.thread.ThreadManager;
|
||||||
import com.bartlomiejpluta.base.engine.time.ChronoMeter;
|
import com.bartlomiejpluta.base.engine.time.ChronoMeter;
|
||||||
import com.bartlomiejpluta.base.engine.ui.ScreenManager;
|
import com.bartlomiejpluta.base.engine.ui.manager.InputManager;
|
||||||
|
import com.bartlomiejpluta.base.engine.ui.manager.ScreenManager;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -20,6 +22,7 @@ public class DefaultGameEngine implements GameEngine {
|
|||||||
private static final String THREAD_NAME = "engine";
|
private static final String THREAD_NAME = "engine";
|
||||||
|
|
||||||
private final ScreenManager screenManager;
|
private final ScreenManager screenManager;
|
||||||
|
private final InputManager inputManager;
|
||||||
private final ThreadManager threadManager;
|
private final ThreadManager threadManager;
|
||||||
private final GameLogic logic;
|
private final GameLogic logic;
|
||||||
private final OffHeapGarbageCollector garbageCollector;
|
private final OffHeapGarbageCollector garbageCollector;
|
||||||
@@ -28,6 +31,7 @@ public class DefaultGameEngine implements GameEngine {
|
|||||||
|
|
||||||
private Thread thread;
|
private Thread thread;
|
||||||
private Screen screen;
|
private Screen screen;
|
||||||
|
private Input input;
|
||||||
private int targetUps;
|
private int targetUps;
|
||||||
|
|
||||||
private Context context;
|
private Context context;
|
||||||
@@ -81,7 +85,7 @@ public class DefaultGameEngine implements GameEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void input() {
|
private void input() {
|
||||||
logic.input(screen);
|
logic.input(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void update(float dt) {
|
private void update(float dt) {
|
||||||
@@ -107,6 +111,7 @@ public class DefaultGameEngine implements GameEngine {
|
|||||||
|
|
||||||
this.screen = screenManager.createScreen(context.getProjectName(), WINDOW_WIDTH, WINDOW_HEIGHT);
|
this.screen = screenManager.createScreen(context.getProjectName(), WINDOW_WIDTH, WINDOW_HEIGHT);
|
||||||
this.thread = threadManager.createThread(THREAD_NAME, this::run);
|
this.thread = threadManager.createThread(THREAD_NAME, this::run);
|
||||||
|
this.input = inputManager.getInput();
|
||||||
|
|
||||||
this.targetUps = TARGET_UPS;
|
this.targetUps = TARGET_UPS;
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,10 @@ package com.bartlomiejpluta.base.engine.logic;
|
|||||||
|
|
||||||
import com.bartlomiejpluta.base.api.game.camera.Camera;
|
import com.bartlomiejpluta.base.api.game.camera.Camera;
|
||||||
import com.bartlomiejpluta.base.api.game.context.Context;
|
import com.bartlomiejpluta.base.api.game.context.Context;
|
||||||
|
import com.bartlomiejpluta.base.api.game.input.Input;
|
||||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||||
import com.bartlomiejpluta.base.engine.core.gl.render.Renderer;
|
import com.bartlomiejpluta.base.engine.core.gl.render.Renderer;
|
||||||
|
import com.bartlomiejpluta.base.engine.ui.manager.InputManager;
|
||||||
import com.bartlomiejpluta.base.engine.world.camera.DefaultCamera;
|
import com.bartlomiejpluta.base.engine.world.camera.DefaultCamera;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -15,6 +17,7 @@ import org.springframework.stereotype.Component;
|
|||||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||||
public class DefaultGameLogic implements GameLogic {
|
public class DefaultGameLogic implements GameLogic {
|
||||||
private final Renderer renderer;
|
private final Renderer renderer;
|
||||||
|
private final InputManager inputManager;
|
||||||
private final Camera camera = new DefaultCamera();
|
private final Camera camera = new DefaultCamera();
|
||||||
|
|
||||||
private Context context;
|
private Context context;
|
||||||
@@ -27,12 +30,12 @@ public class DefaultGameLogic implements GameLogic {
|
|||||||
renderer.init();
|
renderer.init();
|
||||||
|
|
||||||
log.info("Initializing game context");
|
log.info("Initializing game context");
|
||||||
context.init(screen, camera);
|
context.init(screen, inputManager.getInput(), camera);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void input(Screen screen) {
|
public void input(Input input) {
|
||||||
context.input(screen);
|
context.input(input);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,13 +1,14 @@
|
|||||||
package com.bartlomiejpluta.base.engine.logic;
|
package com.bartlomiejpluta.base.engine.logic;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.game.context.Context;
|
import com.bartlomiejpluta.base.api.game.context.Context;
|
||||||
|
import com.bartlomiejpluta.base.api.game.input.Input;
|
||||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||||
import com.bartlomiejpluta.base.api.internal.gc.Cleanable;
|
import com.bartlomiejpluta.base.api.internal.gc.Cleanable;
|
||||||
|
|
||||||
public interface GameLogic extends Cleanable {
|
public interface GameLogic extends Cleanable {
|
||||||
void init(Screen screen, Context context);
|
void init(Screen screen, Context context);
|
||||||
|
|
||||||
void input(Screen screen);
|
void input(Input input);
|
||||||
|
|
||||||
void update(float dt);
|
void update(float dt);
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +0,0 @@
|
|||||||
package com.bartlomiejpluta.base.engine.ui;
|
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
@Component
|
|
||||||
public class ScreenManager {
|
|
||||||
public Screen createScreen(String title, int width, int height) {
|
|
||||||
log.info("Creating GLFW window ([{}], {}x{})", title, width, height);
|
|
||||||
return GLFWScreen.create(title, width, height);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.bartlomiejpluta.base.engine.ui.manager;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.api.game.input.Input;
|
||||||
|
import com.bartlomiejpluta.base.engine.error.AppException;
|
||||||
|
import com.bartlomiejpluta.base.engine.ui.model.GLFWInput;
|
||||||
|
import com.bartlomiejpluta.base.engine.ui.model.GLFWScreen;
|
||||||
|
import lombok.NonNull;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class InputManager {
|
||||||
|
private GLFWScreen screen;
|
||||||
|
private Input input;
|
||||||
|
|
||||||
|
void registerScreen(@NonNull GLFWScreen screen) {
|
||||||
|
this.screen = screen;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Input getInput() {
|
||||||
|
if (input != null) {
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("Creating input model singleton instance");
|
||||||
|
|
||||||
|
if (screen == null) {
|
||||||
|
throw new AppException("GLFWScreen is not registered yet");
|
||||||
|
}
|
||||||
|
|
||||||
|
input = new GLFWInput(screen);
|
||||||
|
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.bartlomiejpluta.base.engine.ui.manager;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||||
|
import com.bartlomiejpluta.base.engine.ui.model.GLFWScreen;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||||
|
public class ScreenManager {
|
||||||
|
private final InputManager inputManager;
|
||||||
|
|
||||||
|
public Screen createScreen(String title, int width, int height) {
|
||||||
|
log.info("Creating GLFW window ([{}], {}x{})", title, width, height);
|
||||||
|
var screen = GLFWScreen.create(title, width, height);
|
||||||
|
inputManager.registerScreen(screen);
|
||||||
|
return screen;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package com.bartlomiejpluta.base.engine.ui.model;
|
||||||
|
|
||||||
|
import com.bartlomiejpluta.base.api.game.input.Input;
|
||||||
|
import com.bartlomiejpluta.base.api.game.input.Key;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class GLFWInput implements Input {
|
||||||
|
private final GLFWScreen screen;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isKeyPressed(Key key) {
|
||||||
|
return screen.isKeyPressed(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.bartlomiejpluta.base.engine.ui;
|
package com.bartlomiejpluta.base.engine.ui.model;
|
||||||
|
|
||||||
import com.bartlomiejpluta.base.api.game.input.Key;
|
import com.bartlomiejpluta.base.api.game.input.Key;
|
||||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||||
@@ -115,7 +115,6 @@ public class GLFWScreen implements Screen {
|
|||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isKeyPressed(Key key) {
|
public boolean isKeyPressed(Key key) {
|
||||||
return glfwGetKey(windowHandle, glfwCode(key)) == GLFW_PRESS;
|
return glfwGetKey(windowHandle, glfwCode(key)) == GLFW_PRESS;
|
||||||
}
|
}
|
||||||
@@ -141,7 +140,7 @@ public class GLFWScreen implements Screen {
|
|||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Screen create(String title, int width, int height) {
|
public static GLFWScreen create(String title, int width, int height) {
|
||||||
return new GLFWScreen(title, width, height);
|
return new GLFWScreen(title, width, height);
|
||||||
}
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user