/*
 *  Copyright 2002-2009 Mikhail Kryshen <mikhail@kryshen.net>
 *
 *  This file is part of BMTron.
 *
 *  BMTron is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  BMTron is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with BMTron.  If not, see <http://www.gnu.org/licenses/>.
 */
 
package kryshen.bmtron;

import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * Player controlled with keyboard.
 */
public class HumanPlayer extends Player implements KeyListener {
    private int control = 0;
    
    HumanPlayer(Field field, String name, Color color, int team, int control) {
        super(field, name, color, team);
        this.control = control;
    }
    
    private int keyCodeToDirection(int keyCode) {
//      System.err.println(keyCode);
        switch(control) {
        case 0:
            switch(keyCode) {
            case 87:
                return field.UP;
            case 68:
                return field.RIGHT;
            case 83:
                return field.DOWN;
            case 65:
                return field.LEFT;
            }
            return -1;
        case 1:
            switch(keyCode) {
            case 89:
                return field.UP;
            case 74:
                return field.RIGHT;
            case 72:
                return field.DOWN;
            case 71:
                return field.LEFT;
            }
            return -1;
        case 2:
            switch(keyCode) {
            case 80:
                return field.UP;
            case 222:
                return field.RIGHT;
            case 59: case 186:
                return field.DOWN;
            case 76:
                return field.LEFT;
            }
            return -1;
        case 3:
            switch(keyCode) {
            case 38:
                return field.UP;
            case 39:
                return field.RIGHT;
            case 40:
                return field.DOWN;
            case 37:
                return field.LEFT;
            }
            return -1;
        }
        return -1;
    }
    
    void stop() {
        field.game.removeKeyListener(this);
        super.stop();
    }
    
    void start() {
        field.game.addKeyListener(this);
        super.start();
    }
    
    public void keyPressed(KeyEvent e) {
        int d = keyCodeToDirection(e.getKeyCode());
        if (d >= 0) setDirection(d);
    }
    
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {}
}
