import java.awt.*; 

public class Penguin { 

    int x, y, dx = 6, dy = 6, look = 2; 

    int animP[] = {    7,  8,  9,  8, //  left ( west)
		       10, 11, 12, 11, // right ( east) 
		       4,  5,  6,  5, //    up (north)
		       1,  2,  3,  2  //  down (south)
    }; 
    
    String playerID, direction = "south"; 

    public Penguin(int x, int y, int width, int height, String owner) {

	this.x = x; this.y = y; 

	playerID = owner; 

    }

    public void moveTo(int x, int y) {
	System.out.println("***(Penguin " + 
			   playerID + 
			   " moves to (" + x + ", " + y + "))***"); 
	this.x = x; 
	this.y = y; 
    }

    public void perform(String action) {

	if (action.equals("happy to be here")) return; 

	if (direction.equals("north")) {

	    if (action.equals("up")) {

	    } else if (action.equals("down")) {
		turnLeft(); turnLeft(); 
	    } else if (action.equals("left")) {
		turnLeft(); 
	    } else if (action.equals("right")) {
		turnRight(); 
	    } else { } 

	} else if (direction.equals("south")) {

	    if (action.equals("up")) {
		turnLeft(); turnLeft(); 
	    } else if (action.equals("down")) {

	    } else if (action.equals("left")) {
		turnRight(); 
	    } else if (action.equals("right")) {
		turnLeft(); 
	    } else { } 

	} else if (direction.equals("east")) {

	    if (action.equals("up")) {
                turnLeft(); 
	    } else if (action.equals("down")) {
		turnRight(); 
	    } else if (action.equals("left")) {
		turnLeft(); turnLeft(); 
	    } else if (action.equals("right")) {

	    } else { } 

	} else if (direction.equals("west")) {

	    if (action.equals("up")) {
		turnRight(); 
	    } else if (action.equals("down")) {
		turnLeft(); 
	    } else if (action.equals("left")) {

	    } else if (action.equals("right")) {
		turnLeft(); turnLeft(); 
	    } else { } 

	} else {

	}

	moveForward(); 

    }

    void turnRight() { 

	if (i % 5 != 0) return; 

	if (direction.equals("south")) { 
	    direction = "west"; 
	    look = 7; 
	} else if (direction.equals("east")) { 
	    direction = "south"; 
	    look = 2; 
	} else if (direction.equals("north")) { 
	    direction = "east"; 
	    look = 12; 
	} else { 
	    direction = "north"; 
	    look = 5; 
	}

    }
    
    int i = 0; 

    void moveForward() {   

	if (direction.equals("south")) { 
	    y += dy; 
	    look = animP[12 + (i + 1) % 4]; 
	} else if (direction.equals("east")) { 
	    x += dx; 
	    look = animP[ 4 + (i + 2) % 4]; 
	} else if (direction.equals("north")) { 
	    y -= dy; 
	    look = animP[ 8 + (i + 1) % 4]; 
	} else { // west 
	    x -= dx; 
	    look = animP[ 0 + i % 4]; 
	}

	i = i + 1; 
	
    }

    public void draw(Graphics g, boolean self) {
	g.drawImage(frames[look], x, y, location); 
	if (self) {
	    g.setColor(Color.white); 
	    g.drawRect(x, y, 30, 30); 
	}
    }

    void turnLeft() { 

	if (i % 5 != 0) return; 

	if (direction.equals("south")) { 
	    direction = "east"; 
	    look = 12; 
	} else if (direction.equals("east")) { 
	    direction = "north"; 
	    look = 5; 
	} else if (direction.equals("north")) { 
	    direction = "west"; 
	    look = 7; 
	} else { 
	    direction = "south"; 
	    look = 2; 
	}

	i = 0; 

    }
    
    Client location; 

    public void placeIn(Client location) {
	this.location = location; 
	frames = this.location.small;
    }

    Image[] frames; 

} // Penguin 

