#include "Weapon.h"
#include "Sprite.h"
#include "Person.h"
#include "Parser.h"
#include "glfuncs.h"
#include "Command.h"
#include "CollisionHandler.h"
#include "CommandManager.h"

#include <sstream>

using namespace std;

WeaponPunch::WeaponPunch()
:Weapon()
{

}

WeaponPunch::WeaponPunch(std::string filename, Person *shooter)
:Weapon(filename, shooter)
{
	List *info = Parser::parse(filename);

	load(info);

	delete info;
}

WeaponPunch *WeaponPunch::copy(Person *shooter)
{
	WeaponPunch *copy = new WeaponPunch(*this);
	copy->_shooter = shooter;
	return copy;
}

void WeaponPunch::shoot()
{
	if( _framesSinceShoot >= _waitShoot)
	{
		//This is here to make sure that the sound effect only plays once per shot
		
		vector<Object*> objs;

		int pels = 32;
		SDL_Rect box;
			
		if(_shooter->getXDirection() == LEFT){
			box = _shooter->getBox();
			box.x-=pels;
			_shooter->setPos(box.x, box.y);
			objs = CollisionHandler::ObjsColliding( _shooter , true);
			box.x+=pels;
			_shooter->setPos(box.x, box.y);
		}
		else{
			box = _shooter->getBox();
			box.x+=pels;
			_shooter->setPos(box.x, box.y);
			objs = CollisionHandler::ObjsColliding(_shooter , true);
			box.x-=pels;
			_shooter->setPos(box.x, box.y);
		}

		for(int i=0; i<objs.size(); i++){
			if(objs[i]->getType()==ENEMY){
				//((Person *)objs[i])->addHealth(-((Person *)objs[i])->getHealth());
				((Person *)objs[i])->die();
				CommandManager::instance()->execute(new PlaySoundCommand("sounds/punch.wav"));
			}
		}


		_framesSinceShoot = 0;
		_ammo -= _ammoSize;
	}
}
