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

import gameEngine.entites.Camera;
import gameEngine.entites.Entity;
import gameEngine.entites.GameObject;
import gameEngine.entites.gameComponents.*;
import gameEngine.geometry.Transform;
import gameEngine.input.Input;
import gameEngine.views.Color;
import gameEngine.views.Window;

import java.awt.event.KeyEvent;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;

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

public abstract class Scene {

    private Camera camera;
    public HashMap<String, Entity> entities = new HashMap<>();
    private final Queue<Runnable> taskQueue = new LinkedList<>();

    private boolean changingScene = false;
    private float timeToChangeScene = 2.0f;
    private final Color editorBackColor = new Color(1,1,1,0);

    public Scene(){
    }

    public abstract void update(float dt);

    public Camera getCamera() {
        return camera;
    }
    public void setCamera(Camera camera) {
        this.camera = camera;
    }

    public Entity getEntity(String eid) {
        return entities.get(eid);
    }
    public void addEntity(String eid, Entity entity) {
        entities.put(eid, entity);
    }
    public void removeEntity(String eid) {
        entities.remove(eid);
    }

    public void addNewObject() {
        enqueueTask(() -> {
            int entitiesLength = entities.size();
            String newId = Integer.toString(entitiesLength);
            GameObject newGameObject = new GameObject(newId);
            addEntity(newId, newGameObject);
            //newGameObject.addComponent(new Mesh(newGameObject, Mesh.MeshType.SPRITE, "GameEngine/resources/test.png"));
            newGameObject.addComponent(new TextMesh(newGameObject, "Hello World"));
            newGameObject.setName("NewEntity" + newId);
        });
    }

    // コンポーネントの追加メソッド
    public void addComponentToGameObject(GameObject gameObject, GameComponent component) {
        enqueueTask(() -> gameObject.addComponent(component));
    }

    // コンポーネントの削除メソッド
    public void removeComponentFromGameObject(GameObject gameObject, GameComponent component) {
        enqueueTask(() -> gameObject.removeComponent(component));
    }

    // タスクを登録
    private synchronized void enqueueTask(Runnable task) {
        taskQueue.add(task);
    }

    // タスクの実行
    public synchronized void processTasks() {
        while (!taskQueue.isEmpty()) {
            taskQueue.poll().run();
        }
    }

    //現状一つのEntityからしか呼び出せない
    public void Instantiate(GameObject original)
    {
        enqueueTask(() -> {
            String newId = Integer.toString(entities.size());
            GameObject newGameObject = new GameObject(newId);

            newGameObject.setName(original.getName() + "_copy");
            newGameObject.transform = new Transform(original.transform);

            for (GameComponent gameComponent : original.gameComponents) {
                if (gameComponent instanceof Mesh) {
                    newGameObject.addComponent(new Mesh((Mesh)gameComponent.copy(), newGameObject));
                }
                if (gameComponent instanceof ColorController) {
                    newGameObject.addComponent(new ColorController());
                }
                if (gameComponent instanceof CopyEntity) {
                    newGameObject.addComponent(new CopyEntity(newGameObject));
                }
                if (gameComponent instanceof MoveImage) {
                    newGameObject.addComponent(new MoveImage(newGameObject));
                }
                if (gameComponent instanceof Physics) {
                    newGameObject.addComponent(new Physics(newGameObject));
                }
            }

            addEntity(newId, newGameObject);
        });
    }


    //シーン切り替え時の演出
    void changeScene(int scene, float dt){
        if(!changingScene && Input.GetKeyDown(KeyEvent.VK_SPACE)){
            changingScene = true;
        }

        if(changingScene && timeToChangeScene > 0){
            timeToChangeScene -= dt * 1.5f;
            editorBackColor.r -= dt * 5.0f;
            editorBackColor.g  -= dt * 5.0f;
            editorBackColor.b  -= dt * 5.0f;
            glClearColor(editorBackColor.r, editorBackColor.g, editorBackColor.b, editorBackColor.a);
        }
        else if(changingScene){
            Window.changeScene(scene);
        }
    }
}