Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / scenes / GameScene.java
NoranekoFelician 3 days ago 3 KB ・Colliderコンポーネントの追加
package gameEngine.scenes;

import gameEngine.Time;
import gameEngine.entites.Camera;
import gameEngine.entites.EditorEntity;
import gameEngine.entites.Entity;
import gameEngine.entites.GameObject;
import gameEngine.entites.editorComponents.CameraView;
import gameEngine.entites.editorComponents.ComponentView;
import gameEngine.entites.editorComponents.EntityView;
import gameEngine.entites.gameComponents.*;
import org.joml.Vector3f;

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

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

public class GameScene extends Scene {

    private Camera gameCamera;

    public GameScene(){
        this(null);
    }

    public GameScene(HashMap<String, EditorEntity> editorEntities) {
        gameCamera = new Camera("001", Camera.ProjectionType.PERSPECTIVE);
        setCamera(gameCamera);
        if(editorEntities != null){
            for(EditorEntity editorEntity : editorEntities.values()){
                if(editorEntity.getEditorComponent(EntityView.class) != null) addNewObject(editorEntity);
                if(editorEntity.getEditorComponent(CameraView.class) != null) gameCamera.transform.setPosition(editorEntity.transform.position);
            }
        }
        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){
                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;
                    }
                }
                object.addComponent(new Collider(object, false)); //コライダー(仮)
            }
            if(connectionType == EditorEntity.Connectiontype.MoveImage){
                object.addComponent(new MoveImage(object));
            }
            if(connectionType == EditorEntity.Connectiontype.MoveStraight){
                object.addComponent(new Move(object));
            }
        }
    }

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

    //カメラの反映を行う(座標調整等)
    public void reflectionCamera(){
        Vector3f cameraPosition = gameCamera.getPosition();
        for (Entity entity : entities.values()) {
            entity.screenTransform.setPosition(
                    entity.transform.position.x - cameraPosition.x,
                    entity.transform.position.y - cameraPosition.y,
                    entity.transform.position.z
            );
        }
    }
}