package mygame;

import com.jme3.bounding.BoundingVolume;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;

/**
 *
 * @author Snowsun
 */
public class WeaponProjectileCollisionControl extends AbstractControl {

    private Node starNode;
    private BoundingVolume boundingVolume;
    private CollisionResults results;

    public WeaponProjectileCollisionControl(Node starNode) {
        this.starNode = starNode;
        results = new CollisionResults();
    }

    @Override
    public void setSpatial(Spatial spatial) {
        super.setSpatial(spatial);
        boundingVolume = spatial.getWorldBound();
    }

    @Override
    protected void controlUpdate(float tpf) {
        for (Spatial s : starNode.getChildren()) {
            results.clear();
            s.collideWith(boundingVolume, results);
            if (results.size() > 0) {
                s.getControl(HitStarControl.class).setIsHited(true);
                s.getControl(StarPointControl.class).setIsHited(true);
            }
        }
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }

    public Control cloneForSpatial(Spatial spatial) {
        WeaponProjectileCollisionControl control = new WeaponProjectileCollisionControl(starNode);
        spatial.addControl(control);
        return control;
    }
}
