src/StateBattle/Bullet.cpp

Go to the documentation of this file.
00001 /* Crown and Cutlass
00002  * Bullet Header
00003  */
00004 
00005 #include "../GLee.h"
00006 #include "../ccmath.h"
00007 #include "../Ship.h"
00008 #include "Bullet.h"
00009 
00010 #define BULLETSPEED .006
00011 #define BULLETGRAVITY -.000002
00012 
00013 GLuint Bullet::s_bulletList = 0;
00014 
00015 Bullet::Bullet(Ship *ship, float angleIn) {
00016   float shipX = cos(-1*ship->m_rot) * ship->m_speed;
00017   float shipZ = sin(-1*ship->m_rot) * ship->m_speed;
00018 
00019   m_owner = ship;
00020 
00021   m_location[0] = m_owner->m_x;
00022   m_location[1] = .01;
00023   m_location[2] = m_owner->m_z;
00024 
00025   m_vector[0] = cos(angleIn) * BULLETSPEED + shipX;
00026   m_vector[1] = .0013;
00027   m_vector[2] = sin(angleIn) * BULLETSPEED + shipZ;
00028 
00029   // This really should be ship->m_rot, not angleIn
00030   m_angle = radiansToDegrees(m_owner->m_rot);
00031 }
00032 
00033 Bullet::~Bullet() {
00034 }
00035 
00036 void Bullet::Update(unsigned int ticks) {
00037   m_location[0] += m_vector[0] * ticks;
00038   m_location[1] += m_vector[1] * ticks;
00039   m_location[2] += m_vector[2] * ticks;
00040 
00041   m_vector[1] += BULLETGRAVITY * ticks;
00042 }
00043 
00044 bool Bullet::CheckCollision(Ship *ship) {
00045   if ((m_location[0]-ship->m_x)*(m_location[0]-ship->m_x) + (m_location[2]-ship->m_z)*(m_location[2]-ship->m_z) <= 1) {
00046     return true;
00047   } else {
00048     return false;
00049   }
00050 }
00051 
00052 void Bullet::Draw() {
00053   glPushMatrix();
00054   glTranslatef(m_location[0], m_location[1], m_location[2]);
00055   glRotatef(m_angle, 0, 1, 0);
00056   glCallList(s_bulletList);
00057   glPopMatrix();
00058 }
00059 
00060 void Bullet::GenerateList() {
00061   // Generate the bullet list
00062   s_bulletList = glGenLists(1);
00063   if (s_bulletList == 0) {
00064     // Should throw an exception
00065     return;
00066   }
00067 
00068   glNewList(s_bulletList, GL_COMPILE);
00069 
00070   glPushMatrix();
00071   DrawBullet();
00072   glTranslatef(0.3, 0, 0);
00073   DrawBullet();
00074   glTranslatef(-0.6, 0, 0);
00075   DrawBullet();
00076   glPopMatrix();
00077 
00078   glEndList();
00079 }
00080 
00081 void Bullet::DeleteList() {
00082   glDeleteLists(s_bulletList, 1);
00083 }
00084 
00085 void Bullet::DrawBullet() {
00086   glBegin(GL_TRIANGLES);
00087   glNormal3f(1, 0, 0);
00088   glVertex3f(.1, 0, 0);
00089   glNormal3f(0, 1, 0);
00090   glVertex3f(0, .1, 0);
00091   glNormal3f(0, 0, 1);
00092   glVertex3f(0, 0, .1);
00093 
00094   glNormal3f(0, 1, 0);
00095   glVertex3f(0, .1, 0);
00096   glNormal3f(-1, 0, 0);
00097   glVertex3f(-.1, 0, 0);
00098   glNormal3f(0, 0, 1);
00099   glVertex3f(0, 0, .1);
00100 
00101   glNormal3f(0, 1, 0);
00102   glVertex3f(0, .1, 0);
00103   glNormal3f(0, 0, -1);
00104   glVertex3f(0, 0, -.1);
00105   glNormal3f(-1, 0, 0);
00106   glVertex3f(-.1, 0, 0);
00107 
00108   glNormal3f(0, 1, 0);
00109   glVertex3f(0, .1, 0);
00110   glNormal3f(1, 0, 0);
00111   glVertex3f(.1, 0, 0);
00112   glNormal3f(0, 0, -1);
00113   glVertex3f(0, 0, -.1);
00114 
00115   glNormal3f(1, 0, 0);
00116   glVertex3f(.1, 0, 0);
00117   glNormal3f(0, 0, 1);
00118   glVertex3f(0, 0, .1);
00119   glNormal3f(0, -1, 0);
00120   glVertex3f(0, -.1, 0);
00121 
00122   glNormal3f(0, -1, 0);
00123   glVertex3f(0, -.1, 0);
00124   glNormal3f(0, 0, 1);
00125   glVertex3f(0, 0, .1);
00126   glNormal3f(-1, 0, 0);
00127   glVertex3f(-.1, 0, 0);
00128 
00129   glNormal3f(0, -1, 0);
00130   glVertex3f(0, -.1, 0);
00131   glNormal3f(-1, 0, 0);
00132   glVertex3f(-.1, 0, 0);
00133   glNormal3f(0, 0, -1);
00134   glVertex3f(0, 0, -.1);
00135 
00136   glNormal3f(0, -1, 0);
00137   glVertex3f(0, -.1, 0);
00138   glNormal3f(0, 0, -1);
00139   glVertex3f(0, 0, -.1);
00140   glNormal3f(1, 0, 0);
00141   glVertex3f(.1, 0, 0);
00142   glEnd();
00143 }
00144 
00145 float Bullet::GetBulletSpeed() {
00146   return BULLETSPEED;
00147 }

Generated on Mon Jan 8 22:34:12 2007 for CrownandCutlass by  doxygen 1.4.7