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

import gameEngine.Time;
import gameEngine.entites.EditorEntity;
import gameEngine.entites.Entity;
import gameEngine.entites.GameObject;
import gameEngine.entites.editorComponents.ComponentView;
import gameEngine.entites.editorComponents.EntityView;
import gameEngine.entites.gameComponents.*;

import java.util.HashMap;
import java.util.Objects;

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

public class GameScene extends Scene {

    public GameScene(){
        this(null);
    }

    public GameScene(HashMap<String, EditorEntity> editorEntities) {
        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) {
        enqueueTask(() -> addNewGameObject(editorObject));
    }


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

    private void addNewGameObject(EditorEntity editorEntity) {
        GameObject object = createGameObject();
        object.transform.setPosition(editorEntity.transform.position.x, editorEntity.transform.position.y,0);

        for(EditorEntity.Connectiontype connectionType: editorEntity.ComponentConnections) {
            if(connectionType == EditorEntity.Connectiontype.Mesh){
                //接続しているComponentView
                for(ComponentView componentView: editorEntity.getEditorComponent(EntityView.class).getConnectedComponentViews()){
                    if(componentView.meshSprite != null) {
                        if(Objects.equals(componentView.getSpritePath(), null)) continue;
                        object.addComponent(new Mesh(object, Mesh.MeshType.SPRITE, componentView.getSpritePath()));
                        break;
                    }
                }
            }
            if(connectionType == EditorEntity.Connectiontype.MoveImage){
                object.addComponent(new MoveImage(object));
            }
        }
    }

    @Override
    public void update(float dt) {
        changeScene(0, dt);    //Editorシーンへの以降処理
        for (Entity entity : entities.values()) {
            if (entity instanceof GameObject) {
                GameObject gameObject = (GameObject) entity;
                if(gameObject.getComponent(TextMesh.class) != null) continue;
                gameObject.updateComponents();
            }
        }
    }
}