flock.cpp

00001 #include "flock.h"
00002 #include "boid.h"
00003 #include "debug.h"
00004 #include "random.h"
00005 
00006 using namespace irr;
00007 using namespace core;
00008 using namespace scene;
00009 using namespace video;
00010 
00011 extern ITimer* timer;
00012 extern Debug* dbg;
00013 
00014 Flock::Flock(IrrlichtDevice* d,
00015         f32 size,
00016         const c8* boidMeshFile,
00017         ISceneNode* parent,
00018         s32 id,
00019         const vector3df& position)
00020 : ISceneNode(parent, d->getSceneManager(), id, position)
00021 {
00022     dbg->log("create Flock");
00023     device = d;
00024 
00025         box.reset(position);
00026 
00027         Random random(timer->getRealTime());
00028         for (s32 i=0; i<size; i++)
00029         {
00030                 vector3df pos(random.randfloat(-250, 250),
00031                         random.randfloat(10, 500),
00032                         random.randfloat(-250, 250) );
00033         addBoid(position + pos, boidMeshFile);
00034         }
00035 }
00036 
00037 Flock::~Flock()
00038 {
00039     //dtor
00040     removeBoids();
00041     device = NULL;
00042     dbg->log("Flock deleted");
00043 }
00044 
00045 void Flock::update(irr::u32 deltaTime)
00046 {
00047     list<Boid*>::Iterator it = boids.begin();
00048     box.reset(box.getCenter());
00049     for (; it != boids.end(); ++it)
00050     {
00051         (*it)->update(deltaTime);
00052         // update boundingbox, keep Boids relative position in mind.
00053         box.addInternalPoint((*it)->position - ISceneNode::getPosition());
00054     }
00055 }
00056 
00057 Boid* Flock::addBoid(vector3df pos, const c8* boidMeshFile)
00058 {
00059     Boid* b = new Boid(device, boidMeshFile);
00060     boids.push_back(b);
00061     b->position = pos;
00062     box.addInternalPoint(pos.X, pos.Y, pos.Z);
00063     return b;
00064 }
00065 
00066 void Flock::removeBoid(Boid* b)
00067 {
00068     list<Boid*>::Iterator it = boids.begin();
00069     for (; it != boids.end(); ++it)
00070     {
00071         if ((*it) == b)
00072         {
00073             delete (*it);
00074             boids.erase(it);
00075             return;
00076         }
00077     }
00078     dbg->log("Flock::removeBoid() cannot find Boid to delete");
00079 }
00080 
00081 void Flock::removeBoids()
00082 {
00083     list<Boid*>::Iterator it = boids.begin();
00084     for (; it != boids.end(); ++it)
00085     {
00086         delete (*it);
00087     }
00088     boids.clear();
00089 }
00090 
00091 s32 Flock::getBoidCount()
00092 {
00093     return boids.getSize();
00094 }
00095 
00096 list<Boid*>& Flock::getBoidList(void)
00097 {
00098     return boids;
00099 }
00100 
00101 void Flock::render()
00102 {
00103     IVideoDriver* driver = device->getVideoDriver();
00104     driver->setMaterial(material);
00105         //driver->setTransform(ETS_WORLD, AbsoluteTransformation);
00106         //driver->draw3DBox(box);
00107 }
00108 
00109 const aabbox3d<f32>& Flock::getBoundingBox() const
00110 {
00111         return box;
00112 }
00113 
00114 void Flock::OnPreRender()
00115 {
00116         if (IsVisible)
00117         {
00118                 SceneManager->registerNodeForRendering(this);
00119         }
00120         ISceneNode::OnPreRender();
00121 }
00122 
00123 video::SMaterial& Flock::getMaterial(s32 i)
00124 {
00125     if (i > 0)
00126     {
00127         dbg->log("Flock::getMaterial() cannot access material", i);
00128     }
00129         return material;
00130 }
00131 
00132 s32 Flock::getMaterialCount()
00133 {
00134         return 1;
00135 }
00136 
00137 void Flock::setScale(f32 scale)
00138 {
00139     list<Boid*>::Iterator it = boids.begin();
00140     for (; it != boids.end(); ++it)
00141     {
00142         (*it)->setScale(scale);
00143     }
00144 }
00145 
00146 void Flock::setSeparationWeight(s32 weight)
00147 {
00148     separationWeight = weight;
00149 }
00150 
00151 s32 Flock::getSeparationWeight()
00152 {
00153     return separationWeight;
00154 }
00155 
00156 void Flock::setAlignmentWeight(s32 weight)
00157 {
00158     alignmentWeight = weight;
00159 }
00160 
00161 s32 Flock::getAlignmentWeight()
00162 {
00163     return alignmentWeight;
00164 }
00165 
00166 void Flock::setCohesionWeight(s32 weight)
00167 {
00168     cohesionWeight = weight;
00169 }
00170 
00171 s32 Flock::getCohesionWeight()
00172 {
00173     return cohesionWeight;
00174 }
00175 
00176 void Flock::setNeighborRadius(s32 radius)
00177 {
00178     neighborRadius = radius;
00179 }
00180 
00181 s32 Flock::getNeighborRadius()
00182 {
00183     return neighborRadius;
00184 }
00185 
00186 void Flock::setTarget(vector3df target)
00187 {
00188     seekTarget = target;
00189 }
00190 
00191 vector3df Flock::getTarget()
00192 {
00193     return seekTarget;
00194 }
00195 
00196 void Flock::setSeekTargetWeight(s32 weight)
00197 {
00198     seekTargetWeight = weight;
00199 }
00200 
00201 s32 Flock::getSeekTargetWeight()
00202 {
00203     return seekTargetWeight;
00204 }
00205 
00206 void Flock::setTornadoWeight(s32 weight)
00207 {
00208     tornadoWeight = weight;
00209 }
00210 
00211 s32 Flock::getTornadoWeight()
00212 {
00213     return tornadoWeight;
00214 }

Generated on Sun Dec 2 17:09:57 2007 for Swarm by  doxygen 1.4.6-NO