ent.cpp

Go to the documentation of this file.
00001 /*
00002  * ent.cpp
00003  *
00004  *  Created on: Dec 18, 2011
00005  *      Author: sushil
00006  */
00007 
00008 #include <iostream>
00009 #include <assert.h>
00010 #include <ent.h>
00011 #include <engine.h>
00012 #include <aspect.h>
00013 #include <command.h>
00014 #include "DEBUG.h"
00015 
00016 int FastEcslent::Entity::count = 0;
00017 
00018 void FastEcslent::Entity::print(){
00019         DEBUG(std::cout << "Id: " << entityId.id << ", instanceId: " << engine->instanceId << std::endl;)
00020         DEBUG(std::cout << "UIName " << uiname << ", meshName: " << meshName << std::endl;)
00021         DEBUG(std::cout << "Position: " << pos.x << ", " << pos.y << ", " << pos.z << std::endl;)
00022         DEBUG(std::cout << "Velocity: " << vel.x << ", " << vel.y << ", " << vel.z << std::endl;)
00023         DEBUG(std::cout << "Accelert: " << acc.x << ", " << acc.y << ", " << acc.z << std::endl;)
00024         DEBUG(std::cout << "Speed:    " << speed << std::endl;)
00025         DEBUG(std::cout << "Heading:  " << heading << std::endl;)
00026         DEBUG(std::cout << "dsrdSpd:  " << desiredSpeed << std::endl;)
00027         DEBUG(std::cout << "dsrdHdg:  " << desiredHeading << std::endl;)
00028         DEBUG(std::cout << "yaw:      " << yaw << std::endl;)
00029         DEBUG(std::cout << "selected: " << isSelected << std::endl;)
00030         DEBUG(std::cout << "-------------------------------------------------------------------" << std::endl;)
00031 }
00032 
00033 
00034 void FastEcslent::Entity::reset(){
00035         //FastEcslent::Entity::tick = &FastEcslent::Entity::gestatingTick;
00036         //this->tick = &FastEcslent::Entity::gestatingTick;
00037         //tic = &FastEcslent::Entity::gestatingTick;
00038         pos = Ogre::Vector3(0.0f, 0.0f, 0.0f);
00039         vel = Ogre::Vector3(0.0f, 0.0f, 0.0f);
00040         acc = Ogre::Vector3(0.0f, 0.0f, 0.0f);
00041         maxSpeed = 40.0f;
00042         minSpeed = -10.0f;
00043         speedRange = maxSpeed - minSpeed;
00044         rot = Ogre::Quaternion(Ogre::Radian(0.0f), Ogre::Vector3::UNIT_Y);
00045         maxAcceleration = 1.0f;
00046 
00047         yaw = 0.0f;
00048         maxRotationalSpeed = 0.1;
00049         desiredSpeed = 0.0f;
00050         desiredHeading = 0.0f;
00051         speed = 0.0f;
00052         heading = 0.0f;
00053         //entityType = CIGARETTE;
00054         //alive = true;
00055         selectable = false;
00056 
00057         turningRadius = 10000;
00058 
00059         //selection
00060         isSelected = false;
00061 
00062         entityId.side = BATTLEMASTER;
00063         entityId.player = ONE;
00064         entityClass   = SURFACE;
00065         entityState   = ALIVE;
00066         gestationPeriod = 10.0;
00067         timeLeftToBirth = 10.0;
00068         timeLeftToDie   = 10.0;
00069 
00070         /*
00071         preReqs.neededResources.gas = 0;
00072         preReqs.neededResources.minerals = 50;
00073         preReqs.neededResources.supply = 1;
00074 
00075         preReqs.neededTypes[0] = BARRACKS;
00076         preReqs.nNeededTypes = 1;
00077 */
00078 
00079         aspects.clear();
00080 
00081 }
00082 
00083 FastEcslent::Entity::Entity(Engine *eng, EntityType entType) {
00084 
00085         engine = eng;
00086         //entityId.id = id;
00087         entityType = entType;
00088         meshName = "cigarette.mesh";
00089         uiname = "Cigarette";
00090         //uiname.append(boost::lexical_cast<std::string>(count++));
00091         reset();
00092 }
00093 
00094 
00095 
00096 
00097 
00098 
00099 // There should only be one aspect of each type
00100 
00101 /*int FastEcslent::Entity::addAspect(Aspect *asp) {
00102         aspects[nAspects++] = asp;
00103         return nAspects - 1;
00104 }*/
00105 void FastEcslent::Entity::init(){
00106 
00107         for (int aType = 0; aType < NASPECTTYPES; aType++){ // the problem with enums in C++n
00108                 switch (aType){
00109                 case (PHYSICS):
00110                         physics = new Physics2D2(this, PHYSICS);
00111                         physics->init();
00112                         this->addAspect(physics);
00113                         break;
00114                 case (UNITAI):
00115                         ai = new UnitAI(this, UNITAI);
00116                         ai->init();
00117                     this->addAspect(ai);
00118                         break;
00119                 case (WEAPON):
00120                         weapon = new Weapon(this, WEAPON, RIFLE);
00121                         weapon->init();
00122                         this->addAspect(weapon);
00123                         break;
00124                 case (NET):
00125                         if(this->engine->options.enableNetworking && this->entityClass == SURFACE){
00126                                 netAspect = new NetAspect(this, NET);
00127                                 netAspect->init();
00128                                 this->addAspect(netAspect);
00129                         }
00130                         break;
00131                 }
00132                 //this->addAspect(asp);
00133         }
00134 
00135 
00136         this->gestationPeriod = this->engine->gameMgr->entTypeData[this->entityType].buildTime;
00137 
00138 }
00139 
00140 bool FastEcslent::Entity::sinking(double dt){
00141         this->timeLeftToDie -= dt;
00142         this->pos.y = -(-this->height  + (this->height * (this->gestationPeriod - this->timeLeftToDie)/this->gestationPeriod));
00143         if (this->timeLeftToDie <= 0.0) {
00144                 //this->entityState = FastEcslent::DEAD;
00145                 this->switchState(DEAD);
00146                 //this->pos.y = -2 * this->height;
00147                 this->pos.y = -100000.0;
00148                 return false;
00149         }
00150 
00151         return true;
00152 }
00153 
00154 bool FastEcslent::Entity::raising(double dt){
00155         this->timeLeftToBirth -= dt;
00156         this->pos.y = -this->height  + (this->height * (this->gestationPeriod - this->timeLeftToBirth)/this->gestationPeriod);
00157         if (this->timeLeftToBirth <= 0.0) {
00158                 this->switchState(ALIVE);
00159                 //this->entityState = FastEcslent::ALIVE;
00160                 //this->engine->entityMgr->addEntity(this);
00161                 this->pos.y = 0.0f;
00162                 return false;
00163         }
00164         return true;
00165 }
00166 
00167 void FastEcslent::Entity::addAspect(UnitAspect *asp) {
00168         aspects.push_back(asp);
00169 }
00170 
00171 FastEcslent::UnitAspect* FastEcslent::Entity::getAspect(UnitAspectType at) {
00172         for (std::deque<UnitAspect*>::iterator it = aspects.begin(); it != aspects.end(); it++){
00173                 if ((*it)->aspectType == at)
00174                         return (*it);
00175         }
00176         return 0;
00177 }
00178 
00179 void FastEcslent::Entity::removeAspect(UnitAspectType at) {
00180         int index = -1;
00181         for (std::deque<UnitAspect*>::iterator it = aspects.begin(); it != aspects.end(); it++){
00182                 index++;
00183                 if ((*it)->aspectType == at) {
00184                         break;
00185                 }
00186         }
00187         if(index >= 0 && index < (int) aspects.size()){
00188                 aspects.erase(aspects.begin()+index);
00189         }
00190 }
00191 
00192 void FastEcslent::Entity::tick(double dt){
00193         switch(this->entityState) {
00194         case GESTATING:
00195                 this->gestatingTick(dt);
00196                 break;
00197 
00198         case ALIVE:
00199                 this->aliveTick(dt);
00200                 break;
00201 
00202         case DYING:
00203                 //this->dyingTick(dt);
00204                 break;
00205 
00206         case DEAD:
00207                 //this->deadTick();
00208                 break;
00209 
00210         default:
00211 
00212                 break;
00213         }
00214 }
00215 
00216 void FastEcslent::Entity::switchState(EntityState newState){
00217         this->entityState = newState;
00218         if(newState == GESTATING) {
00219                 this->timeLeftToBirth = this->engine->gameMgr->entTypeData[this->entityType].buildTime;//this->gestationPeriod;
00220         } else if (newState == DYING) {
00221                 this->timeLeftToDie = this->engine->gameMgr->entTypeData[this->entityType].buildTime;//this->gestationPeriod;
00222         }
00223 }
00224 
00225 void FastEcslent::Entity::gestatingTick(double dt){
00226         boost::mutex::scoped_lock scoped_lock(entLock);
00227         if (!raising(dt)){ // entity rises up through the surface. Return true when done
00228                 this->switchState(ALIVE);
00229         }
00230 }
00231 
00232 void FastEcslent::Entity::aliveTick(double dt){
00233         DEBUG(std::cout << "Ent: " << this->uiname << " EntityState: " << this->entityState << std::endl;)
00234         boost::mutex::scoped_lock scoped_lock(entLock);
00235         for (std::deque<UnitAspect*>::iterator it = aspects.begin(); it != aspects.end(); it++){
00236                 (*it)->tick(dt);
00237         }
00238 }
00239 
00240 void FastEcslent::Entity::dyingTick(double dt){
00241         boost::mutex::scoped_lock scoped_lock(entLock);
00242         if (!sinking(dt)) { //entity sinks. Returns true when sunk below surface of water
00243                 this->switchState(DEAD);
00244         }
00245 }
00246 
00247 void FastEcslent::Entity::deadTick(double dt){
00248         return;
00249 }

Generated on Fri Dec 13 14:54:17 2013 for FastECSLent by  doxygen 1.5.4