/***************************************************************************
 *   Copyright (C) 2004-2005 by                                            *
 *     Paolo Sacconier   <axa1981@tin.it>                                  *
 *     Francesco Tamagni <minchiahead@hacari.org>                          *
 *                                                                         *
 *   This program 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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program 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 this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "player.h"

namespace gillo {

Player::Player() : ePtr(NULL), car(CAR_OMNICAR){
	initKeys();
}

Player::Player(Entity& e)
: ePtr(&e), car(CAR_OMNICAR)
{
	initKeys();
}

void Player::initKeys() {
	this->keysStatus.down  = 0;
	this->keysStatus.fire  = 0;
	this->keysStatus.jump  = 0;
	this->keysStatus.left  = 0;
	this->keysStatus.right = 0;
	this->keysStatus.up    = 0;
}

Player::~Player()
{
}

void Player::react(SDL_Event *event)
{
	switch( event->type ) {
		case SDL_KEYDOWN:
			handleKeyDown(&event->key.keysym);
			break;
		case SDL_KEYUP:
			handleKeyUp(&event->key.keysym);
			break;
		default:
//			throw Error::Event("Unhandled event");
			break;
	}
}

void Player::handleKeyDown( SDL_keysym* keysym )
{
	if (this->ePtr == NULL)
		return;
		
	if( keysym->sym == keys.up) {
		this->keysStatus.up = 1;
		this->ePtr->move(Entity::GO_FWRD);
	} else if( keysym->sym == keys.down) {
		this->keysStatus.down = 1;
		this->ePtr->move(Entity::GO_BWRD);
	} else if( keysym->sym == keys.left) {
		this->keysStatus.left = 1;
		this->ePtr->move(Entity::TURN_LEFT);
	} else if( keysym->sym == keys.right) {
		this->keysStatus.right = 1;
		this->ePtr->move(Entity::TURN_RIGHT);
	} else if( keysym->sym == keys.fire) {
		this->keysStatus.fire = 1;
		this->ePtr->move(Entity::FIRE);
	} else if( keysym->sym == keys.jump) {
		this->keysStatus.jump = 1;
		this->ePtr->move(Entity::JUMP);
	} else {}
}

void Player::handleKeyUp  ( SDL_keysym* keysym )
{
	if (this->ePtr == NULL)
		return;	

	if( keysym->sym == keys.up) { if (!this->keysStatus.down) this->ePtr->move(Entity::GO_STOP); this->keysStatus.up=0;}
	else if( keysym->sym == keys.down) { if (!this->keysStatus.up) this->ePtr->move(Entity::GO_STOP); this->keysStatus.down=0;}
	else if( keysym->sym == keys.left) { if (!this->keysStatus.right) this->ePtr->move(Entity::TURN_STOP); this->keysStatus.left=0;}
	else if( keysym->sym == keys.right) { if (!this->keysStatus.left) this->ePtr->move(Entity::TURN_STOP); this->keysStatus.right=0;}
	else if( keysym->sym == keys.fire) { this->keysStatus.fire=0; }
	else if( keysym->sym == keys.jump) { this->ePtr->move(Entity::JUMP_STOP); this->keysStatus.jump=0;}
	else {}
}

};
