package electrosphere.entity.state;

import electrosphere.entity.Entity;
import electrosphere.entity.EntityUtils;
import electrosphere.entity.btree.BehaviorTree;

import org.joml.Vector3d;
import org.joml.Vector3f;

/**
 * Particle b tree
 */
public class ParticleTree implements BehaviorTree {
    Entity parent;
    boolean hasLife = true;
    int maxLife;
    int lifeCurrent;
    Vector3f destination;
    float velocity;
    float acceleration;
    
    public ParticleTree(Entity parent, int maxLife, Vector3f destination, float velocity, float acceleration, boolean hasLife){
        this.parent = parent;
        this.maxLife = maxLife;
        this.destination = destination;
        this.velocity = velocity;
        this.acceleration = acceleration;
        this.hasLife = hasLife;
        lifeCurrent = maxLife;
    }

    public int getMaxLife() {
        return maxLife;
    }

    public int getLifeCurrent() {
        return lifeCurrent;
    }

    public Vector3f getDestination() {
        return destination;
    }

    public float getVelocity() {
        return velocity;
    }

    public float getAcceleration() {
        return acceleration;
    }
    
    public void simulate(float deltaTime){
        Vector3d parentPosition = EntityUtils.getPosition(parent);
        parentPosition.add(new Vector3f(destination).mul(velocity));
        velocity = velocity - acceleration;
        if(velocity < 0){
            velocity = 0;
            acceleration = 0;
        }
        if(hasLife){
            lifeCurrent--;
            if(lifeCurrent <= 0){
                EntityUtils.cleanUpEntity(parent);
            }
        }
    }
    
}
