/*	Copyright Alex Hornby 1994/1995. All rights reserved.
 	See file README for details
*/

/*	This really needs improving, until it is I recommend joystick 
	control.
*/

#include <iostream.h>
#include <vga.h>
#include "Keyboard.h"

Keyboard::Keyboard() : Control()
{
	//calibrate();
	left='z';right='x';up=';';down='.';fire1=' ';fire4='q';
}

int
Keyboard::getkey(void)
{
	int k=-1,j=25;
	while(k==-1)
		k=vga_getkey();
	while(j!=-1)
		j=vga_getkey();
	return k;
}

void
Keyboard::calibrate(void)
{
	cout << "Press key for LEFT\n";
	left=getkey();
	cout << "Press key for RIGHT\n";
	right=getkey();
	cout << "Press key for UP\n";
	up=getkey();
	cout << "Press key for DOWN\n";
	down=getkey();
	cout << "Press key for FIRE1\n";
	fire1=getkey();
	cout << "Press key for FIRE2\n";
	fire2=getkey();
}

void
Keyboard::fetch(void)
{
	static int scount;
	int key=vga_getkey();
	if(key==0)
	{
		if(scount>1)
		{
			scount=0;
			for(int i=0;i<FIRE4;i++)
				last[i]=FALSE;
			last[STRAIGHT]==TRUE;
		}
		else
			scount++;
	}
	else
	{
	scount=0;
	for(int i=0;i<FIRE4;i++)
		last[i]=FALSE;
	if(key==right)
		last[RIGHT]=TRUE;
	else if(key==left)
		last[LEFT]=TRUE;
	else if(key==down)
		last[DOWN]=TRUE;
	else if(key==up)
		last[UP]=TRUE;
	else if(key==fire1) 
		last[FIRE1]=TRUE;
	else if(key==fire2) 
		last[FIRE2]=TRUE;
	else if(key==fire3) 
		last[FIRE4]=TRUE;
	else if(key==fire4) 
		last[FIRE4]=TRUE;
	}
}

