ASR::ControlGroup Class Reference

#include <ControlGroup.h>

Collaboration diagram for ASR::ControlGroup:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ControlGroup ()
 ~ControlGroup ()
int addUnit (Unit *unit)
void removeUnit (size_t index)
void removeUnit (Unit *unit)
int getUnitIndex (Unit *unit)
size_t getNumUnits ()
UnitgetUnit (int index)
UnitgetLeaderUnit ()
Vector3 getFormationCenter ()
Vector3 getUnitOffset (size_t index)
Vector3 getUnitOffset (Unit *unit)
Vector3 getUnitDestination (int index)
Vector3 getUnitDestination (Unit *unit)
void setDestination (const Vector3 &dest)
void update ()
 Recalculates information regarding unit position and orientation for the formation layout.

Protected Member Functions

void _updateFormation ()
void _calcCenter ()
void _calcOrientation ()
 Determines the orientation of the group based on the center of the current positions and the destination.

Private Types

typedef vector< Unit * > UnitList

Private Attributes

bool mDirty
FormationmCurFormation
UnitList mUnits
Vector3 mFormationCenter
Vector3 mDestination
Vector3 mForward
Vector3 mRight

Detailed Description

Definition at line 17 of file ControlGroup.h.


Member Typedef Documentation

typedef vector<Unit*> ASR::ControlGroup::UnitList [private]

Definition at line 25 of file ControlGroup.h.


Constructor & Destructor Documentation

ASR::ControlGroup::ControlGroup (  ) 

Definition at line 17 of file ControlGroup.cpp.

References mCurFormation, mDirty, and mUnits.

00018                 : mForward ( Vector3::ZERO ), mRight ( Vector3::ZERO )
00019         {
00020                 mCurFormation = new LineFormation ();
00021                 mUnits.clear ();
00022                 mDirty = false;
00023         }

ASR::ControlGroup::~ControlGroup (  ) 

Definition at line 27 of file ControlGroup.cpp.

References mCurFormation, and mUnits.

00028         {
00029                 delete mCurFormation;
00030                 mUnits.clear ();
00031         }


Member Function Documentation

void ASR::ControlGroup::_calcCenter (  )  [protected]

Definition at line 92 of file ControlGroup.cpp.

References mFormationCenter, and mUnits.

Referenced by _updateFormation(), and update().

00093         {
00094                 UnitList::iterator iter;
00095                 Vector3 total = Vector3::ZERO;
00096 
00097                 if ( mUnits.size () == 0 )
00098                         return;
00099 
00100                 for ( iter = mUnits.begin(); iter != mUnits.end(); iter++ )
00101                 {
00102                         total += Vector3( (*iter)->getPosition ().x, 0.0f, (*iter)->getPosition ().z );
00103                 }
00104 
00105                 mFormationCenter = total / mUnits.size();
00106         }

void ASR::ControlGroup::_calcOrientation (  )  [protected]

Determines the orientation of the group based on the center of the current positions and the destination.

This assumes that the current center of formation is valid as well as having a valid destination.

Definition at line 221 of file ControlGroup.cpp.

References mDestination, mFormationCenter, mForward, and mRight.

Referenced by _updateFormation(), and update().

00222         {
00223                 mForward = mDestination - mFormationCenter;
00224                 mForward.normalise ();
00225 
00226                 // X-axis heavy
00227                 if ( Math::Abs(mForward.x) >= Math::Abs(mForward.z) )
00228                         mForward.y = mForward.z = 0.0f;
00229                 // Z-axis heavy
00230                 else
00231                         mForward.y = mForward.x = 0.0f;
00232 
00233                 mForward.normalise ();
00234         
00235                 mRight = mForward.crossProduct ( -Vector3::UNIT_Y );
00236                 mRight.normalise ();
00237         }

void ASR::ControlGroup::_updateFormation (  )  [protected]

Definition at line 82 of file ControlGroup.cpp.

References _calcCenter(), _calcOrientation(), and mDirty.

Referenced by getFormationCenter(), and getUnitDestination().

00083         {
00084                 _calcCenter ();
00085                 _calcOrientation ();
00086 
00087                 mDirty = false;
00088         }

int ASR::ControlGroup::addUnit ( Unit unit  ) 

Definition at line 35 of file ControlGroup.cpp.

References mDirty, and mUnits.

Referenced by ASR::UnitSelecter::_createActiveGroup().

00036         {
00037                 mUnits.push_back ( unit );
00038                 mDirty = true;
00039                 return mUnits.size() - 1;
00040         }

Vector3 ASR::ControlGroup::getFormationCenter (  ) 

Definition at line 72 of file ControlGroup.cpp.

References _updateFormation(), mDirty, and mFormationCenter.

Referenced by getUnitOffset().

00073         {
00074                 if ( mDirty )
00075                         _updateFormation ();
00076 
00077                 return mFormationCenter;
00078         }

Unit * ASR::ControlGroup::getLeaderUnit (  ) 

Definition at line 154 of file ControlGroup.cpp.

References mUnits.

Referenced by ASR::UnitSelecter::_moveUnits().

00155         {
00156                 assert ( !mUnits.empty() );
00157                 return mUnits[0];
00158         }

size_t ASR::ControlGroup::getNumUnits (  ) 

Definition at line 214 of file ControlGroup.cpp.

References mUnits.

Referenced by ASR::UnitSelecter::_moveUnits(), ASR::UnitSelecter::_unselectCurrect(), and ASR::UnitSelecter::_unselectDeadUnits().

00215         {
00216                 return mUnits.size();
00217         }

Unit * ASR::ControlGroup::getUnit ( int  index  ) 

Definition at line 206 of file ControlGroup.cpp.

References mUnits.

Referenced by ASR::UnitSelecter::_unselectCurrect(), and ASR::UnitSelecter::_unselectDeadUnits().

00207         {
00208                 OgreAssert ( index < mUnits.size (), "Unit index out of bounds" );
00209                 return mUnits[index];
00210         }

Vector3 ASR::ControlGroup::getUnitDestination ( Unit unit  ) 

Definition at line 162 of file ControlGroup.cpp.

References _updateFormation(), getUnitDestination(), getUnitIndex(), and mDirty.

00163         {
00164                 if ( mDirty )
00165                         _updateFormation ();
00166 
00167                 int index = getUnitIndex ( unit );
00168 
00169                 return getUnitDestination ( index );
00170         }

Vector3 ASR::ControlGroup::getUnitDestination ( int  index  ) 

Definition at line 174 of file ControlGroup.cpp.

References mDestination, mForward, mRight, and mUnits.

Referenced by getUnitDestination().

00175         {
00176                 OgreAssert ( index < mUnits.size (), "Unit index out of bounds" );
00177 
00178                 //update ();
00179 
00180                 // TODO
00181                 //              Ask the Formation for the offset
00182                 //              Currently just returning a huge line
00183                 int xPos;
00184                 if ( (index % 5) % 2 == 0 )
00185                         xPos = (index % 5) / 2;
00186                 else
00187                         xPos = -( (index % 5) + 1 ) / 2;
00188 
00189                 int zPos;
00190                 zPos = -(index / 5);
00191 
00192                 // TODO
00193                 //              Take the orientation of the ControlGroup into account
00194                 //              Use the "right" vector as well as the negated "forward"
00195                 //              vector (mOrientation)
00196                 //              Possibly snap to 90° rotations ala Starcraft
00197                 Vector3 xOffset = mRight * 30;
00198                 Vector3 zOffset = mForward * 30;
00199 
00200                 Vector3 localPos = mDestination + ( xOffset * xPos ) + ( zOffset * zPos );
00201                 return localPos;
00202         }

int ASR::ControlGroup::getUnitIndex ( Unit unit  ) 

Definition at line 130 of file ControlGroup.cpp.

References mUnits.

Referenced by getUnitDestination(), and getUnitOffset().

00131         {
00132                 for ( size_t i = 0; i < mUnits.size(); i++ )
00133                 {
00134                         if ( mUnits[i] == unit )
00135                                 return i;
00136                 }
00137 
00138                 throw Exception ( Exception::ERR_ITEM_NOT_FOUND, "Unit not found", "ControlGroup::getUnitIndex" );
00139         }

Vector3 ASR::ControlGroup::getUnitOffset ( Unit unit  ) 

Definition at line 110 of file ControlGroup.cpp.

References getFormationCenter(), and getUnitIndex().

00111         {
00112                 int index = getUnitIndex ( unit );
00113 
00114                 // TODO
00115                 //              Ask the Formation for the offset
00116                 //              Currently just returning a huge line
00117                 int position;
00118                 if ( index % 2 == 0 )
00119                         position = index / 2;
00120                 else
00121                         int position = -( index + 1 ) / 2;
00122 
00123                 Vector3 singleOffset ( 0, 0, 10 );
00124                 Vector3 localPos = getFormationCenter () + ( singleOffset * position );
00125                 return localPos;
00126         }

Vector3 ASR::ControlGroup::getUnitOffset ( size_t  index  ) 

void ASR::ControlGroup::removeUnit ( Unit unit  ) 

Definition at line 60 of file ControlGroup.cpp.

References mDirty, and mUnits.

00061         {
00062                 UnitList::iterator iter = find ( mUnits.begin(), mUnits.end(), unit );
00063                 if ( iter == mUnits.end () )
00064                         throw Exception ( Exception::ERR_ITEM_NOT_FOUND, "Unit not found", "ControlGroup::removeUnit" );
00065 
00066                 mUnits.erase ( iter );
00067                 mDirty = true;
00068         }

void ASR::ControlGroup::removeUnit ( size_t  index  ) 

Definition at line 44 of file ControlGroup.cpp.

References mDirty, and mUnits.

Referenced by ASR::UnitSelecter::_unselectDeadUnits().

00045         {
00046                 assert ( index < mUnits.size() && index <= 0 );
00047                 UnitList::iterator iter = mUnits.begin();
00048                 
00049                 for ( size_t i = 0; i < index; i++ )
00050                 {
00051                         iter++;
00052                 }
00053 
00054                 mUnits.erase ( iter );
00055                 mDirty = true;
00056         }

void ASR::ControlGroup::setDestination ( const Vector3 &  dest  ) 

Definition at line 143 of file ControlGroup.cpp.

References mDestination.

Referenced by ASR::UnitSelecter::_moveUnits().

00144         {
00145                 // TODO
00146                 //      Figure out a path for each and every unit in this list
00147                 //      using their offsets. We just move each unit as close
00148                 //      to where it is supposed to be as possible
00149                 mDestination = dest;
00150         }

void ASR::ControlGroup::update (  ) 

Recalculates information regarding unit position and orientation for the formation layout.

This doesn't actually change the units positioning but does setup the ControlGroup to allow for proper calculation of the orientation and center of formation values.

Definition at line 241 of file ControlGroup.cpp.

References _calcCenter(), and _calcOrientation().

00242         {
00243                 _calcCenter ();
00244                 _calcOrientation ();
00245         }


Member Data Documentation

Formation* ASR::ControlGroup::mCurFormation [private]

Definition at line 23 of file ControlGroup.h.

Referenced by ControlGroup(), and ~ControlGroup().

Vector3 ASR::ControlGroup::mDestination [private]

Definition at line 29 of file ControlGroup.h.

Referenced by _calcOrientation(), getUnitDestination(), and setDestination().

bool ASR::ControlGroup::mDirty [private]

Definition at line 22 of file ControlGroup.h.

Referenced by _updateFormation(), addUnit(), ControlGroup(), getFormationCenter(), getUnitDestination(), and removeUnit().

Vector3 ASR::ControlGroup::mFormationCenter [private]

Definition at line 28 of file ControlGroup.h.

Referenced by _calcCenter(), _calcOrientation(), and getFormationCenter().

Vector3 ASR::ControlGroup::mForward [private]

Definition at line 31 of file ControlGroup.h.

Referenced by _calcOrientation(), and getUnitDestination().

Vector3 ASR::ControlGroup::mRight [private]

Definition at line 32 of file ControlGroup.h.

Referenced by _calcOrientation(), and getUnitDestination().

UnitList ASR::ControlGroup::mUnits [private]

Definition at line 26 of file ControlGroup.h.

Referenced by _calcCenter(), addUnit(), ControlGroup(), getLeaderUnit(), getNumUnits(), getUnit(), getUnitDestination(), getUnitIndex(), removeUnit(), and ~ControlGroup().


The documentation for this class was generated from the following files:
Generated on Sun Jun 25 19:23:43 2006 for Valors End by  doxygen 1.4.7