group.cpp

Go to the documentation of this file.
00001 /*
00002  * group.cpp
00003  *
00004  *  Created on: Jan 28, 2012
00005  *      Author: sushil
00006  */
00007 
00008 
00009 //#include <ent.h>
00010 
00011 #include <engine.h>
00012 #include <group.h>
00013 #include <aspect.h>
00014 
00015 #include <boost/lexical_cast.hpp>
00016 #include "DEBUG.h"
00017 
00018 std::string FastEcslent::Group::createGroupName(Identity gid){
00019         std::string tmp = "Group:";
00020         tmp.append(boost::lexical_cast<std::string>(gid.id));
00021         return tmp;
00022 }
00023 
00024 void FastEcslent::Group::setName(std::string name){
00025         groupName = name;
00026 }
00027 
00028 void FastEcslent::Group::setMembers(Entity** ents, int nEntsInGroup){
00029         for (int i = 0; i < nEntsInGroup; i++){
00030                 members[i] = ents[i];
00031         }
00032         nEntitiesInGroup = nEntsInGroup;
00033 }
00034 
00035 
00036 void FastEcslent::Group::addMember(Entity* ent){
00037         members[nEntitiesInGroup++] = ent;
00038         //ent->aspects.clear(); maybe only for flock?
00039 
00040         DEBUG(std::cout << "\t\tAdded " << ent->uiname << " to group " << this->gid.id << std::endl;)
00041 }
00042 
00043 bool FastEcslent::Group::isMember(Entity *ent){
00044         for (int i = 0; i < this->nEntitiesInGroup; i++){
00045                 if(ent->entityId.id == this->members[i]->entityId.id){
00046                         return true;
00047                 }
00048         }
00049         return false;
00050 }
00051 
00052 void FastEcslent::Group::reset(Engine *eng){
00053         engine = eng;
00054         nEntitiesInGroup = 0;
00055         leaderIndex = -1;
00056         this->aspects.clear();
00057 }
00058 
00059 FastEcslent::Group::Group(Engine*eng, FastEcslent::Identity id){
00060         reset(eng);
00061 
00062         gid = id;
00063         groupName = createGroupName(id);
00064 }
00065 
00066 FastEcslent::Group::Group(Engine*eng, Entity** ents, int nEntsInGroup, Identity id){
00067         reset(eng);
00068         setMembers(ents, nEntsInGroup);// sets nEntitiesInGroup
00069         groupName = createGroupName(id);
00070         gid = id;
00071 }
00072 
00073 FastEcslent::Group::Group(Engine*eng, Entity** ents, int nEntsInGroup, std::string name, Identity id){
00074         reset(eng);
00075         setMembers(ents, nEntsInGroup);// sets nEntitiesInGroup
00076         groupName = name;
00077         gid = id;
00078 }
00079 
00080 void FastEcslent::Group::init(){
00081         for (std::deque<GroupAspect*>::iterator it = aspects.begin(); it != aspects.end(); it++){
00082                 (*it)->init();
00083         }
00084 }
00085 
00086 
00087 void FastEcslent::Group::tick(double dt){
00088         for (std::deque<GroupAspect*>::iterator it = aspects.begin(); it != aspects.end(); it++){
00089                 (*it)->tick(dt);
00090         }
00091 }
00092 
00093 void FastEcslent::Group::print(){
00094         DEBUG(std::cout << "Group: " << gid.id << std::endl;)
00095         for(int i = 0; i < nEntitiesInGroup; i++){
00096                 this->members[i]->print();
00097         }
00098 }
00099 
00100 FastEcslent::GroupAspect* FastEcslent::Group::getAspect(GroupAspectType gaspectType) {
00101         int n = findAspect(gaspectType);
00102         if (n >= 0){
00103                 return this->aspects[n];
00104         }else{
00105                 return NULL;
00106         }
00107 }
00108 
00109 void FastEcslent::Group::resetAspect(GroupAspectType gaspType, FastEcslent::GroupAspect* asp) {
00110         assert(gaspType < NGROUPASPECTTYPES);
00111         int n = findAspect(gaspType);
00112         if(n >= 0){
00113                 this->aspects[gaspType] = asp;
00114 
00115         } else {
00116                 addAspect(asp);
00117         }
00118         asp->init();
00119 }
00120 
00121 int FastEcslent::Group::findAspect(GroupAspectType gat){
00122         int index = 0;
00123         for (std::deque<GroupAspect*>::iterator it = aspects.begin(); it != aspects.end(); it++){
00124                 if(gat == (*it)->aspectType){
00125                         return index;
00126                 }
00127                 index++;
00128         }
00129         return -1;
00130 }
00131 
00132 
00133 
00134 void FastEcslent::Group::addAspect(FastEcslent::GroupAspect* asp) {
00135         this->aspects.push_back(asp);
00136         asp->init();
00137 }
00138 
00139 
00140 
00141 

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