/*
 * Animation.java
 *
 * Created on 9 janvier 2005, 23:13
 */

package hyperion;
import java.applet.*;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Color;
/**
 *
 * @author CryoGen
 */
public class Animation {
    
    private Image image;
    
    private int height, width;
    private int nbr_anim_x, nbr_anim_y;
    private int taille_anim;
    
    private int offset_x, offset_y;
    private float speed;
    private float indexf;
    
    private Applet applet;
    
    private boolean rendered;
    
    /** Creates a new instance of Animation */
    public Animation(Image img, int taille, int width, int height, Applet applet) {
        this.image = img;
        this.taille_anim = taille;
        this.applet = applet;
        
        this.height = height;
        this.width  = width;
        
        this.nbr_anim_x = this.width / taille;
        this.nbr_anim_y = this.height / taille;
        
        this.indexf = 0;
        this.speed = 0;
        this.offset_y = 0;
        this.rendered = false;
    }
    
    public void setSpeed(float speed) { this.speed = speed; };
    public void setImage(Image img) { this.image = img; }
    
    public boolean rendered()
    {
        return rendered;
    }
    
    public void draw(Graphics g, int x, int y)
    {
        this.indexf = (this.indexf + this.speed);
        if((int)indexf >= this.nbr_anim_x)
        {
            indexf = 0;
            this.offset_y = (this.offset_y+1) % this.nbr_anim_y;
            if(this.offset_y == 0) this.rendered = true;
        }
        int dx1 = x;
        int dy1 = y;
        int dx2 = dx1 + this.taille_anim;
        int dy2 = dy1 + this.taille_anim;
        
        int sx1 = ((int)this.indexf) * this.taille_anim;
        int sy1 = this.offset_y * this.taille_anim;
        int sx2 = sx1 + this.taille_anim;
        int sy2 = sy1 + this.taille_anim;
        
        
        g.drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, this.applet);
    }
    
}
