| [4744] | 1 | /* | 
|---|
| [1853] | 2 |    orxonox - the future of 3D-vertical-scrollers | 
|---|
 | 3 |  | 
|---|
 | 4 |    Copyright (C) 2004 orx | 
|---|
 | 5 |  | 
|---|
 | 6 |    This program is free software; you can redistribute it and/or modify | 
|---|
 | 7 |    it under the terms of the GNU General Public License as published by | 
|---|
 | 8 |    the Free Software Foundation; either version 2, or (at your option) | 
|---|
 | 9 |    any later version. | 
|---|
| [1855] | 10 |  | 
|---|
 | 11 |    ### File Specific: | 
|---|
| [4779] | 12 |    main-programmer: Benjamin Grauer | 
|---|
| [1855] | 13 |    co-programmer: ... | 
|---|
| [1853] | 14 | */ | 
|---|
 | 15 |  | 
|---|
| [5357] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON | 
|---|
| [1853] | 17 |  | 
|---|
| [5556] | 18 | #include "aim.h" | 
|---|
| [4781] | 19 |  | 
|---|
| [4832] | 20 | #include "load_param.h" | 
|---|
| [4781] | 21 | #include "graphics_engine.h" | 
|---|
 | 22 | #include "state.h" | 
|---|
| [4832] | 23 | #include "material.h" | 
|---|
| [5750] | 24 | #include "t_animation.h" | 
|---|
 | 25 | #include "text.h" | 
|---|
| [4781] | 26 |  | 
|---|
| [5750] | 27 | #include "world_entity.h" | 
|---|
 | 28 |  | 
|---|
| [1856] | 29 | using namespace std; | 
|---|
| [1853] | 30 |  | 
|---|
| [1856] | 31 |  | 
|---|
| [3245] | 32 | /** | 
|---|
| [4832] | 33 |  * standart constructor | 
|---|
 | 34 |  */ | 
|---|
| [5750] | 35 | Aim::Aim (PNode* source, const TiXmlElement* root) | 
|---|
| [4832] | 36 | { | 
|---|
 | 37 |   this->init(); | 
|---|
 | 38 |  | 
|---|
| [5750] | 39 |   this->source = source; | 
|---|
 | 40 |  | 
|---|
| [4832] | 41 |   if (root) | 
|---|
 | 42 |     this->loadParams(root); | 
|---|
 | 43 |   else | 
|---|
 | 44 |     this->setTexture("maps/aim.png"); | 
|---|
 | 45 | } | 
|---|
 | 46 |  | 
|---|
 | 47 | /** | 
|---|
| [5556] | 48 |  * destroys a Aim | 
|---|
| [3245] | 49 | */ | 
|---|
| [5556] | 50 | Aim::~Aim () | 
|---|
| [3365] | 51 | { | 
|---|
| [4832] | 52 |   if (this->material) | 
|---|
| [5557] | 53 |     delete this->material; | 
|---|
| [5750] | 54 |  | 
|---|
| [6305] | 55 | /*  if (this->text != NULL) | 
|---|
 | 56 |     delete this->text;*/ | 
|---|
| [4832] | 57 | } | 
|---|
 | 58 |  | 
|---|
 | 59 | /** | 
|---|
| [5556] | 60 |  * initializes the Aim | 
|---|
| [4832] | 61 |  */ | 
|---|
| [5556] | 62 | void Aim::init() | 
|---|
| [4832] | 63 | { | 
|---|
| [5556] | 64 |   this->setClassID(CL_CROSSHAIR, "Aim"); | 
|---|
 | 65 |   this->setName("Aim"); | 
|---|
| [4320] | 66 |  | 
|---|
| [5398] | 67 |   this->setLayer(E2D_LAYER_TOP); | 
|---|
| [5750] | 68 |   this->setRotationSpeed(30.0* (float)rand()/RAND_MAX + 10.0); | 
|---|
| [4832] | 69 |   this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0); | 
|---|
| [4830] | 70 |  | 
|---|
| [5750] | 71 |   this->setBindNode(this); | 
|---|
| [4832] | 72 |   this->material = new Material; | 
|---|
| [5750] | 73 |   this->source = NULL; | 
|---|
 | 74 |  | 
|---|
| [6637] | 75 |   this->range = 0; | 
|---|
 | 76 |   this->angle = 0; | 
|---|
| [5750] | 77 |   this->anim = new tAnimation<Aim>(this, &Aim::setSize); | 
|---|
 | 78 |   this->anim->setInfinity(ANIM_INF_CONSTANT); | 
|---|
 | 79 |   this->anim->addKeyFrame(500, .3, ANIM_LINEAR); | 
|---|
 | 80 |   this->anim->addKeyFrame(100, .2, ANIM_LINEAR); | 
|---|
 | 81 |   this->anim->addKeyFrame(50, .01, ANIM_LINEAR); | 
|---|
 | 82 |  | 
|---|
| [6305] | 83 | /*  this->text = new Text(); | 
|---|
| [5779] | 84 |   this->text->setLayer(this->getLayer()); | 
|---|
| [5750] | 85 |   this->text->setParent2D(this); | 
|---|
 | 86 |   this->text->setRelCoor2D(10, -50); | 
|---|
 | 87 |   this->text->setParentMode2D(E2D_PARENT_MOVEMENT); | 
|---|
| [6305] | 88 |   this->text->setText("Testing");*/ | 
|---|
| [3365] | 89 | } | 
|---|
| [1853] | 90 |  | 
|---|
| [5556] | 91 | void Aim::loadParams(const TiXmlElement* root) | 
|---|
| [3543] | 92 | { | 
|---|
| [6512] | 93 |   PNode::loadParams(root); | 
|---|
| [4830] | 94 |  | 
|---|
| [5671] | 95 |   LoadParam(root, "texture", this, Aim, setTexture) | 
|---|
| [5556] | 96 |       .describe("the texture-file to load onto the Aim"); | 
|---|
| [4831] | 97 |  | 
|---|
| [5671] | 98 |   LoadParam(root, "size", this, Aim, setSize) | 
|---|
| [5556] | 99 |       .describe("the size of the Aim in Pixels"); | 
|---|
| [4832] | 100 |  | 
|---|
| [5671] | 101 |   LoadParam(root, "rotation-speed", this, Aim, setRotationSpeed) | 
|---|
| [5556] | 102 |       .describe("the Speed with which the Aim should rotate"); | 
|---|
| [3543] | 103 | } | 
|---|
| [4779] | 104 |  | 
|---|
| [6637] | 105 | void Aim::searchTarget() | 
|---|
| [5750] | 106 | { | 
|---|
| [6222] | 107 |   std::list<WorldEntity*>::iterator entity; | 
|---|
| [6637] | 108 |   Vector diffVec(0.0, 0.0, 0.0); | 
|---|
 | 109 |   diffVec = ( this->getAbsCoor() - this->source->getAbsCoor() ); | 
|---|
| [6222] | 110 |  | 
|---|
| [6637] | 111 | //only look for target if the aim hasn`t locked a target yet or if the actual target is out of range | 
|---|
 | 112 |   if( this == PNode::getNullParent() || diffVec.len() > range  || ( acos( (this->source->getAbsDirX()).dot(diffVec)/(diffVec.len() * (this->source->getAbsDirX()).len() ) ) > angle)) | 
|---|
| [6222] | 113 |   for (entity = State::getObjectManager()->getObjectList(OM_GROUP_00).begin(); | 
|---|
 | 114 |        entity != State::getObjectManager()->getObjectList(OM_GROUP_00).end(); | 
|---|
 | 115 |        entity ++) | 
|---|
| [5750] | 116 |   { | 
|---|
| [6637] | 117 |     diffVec = ( (*entity)->getAbsCoor() - this->source->getAbsCoor() ); | 
|---|
 | 118 |  | 
|---|
 | 119 |     if ( diffVec.len() < range  &&  acos( (this->source->getAbsDirX()).dot(diffVec)/(diffVec.len() * (this->source->getAbsDirX()).len() ) ) < angle) | 
|---|
| [5750] | 120 |     { | 
|---|
| [6222] | 121 |       if (this->getParent() != (*entity)) | 
|---|
| [5750] | 122 |       { | 
|---|
 | 123 |         this->anim->replay(); | 
|---|
| [6222] | 124 |         this->setParentSoft(*entity, 5); | 
|---|
 | 125 |         return; | 
|---|
| [5750] | 126 |       } | 
|---|
 | 127 |     } | 
|---|
 | 128 |   } | 
|---|
| [6637] | 129 | //if no target found: | 
|---|
 | 130 |   this->setParent(PNode::getNullParent()); | 
|---|
| [5750] | 131 | } | 
|---|
 | 132 |  | 
|---|
 | 133 |  | 
|---|
| [4832] | 134 | /** | 
|---|
| [6305] | 135 |  * @brief sets the size of the Aim. | 
|---|
| [4832] | 136 |  * @param size the size in pixels | 
|---|
 | 137 |  */ | 
|---|
| [5556] | 138 | void Aim::setSize(float size) | 
|---|
| [4832] | 139 | { | 
|---|
| [5378] | 140 |   this->setSize2D(size/2, size/2); | 
|---|
 | 141 | } | 
|---|
| [4832] | 142 |  | 
|---|
 | 143 | /** | 
|---|
 | 144 |  * sets the material to load | 
|---|
 | 145 |  * @param textureFile The texture-file to load onto the crosshair | 
|---|
 | 146 |  */ | 
|---|
| [5556] | 147 | void Aim::setTexture(const char* textureFile) | 
|---|
| [4832] | 148 | { | 
|---|
 | 149 |   this->material->setDiffuseMap(textureFile); | 
|---|
 | 150 | } | 
|---|
 | 151 |  | 
|---|
 | 152 | /** | 
|---|
| [5556] | 153 |  * ticks the Aim | 
|---|
| [4832] | 154 |  * @param dt the time to ticks | 
|---|
 | 155 |  */ | 
|---|
| [5556] | 156 | void Aim::tick(float dt) | 
|---|
| [4832] | 157 | { | 
|---|
| [4834] | 158 |   // let the crosshair rotate | 
|---|
| [5378] | 159 |   this->shiftDir2D(dt * rotationSpeed); | 
|---|
| [4781] | 160 |  | 
|---|
| [6305] | 161 | //   char outputText[100]; | 
|---|
 | 162 | //   sprintf(outputText, "%s - distance: %f\n", this->getParent()->getName(), (this->source->getAbsCoor() - this->getAbsCoor()).len()); | 
|---|
 | 163 | //   this->text->setText(outputText); | 
|---|
| [4832] | 164 |  | 
|---|
 | 165 |  | 
|---|
| [6637] | 166 | //  if (this->source->getAbsCoor().x > this->getAbsCoor().x ) | 
|---|
 | 167 |      this->searchTarget(); | 
|---|
| [5750] | 168 | //   float z = 0.0f; | 
|---|
 | 169 | //   glReadPixels ((int)this->getAbsCoor2D().x, | 
|---|
 | 170 | //                  GraphicsEngine::getInstance()->getResolutionY()-(int)this->getAbsCoor2D().y-1, | 
|---|
 | 171 | //                  1, | 
|---|
 | 172 | //                  1, | 
|---|
 | 173 | //                  GL_DEPTH_COMPONENT, | 
|---|
 | 174 | //                  GL_FLOAT, | 
|---|
 | 175 | //                  &z); | 
|---|
 | 176 | // | 
|---|
 | 177 | // | 
|---|
 | 178 | //   GLdouble objX=.0, objY=.0, objZ=.0; | 
|---|
 | 179 | //   gluUnProject(this->getAbsCoor2D().x, | 
|---|
 | 180 | //                GraphicsEngine::getInstance()->getResolutionY()-this->getAbsCoor2D().y-1, | 
|---|
 | 181 | //                .99,  // z | 
|---|
 | 182 | //                GraphicsEngine::modMat, | 
|---|
 | 183 | //                GraphicsEngine::projMat, | 
|---|
 | 184 | //                GraphicsEngine::viewPort, | 
|---|
 | 185 | //                &objX, | 
|---|
 | 186 | //                &objY, | 
|---|
 | 187 | //                &objZ ); | 
|---|
 | 188 | // | 
|---|
 | 189 | //   this->setAbsCoor(objX, objY, objZ); | 
|---|
| [4834] | 190 | } | 
|---|
 | 191 |  | 
|---|
 | 192 | /** | 
|---|
 | 193 |  * draws the crosshair | 
|---|
 | 194 |  */ | 
|---|
| [5556] | 195 | void Aim::draw() const | 
|---|
| [4834] | 196 | { | 
|---|
| [5750] | 197 |  | 
|---|
| [4955] | 198 |   glPushMatrix(); | 
|---|
| [5378] | 199 |   glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); | 
|---|
| [4830] | 200 |  | 
|---|
| [5378] | 201 |   glRotatef(this->getAbsDir2D(), 0,0,1); | 
|---|
| [4832] | 202 |   this->material->select(); | 
|---|
 | 203 |   glBegin(GL_TRIANGLE_STRIP); | 
|---|
 | 204 |   glTexCoord2f(0, 0); | 
|---|
| [5378] | 205 |   glVertex2f(-this->getSizeX2D(), -this->getSizeY2D()); | 
|---|
| [4832] | 206 |   glTexCoord2f(1, 0); | 
|---|
| [5378] | 207 |   glVertex2f(this->getSizeX2D(), -this->getSizeY2D()); | 
|---|
| [4832] | 208 |   glTexCoord2f(0, 1); | 
|---|
| [5378] | 209 |   glVertex2f(-this->getSizeX2D(), this->getSizeY2D()); | 
|---|
| [4832] | 210 |   glTexCoord2f(1, 1); | 
|---|
| [5378] | 211 |   glVertex2f(this->getSizeX2D(), this->getSizeY2D()); | 
|---|
| [4832] | 212 |   glEnd(); | 
|---|
| [4955] | 213 |   glPopMatrix(); | 
|---|
| [4830] | 214 |  | 
|---|
| [4781] | 215 | } | 
|---|