Main Page   Modules   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   Related Pages  

/home/rdbrown/doxygen/cse125g1/src/modules/arrows/Arrows.cpp

00001 #include <stdlib.h>
00002 #include <iostream>
00003 #include <list>
00004 #include "Arrows.h"
00005 #include "net/NetArrow.h"
00006 #include "audio/audio.h"
00007 
00008 #define GRAVITY -25.8
00009 
00010 extern MMAudio::Source *arrowSources;
00011 extern std::list<int> freeArrowSources;
00012 static unsigned int soundname;
00013 static MMNet::Game *game;
00014 
00015 using namespace std;
00016 
00017 void ArrowSystem::DrawTarget(Vector3 pos, const Vector3 &vel, TNL::U32 chargeTime) {
00018         MMNet::NetArrow na;
00019         Vector3 lastPos;
00020         na.Init(pos, vel, chargeTime, NULL);
00021 
00022         GroundY = 0 ;
00023         float deltaTime = .01f;
00024 
00025         glEnable(GL_BLEND);
00026         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00027         float opacity = 0.9f;
00028 
00029         glLineStipple(1, 0xAAAA);
00030         glEnable(GL_LINE_STIPPLE);
00031         glBegin(GL_LINES);
00032         while (pos.y > GroundY)
00033         {
00034                 glColor4f(1.0, 1.0, 1.0, opacity);
00035                 if (opacity > .1)
00036                         opacity -= deltaTime/2.0;
00037                 
00038                 na.Update(deltaTime);
00039 
00040                 pos = na.getPos();
00041                 lastPos = na.getLastPos();
00042                 glVertex3f(lastPos.x, lastPos.y, lastPos.z);
00043                 glVertex3f(pos.x, pos.y, pos.z);
00044                 
00045         }
00046         glEnd();
00047         glDisable(GL_LINE_STIPPLE);
00048         glDisable(GL_BLEND);
00049 }
00050 
00051 
00052 
00053 
00054 
00055 /************************
00056  *  ARROW CLASS
00057  ************************/
00058 static const Vector3 gravity(0.0, -9.8, 0.0);
00059 
00060 void Arrow::Init(MMNet::Game *gm, MMNet::NetArrow *na, float r, float g, float b) {
00061         game = gm;
00062         netArrow = na;
00063         Position = netArrow->getPos();
00064         LastPosition.Set(Position.x-.01,Position.y-.01,Position.z-.01);
00065         Color.Set(r,g,b);
00066         immuneToForces = true;
00067 
00068         Velocity = netArrow->getVel();
00069         Force.Zero();
00070 
00071         Mass = netArrow->getMass();;
00072 }
00073 
00074 
00075 void Arrow::draw() {
00076         Vector3 normalVelocity;
00077         glColor3f( 139/255.0f, 69/255.0f,19/255.0f);
00078         Vector3 pos = netArrow->getPos();
00079         glVertex3f(pos.x, pos.y, pos.z);
00080         //if (immuneToForces)
00081         //      glColor3f( 0.0, 1.0, 0.0);//, P[i].Color.y, P[i].Color.z);
00082         //else
00083                 glColor3f(      205/255.0f, 133/255.0f, 63/255.0f);
00084         normalVelocity = netArrow->getVel();
00085         normalVelocity.Normalize();
00086         normalVelocity.Scale(5.0);
00087         glVertex3f(pos.x-normalVelocity.x, 
00088     pos.y-normalVelocity.y, 
00089     pos.z-normalVelocity.z);
00090 }
00091 
00092 void Arrow::ReverseUpdate(float deltaTime) {
00093         // Compute acceleration (Newton’s second law)
00094         Vector3 Accel= Vector3( (1.0/Mass) * Force.x,
00095                                                         (1.0/Mass) * Force.y,
00096                                                         (1.0/Mass) * Force.z);
00097 
00098         Position.x -= Velocity.x * deltaTime;
00099         Position.y -= Velocity.y * deltaTime;
00100         Position.z -= Velocity.z * deltaTime;
00101 
00102         // Compute new position & velocity
00103         Velocity.x -= Accel.x * deltaTime;
00104         Velocity.y -= Accel.y * deltaTime;
00105         Velocity.z -= Accel.z * deltaTime;
00106         Force.Zero();
00107 }
00108 
00109 float Arrow::getSquaredDistanceFromPlayer(void) const {
00110         const Vector3 &playerpos = game->myPlayer->world.d;
00111         const Vector3 &mypos = netArrow->getPos();
00112         float t = 0;
00113         float c = mypos.x - playerpos.x;
00114         t += c*c;
00115         c = mypos.y - playerpos.y;
00116         t += c*c;
00117         c = mypos.z - playerpos.z;
00118         t += c*c;
00119         return c;
00120 }
00121 
00122 void Arrow::update(float deltaTime) {
00123         try {
00124         if (sourceno >= 0) {
00125                 if (netArrow->getImmuneToForces()) {// || getSquaredDistanceFromPlayer() > 3.0) {
00126                         arrowSources[sourceno].stop();
00127                         if (netArrow->getImmuneToForces()) {
00128                                 arrowSources[sourceno].setGain(10);
00129                                 arrowSources[sourceno].setRolloff(.5);
00130                                 arrowSources[sourceno].playSound(1);
00131                                 freeArrowSources.push_back(sourceno);
00132                         }
00133                         /*
00134                         for (std::list<int>::iterator i = freeArrowSources.begin();
00135                                  i != freeArrowSources.end(); i++)
00136                         */
00137                         sourceno = -1;
00138                 } else {
00139                         arrowSources[sourceno].setPosition(netArrow->getPos());
00140                 }
00141         } else if (!netArrow->getImmuneToForces()) {
00142                 if (!freeArrowSources.empty()) {
00143                         if (immuneToForces) { // just fired
00144                                 sourceno = freeArrowSources.front();
00145                                 freeArrowSources.pop_front();
00146                                 freeArrowSources.push_back(sourceno);
00147                                 arrowSources[sourceno].setGain(1);
00148                                 arrowSources[sourceno].setRolloff(2);
00149                                 arrowSources[sourceno].setPosition(netArrow->getPos());
00150                                 arrowSources[sourceno].playSound(2);
00151                                 sourceno = -1;
00152                         }
00153                         if (getSquaredDistanceFromPlayer() < 9.0) {
00154                                 sourceno = freeArrowSources.front();
00155                                 freeArrowSources.pop_front();;
00156                                 arrowSources[sourceno].setGain(0.6);
00157                                 arrowSources[sourceno].setRolloff(4);
00158                                 arrowSources[sourceno].setPosition(netArrow->getPos());
00159                                 arrowSources[sourceno].loopSound(soundname);
00160                                 /*
00161                                 for (std::list<int>::iterator i = freeArrowSources.begin();
00162                                          i != freeArrowSources.end(); i++)
00163                                 */
00164                         }
00165                 }
00166         }
00167         } catch (AudioException ex) { }
00168         immuneToForces = netArrow->getImmuneToForces();
00169         return;
00170 }
00171 
00172 
00173 // These functions are provided for compatibility w/ base class
00174 const float 
00175 Arrow::getTop() const {
00176         return -1.0;
00177 }
00178 
00179 const float 
00180 Arrow::getBottom() const {
00181         return -1.0;
00182 }
00183 
00184 const float 
00185 Arrow::getRadius() const {
00186         return -1.0;
00187 }
00188 
00189 
00190 void setFlyingArrowSound(unsigned int sname) {
00191         soundname = sname;
00192 }
00193 

Generated on Thu Aug 18 16:03:09 2005 for Robin Hood: Thieves & Knights by doxygen1.2.18