package at.oefai.aaa.animation;

import org.w3c.dom.svg.SVGPoint;
import org.w3c.dom.svg.SVGSVGElement;

/**
 * SVG animation for moving an actor directly from named position to another one.
 * @author Stefan Rank
 */
public class MoveToPosition extends BaseSVGAnim {
    private static final int DEFAULT_ROTATION_WIDTH = 5;
    private volatile boolean done = false;
    private SVGPoint dest = null;
    private int rotation = DEFAULT_ROTATION_WIDTH;

    public BaseAnim newInstance() { return new MoveToPosition(); }

    protected void doFirstTime() {
        assert (this.args.size() == 2) : "wrong no of arguments for MoveToPosition";

        this.dest = getNamedPointRandDisplace(this.args.get(1).getString());
        if (this.dest == null) {
            this.done = true;
        } else {
            displacePointByNamed(this.dest, this.actor + "Displace");
        }
    }


    public boolean isFinished() {
        return this.done;
    }

    protected void doIt() {
        SVGSVGElement eleActor = getNamedElement(this.actor);
        SVGPoint curr = getElementsPos(eleActor);
        if (curr == null) {
            this.done = true;
        } else {
            SVGPoint nextPos = getNextAgentPos(curr, this.dest);
            if (nextPos == this.dest) {
                this.done = true;
                setElementsRotate(eleActor, 0);
            } else {
                this.rotation = -this.rotation;
                setElementsRotate(eleActor, this.rotation);
            }
            setElementsPos(eleActor, nextPos);
        }
    }
}
