Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / entites / gameComponents / MoveImage.java
package gameEngine.entites.gameComponents;
import gameEngine.Time;
import gameEngine.entites.GameObject;
import gameEngine.geometry.Transform;
import gameEngine.input.*;

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;
    }
    @Override
    public GameComponent copy() {
        return this;
    }

    public void update() {
        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;
    }

}