Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / scenes / EditorScene.java
package gameEngine.scenes;

import gameEngine.entites.Entity;
import gameEngine.entites.GameObject;
import gameEngine.entites.gameComponents.ComponentView;
import gameEngine.entites.gameComponents.Draggable;
import gameEngine.entites.gameComponents.EntityView;
import gameEngine.entites.gameComponents.TextMesh;
import gameEngine.views.Window;
import org.joml.Vector3f;

import java.util.HashMap;

import static org.lwjgl.opengl.GL11.glClearColor;

public class EditorScene extends Scene {

    public HashMap<String, Entity> editorEntities = new HashMap<>();
    private Entity selectedEntity = null;

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

    public EditorScene(){
        System.out.println("Active Editor scene");
        glClearColor(1, 1, 1, 0);
    }
    public EditorScene(HashMap<String, Entity> editorEntities){
        this.editorEntities = editorEntities;
        System.out.println("Active Editor scene");
        glClearColor(1, 1, 1, 0);
    }

    @Override
    public void update(float dt) {
        updateDraggable();
        changeScene(1, dt);    //Gameシーンへの以降処理
    }

    private GameObject createGameObject(){
        int entitiesLength = editorEntities.size();
        String newId = Integer.toString(entitiesLength);
        GameObject gameObject = new GameObject(newId);
        addEditorEntity(newId, gameObject);
        return gameObject;
    }

    public Runnable addNewEntity() {
        enqueueTask(this::addEntityView);
        return null;
    }

    private void addEntityView(){
        GameObject object = createGameObject();
        object.transform.setPosition((float) Window.get().width / 2 ,(float) Window.get().height /2, 0);
        object.addComponent(new EntityView(object));
        object.addComponent(new TextMesh(object, "Entity", 16));
        object.getComponent(TextMesh.class).setLocalPosition(new Vector3f(0,-20,0));
    }

    public Runnable addNewMeshComponent() {
        enqueueTask(this::addMeshComponentView);
        return null;
    }

    private void addMeshComponentView(){
        GameObject object = createGameObject();
        object.transform.setPosition((float) Window.get().width / 2 ,(float) Window.get().height /2, 0);
        object.addComponent(new ComponentView(object, GameObject.Connectiontype.Mesh));
        object.addComponent(new TextMesh(object, "Mesh", 20));
        object.getComponent(TextMesh.class).setLocalPosition(new Vector3f(10,2,0));
    }

    public Runnable addNewMoveImageComponent() {
        enqueueTask(this::addMoveImageComponentView);
        return null;
    }

    private void addMoveImageComponentView(){
        GameObject object = createGameObject();
        object.transform.setPosition((float) Window.get().width / 2 ,(float) Window.get().height /2, 0);
        object.addComponent(new ComponentView(object, GameObject.Connectiontype.MoveImage));
        object.addComponent(new TextMesh(object, "MoveImage", 20));
        object.getComponent(TextMesh.class).setLocalPosition(new Vector3f(10,2,0));
    }

    ///----------------------------------------------------------------
    /// 選択、ドラッグ処理
    ///----------------------------------------------------------------

    public Entity getSelectedEntity() {
        return selectedEntity;
    }

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

    public void clearSelectedEntity() {
        selectedEntity = null;
    }

    public void updateDraggable() {
        if (selectedEntity != null) {
            GameObject gameObject = (GameObject) selectedEntity;
            if (gameObject == null) return;
            if (gameObject.getComponent(Draggable.class) != null) {
                gameObject.getComponent(Draggable.class).handleDragging();
            }
        } else {
            for (Entity entity : editorEntities.values()) {
                if (entity instanceof GameObject) {
                    GameObject gameObject = (GameObject) entity;
                    if (gameObject.getComponent(Draggable.class) != null) {
                        gameObject.getComponent(Draggable.class).handleDragging();
                    }
                }
            }
        }
    }
}