#include "Command.h"
#include "Person.h"
#include "List.h"
#include "Level.h"
#include "Player.h"

#include <iostream>
#include <cmath>

using namespace std;

const std::string ShootInRangeCommand::_id = "shootPlayer";

ShootInRangeCommand::ShootInRangeCommand(Person *person, int range)
:_person(person), _shootRange(range)
{
}

ShootInRangeCommand::ShootInRangeCommand(List *command)
{
	command = command->rest();
	_person = (Person *)Level::instance()->getObject(command->firstString());
	command = command->rest();
	_shootRange = command->firstInt();
}

void ShootInRangeCommand::execute()
{
	Player* player = Level::instance()->getPlayer();
	int dist = sqrt( pow(player->getBox().x-_person->getBox().x,2) + 
					 pow(player->getBox().y-_person->getBox().y,2) );
	bool shootOpportunity = 
		glfuncs::intersectX(player->getBox(), _person->getBox()) || 
		glfuncs::intersectY(player->getBox(), _person->getBox());
	

	if ((abs(dist) < _shootRange) && shootOpportunity)
		ShootCommand(_person).execute();
}
std::string ShootInRangeCommand::getString()
{
	List *command = new List();
	command->snoc(new Atom(_id));
	command->snoc(new Atom(_person->id()));
	command->snoc(new Atom(_shootRange));

	string commandString = command->getString();
	delete command;

	return commandString;
}