
package spacewar;

import java.util.Enumeration;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

class Player extends Pilot implements KeyListener {

    private KeyMapping keyMapping;

    private int        rotation_direction = Ship.STOP; // current rotation key
    private boolean    thrust_on = false;              // current thrust

    Player(Game theGame, int number) {
        super(theGame,number);

        if (getNumber() == 1)
            keyMapping = KeyMapping.keyMapping1;
        else if (getNumber() == 2)
            keyMapping = KeyMapping.keyMapping2;

    }

    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        boolean consumed = true;

        if (keyCode == keyMapping.fire) {
            ship.fire();
        }
        else if (keyCode == keyMapping.thrust && !thrust_on) {
            ship.thrust(true);
            thrust_on = true;
        }
        else if (keyCode == keyMapping.right &&
                 rotation_direction != Ship.COUNTERCLOCKWISE) {
            //start rotating clockwise unless already rotating in the
            //opposite direction
            rotation_direction = Ship.CLOCKWISE;
            ship.rotate(Ship.CLOCKWISE);
        }
        else if (keyCode == keyMapping.left &&
                 rotation_direction != Ship.CLOCKWISE) {
            //start rotating counterclockwise unless already rotating in the
            //opposite direction
            rotation_direction = Ship.COUNTERCLOCKWISE;
            ship.rotate(Ship.COUNTERCLOCKWISE);
        }
        else {
            consumed = false;
        }

        if (consumed) e.consume();
    }

    public void keyReleased(KeyEvent e) {
        int keyCode = e.getKeyCode();

        if (keyCode == keyMapping.thrust) {
            ship.thrust(false);                 //engine off
            thrust_on = false;
        }
        else if (keyCode == keyMapping.right &&
                 rotation_direction == Ship.CLOCKWISE
                 ||
                 keyCode == keyMapping.left &&
                 rotation_direction == Ship.COUNTERCLOCKWISE) {
            ship.rotate(Ship.STOP);             //stop rotation
            rotation_direction = Ship.STOP;
        }
    }

    public void keyTyped(KeyEvent e) {
        // have to implement this because it's in KeyListener
    }
}

class KeyMapping {

    static final KeyMapping keyMapping1 =
        new KeyMapping(KeyEvent.VK_LEFT,
                       KeyEvent.VK_RIGHT,
                       KeyEvent.VK_UP,
                       KeyEvent.VK_SPACE);

    static final KeyMapping keyMapping2 =
        new KeyMapping(KeyEvent.VK_X,
                       KeyEvent.VK_V,
                       KeyEvent.VK_D,
                       KeyEvent.VK_ALT);

    int left, right, thrust, fire;

    KeyMapping(int k_left, int k_right, int k_thrust, int k_fire) {
        left   = k_left;
        right  = k_right;
        thrust = k_thrust;
        fire   = k_fire;
    }
}
