package de.frajul.particlelab.rendering;

import de.frajul.particlelab.data.Vector;
import de.frajul.particlelab.entities.particles.ParticleSystem;
import de.frajul.particlelab.entities.textures.TexturePack;
import de.frajul.particlelab.main.MainActivity;
import de.frajul.particlelab.main.Scene;

/**
 * Created by Julian on 26.11.2015.
 */
public class Rendering {

    private MainActivity mainActivity;
    private ParticleSystem particleSystem;
    private Timer timer;
    private Scene scene;
    private boolean alreadyInitiated = false;

    public Rendering(MainActivity mainActivity, ParticleSystem particleSystem) {
        this.mainActivity = mainActivity;
        this.particleSystem = particleSystem;
    }

    public void initiate(TexturePack texturePack, Timer timer) {
        this.timer = timer;
        if(!alreadyInitiated)
            scene = new Scene(mainActivity, texturePack, particleSystem);
        else {
            scene.setTexturePack(texturePack);
        }
        try {
            particleSystem.loadTextures();
        }catch (Exception e){
            onException(e);
        }
        alreadyInitiated = true;
    }

    public void update() {
        particleSystem.update(timer);
        scene.update(timer);
    }

    public void setScreenSize(int width, int height) {
        if (scene != null)
            scene.setScreenSize(new Vector(width, height));
    }

    public void onException(Exception e) {
        mainActivity.onException(e);
    }

    public Scene getScene() {
        return scene;
    }
}
