random_racer/src/ItemDistributor.cpp

Go to the documentation of this file.
00001 #include "ItemDistributor.h"
00002 
00003 #include "CenterScreenDisplay.h"
00004 #include "VRSTerrainLoader.h"
00005 
00006 #include "vrsode/PhysicsManager.h"
00007 
00008 #include <vrs/cubeenvmirror.h>
00009 #include <vrs/opengl/shapematerialgl.h>
00010 #include <vrs/sg/scenething.h>
00011 
00012 
00013 namespace random_racer
00014 {
00015 
00016 ItemDistributor::ItemDistributor(
00017     VRS::SO<VRS::SceneThing> p_parent,
00018     VRS::SO<VRSTerrainLoader> p_loader,
00019     VRS::SO<CenterScreenDisplay> p_display)
00020 {
00021     m_loader = p_loader;
00022     m_parent = p_parent;
00023     m_maxItems = 100;
00024     m_display = p_display;
00025 
00026     m_enabled = true;
00027     m_score = 0;
00028     m_superCoinCreated = false;
00029 
00030     m_parent->append(new VRS::ShapeMaterialGL(
00031             VRS::Color(1.0, 1.0, 0.4), VRS::Color(1.0, 1.0, 1.0), 250));
00032 }
00033 
00034 ItemDistributor::~ItemDistributor()
00035 {
00036     removeAllItems();
00037 }
00038 
00039 void
00040 ItemDistributor::removeAllItems()
00041 {
00042     m_coins.clear();
00043 
00044     // while(m_coins.size())
00045     //     m_coins.pop_back();
00046 }
00047 
00048 void
00049 ItemDistributor::distributeItems(unsigned int p_count, VRS::Vector p_around,
00050         double p_radius)
00051 {
00052     // just return if we are diabled
00053     if(!m_enabled)
00054         return;
00055 
00056 
00057     unsigned int itemsStillToCreate = m_maxItems - m_coins.size();
00058     unsigned int itemsToCreateNow = 
00059         p_count < itemsStillToCreate ? p_count : itemsStillToCreate;
00060     unsigned int itemsToRelocate = p_count - itemsToCreateNow;
00061     
00062     VRS::Vector position;
00063     unsigned int value;
00064 
00065     for(unsigned int i = 0; i < itemsToCreateNow; i++)
00066     {
00067         if(!m_superCoinCreated && randomNumber() < 0.13)
00068         {
00069             m_superCoinCreated = true;
00070             value = 30;
00071         }
00072         else
00073             value = 1;
00074 
00075 
00076         position = randomPosition(p_radius, p_around);
00077         m_coins.push_back(new CollectableCoin(
00078                 new VRS::SceneThing(m_parent), position, value, this));
00079     }
00080 
00081     for(unsigned int i = 0; i < itemsToRelocate; i++)
00082     {
00083         position = randomPosition(p_radius, p_around);
00084         VRS::SO<CollectableCoin> coin = m_coins.front();
00085         m_coins.erase(m_coins.begin());
00086         coin->setPosition(position);
00087         m_coins.push_back(coin);
00088     }
00089 }
00090 
00091 VRS::Vector
00092 ItemDistributor::randomPosition(double p_radius, VRS::Vector p_basePosition)
00093 {
00094     VRS::Vector position = p_basePosition + VRS::Vector(
00095             p_radius * randomNumber(), 0, p_radius * randomNumber());
00096 
00097     position = m_loader->heightAt(position[0], position[2]);
00098     position[1] += 1.85;
00099 
00100     return position;
00101 }
00102 
00103 double
00104 ItemDistributor::randomNumber()
00105 {
00106     long    lMax         = powl(2, 31) - 1;
00107     double  randomNumber = (lMax / 2.0) - random();
00108     return (randomNumber / (lMax / 2.0));
00109 }
00110 
00111 void
00112 ItemDistributor::rotateItems(double p_radius)
00113 {
00114     std::list<VRS::SO<CollectableCoin> >::iterator it;
00115 
00116     for(it = m_coins.begin(); it != m_coins.end(); it++)
00117         if(*it)
00118             (*it)->rotateOn(p_radius);
00119 }
00120 
00121 void
00122 ItemDistributor::destroyItem(VRS::SO<CollectableCoin> p_coin)
00123 {
00124     std::list<VRS::SO<CollectableCoin> >::iterator it;
00125 
00126     for(it = m_coins.begin(); it != m_coins.end(); it++ )
00127         if(*it == p_coin)
00128         {
00129             m_coins.erase(it);
00130             break;
00131         }
00132 
00133     m_parent->remove(p_coin->sceneThing());
00134     p_coin->wantToDestroy();
00135 }
00136 
00137 void
00138 ItemDistributor::relocateItem(VRS::SO<CollectableCoin> p_coin)
00139 {
00140     std::list<VRS::SO<CollectableCoin> >::iterator it;
00141 
00142     for(it = m_coins.begin(); it != m_coins.end(); it++ )
00143         if(*it == p_coin)
00144         {
00145             m_coins.erase(it);
00146             break;
00147         }
00148 
00149     m_coins.push_back(p_coin);
00150 
00151     VRS::Vector position = randomPosition(170, p_coin->position());
00152     p_coin->setPosition(position);
00153 
00154     return;
00155 }
00156 
00157 void
00158 ItemDistributor::checkForCollisionsAndRotate(double p_angle)
00159 {
00160     // just return if disabled
00161     if(!m_enabled)
00162         return;
00163 
00164 
00165     std::list<VRS::SO<CollectableCoin> >::iterator it;
00166     std::string line2;
00167 
00168     for(it = m_coins.begin(); it != m_coins.end(); it++)
00169         if((*it)->didRecentlyCollide())
00170         {
00171             m_score += (*it)->points();
00172             
00173             if((*it)->points() > 1)
00174                 line2 = "Woohoo, you got the SUPER-COIN!!!11";
00175 
00176             else if(m_score == 23)
00177                 line2 = "OMG!";
00178 
00179             else if(m_score == 42)
00180                 line2 = "WTF?!";
00181 
00182 
00183             std::ostringstream s;
00184             s << m_score;
00185 
00186             m_display->showText(2000, s.str(), line2);
00187 
00188             vrsode::PhysicsManager::get()->lock();
00189                 relocateItem(*it);
00190 
00191             vrsode::PhysicsManager::get()->unlock();
00192         }
00193         else if(*it)
00194             (*it)->rotateOn(p_angle);
00195 }
00196 
00197 void
00198 ItemDistributor::setEnabled(bool p_enabled)
00199 {
00200     m_enabled = p_enabled;
00201 
00202     if(m_enabled)
00203         m_parent->setNodeState(
00204             VRS::SceneNode::EvaluationOn|VRS::SceneNode::TraversalOn);
00205     else
00206         m_parent->setNodeState(0);
00207 }
00208 
00209 } /* random_racer */

Generated on Fri May 11 21:01:57 2007 for Random Racer by  doxygen 1.5.1