00001 
00002 
00003 
00004 
00005 
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         
00036         
00037         
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         
00054         
00055         selectable = false;
00056 
00057         turningRadius = 10000;
00058 
00059         
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 
00072 
00073 
00074 
00075 
00076 
00077 
00078 
00079         aspects.clear();
00080 
00081 }
00082 
00083 FastEcslent::Entity::Entity(Engine *eng, EntityType entType) {
00084 
00085         engine = eng;
00086         
00087         entityType = entType;
00088         meshName = "cigarette.mesh";
00089         uiname = "Cigarette";
00090         
00091         reset();
00092 }
00093 
00094 
00095 
00096 
00097 
00098 
00099 
00100 
00101 
00102 
00103 
00104 
00105 void FastEcslent::Entity::init(){
00106 
00107         for (int aType = 0; aType < NASPECTTYPES; aType++){ 
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                 }
00125                 
00126         }
00127 
00128 
00129         this->gestationPeriod = this->engine->gameMgr->entTypeData[this->entityType].buildTime;
00130 
00131 }
00132 
00133 bool FastEcslent::Entity::sinking(double dt){
00134         this->timeLeftToDie -= dt;
00135         this->pos.y = -(-this->height  + (this->height * (this->gestationPeriod - this->timeLeftToDie)/this->gestationPeriod));
00136         if (this->timeLeftToDie <= 0.0) {
00137                 
00138                 this->switchState(DEAD);
00139                 
00140                 this->pos.y = -100000.0;
00141                 return false;
00142         }
00143 
00144         return true;
00145 }
00146 
00147 bool FastEcslent::Entity::raising(double dt){
00148         this->timeLeftToBirth -= dt;
00149         this->pos.y = -this->height  + (this->height * (this->gestationPeriod - this->timeLeftToBirth)/this->gestationPeriod);
00150         if (this->timeLeftToBirth <= 0.0) {
00151                 this->switchState(ALIVE);
00152                 
00153                 
00154                 this->pos.y = 0.0f;
00155                 return false;
00156         }
00157         return true;
00158 }
00159 
00160 void FastEcslent::Entity::addAspect(UnitAspect *asp) {
00161         aspects.push_back(asp);
00162 }
00163 
00164 FastEcslent::UnitAspect* FastEcslent::Entity::getAspect(UnitAspectType at) {
00165         for (std::deque<UnitAspect*>::iterator it = aspects.begin(); it != aspects.end(); it++){
00166                 if ((*it)->aspectType == at)
00167                         return (*it);
00168         }
00169         return 0;
00170 }
00171 
00172 void FastEcslent::Entity::removeAspect(UnitAspectType at) {
00173         int index = -1;
00174         for (std::deque<UnitAspect*>::iterator it = aspects.begin(); it != aspects.end(); it++){
00175                 index++;
00176                 if ((*it)->aspectType == at) {
00177                         break;
00178                 }
00179         }
00180         if(index >= 0 && index < (int) aspects.size()){
00181                 aspects.erase(aspects.begin()+index);
00182         }
00183 }
00184 
00185 void FastEcslent::Entity::tick(double dt){
00186         switch(this->entityState) {
00187         case GESTATING:
00188                 this->gestatingTick(dt);
00189                 break;
00190 
00191         case ALIVE:
00192                 this->aliveTick(dt);
00193                 break;
00194 
00195         case DYING:
00196                 
00197                 break;
00198 
00199         case DEAD:
00200                 
00201                 break;
00202 
00203         default:
00204 
00205                 break;
00206         }
00207 }
00208 
00209 void FastEcslent::Entity::switchState(EntityState newState){
00210         this->entityState = newState;
00211         if(newState == GESTATING) {
00212                 this->timeLeftToBirth = this->engine->gameMgr->entTypeData[this->entityType].buildTime;
00213         } else if (newState == DYING) {
00214                 this->timeLeftToDie = this->engine->gameMgr->entTypeData[this->entityType].buildTime;
00215         }
00216 }
00217 
00218 void FastEcslent::Entity::gestatingTick(double dt){
00219         boost::mutex::scoped_lock scoped_lock(entLock);
00220         if (!raising(dt)){ 
00221                 this->switchState(ALIVE);
00222         }
00223 }
00224 
00225 void FastEcslent::Entity::aliveTick(double dt){
00226         DEBUG(std::cout << "Ent: " << this->uiname << " EntityState: " << this->entityState << std::endl;)
00227         boost::mutex::scoped_lock scoped_lock(entLock);
00228         for (std::deque<UnitAspect*>::iterator it = aspects.begin(); it != aspects.end(); it++){
00229                 (*it)->tick(dt);
00230         }
00231 }
00232 
00233 void FastEcslent::Entity::dyingTick(double dt){
00234         boost::mutex::scoped_lock scoped_lock(entLock);
00235         if (!sinking(dt)) { 
00236                 this->switchState(DEAD);
00237         }
00238 }
00239 
00240 void FastEcslent::Entity::deadTick(double dt){
00241         return;
00242 }