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 void FastEcslent::Entity::reset() {
00034     //FastEcslent::Entity::tick = &FastEcslent::Entity::gestatingTick;
00035     //this->tick = &FastEcslent::Entity::gestatingTick;
00036     //tic = &FastEcslent::Entity::gestatingTick;
00037     pos = Ogre::Vector3(0.0f, 0.0f, 0.0f);
00038     vel = Ogre::Vector3(0.0f, 0.0f, 0.0f);
00039     acc = Ogre::Vector3(0.0f, 0.0f, 0.0f);
00040     maxSpeed = 40.0f;
00041     minSpeed = -10.0f;
00042     speedRange = maxSpeed - minSpeed;
00043     rot = Ogre::Quaternion(Ogre::Radian(0.0f), Ogre::Vector3::UNIT_Y);
00044     maxAcceleration = 1.0f;
00045 
00046     yaw = 0.0f;
00047     maxRotationalSpeed = 0.1;
00048     desiredSpeed = 0.0f;
00049     desiredHeading = 0.0f;
00050     speed = 0.0f;
00051     heading = 0.0f;
00052     //entityType = CIGARETTE;
00053     //alive = true;
00054     selectable = false;
00055 
00056     turningRadius = 10000;
00057 
00058     //selection
00059     isSelected = false;
00060     
00061     //selection sound
00062     didSelectSoundPlayed = false;
00063     //newborn sound
00064     isNewBorn = false;
00065 
00066     entityId.side = BATTLEMASTER;
00067     entityId.player = ONE;
00068     entityClass = SURFACE;
00069     entityState = ALIVE;
00070     gestationPeriod = 10.0;
00071     timeLeftToBirth = 10.0;
00072     timeLeftToDie = 10.0;
00073 
00074     /*
00075     preReqs.neededResources.gas = 0;
00076     preReqs.neededResources.minerals = 50;
00077     preReqs.neededResources.supply = 1;
00078 
00079     preReqs.neededTypes[0] = BARRACKS;
00080     preReqs.nNeededTypes = 1;
00081      */
00082 
00083     aspects.clear();
00084 
00085 }
00086 
00087 FastEcslent::Entity::Entity(Engine *eng, EntityType entType):builder(NULL) {
00088 
00089         engine = eng;
00090         //entityId.id = id;
00091         entityType = entType;
00092         entityClass = SURFACE;
00093         meshName = "cigarette.mesh";
00094         uiname = "Cigarette";
00095         //uiname.append(boost::lexical_cast<std::string>(count++));
00096         reset();
00097 
00098         init();
00099 }
00100 
00101 
00102 
00103 
00104 
00105 
00106 // There should only be one aspect of each type
00107 
00108 /*int FastEcslent::Entity::addAspect(Aspect *asp) {
00109         aspects[nAspects++] = asp;
00110         return nAspects - 1;
00111 }*/
00112 void FastEcslent::Entity::init() {
00113 
00114         for (int aType = 0; aType < NASPECTTYPES; aType++){ // the problem with enums in C++n
00115                 switch (aType){
00116                 case (PHYSICS):
00117                         physics = new Physics2D2(this, PHYSICS);
00118                         physics->init();
00119                         this->addAspect(physics);
00120                         break;
00121                 case (UNITAI):
00122                         ai = new UnitAI(this, UNITAI);
00123                         ai->init();
00124                     this->addAspect(ai);
00125                         break;
00126                 case (WEAPON):
00127                         weapon = new Weapon(this, WEAPON);
00128                         this->addAspect(weapon);
00129                         break;
00130                 case (NET):
00131                         if(this->engine->options.enableNetworking){
00132                                 netAspect = new NetAspect(this, NET);
00133                                 netAspect->init();
00134                                 this->addAspect(netAspect);
00135                         }
00136                         break;
00137                 }
00138                 //this->addAspect(asp);
00139         }
00140 
00141 
00142     this->gestationPeriod = this->engine->gameMgr->entTypeData[this->entityType].buildTime;
00143 
00144 }
00145 
00146 bool FastEcslent::Entity::sinking(double dt) {
00147     this->timeLeftToDie -= dt;
00148     int dyingPeriod = 5;  //all units dying with the same speed.
00149     this->pos.y = -(-this->height + (this->height * (dyingPeriod - this->timeLeftToDie) / dyingPeriod));
00150     if (this->timeLeftToDie <= 0.0) {
00151         //this->entityState = FastEcslent::DEAD;
00152         this->switchState(DEAD);
00153         //this->pos.y = -2 * this->height;
00154         this->pos.y = -100000.0;
00155         return false;
00156     }
00157 
00158     return true;
00159 }
00160 
00161 bool FastEcslent::Entity::raising(double dt) {
00162     this->timeLeftToBirth -= dt;
00163     this->pos.y = -this->height + (this->height * (this->gestationPeriod - this->timeLeftToBirth) / this->gestationPeriod);
00164     if (this->timeLeftToBirth <= 0.0) {
00165         this->switchState(ALIVE);
00166         //this->entityState = FastEcslent::ALIVE;
00167         //this->engine->entityMgr->addEntity(this);
00168         this->pos.y = 0.0f;
00169         return false;
00170     }
00171     return true;
00172 }
00173 
00174 void FastEcslent::Entity::addAspect(UnitAspect *asp) {
00175     aspects.push_back(asp);
00176 }
00177 
00178 FastEcslent::UnitAspect* FastEcslent::Entity::getAspect(UnitAspectType at) {
00179     for (std::deque<UnitAspect*>::iterator it = aspects.begin(); it != aspects.end(); it++) {
00180         if ((*it)->aspectType == at)
00181             return (*it);
00182     }
00183     return 0;
00184 }
00185 
00186 void FastEcslent::Entity::removeAspect(UnitAspectType at) {
00187     int index = -1;
00188     for (std::deque<UnitAspect*>::iterator it = aspects.begin(); it != aspects.end(); it++) {
00189         index++;
00190         if ((*it)->aspectType == at) {
00191             break;
00192         }
00193     }
00194     if (index >= 0 && index < (int) aspects.size()) {
00195         aspects.erase(aspects.begin() + index);
00196     }
00197 }
00198 
00199 void FastEcslent::Entity::tick(double dt) {
00200     switch (this->entityState) {
00201         case GESTATING:
00202             this->gestatingTick(dt);
00203             break;
00204 
00205         case ALIVE:
00206             this->aliveTick(dt);
00207             break;
00208 
00209         case DYING:
00210                 this->dyingTick(dt);
00211                 break;
00212 
00213         case DEAD:
00214             //this->deadTick();
00215             break;
00216 
00217         default:
00218 
00219             break;
00220     }
00221 }
00222 
00223 void FastEcslent::Entity::switchState(EntityState newState) {
00224     this->entityState = newState;
00225     if (newState == GESTATING) {
00226         this->timeLeftToBirth = this->engine->gameMgr->entTypeData[this->entityType].buildTime; //this->gestationPeriod;
00227     } else if (newState == DYING) {
00228         this->timeLeftToDie = this->engine->gameMgr->entTypeData[this->entityType].buildTime; //this->gestationPeriod;
00229         this->selectable = false;
00230         this->isSelected = false;
00231         this->engine->selectionMgr->unselect(this);
00232     }
00233 }
00234 
00235 void FastEcslent::Entity::gestatingTick(double dt) {
00236     boost::mutex::scoped_lock scoped_lock(entLock);
00237     if (!raising(dt)) { // entity rises up through the surface. Return true when done
00238         this->switchState(ALIVE);
00239         //this->init();
00240         this->isNewBorn = true;
00241     }
00242 }
00243 
00244 void FastEcslent::Entity::aliveTick(double dt) {
00245     DEBUG(std::cout << "Ent: " << this->uiname << " EntityState: " << this->entityState << std::endl;)
00246     boost::mutex::scoped_lock scoped_lock(entLock);
00247     for (std::deque<UnitAspect*>::iterator it = aspects.begin(); it != aspects.end(); it++) {
00248         (*it)->tick(dt);
00249     }
00250 }
00251 
00252 void FastEcslent::Entity::dyingTick(double dt) {
00253     boost::mutex::scoped_lock scoped_lock(entLock);
00254     if (!sinking(dt)) { //entity sinks. Returns true when sunk below surface of water
00255         this->switchState(DEAD);
00256     }
00257 }
00258 
00259 void FastEcslent::Entity::deadTick(double dt) {
00260     return;
00261 }
00262 
00263 void FastEcslent::SCVehicle::init() {
00264     builder = new Builder(this,BUILDER, Ogre::Vector3(200,0,200),Ogre::Vector3(200,0,200));
00265     builder->init();
00266     this->addAspect(builder);
00267     //builder->buildableEntities[BARRACKS]   = true;
00268     
00269     //Entity::init();
00270 
00271 }

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