/***********************************************************************

GECCO, a platform for command and control gaming

Copyright (C) 2001 Henrik Bäärnhielm, Andreas Enblom, Niklas
Hallenfur, Karl Hasselström, Henrik Hägerström, Oskar Linde, Jing Fu
Zi and Jon Åslund

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.

***********************************************************************/

package gecco.client.animation;
import gecco.client.*;

import java.awt.*;

/**
 *  This is a piece with animated symbol. The piece should be instanciated with
 *  a number of images (known as frames) that will be painted in order. The
 *  piece should be given a non-null Animator that handles the animation. The
 *  delay (in milliseconds) between the frames should also be set. An animated
 *  piece can either stop after the last frame has been drawn, or repeat forever
 *  (at least until the stopAnimation method is called). After the piece has
 *  been instanciated startAnimation should be called.
 *
 * @author     Andreas Enblom
 * @created    June 27, 2001
 */
public class AnimatedPiece extends Piece {
    /**
     *  Description of the Field
     */
    Image[] frames;
    /**
     *  Description of the Field
     */
    int delayBetweenFrames;
    /**
     *  Description of the Field
     */
    boolean repeat;
    /**
     *  Description of the Field
     */
    Animator animator;
    /**
     *  Description of the Field
     */
    int currentFrame;
    /**
     *  Description of the Field
     */
    Thread animationThread;


    /**
     * @param  handle              The piece handle.
     * @param  position            The piece postion.
     * @param  name                The piece name.
     * @param  type                The piece type. Note that this does not
     *      affect the symbols used.
     * @param  frames              The symbol used in order to animate the
     *      piece.
     * @param  delayBetweenFrames  Delay between animation frames (in ms).
     * @param  repeat              Should the piece animate over and over again?
     * @param  animator            The animator that animates this piece.
     */
    public AnimatedPiece(int handle, Position position,
            String name, String type,
            Image[] frames, int delayBetweenFrames,
            boolean repeat, Animator animator) {
        super(handle, position, name, type);
        this.frames = frames;
        this.delayBetweenFrames = delayBetweenFrames;
        this.repeat = repeat;
        this.animator = animator;
        animationThread =
            new Thread() {
                /**
                 *  Main processing method for the AnimatedPiece object
                 */
                public void run() {
                    while (AnimatedPiece.this.currentFrame
                             < AnimatedPiece.this.frames.length) {
                        AnimatedPiece.this.animator.
                                repaintAnimation(AnimatedPiece.this);
                        try {
                            sleep(AnimatedPiece.this.delayBetweenFrames);
                        } catch (Exception ex) {
                        }
                        ++AnimatedPiece.this.currentFrame;
                        if (AnimatedPiece.this.currentFrame
                                 >= AnimatedPiece.this.frames.length) {
                            if (AnimatedPiece.this.repeat) {
                                currentFrame = 0;
                            } else {
                                stopAnimation();
                            }
                        }
                    }
                }
            };
    }


    /**
     *  Gets the Symbol attribute of the AnimatedPiece object
     *
     * @return    The Symbol value
     */
    public Image getSymbol() {
        if (currentFrame >= frames.length) {
            return frames[frames.length - 1];
        } else {
            return frames[currentFrame];
        }
    }


    /**
     *  Description of the Method
     */
    public void startAnimation() {
        currentFrame = 0;
        animationThread.start();
    }


    /**
     *  Stops the animation and notifies the animator that the animation is
     *  done.
     */
    public void stopAnimation() {
        repeat = false;
        currentFrame = frames.length;
        animator.animationDone(this);
    }
}
