import java.util.Random;
import java.awt.Color;


public class Astro extends MobileSimObject {
    	// OVERVIEW: An Astro is a MobileSimObject that starts in a RandomLocation and falls down until:
	// 1) It is shot.  2) It hits the ground.  3) It hits the gun.

    static private Random random = new Random();

    public Astro () {
         ;
    }

    public Color getColor()
    	//EFFECTS: Returns colors according to a Random for a fun and distracting visual effect.
	{
        int i = random.nextInt(5);
        if (i==1)
	  return Color.red;
        if (i==2)
        return Color.green;
        if (i==3)
        return Color.orange;
        if (i==4)
        return Color.pink;
        if (i==5)
        return Color.cyan;
        else
        return Color.blue;
    }

    public void newRandomAstro() 
	//EFFECTS: Creates a new Astro at a random location in space.
	{
      int i = random.nextInt(50);
      if (getGrid().validLocation(0,i)) {
          if (getGrid().isSquareEmpty(0,i)) {
              Astro A = new Astro();
              getGrid().setObjectAt(0,i,A);
              getGrid().startObject(A);
          }
      }
    }

    public void executeTurn() throws RuntimeException
    	//EFFECTS:  The primary game rules occur in reference to each Astro Object
	// If the Astro hits the ground, 5 points are lost, a new Astro is created, and this ends.
	// If the Astro is hit by a laser, both this and the laser end, 10 points are awarded, and
	// new Astros are created depending on level.
	// Otherwise, Astro moves south one block
	{
	Direction dir = new Direction(1,0);
	int newrow = getRow () + dir.northerlyDirection ();
	int newcol = getColumn () + dir.easterlyDirection ();


	if (getGrid ().validLocation (newrow, newcol)) {
	    if (getGrid().isSquareEmpty (newrow, newcol))
		{
		    setLocation (newrow, newcol);
		}
            else {
                SimObject neighbor = getGrid().getObjectAt(newrow, newcol);
                if (neighbor instanceof Gun) {
                        System.out.println("Game Over");
                        System.out.println("Final Score: " + getGrid().getScore());
                        System.exit(1);
                     }
                else if (neighbor instanceof Laser) {
                        getGrid().removeObject(this);
                        getGrid().removeObject(neighbor);
                        neighbor.kill();
                        this.kill();
                        getGrid().addToScore(10);
                        int i = getGrid().getScore();
                        if (i < 200) {getGrid().setLevel(1);}
                        else if (i >= 200 && i <= 215) {
                            newRandomAstro();
                            getGrid().setLevel(2);
                        }
                        else if (i >= 400 && i <= 415) {
                            newRandomAstro();
                            newRandomAstro();
                            getGrid().setLevel(3);
                        }
                        else if (i >= 600 && i <= 615) {
                            newRandomAstro();
                            newRandomAstro();
                            newRandomAstro();
                            getGrid().setLevel(4);
                        }
                        else if (i >= 1000) {
                            newRandomAstro();
                            getGrid().setLevel(5);
                        }
                        newRandomAstro();
                   }
              }
	}
	else {
		this.kill();
                getGrid().removeObject(this);
		if (newrow > 40) {
                  newRandomAstro();
                }
                getGrid().addToScore(-5);

            }
       }



}