// Sprite Class
// ToggleSprite.java
// Only two images for animated

// Imports
import java.awt.*;
import java.util.BitSet;

public class ToggleSprite extends Sprite 
{

	public ToggleSprite(Component comp, Image img, Point pos, Point vel, int z,
		int ba) 
	{
		super( comp,  img,  pos,  vel,  z,  ba);
	}

	public ToggleSprite(Component comp, Image[] img, int f, int fi, int fd,
    	Point pos, Point vel, int z, int ba) 
    {
		super( comp,  img,  f,  fi,  fd, pos,  vel,  z,  ba);
	}

	protected void toggleFrame() 
	{
		if ((frameDelay > 0) && (--frameTrigger <= 0)) 
		{
      		// Reset the frame trigger
			frameTrigger = frameDelay;

			// Increment the frame
			frame  = 1 - frame;
		}
  }

  public BitSet update() 
  {
    BitSet action = new BitSet();

    // Increment the frame
    toggleFrame();

    // Update the position
    Point pos = new Point(position.x, position.y);
    pos.translate(velocity.x, velocity.y);

    // Check the bounds
    // Wrap?
    if (boundsAction == Sprite.BA_WRAP) {
      if ((pos.x + position.width) < bounds.x)
        pos.x = bounds.x + bounds.width;
      else if (pos.x > (bounds.x + bounds.width))
        pos.x = bounds.x - position.width;
      if ((pos.y + position.height) < bounds.y)
        pos.y = bounds.y + bounds.height;
      else if (pos.y > (bounds.y + bounds.height))
        pos.y = bounds.y - position.height;
    }
    // Bounce?
    else if (boundsAction == Sprite.BA_BOUNCE) {
      boolean bounce = false;
      Point   vel = new Point(velocity.x, velocity.y);
      if (pos.x < bounds.x) {
        bounce = true;
        pos.x = bounds.x;
        vel.x = -vel.x;
      }
      else if ((pos.x + position.width) >
        (bounds.x + bounds.width)) {
        bounce = true;
        pos.x = bounds.x + bounds.width - position.width;
        vel.x = -vel.x;
      }
      if (pos.y < bounds.y) {
        bounce = true;
        pos.y = bounds.y;
        vel.y = -vel.y;
      }
      else if ((pos.y + position.height) >
        (bounds.y + bounds.height)) {
        bounce = true;
        pos.y = bounds.y + bounds.height - position.height;
        vel.y = -vel.y;
      }
      if (bounce)
        setVelocity(vel);
    }
    // Die?
    else if (boundsAction == Sprite.BA_DIE) {
      if ((pos.x + position.width) < bounds.x ||
        pos.x > bounds.width ||
        (pos.y + position.height) < bounds.y ||
        pos.y > bounds.height) {
        action.set(Sprite.SA_KILL);
        return action;
      }
    }
    // Stop (default)
    else {
      if (pos.x  < bounds.x ||
        pos.x > (bounds.x + bounds.width - position.width)) {
        pos.x = Math.max(bounds.x, Math.min(pos.x,
          bounds.x + bounds.width - position.width));
        setVelocity(new Point(0, 0));
      }
      if (pos.y  < bounds.y ||
        pos.y > (bounds.y + bounds.height - position.height)) {
        pos.y = Math.max(bounds.y, Math.min(pos.y,
          bounds.y + bounds.height - position.height));
        setVelocity(new Point(0, 0));
      }
    }
    setPosition(pos);

    return action;
  }

}
