package electrosphere.entity.types.particle;

import electrosphere.entity.CameraEntityUtils;
import electrosphere.entity.Entity;
import electrosphere.entity.EntityDataStrings;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.state.ParticleTree;
import electrosphere.main.Globals;
import org.joml.AxisAngle4f;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.joml.Vector3d;
import org.joml.Vector3f;

/**
 *
 * @author amaterasu
 */
public class ParticleUtils {
    
    
    
    public static Entity spawnBillboardParticle(String texture, int maxLife, Vector3f destination, float velocity, float acceleration){
        Entity rVal = EntityUtils.spawnDrawableEntity(Globals.particleBillboardModel);
        EntityUtils.getActor(rVal).setTextureOverride(texture);
        Globals.assetManager.addTexturePathtoQueue(texture);
        ParticleTree particleTree = new ParticleTree(rVal, maxLife, destination, velocity, acceleration);
        rVal.putData(EntityDataStrings.PARTICLE_TREE, particleTree);
        rVal.putData(EntityDataStrings.IS_PARTICLE, true);
        Globals.entityManager.registerParticle(rVal);
        return rVal;
    }
    
    public static void makeParticleBillboardFaceCamera(Entity particle){
        Vector3f cameraCenter = CameraEntityUtils.getCameraCenter(Globals.playerCamera);
        Vector3f cameraEye = new Vector3f(CameraEntityUtils.getCameraEye(Globals.playerCamera)).add(cameraCenter).mul(-1.0f);
        Vector3d particlePosition = EntityUtils.getPosition(particle);
//        Vector3f cameraEye = new Vector3f(CameraEntityUtils.getCameraEye(Globals.playerCamera)).mul(1.0f,-1.0f,-1.0f);
//        Vector3f directionVector = new Vector3f(cameraCenter).sub(cameraEye);
        Matrix4f rotationMatrix = new Matrix4f(Globals.viewMatrix).invert();
        Quaternionf rotation = new Quaternionf(rotationMatrix.getRotation(new AxisAngle4f()));
        EntityUtils.getRotation(particle).set(rotation);
    }
    
    public static ParticleTree getParticleTree(Entity particle){
        return (ParticleTree)particle.getData(EntityDataStrings.PARTICLE_TREE);
    }
}
