package com.haru.input;

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



/**
 * KeyListenerから読み取った値を保持しておくBuffer.
 */
public class KeyBuffer extends InputBuffer implements  KeyListener{

	// キーバインド
	private KeyBind keyBind;

	// 連続回転ありか
	private boolean rotate;

	// 回転ボタンが押されているか
	private boolean pressed = false;

	public KeyBuffer(KeyBind keyBind, boolean rotate) {
		this.rotate = rotate;
		this.keyBind = keyBind;
	}



	public boolean f_enter = false;	// debug


	public void keyPressed(KeyEvent e) {

		Key k = keyBind.get(e.getKeyCode());
		if (k != null ) {

			switch(k) {
			case UP:
				isDrop = InputBuffer.TRUE;
				break;
			case DOWN:
				dir.x = 0;		// readするまでに２回以上呼ばれることもある(たぶん)
				dir.y = 1;
				break;
			case LEFT:
				dir.x = -1;
				dir.y = 0;
				break;
			case RIGHT:
				dir.x = 1;
				dir.y = 0;
				break;
			case R_RIGHT:
				if (rotate || !pressed) {
					d_t = InputBuffer.R_RIGHT;
					pressed = true;
				}
				break;
			case R_LEFT:
				if (rotate|| !pressed) {
					d_t = InputBuffer.R_LEFT;
					pressed = true;
				}
				break;
			}
		}


		// cheat
		switch(e.getKeyCode()) {
		case KeyEvent.VK_ENTER:
			f_enter = true;
			break;
		}
	}
	public void keyReleased(KeyEvent e) {
		Key k = keyBind.get(e.getKeyCode());
		if (k != null ) {
			switch (k) {
			case R_RIGHT:
			case R_LEFT:
				pressed = false;
				break;
			}
		}
	}
	public void keyTyped(KeyEvent arg0) {
	}

}
