#include "stdafx.h"
#include "UnitController.h"
UnitController::UnitController()
{
_recycledIDs.reserve( 10 );
_units.reserve( 10 );
}
UnitController::~UnitController()
{
_units.clear();
_recycledIDs.clear();
}
const Unit& UnitController::addUnit( const Player& forPlayer, const std::string& filepath )
{
try
{
if( filepath.size() > 0 )
{
// valid filepath
if( _recycledIDs.size() > 0 )
{
_units.insert( _units.begin() + _recycledIDs.back(), new Unit( forPlayer, _recycledIDs.back() ) );
_recycledIDs.pop_back();
}
else
{
_units.push_back( new Unit( forPlayer, _units.size() ) );
}
// set values for unit
Unit& u = (*_units.back());
u.setName( "TestUnit" );
return (*_units.back());
}
throw( filepath );
}
catch( std::string& file )
{
std::cout << "Exception: Wrong filepath for addUnit - " << file << std::endl;
}
catch( std::bad_alloc& ba )
{
std::cout << "Exception: Bad alloc in addUnit - " << ba.what() << std::endl;
}
}
void UnitController::removeUnit( const int unitID )
{
if( unitID < _units.size() )
{
_units.erase( _units.begin() + unitID );
if( unitID != _units.size() )
_recycledIDs.push_back( unitID );
}
}
const Unit& UnitController::getUnit( const int unitID )
{
try
{
if( unitID < _units.size() )
return *(_units.at( unitID ));
throw( unitID );
}
catch( int wrongUID )
{
std::cout << "Exception: Passed wrond unitID to getUnit - " << wrongUID << std::endl;
}
}
void UnitController::issueCastOrder( const Unit& source, const Unit& target, Ability& ability)
{
Unit& src = *(_units.at( source.getID() ) );
Unit& tgt = *(_units.at( target.getID() ) );
src.order( Order( src, Order::ORDER_TARGET, Order::USE_ABILITY ) );
Order* order = src.getCurrentOrder();
order->setTarget( tgt );
order->setAbility( ability );
std::cout << "Unit " << source.getName() << " is casting ability on target " << target.getName() << std::endl;
}
void UnitController::issueCastOrder( const Unit& source, Ability& ability)
{
Unit& src = *(_units.at( source.getID() ) );
src.order( Order( src, Order::ORDER_NO_TARGET, Order::USE_ABILITY ) );
Order* order = src.getCurrentOrder();
order->setAbility( ability );
std::cout << "Unit " << source.getName() << " is casting ability on self " << std::endl;
}
void UnitController::issueItemOrder( const Unit& source, const Unit& target, Item& item )
{
Unit& src = *(_units.at( source.getID() ) );
Unit& tgt = *(_units.at( target.getID() ) );
src.order( Order( src, Order::ORDER_TARGET, Order::USE_ITEM ) );
Order* order = src.getCurrentOrder();
order->setTarget( tgt );
order->setItem( item );
std::cout << "Unit " << source.getName() << " is using itemon target " << target.getName() << std::endl;
}
void UnitController::issueItemOrder( const Unit& source, Item& item )
{
Unit& src = *(_units.at( source.getID() ) );
src.order( Order( src, Order::ORDER_NO_TARGET, Order::USE_ITEM ) );
Order* order = src.getCurrentOrder();
order->setItem( item );
std::cout << "Unit " << source.getName() << " is using item on self " << std::endl;
}
void UnitController::damageOrder( const Unit& source, const Unit& target, DAMAGE_TYPE damageType, float damage )
{
Unit& src = *(_units.at( source.getID() ) );
Unit& tgt = *(_units.at( target.getID() ) );
src.order( Order( src, Order::ORDER_TARGET, Order::ATTACK ) );
Order* order = src.getCurrentOrder();
order->setTarget( tgt );
std::cout << "Unit " << source.getName() << " is damaging target " << target.getName() << std::endl;
}
void UnitController::addItem( const Unit& unit, Item& item )
{
Unit& src = *(_units.at( unit.getID() ) );
src.addItem( item );
}
void UnitController::addAbility( const Unit& unit, Ability& ability )
{
Unit& src = *(_units.at( unit.getID() ) );
src.addAbility( ability );
}
void UnitController::addExperience( const Unit& unit, int experience )
{
Unit& src = *(_units.at( unit.getID() ) );
if( dynamic_cast<Hero*>(&src) )
{
Hero* h = (Hero*)(&src);
h->setExperience( h->getExperience() + experience );
std::cout << "Hero got experience" << std::endl;
}
}