diff --git a/GameEngine/src/main/java/org/example/ColorController.class b/GameEngine/src/main/java/org/example/ColorController.class deleted file mode 100644 index 1a0c83d..0000000 --- a/GameEngine/src/main/java/org/example/ColorController.class +++ /dev/null Binary files differ diff --git a/GameEngine/src/main/java/org/example/ColorController.java b/GameEngine/src/main/java/org/example/ColorController.java index 375eb44..7949bda 100644 --- a/GameEngine/src/main/java/org/example/ColorController.java +++ b/GameEngine/src/main/java/org/example/ColorController.java @@ -1,6 +1,7 @@ package org.example; import static org.lwjgl.glfw.GLFW.*; +import static org.lwjgl.opengl.GL11.glClearColor; public class ColorController implements IGameComponent { @@ -13,18 +14,25 @@ @Override public void update() { - if (KeyInput.KeyPress(GLFW_KEY_R)) { - setColor(1.0f, 0.0f, 0.0f); // Rキーで赤に変更 + + if (KeyInput.KeyDown(GLFW_KEY_R)) { + setColor(1.0f, 0.0f, 0.0f); // Rキーが押された瞬間 + System.out.println("R"); } if (KeyInput.KeyPress(GLFW_KEY_G)) { - setColor(0.0f, 1.0f, 0.0f); // Gキーで緑に変更 + setColor(0.0f, 1.0f, 0.0f); // Gキーが押され続けている間 + System.out.println("G"); } - if (KeyInput.KeyPress(GLFW_KEY_B)) { - setColor(0.0f, 0.0f, 1.0f); // Bキーで青に変更 + if (KeyInput.KeyUp(GLFW_KEY_B)) { + setColor(0.0f, 0.0f, 1.0f); // Bキーが離された瞬間 + System.out.println("B"); } - if (KeyInput.KeyPress(GLFW_KEY_Y)) { - setColor(1.0f, 1.0f, 0.0f); // Yキーで黄色に変更 + if (KeyInput.KeyUp(GLFW_KEY_Y)) { + setColor(1.0f, 1.0f, 0.0f); + System.out.println("Y"); } + + glClearColor(getRed(), getGreen(), getBlue(), 0.0f); } public float getRed() { return red; } diff --git a/GameEngine/src/main/java/org/example/IGameComponent.class b/GameEngine/src/main/java/org/example/IGameComponent.class deleted file mode 100644 index 1995c59..0000000 --- a/GameEngine/src/main/java/org/example/IGameComponent.class +++ /dev/null Binary files differ diff --git a/GameEngine/src/main/java/org/example/KeyInput.class b/GameEngine/src/main/java/org/example/KeyInput.class deleted file mode 100644 index fc7b351..0000000 --- a/GameEngine/src/main/java/org/example/KeyInput.class +++ /dev/null Binary files differ diff --git a/GameEngine/src/main/java/org/example/KeyInput.java b/GameEngine/src/main/java/org/example/KeyInput.java index 64d33fc..c9c198b 100644 --- a/GameEngine/src/main/java/org/example/KeyInput.java +++ b/GameEngine/src/main/java/org/example/KeyInput.java @@ -1,10 +1,14 @@ package org.example; import static org.lwjgl.glfw.GLFW.*; + public class KeyInput { private static KeyInput instance; private static long window; + private static final boolean[] lastKeyStates = new boolean[GLFW_KEY_LAST + 1]; // 前フレームのキー状態 + private static final boolean[] currentKeyStates = new boolean[GLFW_KEY_LAST + 1]; // 現在のキー状態 + private KeyInput(long window) { KeyInput.window = window; } @@ -15,7 +19,42 @@ } } + // キーを押している時 public static boolean KeyPress(int keyCode) { - return glfwGetKey(window, keyCode) == GLFW_PRESS; + if (isValidKeyCode(keyCode)) { + return currentKeyStates[keyCode]; + } + return false; + } + + //キーを押した時 + public static boolean KeyDown(int keyCode) { + if (isValidKeyCode(keyCode)) { + return currentKeyStates[keyCode] && !lastKeyStates[keyCode]; + } + return false; + } + + //キーを離した時 + public static boolean KeyUp(int keyCode) { + if (isValidKeyCode(keyCode)) { + return !currentKeyStates[keyCode] && lastKeyStates[keyCode]; // 今押されておらず、前は押されていた + } + return false; + } + + // キー状態を更新 + public static void updateKeyStates() { + // 前回のキー状態を保存 + System.arraycopy(currentKeyStates, 0, lastKeyStates, 0, currentKeyStates.length); + + for (int i = GLFW_KEY_SPACE; i <= GLFW_KEY_LAST; i++) { // 有効な範囲でのみ更新 + currentKeyStates[i] = glfwGetKey(window, i) == GLFW_PRESS; + } + } + + // 有効なキーコードかチェック + private static boolean isValidKeyCode(int keyCode) { + return keyCode >= GLFW_KEY_SPACE && keyCode <= GLFW_KEY_LAST; } } diff --git a/GameEngine/src/main/java/org/example/Main.class b/GameEngine/src/main/java/org/example/Main.class deleted file mode 100644 index 751b51b..0000000 --- a/GameEngine/src/main/java/org/example/Main.class +++ /dev/null Binary files differ diff --git a/GameEngine/src/main/java/org/example/Main.java b/GameEngine/src/main/java/org/example/Main.java index 3848858..7bef3d7 100644 --- a/GameEngine/src/main/java/org/example/Main.java +++ b/GameEngine/src/main/java/org/example/Main.java @@ -19,7 +19,6 @@ private long window; private final List iGameComponents = new ArrayList<>(); - private ColorController colorController = new ColorController(); public static void main(String[] args) { new Main().run(); } @@ -84,7 +83,7 @@ //--------------------------------------------------------------- KeyInput.init(window); - iGameComponents.add(colorController); + iGameComponents.add(new ColorController()); //GameComponentのinit処理 for (IGameComponent gameComponents : iGameComponents) { @@ -98,13 +97,15 @@ while (!glfwWindowShouldClose(window)) { //--------------------------------------------------------------- + // 毎フレームのキー状態を更新 + KeyInput.updateKeyStates(); + //GameComponentのupdate処理 for (IGameComponent gameComponents : iGameComponents) { gameComponents.update(); } //--------------------------------------------------------------- - glClearColor(colorController.getRed(), colorController.getGreen(), colorController.getBlue(), 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // フレームバッファをクリア glfwSwapBuffers(window); // カラーバッファを交換 glfwPollEvents(); // ウィンドウイベントをポーリング