Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / scenes / Scene.java
NoranekoFelician 10 days ago 3 KB ColorControllerを削除
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.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 Color backColor = 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 addComponentToGameObject(GameObject gameObject, GameComponent component) {
        enqueueTask(() -> gameObject.addComponent(component));
    }

    public void removeComponentFromGameObject(GameObject gameObject, GameComponent component) {
        enqueueTask(() -> gameObject.removeComponent(component));
    }

    synchronized void enqueueTask(Runnable task) {
        taskQueue.add(task);
    }

    public synchronized void processTasks() {
        while (!taskQueue.isEmpty()) {
            taskQueue.poll().run();
        }
    }

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


            if (original.getComponent(Mesh.class) != null) {
                newGameObject.addComponent(new Mesh((Mesh)original.getComponent(Mesh.class).copy(), newGameObject));
            }
            if (original.getComponent(TextMesh.class) != null) {
                newGameObject.addComponent(new TextMesh((TextMesh)original.getComponent(TextMesh.class).copy(), newGameObject));
            }
            if (original.getComponent(CopyEntity.class) != null) {
                newGameObject.addComponent(new CopyEntity(newGameObject));
            }
            if (original.getComponent(MoveImage.class) != null) {
                newGameObject.addComponent(new MoveImage(newGameObject));
            }
            if (original.getComponent(Physics.class) != null) {
                newGameObject.addComponent(new Physics(newGameObject));
            }


            addEntity(newId, newGameObject);
        });
    }

    public void changeSceneStart(){
        if(!changingScene) changingScene = true;
    }

    public void changeScene(int scene, float dt){
        if(!changingScene && Input.GetKeyDown(KeyEvent.VK_SPACE)){
            changingScene = true;
        }

        if(changingScene) {
            if (timeToChangeScene > 0) {
                timeToChangeScene -= dt * 1.5f;
                backColor.r -= dt * 5.0f;
                backColor.g -= dt * 5.0f;
                backColor.b -= dt * 5.0f;
            } else {
                changingScene = false;
                timeToChangeScene = 2.0f;
                backColor = new Color(1,1,1,0);
                Window.changeScene(scene);
            }
            glClearColor(backColor.r, backColor.g, backColor.b, backColor.a);
        }
    }
}