Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / scenes / EditorScene.java
NoranekoFelician 12 days ago 6 KB ・DraggableをDraggableComponentに変更
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.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;

    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());
    }

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

        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 addNewEntity() {
        enqueueTask(this::addEntityView);
    }

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

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

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

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

    private void addMoveImageComponentView(){
        EditorEntity object = createEditorEntity();
        object.transform.setPosition(0 ,0, 0);
        object.addEditorComponent(new ComponentView(object, EditorEntity.Connectiontype.MoveImage, "MoveImage"));
    }

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

    private void handleCameraMovement() {
        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 - cameraPosition.x,
                    editorEntity.transform.position.y - cameraPosition.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();
                }
            }
        }
    }
}