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         entityClass = SURFACE;
00089         meshName = "cigarette.mesh";
00090         uiname = "Cigarette";
00091         //uiname.append(boost::lexical_cast<std::string>(count++));
00092         reset();
00093 
00094         init();
00095 }
00096 
00097 
00098 
00099 
00100 
00101 
00102 // There should only be one aspect of each type
00103 
00104 /*int FastEcslent::Entity::addAspect(Aspect *asp) {
00105         aspects[nAspects++] = asp;
00106         return nAspects - 1;
00107 }*/
00108 void FastEcslent::Entity::init(){
00109 
00110         for (int aType = 0; aType < NASPECTTYPES; aType++){ // the problem with enums in C++n
00111                 switch (aType){
00112                 case (PHYSICS):
00113                         physics = new Physics2D2(this, PHYSICS);
00114                         physics->init();
00115                         this->addAspect(physics);
00116                         break;
00117                 case (UNITAI):
00118                         ai = new UnitAI(this, UNITAI);
00119                         ai->init();
00120                     this->addAspect(ai);
00121                         break;
00122                 case (WEAPON):
00123                         weapon = new Weapon(this, WEAPON);
00124                         this->addAspect(weapon);
00125                         break;
00126                 case (NET):
00127                         if(this->engine->options.enableNetworking && this->entityClass == SURFACE){
00128                                 netAspect = new NetAspect(this, NET);
00129                                 netAspect->init();
00130                                 this->addAspect(netAspect);
00131                         }
00132                         break;
00133                 }
00134                 //this->addAspect(asp);
00135         }
00136 
00137 
00138         this->gestationPeriod = this->engine->gameMgr->entTypeData[this->entityType].buildTime;
00139 
00140 }
00141 
00142 bool FastEcslent::Entity::sinking(double dt){
00143         this->timeLeftToDie -= dt;
00144         this->pos.y = -(-this->height  + (this->height * (this->gestationPeriod - this->timeLeftToDie)/this->gestationPeriod));
00145         if (this->timeLeftToDie <= 0.0) {
00146                 //this->entityState = FastEcslent::DEAD;
00147                 this->switchState(DEAD);
00148                 //this->pos.y = -2 * this->height;
00149                 this->pos.y = -100000.0;
00150                 return false;
00151         }
00152 
00153         return true;
00154 }
00155 
00156 bool FastEcslent::Entity::raising(double dt){
00157         this->timeLeftToBirth -= dt;
00158         this->pos.y = -this->height  + (this->height * (this->gestationPeriod - this->timeLeftToBirth)/this->gestationPeriod);
00159         if (this->timeLeftToBirth <= 0.0) {
00160                 this->switchState(ALIVE);
00161                 //this->entityState = FastEcslent::ALIVE;
00162                 //this->engine->entityMgr->addEntity(this);
00163                 this->pos.y = 0.0f;
00164                 return false;
00165         }
00166         return true;
00167 }
00168 
00169 void FastEcslent::Entity::addAspect(UnitAspect *asp) {
00170         aspects.push_back(asp);
00171 }
00172 
00173 FastEcslent::UnitAspect* FastEcslent::Entity::getAspect(UnitAspectType at) {
00174         for (std::deque<UnitAspect*>::iterator it = aspects.begin(); it != aspects.end(); it++){
00175                 if ((*it)->aspectType == at)
00176                         return (*it);
00177         }
00178         return 0;
00179 }
00180 
00181 void FastEcslent::Entity::removeAspect(UnitAspectType at) {
00182         int index = -1;
00183         for (std::deque<UnitAspect*>::iterator it = aspects.begin(); it != aspects.end(); it++){
00184                 index++;
00185                 if ((*it)->aspectType == at) {
00186                         break;
00187                 }
00188         }
00189         if(index >= 0 && index < (int) aspects.size()){
00190                 aspects.erase(aspects.begin()+index);
00191         }
00192 }
00193 
00194 void FastEcslent::Entity::tick(double dt){
00195         switch(this->entityState) {
00196         case GESTATING:
00197                 this->gestatingTick(dt);
00198                 break;
00199 
00200         case ALIVE:
00201                 this->aliveTick(dt);
00202                 break;
00203 
00204         case DYING:
00205                 this->dyingTick(dt);
00206                 break;
00207 
00208         case DEAD:
00209                 //this->deadTick();
00210                 break;
00211 
00212         default:
00213 
00214                 break;
00215         }
00216 }
00217 
00218 void FastEcslent::Entity::switchState(EntityState newState){
00219         this->entityState = newState;
00220         if(newState == GESTATING) {
00221                 this->timeLeftToBirth = this->engine->gameMgr->entTypeData[this->entityType].buildTime;//this->gestationPeriod;
00222         } else if (newState == DYING) {
00223                 this->timeLeftToDie = this->engine->gameMgr->entTypeData[this->entityType].buildTime;//this->gestationPeriod;
00224         }
00225 }
00226 
00227 void FastEcslent::Entity::gestatingTick(double dt){
00228         boost::mutex::scoped_lock scoped_lock(entLock);
00229         if (!raising(dt)){ // entity rises up through the surface. Return true when done
00230                 this->switchState(ALIVE);
00231         }
00232 }
00233 
00234 void FastEcslent::Entity::aliveTick(double dt){
00235         DEBUG(std::cout << "Ent: " << this->uiname << " EntityState: " << this->entityState << std::endl;)
00236         boost::mutex::scoped_lock scoped_lock(entLock);
00237         for (std::deque<UnitAspect*>::iterator it = aspects.begin(); it != aspects.end(); it++){
00238                 (*it)->tick(dt);
00239         }
00240 }
00241 
00242 void FastEcslent::Entity::dyingTick(double dt){
00243         boost::mutex::scoped_lock scoped_lock(entLock);
00244         if (!sinking(dt)) { //entity sinks. Returns true when sunk below surface of water
00245                 this->switchState(DEAD);
00246         }
00247 }
00248 
00249 void FastEcslent::Entity::deadTick(double dt){
00250         return;
00251 }

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