Create Input model
This commit is contained in:
@@ -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.gui.base.GUI;
|
||||
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.runner.GameRunner;
|
||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||
@@ -52,6 +53,9 @@ public class DefaultContext implements Context {
|
||||
@NonNull
|
||||
private final String projectName;
|
||||
|
||||
@Getter
|
||||
private Input input;
|
||||
|
||||
@Getter
|
||||
private Screen screen;
|
||||
|
||||
@@ -65,8 +69,9 @@ public class DefaultContext implements Context {
|
||||
|
||||
@SneakyThrows
|
||||
@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.input = input;
|
||||
this.camera = camera;
|
||||
|
||||
gameRunner.init(this);
|
||||
@@ -132,11 +137,11 @@ public class DefaultContext implements Context {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void input(Screen screen) {
|
||||
gameRunner.input(screen);
|
||||
public void input(Input input) {
|
||||
gameRunner.input(input);
|
||||
|
||||
if (mapHandler != null) {
|
||||
mapHandler.input(screen);
|
||||
mapHandler.input(input);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package com.bartlomiejpluta.base.engine.core.engine;
|
||||
|
||||
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.engine.gc.OffHeapGarbageCollector;
|
||||
import com.bartlomiejpluta.base.engine.logic.GameLogic;
|
||||
import com.bartlomiejpluta.base.engine.thread.ThreadManager;
|
||||
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.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -20,6 +22,7 @@ public class DefaultGameEngine implements GameEngine {
|
||||
private static final String THREAD_NAME = "engine";
|
||||
|
||||
private final ScreenManager screenManager;
|
||||
private final InputManager inputManager;
|
||||
private final ThreadManager threadManager;
|
||||
private final GameLogic logic;
|
||||
private final OffHeapGarbageCollector garbageCollector;
|
||||
@@ -28,6 +31,7 @@ public class DefaultGameEngine implements GameEngine {
|
||||
|
||||
private Thread thread;
|
||||
private Screen screen;
|
||||
private Input input;
|
||||
private int targetUps;
|
||||
|
||||
private Context context;
|
||||
@@ -81,7 +85,7 @@ public class DefaultGameEngine implements GameEngine {
|
||||
}
|
||||
|
||||
private void input() {
|
||||
logic.input(screen);
|
||||
logic.input(input);
|
||||
}
|
||||
|
||||
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.thread = threadManager.createThread(THREAD_NAME, this::run);
|
||||
this.input = inputManager.getInput();
|
||||
|
||||
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.context.Context;
|
||||
import com.bartlomiejpluta.base.api.game.input.Input;
|
||||
import com.bartlomiejpluta.base.api.game.screen.Screen;
|
||||
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 lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -15,6 +17,7 @@ import org.springframework.stereotype.Component;
|
||||
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
||||
public class DefaultGameLogic implements GameLogic {
|
||||
private final Renderer renderer;
|
||||
private final InputManager inputManager;
|
||||
private final Camera camera = new DefaultCamera();
|
||||
|
||||
private Context context;
|
||||
@@ -27,12 +30,12 @@ public class DefaultGameLogic implements GameLogic {
|
||||
renderer.init();
|
||||
|
||||
log.info("Initializing game context");
|
||||
context.init(screen, camera);
|
||||
context.init(screen, inputManager.getInput(), camera);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void input(Screen screen) {
|
||||
context.input(screen);
|
||||
public void input(Input input) {
|
||||
context.input(input);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
package com.bartlomiejpluta.base.engine.logic;
|
||||
|
||||
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.internal.gc.Cleanable;
|
||||
|
||||
public interface GameLogic extends Cleanable {
|
||||
void init(Screen screen, Context context);
|
||||
|
||||
void input(Screen screen);
|
||||
void input(Input input);
|
||||
|
||||
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.screen.Screen;
|
||||
@@ -115,7 +115,6 @@ public class GLFWScreen implements Screen {
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isKeyPressed(Key key) {
|
||||
return glfwGetKey(windowHandle, glfwCode(key)) == GLFW_PRESS;
|
||||
}
|
||||
@@ -141,7 +140,7 @@ public class GLFWScreen implements Screen {
|
||||
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user