/*********************************************************************//**
*	\brief Implementace zbrane: Pistol 9mm.
*	
*	\author Michal Jirous
*	\date 9.12.2008
*	\file wpn_pistol.cpp
**********************************************************************/

#include "wpn_pistol.h"
#include "ctrl_var_definition.h"
#include "game.h"
#include "damage_types.h"
#include "crosshairs.h"
using namespace modelLib;

//poradi a identifikacni cisla animaci
enum
{
	IDLE_S = 0,
	SHOOT1,
	SHOOT2,
	SHOOT3,
	SHOOT_LAST_S,
	RELOAD_S,
	DRAW_S,
	SUP_ON,
	IDLE,
	SHOOT4,
	SHOOT5,
	SHOOT6,
	SHOOT_LAST,
	RELOAD,
	DRAW,
	SUP_OFF
};


WeaponPistol9mm::WeaponPistol9mm( ) : BasicWeapon( )
{
	m_pMaxStackAmmo = gamevarsLibrary::getVariable( vardef::WEAPON_9MMPISTOL_MAX_NAME );
	if( m_pMaxStackAmmo )
	{
		m_iMaxAmmoAmount = m_iAmmoAmount = gamevarsLibrary::getiData( m_pMaxStackAmmo );
	}
	m_iAmmoType = AMMO_9MM;
	m_bNeedAmmo = true;
	setModel( "v_usp.mdl" );
	soundLibrary.loadSound( m_ShootSounds[0], "weapons/usp/shoot1.wav" );
	soundLibrary.loadSound( m_ShootSounds[1], "weapons/usp/shoot2.wav" );
	soundLibrary.loadSound( m_ClipOut, "weapons/usp/usp_clipout.wav");
	soundLibrary.loadSound( m_ClipIn, "weapons/usp/usp_clipin.wav");
	soundLibrary.loadSound( m_SliderRelease, "weapons/usp/usp_sliderelease.wav");
	m_pPrimaryAttackDmg = gamevarsLibrary::getVariable( vardef::WEAPON_9MMPISTOL_DMG_NAME );
	m_iCrosshair = CROSSHAIR_DEFAULT;
}


void WeaponPistol9mm::primaryAttack( bool pulse )
{
	if( pulse )
	if( m_iAmmoAmount > 0 && playAnimation( rand()%3 + SHOOT4, 200 ))	//mame 3 animace strelby
	{
		m_ShootSounds[ rand()%2].play();	//mame 2 zvuky strelby
		int iDamage = gamevarsLibrary::getiData( m_pPrimaryAttackDmg );
		AttackInfo info;
		basic_fight( iDamage, info, INFINITE_DISTANCE, dmg::BULLET );
		m_iAmmoAmount--;
	}
}


void WeaponPistol9mm::reload( bool pulse )
{
	if( game.m_pPlayer && m_iAmmoAmount < m_iMaxAmmoAmount && game.m_pPlayer->AmmoAndWeapons.getAmmoAmount( m_iAmmoType ) > 0 )
	{
		if( playAnimation( RELOAD, 2700, false, NORMAL_LOCK, (WpnFunc)&WeaponPistol9mm::finnish, this ) )
		{
			m_ClipOut.play();
			soundLibrary.postSoundPlay( &m_ClipIn, 1300 );	//zpozdeni zvuku
			soundLibrary.postSoundPlay( &m_SliderRelease, 1700 );	//zpozdeni zvuku
		}
	}
}

void WeaponPistol9mm::finnish()
{	
	if( game.m_pPlayer )
	{
		AmmoElement *pAmmo = game.m_pPlayer->AmmoAndWeapons.getAmmo( m_iAmmoType );
		unsigned int totalammo = pAmmo->getAmount();
		unsigned int canuse = m_iMaxAmmoAmount - m_iAmmoAmount;
		unsigned int change = min( totalammo, canuse );
		m_iAmmoAmount+=change;
		pAmmo->removeAmmo( change );
	}
}

void WeaponPistol9mm::grab()
{
	playAnimation( DRAW, 1500 );
}


void WeaponPistol9mm::idle()
{
	playAnimation( IDLE, 2000, true, UNLOCKED );
}

void WeaponPistol9mm::secondaryAttack( bool pulse )
{

}

WeaponPistol9mm::~WeaponPistol9mm()
{
	soundLibrary.unloadSound( m_ShootSounds[0] );
	soundLibrary.unloadSound( m_ShootSounds[1] );
	soundLibrary.unloadSound( m_ClipOut );
	soundLibrary.unloadSound( m_ClipIn );
	soundLibrary.unloadSound( m_SliderRelease );
}
