Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / scenes / EditorScene.java
NoranekoFelician 7 days ago 7 KB ・modelファイル出力を更新
package gameEngine.scenes;

import gameEngine.ConnectionManager;
import gameEngine.GameEditor;
import gameEngine.ResourceManager;
import gameEngine.Time;
import gameEngine.entites.Camera;
import gameEngine.entites.EditorEntity;
import gameEngine.entites.Entity;
import gameEngine.entites.editorComponents.CameraView;
import gameEngine.entites.editorComponents.ComponentView;
import gameEngine.entites.editorComponents.DraggableComponent;
import gameEngine.entites.editorComponents.EntityView;
import gameEngine.geometry.Transform;
import gameEngine.input.Input;
import gameEngine.views.Window;
import org.joml.Vector3f;

import java.util.HashMap;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.glClearColor;

public class EditorScene extends Scene {

    private static GameEditor gameEditor;
    public HashMap<String, EditorEntity> editorEntities = new HashMap<>();
    private EditorEntity selectedEntity = null;
    public ConnectionManager connectionManager = new ConnectionManager();
    public ResourceManager resourceManager = new ResourceManager();
    private EditorEntity clickedEntity = null;
    private Camera editorCamera;
    private Vector3f centerPosition = new Vector3f((float) Window.get().width / 2, (float) Window.get().height / 2, 0);

    public EditorScene(float windowWidth, float windowHeight){
        System.out.println("Active Editor scene");
        gameEditor = new GameEditor(this, windowWidth, windowHeight);
        gameEditor.setScene(this);
        this.editorCamera = new Camera("001", Camera.ProjectionType.PERSPECTIVE);
        this.setCamera(editorCamera);
        glClearColor(1, 1, 1, 0);
        resourceManager.addPath(Window.resourcePath + "empty.png");
        resourceManager.addPath(Window.resourcePath + "enemy1.png");
        resourceManager.addPath(Window.resourcePath + "enemy2.png");
        System.out.println(resourceManager.getPathList());
        addCamera();
    }

    @Override
    public void update(float dt) {
        handleEditorCameraMovement();

        updateDraggable();
        changeScene(1, dt);    //Gameシーンへの以降処理

        connectionManager.update();
        for (EditorEntity editorEntity : editorEntities.values()) {
            editorEntity.updateComponents();
        }
        gameEditor.update();
    }

    public Entity getEditorEntity(String eid) {
        return editorEntities.get(eid);
    }
    public void addEditorEntity(String eid, EditorEntity entity) {
        editorEntities.put(eid, entity);
    }
    public void removeEditorEntity(String eid) {
        editorEntities.remove(eid);
    }

    private EditorEntity createEditorEntity(){
        int entitiesLength = editorEntities.size();
        String newId = Integer.toString(entitiesLength);
        EditorEntity editorEntity = new EditorEntity(newId);
        addEditorEntity(newId, editorEntity);
        return editorEntity;
    }

    public void addCamera() {
        enqueueTask(this::addCameraView);
    }

    private void addCameraView(){
        EditorEntity object = createEditorEntity();
        object.transform.setPosition(0 ,0, 0);
        object.addEditorComponent(new CameraView(object));
    }

    private EditorEntity createObject(){
        EditorEntity object = createEditorEntity();
        object.transform.setPosition(new Vector3f(centerPosition).add(
                new Vector3f(editorCamera.getPosition().x,editorCamera.getPosition().y,editorCamera.getPosition().z)));
        return object;
    }

    public void addNewEntity() {
        enqueueTask(this::addEntityView);
    }

    private void addEntityView(){
        EditorEntity object = createObject();
        object.addEditorComponent(new EntityView(object));
    }

    public void addNewMeshComponent() {
        enqueueTask(this::addMeshComponentView);
    }

    private void addMeshComponentView(){
        EditorEntity object = createObject();
        object.addEditorComponent(new ComponentView(object, EditorEntity.Connectiontype.Mesh, resourceManager,"Mesh"));
    }

    public void addNewMoveImageComponent() {
        enqueueTask(this::addMoveImageComponentView);
    }

    private void addMoveImageComponentView(){
        EditorEntity object = createObject();
        object.addEditorComponent(new ComponentView(object, EditorEntity.Connectiontype.MoveImage, "MoveImage"));
    }

    public void addMoveStraightComponent() {
        enqueueTask(this::addMoveStraightComponentView);
    }

    private void addMoveStraightComponentView(){
        EditorEntity object = createObject();
        object.addEditorComponent(new ComponentView(object, EditorEntity.Connectiontype.Move, "MoveStraight"));
    }

    ///----------------------------------------------------------------
    /// Editorのカメラ
    ///----------------------------------------------------------------

    private void handleEditorCameraMovement() {
        Vector3f velocity = new Vector3f(0, 0, 0);
        float speed = 200.0f;

        if (Input.GetKey(GLFW_KEY_UP)) {
            velocity.y -= speed * Time.deltaTime;
        }
        if (Input.GetKey(GLFW_KEY_DOWN)) {
            velocity.y += speed * Time.deltaTime;
        }
        if (Input.GetKey(GLFW_KEY_LEFT)) {
            velocity.x -= speed * Time.deltaTime;
        }
        if (Input.GetKey(GLFW_KEY_RIGHT)) {
            velocity.x += speed * Time.deltaTime;
        }

        // カメラを移動
        editorCamera.move(velocity.x, velocity.y, 0);

        // 各オブジェクトのscreenTransformをカメラ位置に応じて補正
        Vector3f cameraPosition = editorCamera.getPosition();
        for (EditorEntity editorEntity : editorEntities.values()) {
            editorEntity.screenTransform.setPosition(
                    editorEntity.transform.position.x,
                    editorEntity.transform.position.y,
                    editorEntity.transform.position.z
            );
        }
    }
    ///----------------------------------------------------------------
    /// 選択、ドラッグ処理
    ///----------------------------------------------------------------

    public EditorEntity getSelectedEntity() {
        return selectedEntity;
    }

    public EditorEntity getClickedEntity(){
        return clickedEntity;
    }

    public void setSelectedEntity(EditorEntity editorEntity) {
        selectedEntity = editorEntity;
        System.out.println("Selected Entity: " + selectedEntity);
    }

    public void setClickedEntity(EditorEntity editorEntity){
        clickedEntity = editorEntity;

        if (editorEntity != null) {
            Transform transform = editorEntity.transform;

            // 初期値をInspectorに設定
            gameEditor.updateInspectorField(gameEditor.inspectorInputFields[0], transform.position); // Position
            gameEditor.updateInspectorField(gameEditor.inspectorInputFields[1], transform.rotation); // Rotation
            gameEditor.updateInspectorField(gameEditor.inspectorInputFields[2], transform.scale);    // Scale
        }
    }

    public void clearSelectedObject() {
        selectedEntity = null;
    }

    public void updateDraggable() {
        if (selectedEntity != null) {
            if (selectedEntity.getEditorComponent(DraggableComponent.class) != null) {
                selectedEntity.getEditorComponent(DraggableComponent.class).handleDragging();
            }
        } else {
            for (EditorEntity entity : editorEntities.values()) {
                    if (entity.getEditorComponent(DraggableComponent.class) != null) {
                        entity.getEditorComponent(DraggableComponent.class).handleDragging();
                }
            }
        }
    }
}