00001 #include "Team.h"
00002 #include "Game.h"
00003
00004 #include "FogOfWar.h"
00005
00006 #include "Unit.h"
00007 #include "Building.h"
00008 #include "TeamUpdater.h"
00009
00010 #include "TeamUnits.h"
00011 #include "TeamBuildings.h"
00012 #include "Level.h"
00013 #include "ControlGroup.h"
00014
00015 #include "OgreEntity.h"
00016 #include "OgreSubEntity.h"
00017 #include "OgreException.h"
00018 #include "OgreMaterial.h"
00019 #include "OgreColourValue.h"
00020 #include "OgreString.h"
00021 #include "OgreStringConverter.h"
00022 #include "OgreMaterialManager.h"
00023 #include "Ogre.h"
00024 using namespace Ogre;
00025 using Ogre::SubEntity;
00026 using Ogre::Entity;
00027 using Ogre::MaterialManager;
00028 using Ogre::Material;
00029 using Ogre::ColourValue;
00030 using Ogre::String;
00031 using Ogre::StringConverter;
00032 using Ogre::Exception;
00033
00034
00035 namespace ASR
00036 {
00037
00038 Team::Team ( const String& name, const ColourValue& teamColor )
00039 : mName ( name )
00040 {
00041 mUnits = new TeamUnits ( this );
00042 mBuildings = new TeamBuildings ();
00043
00044 mFogOfWar = new FogOfWar ( Level::getSingleton ().getNumNodesWide (), Level::getSingleton ().getNumNodesHigh () );
00045
00046 mUpdater = new TeamUpdater ( this );
00047 Game::getSingleton ().addUpdater ( mUpdater );
00048
00049 mControlGroups.clear ();
00050 mActiveGroup = createControlGroup();
00051 setActiveControlGroup ( mActiveGroup );
00052
00053 mUnits->setSpatialGridSize ( 12, 12 );
00054
00055
00056
00057 MaterialPtr baseMat = MaterialManager::getSingleton ().getByName ("LizardMan" );
00058 MaterialPtr feathersMat = MaterialManager::getSingleton ().getByName ("LizardMan_Feathers" );
00059 MaterialPtr shoulderMat = MaterialManager::getSingleton ().getByName ("Lizardman_Armor" );
00060
00061 baseMat->load ();
00062 feathersMat->load ();
00063 shoulderMat->load ();
00064
00065 mFeathersMaterialName = feathersMat->getName() + "_team" + StringConverter::toString ( Level::getSingleton().getNumTeams() );
00066 mBaseMaterialName = baseMat->getName() + "_team" + StringConverter::toString ( Level::getSingleton().getNumTeams() );
00067 mShoulderMaterialName = shoulderMat->getName() + "_team" + StringConverter::toString ( Level::getSingleton().getNumTeams() );
00068
00069 MaterialPtr teamFeathersMaterial = feathersMat->clone ( mFeathersMaterialName );
00070 MaterialPtr teamBaseMaterial = baseMat->clone ( mBaseMaterialName );
00071 MaterialPtr teamShoulderMaterial = shoulderMat->clone ( mShoulderMaterialName );
00072
00073 teamBaseMaterial->getTechnique(0)->getPass(0)->getTextureUnitState(0)->setColourOperationEx(LBX_BLEND_TEXTURE_ALPHA, LBS_TEXTURE, LBS_MANUAL, ColourValue::White, teamColor );
00074 teamFeathersMaterial->getSupportedTechnique(0)->getPass(0)->getTextureUnitState(1)->setColourOperationEx(LBX_MODULATE, LBS_MANUAL, LBS_CURRENT, teamColor );
00075 teamShoulderMaterial->getSupportedTechnique(0)->getPass(0)->getTextureUnitState(0)->setColourOperationEx(LBX_BLEND_TEXTURE_ALPHA, LBS_TEXTURE, LBS_MANUAL, ColourValue::White, teamColor );
00076 }
00077
00078
00079
00080 Team::~Team ()
00081 {
00082 _destroyAllControlGroups ();
00083 delete mUnits;
00084 delete mBuildings;
00085 delete mUpdater;
00086 delete mFogOfWar;
00087 }
00088
00089
00090
00091 const String& Team::getName () const
00092 {
00093 return mName;
00094 }
00095
00096
00097
00098 Unit* Team::createUnit ( const UnitType& type )
00099 {
00100 Unit* newUnit = mUnits->createUnit( type.getMeshName() );
00101 _applyTeamColors ( newUnit );
00102 return newUnit;
00103 }
00104
00105
00106
00107 Building* Team::createBuilding ( const String& meshName, const Vector3& position )
00108 {
00109 Building* newBuilding = mBuildings->createBuilding( meshName, position );
00110 _applyTeamColors ( newBuilding );
00111 return newBuilding;
00112 }
00113
00114
00115
00116 void Team::findTargets ()
00117 {
00118 Level& lev = Level::getSingleton();
00119
00120 TeamUnits::UnitIterator iter = mUnits->getUnitIterator();
00121 while ( iter.hasMoreElements() )
00122 {
00123 Unit* unit = iter.getNext();
00124
00125 lev.getAdjacentUnits ( unit->getPosition(), unit->getSightRadius () );
00126 }
00127 }
00128
00129
00130
00131 ControlGroup* Team::createControlGroup ()
00132 {
00133
00134
00135
00136 ControlGroup* group = new ControlGroup ();
00137 mControlGroups.push_back ( group );
00138 return group;
00139 }
00140
00141
00142
00143 void Team::destroyControlGroup ( ControlGroup* group )
00144 {
00145 ControlGroups::iterator iter = find ( mControlGroups.begin(), mControlGroups.end(), group );
00146 if ( iter == mControlGroups.end() )
00147 throw Exception ( Exception::ERR_ITEM_NOT_FOUND, "Control group not found", "Team::destroyControlGroup" );
00148
00149 delete (*iter);
00150 mControlGroups.erase ( iter );
00151 }
00152
00153
00154
00155 void Team::_destroyAllControlGroups ()
00156 {
00157 ControlGroups::iterator iter = mControlGroups.begin ();
00158 while ( iter != mControlGroups.end () )
00159 {
00160 delete (*iter);
00161 iter++;
00162 }
00163
00164 mControlGroups.clear ();
00165 }
00166
00167
00168
00169 TeamUnits::UnitIterator Team::getUnitIterator () const
00170 {
00171 return mUnits->getUnitIterator ();
00172 }
00173
00174
00175
00176 void Team::setActiveControlGroup ( ControlGroup* group )
00177 {
00178 mActiveGroup = group;
00179 }
00180
00181
00182
00183 ControlGroup* Team::getActiveControlGroup () const
00184 {
00185 return mActiveGroup;
00186 }
00187
00188
00189
00190 void Team::destroyActiveControlGroup ()
00191 {
00192 destroyControlGroup ( mActiveGroup );
00193 }
00194
00195
00196
00197 void Team::_applyTeamColors ( Unit* unit )
00198 {
00199 unit->getEntity()->getSubEntity(1)->setMaterialName ( mFeathersMaterialName );
00200 unit->getEntity()->getSubEntity (0)->setMaterialName ( mBaseMaterialName );
00201
00202
00203 unit->mShield->setMaterialName ( mShoulderMaterialName );
00204 unit->mShoulder->setMaterialName ( mShoulderMaterialName );
00205 }
00206
00207
00208
00209 void Team::_applyTeamColors ( Building* building )
00210 {
00211
00212
00213 }
00214
00215
00216
00217 TeamUnits::UnitList Team::getAdjacentUnits ( const Vector3& worldPos, float radius )
00218 {
00219 return mUnits->getAdjacentUnits ( worldPos, radius );
00220 }
00221
00222
00223
00224 FogOfWar* Team::getFogOfWar () const
00225 {
00226 return mFogOfWar;
00227 }
00228 };