diff --git a/GameEngine/src/main/java/gameEngine/Time.java b/GameEngine/src/main/java/gameEngine/Time.java index d7b29f2..a020435 100644 --- a/GameEngine/src/main/java/gameEngine/Time.java +++ b/GameEngine/src/main/java/gameEngine/Time.java @@ -5,17 +5,46 @@ private static long lastFrameTime = System.nanoTime(); public static float deltaTime = 0; - public static float getTime(){return (float)((System.nanoTime() - timeStarted) * 1E-9); } + private static final long targetFrameTimeNanos = (long) (1E9 / 120); // <- フレームタイム + private static long lastFpsCheckTime = System.nanoTime(); + private static int frameCount = 0; + private static float currentFps = 0; + + public static float getTime() { + return (float) ((System.nanoTime() - timeStarted) * 1E-9); + } public static void update() { long currentTime = System.nanoTime(); - deltaTime = (float)((currentTime - lastFrameTime) * 1E-9); + deltaTime = (float) ((currentTime - lastFrameTime) * 1E-9); lastFrameTime = currentTime; + + // FPSの計算 + frameCount++; + if (currentTime - lastFpsCheckTime >= 1E9) { + currentFps = frameCount / ((currentTime - lastFpsCheckTime) * 1E-9f); + frameCount = 0; + lastFpsCheckTime = currentTime; + System.out.println("Current FPS: " + currentFps); + } + + // フレームレート制限 + long elapsedTime = currentTime - lastFrameTime; + if (elapsedTime < targetFrameTimeNanos) { + try { + Thread.sleep((targetFrameTimeNanos - elapsedTime) / 1_000_000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } } public static void reset() { timeStarted = System.nanoTime(); lastFrameTime = timeStarted; deltaTime = 0; + lastFpsCheckTime = timeStarted; + frameCount = 0; + currentFps = 0; } } diff --git a/GameEngine/src/main/java/gameEngine/entites/gameComponents/MoveImage.java b/GameEngine/src/main/java/gameEngine/entites/gameComponents/MoveImage.java index 4f0e405..a3ed53f 100644 --- a/GameEngine/src/main/java/gameEngine/entites/gameComponents/MoveImage.java +++ b/GameEngine/src/main/java/gameEngine/entites/gameComponents/MoveImage.java @@ -1,15 +1,16 @@ package gameEngine.entites.gameComponents; -import gameEngine.entites.Entity; +import gameEngine.Time; import gameEngine.entites.GameObject; +import gameEngine.geometry.Transform; import gameEngine.input.*; -import gameEngine.views.Color; import static org.lwjgl.glfw.GLFW.*; - public class MoveImage extends GameComponent{ private GameObject parent; + private final int moveSpeed = 100; + private final int rotateSpeed = 100; public MoveImage(GameObject parent){ this.parent = parent; @@ -20,90 +21,70 @@ } public void update() { - - if(Input.GetKey(GLFW_KEY_O)){ - GameObject gameObject = (GameObject) parent; - TextMesh textmesh = gameObject.getComponent(TextMesh.class); - textmesh.setTextSize(textmesh.getTextSize() + 1); - } - if(Input.GetKey(GLFW_KEY_P)){ - GameObject gameObject = (GameObject) parent; - TextMesh textmesh = gameObject.getComponent(TextMesh.class); - textmesh.setTextSize(textmesh.getTextSize() - 1); - } - if(Input.GetKeyDown(GLFW_KEY_I)){ - GameObject gameObject = (GameObject) parent; - TextMesh textmesh = gameObject.getComponent(TextMesh.class); - textmesh.setText("popopo"); - } - if(Input.GetKeyDown(GLFW_KEY_U)){ - GameObject gameObject = (GameObject) parent; - TextMesh textmesh = gameObject.getComponent(TextMesh.class); - textmesh.setColor(new Color(1f,0f,1f,1f)); - } - - if (Input.GetKey(GLFW_KEY_W)) { - float y = parent.transform.position.y; - parent.transform.setPosition(parent.transform.position.x, y - 1, parent.transform.position.z); - } - if (Input.GetKey(GLFW_KEY_A)) { - float x = parent.transform.position.x; - parent.transform.setPosition(x - 1, parent.transform.position.y, parent.transform.position.z); - } - if (Input.GetKey(GLFW_KEY_S)) { - float y = parent.transform.position.y; - parent.transform.setPosition(parent.transform.position.x, y + 1, parent.transform.position.z); - } - if (Input.GetKey(GLFW_KEY_D)) { - float x = parent.transform.position.x; - parent.transform.setPosition(x + 1, parent.transform.position.y, parent.transform.position.z); - } - if (Input.GetKey(GLFW_KEY_Q)) { - float z = parent.transform.position.z; - parent.transform.setPosition(parent.transform.position.x, parent.transform.position.y, z - 1); - } - if (Input.GetKey(GLFW_KEY_E)) { - float z = parent.transform.position.z; - parent.transform.setPosition(parent.transform.position.x, parent.transform.position.y, z + 1); - } - if (Input.GetKey(GLFW_KEY_Z)) { - float rotation = parent.transform.rotation.x; - rotation -= 1; - if (rotation < 0) rotation += 360; - - parent.transform.setRotation(rotation, parent.transform.rotation.y, parent.transform.rotation.z); // 左回転 - } - if (Input.GetKey(GLFW_KEY_C)) { - float rotation = parent.transform.rotation.x; - rotation += 1; - if (rotation >= 360) rotation -= 360; - parent.transform.setRotation(rotation , parent.transform.rotation.y, parent.transform.rotation.z); // 右回転 - } - if (Input.GetKey(GLFW_KEY_UP)) { - float rotation = parent.transform.rotation.y; - rotation -= 1; - if (rotation < 0) rotation += 360; - - parent.transform.setRotation(parent.transform.rotation.x, rotation, parent.transform.rotation.z); // 左回転 - } - if (Input.GetKey(GLFW_KEY_DOWN)) { - float rotation = parent.transform.rotation.y; - rotation += 1; - if (rotation >= 360) rotation -= 360; - parent.transform.setRotation(parent.transform.rotation.x, rotation, parent.transform.rotation.z); // 右回転 - } - if (Input.GetKey(GLFW_KEY_LEFT)) { - float rotation = parent.transform.rotation.z; - rotation -= 1; - if (rotation < 0) rotation += 360; - - parent.transform.setRotation(parent.transform.rotation.x, parent.transform.rotation.y, rotation); // 左回転 - } - if (Input.GetKey(GLFW_KEY_RIGHT)) { - float rotation = parent.transform.rotation.z; - rotation += 1; - if (rotation >= 360) rotation -= 360; - parent.transform.setRotation(parent.transform.rotation.x, parent.transform.rotation.y, rotation); // 右回転 + Transform transform = parent.transform; + if (transform != null) { + handlePositionInput(transform); + handleRotationInput(transform); } } + + private void handlePositionInput(Transform transform) { + float x = transform.position.x; + float y = transform.position.y; + float z = transform.position.z; + + if (Input.GetKey(GLFW_KEY_W)) { + transform.setPosition(x, y - moveSpeed * Time.deltaTime, z); + } + if (Input.GetKey(GLFW_KEY_A)) { + transform.setPosition(x - moveSpeed * Time.deltaTime, y, z); + } + if (Input.GetKey(GLFW_KEY_S)) { + transform.setPosition(x, y + moveSpeed * Time.deltaTime, z); + } + if (Input.GetKey(GLFW_KEY_D)) { + transform.setPosition(x + moveSpeed * Time.deltaTime, y, z); + } + if (Input.GetKey(GLFW_KEY_Q)) { + transform.setPosition(x, y, z - moveSpeed * Time.deltaTime); + } + if (Input.GetKey(GLFW_KEY_E)) { + transform.setPosition(x, y, z + moveSpeed * Time.deltaTime); + } + } + + private void handleRotationInput(Transform transform) { + float rotX = transform.rotation.x; + float rotY = transform.rotation.y; + float rotZ = transform.rotation.z; + + if (Input.GetKey(GLFW_KEY_Z)) { + transform.setRotation(wrapAngle(rotX - rotateSpeed * Time.deltaTime), rotY, rotZ); // 左回転 + } + if (Input.GetKey(GLFW_KEY_C)) { + transform.setRotation(wrapAngle(rotX + rotateSpeed * Time.deltaTime), rotY, rotZ); // 右回転 + } + if (Input.GetKey(GLFW_KEY_UP)) { + transform.setRotation(rotX, wrapAngle(rotY - rotateSpeed * Time.deltaTime), rotZ); // 上方向 + } + if (Input.GetKey(GLFW_KEY_DOWN)) { + transform.setRotation(rotX, wrapAngle(rotY + rotateSpeed * Time.deltaTime), rotZ); // 下方向 + } + if (Input.GetKey(GLFW_KEY_LEFT)) { + transform.setRotation(rotX, rotY, wrapAngle(rotZ - rotateSpeed * Time.deltaTime)); // 左方向 + } + if (Input.GetKey(GLFW_KEY_RIGHT)) { + transform.setRotation(rotX, rotY, wrapAngle(rotZ + rotateSpeed * Time.deltaTime)); // 右方向 + } + } + + private float wrapAngle(float angle) { + if (angle < 0) { + angle += 360; + } else if (angle >= 360) { + angle -= 360; + } + return angle; + } + } diff --git a/GameEngine/src/main/java/gameEngine/scenes/GameScene.java b/GameEngine/src/main/java/gameEngine/scenes/GameScene.java index dccf701..fe556bf 100644 --- a/GameEngine/src/main/java/gameEngine/scenes/GameScene.java +++ b/GameEngine/src/main/java/gameEngine/scenes/GameScene.java @@ -1,5 +1,6 @@ package gameEngine.scenes; +import gameEngine.Time; import gameEngine.entites.EditorEntity; import gameEngine.entites.Entity; import gameEngine.entites.GameObject; @@ -15,16 +16,18 @@ public class GameScene extends Scene { public GameScene(){ - System.out.println("Active Game scene"); - glClearColor(1, 1, 1, 0); + this(null); } public GameScene(HashMap editorEntities) { - for(EditorEntity editorEntity : editorEntities.values()){ - if(editorEntity.getEditorComponent(EntityView.class) != null) addNewObject(editorEntity); + if(editorEntities != null){ + for(EditorEntity editorEntity : editorEntities.values()){ + if(editorEntity.getEditorComponent(EntityView.class) != null) addNewObject(editorEntity); + } } System.out.println("Active Game scene"); glClearColor(1, 1, 1, 0); + Time.reset(); } public void addNewObject(EditorEntity editorObject) { diff --git a/GameEngine/src/main/java/gameEngine/views/Window.java b/GameEngine/src/main/java/gameEngine/views/Window.java index e7edb39..afea17e 100644 --- a/GameEngine/src/main/java/gameEngine/views/Window.java +++ b/GameEngine/src/main/java/gameEngine/views/Window.java @@ -132,9 +132,7 @@ float dt = Time.deltaTime; if (dt >= 0) { - resetScene(); - // シーン全体の update を呼び出す currentScene.update(dt); currentScene.processTasks(); @@ -146,17 +144,6 @@ } } - //Gameシーン開始時にタイマーをリセット、Editorシーン戻ったときにフラグをfalseにする - private boolean startGameScene = false; - private void resetScene() { - boolean isGameScene = currentScene instanceof GameScene; - if (isGameScene && !startGameScene) { - Time.reset(); - startGameScene = true; - } - if (!isGameScene) startGameScene = false; - } - public Scene getScene(){ return currentScene; }