/////////////////////
// Sidney Chang    //
// 6.837 Project 1 //
// TA: Jacob F11   //
// 9/28/98         //
/////////////////////

import java.awt.*;
import java.awt.image.*;

public class Snake extends AnimatedSprite {

  public static final int COILED = 0;
  public static final int STARTLED = 1;
  public static final int SLITHERING = 2;
  public static final int COLLISION = 6;

  // Variables
  public int state;
  public int lastState;
  public boolean readyToRun;

  // Constructors
  public Snake(Image images, int xPos, int yPos) {
   super(images,6,xPos,yPos);

    state = COILED;
    lastState = COILED;

    // track for coiled
    addFrame(0,0);

    // track for startled
    addFrame(1,1);
    addFrame(1,2);
    addFrame(1,3);

    // track for slithering
    addFrame(2,4);
    addFrame(2,5);

    // track for hiding
    addFrame(3,0);
  }

  // Methods
  public void Draw(Raster bgnd) {
    updateState();
    super.Draw(bgnd);
    if (state == STARTLED && readyToRun) {
      setState(SLITHERING);
      readyToRun = false;
    }
  }

  public void setState(int s) {
    state = s;
    setFrame(0);
  }

  public void updateState() {
    
    switch (state) {
      
    case (COILED) : {
      currentTrack = 0;
      return;
    }
    case (STARTLED) : {
      currentTrack = 1;
      x = x - 5;
      if (currentFrame == 2)
	readyToRun = true;
      return;
    }
    case (SLITHERING) : {
      currentTrack = 2;
      x = x - 5;
      return;
    }
    case (COLLISION) :
      if (lastState == COILED) {
	x = x - 5;
	setState(STARTLED);
      }
      else if (lastState == STARTLED)
	setState(SLITHERING);
      else if (lastState == SLITHERING)
	setState(STARTLED);
      return;
    }
  }
}
