Newer
Older
AlgebraicDataflowArchitectureModel / GameEngine / src / main / java / gameEngine / entites / GameObject.java
package gameEngine.entites;

import gameEngine.entites.gameComponents.GameComponent;
import gameEngine.entites.gameComponents.Mesh;
import gameEngine.scenes.GameScene;
import gameEngine.views.Window;

import java.util.ArrayList;
import java.util.List;

public class GameObject extends Entity {

    Window window = Window.get();
    public final List<GameComponent> gameComponents = new ArrayList<>();

    public GameObject(String id) {
        super(id);
    }

    public void addComponent(GameComponent component) {
        for (GameComponent existingComponent : gameComponents) {
            if (existingComponent.getClass().equals(component.getClass())) {
                System.out.println("Component already exists: " + component.getClass().getSimpleName());
                return;
            }
        }
        this.gameComponents.add(component);
        if(window.getScene() instanceof GameScene) component.setGameSceneComponent(true);
        System.out.println("Component added: " + component.getClass().getSimpleName());
    }

    public void removeComponent(GameComponent component) {
        boolean removed = this.gameComponents.remove(component);
        if (removed) {
            System.out.println("Component removed: " + component.getClass().getSimpleName());
        } else {
            System.out.println("Component not found: " + component.getClass().getSimpleName());
        }
    }

    public void initComponents() {
        for (GameComponent component : gameComponents) {
            component.init();
        }
    }

    public void updateComponents(boolean isEditorScene) {
        for (GameComponent component : gameComponents) {
            if (isEditorScene) {
                if (component instanceof Mesh) {
                    component.update();
                }
            } else {
                component.update();
            }
        }
    }

    public <T extends GameComponent> T getComponent(Class<T> componentClass){
        for (GameComponent gameComponent :gameComponents) {
            if (componentClass.isInstance(gameComponent)) {
                return componentClass.cast(gameComponent);
            }
        }
        return null;
    }

}