| 1 | /* | 
|---|
| 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. | 
|---|
| 10 |  | 
|---|
| 11 |    ### File Specific: | 
|---|
| 12 |    main-programmer: Benjamin Grauer | 
|---|
| 13 |    co-programmer: ... | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON | 
|---|
| 17 |  | 
|---|
| 18 | #include "aim.h" | 
|---|
| 19 |  | 
|---|
| 20 | #include "util/loading/load_param.h" | 
|---|
| 21 | #include "graphics_engine.h" | 
|---|
| 22 | #include "state.h" | 
|---|
| 23 | #include "material.h" | 
|---|
| 24 | #include "t_animation.h" | 
|---|
| 25 | #include "text.h" | 
|---|
| 26 |  | 
|---|
| 27 | #include "world_entity.h" | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 |  | 
|---|
| 31 |  | 
|---|
| 32 | /** | 
|---|
| 33 |  * standart constructor | 
|---|
| 34 |  */ | 
|---|
| 35 | Aim::Aim (PNode* source, const TiXmlElement* root) | 
|---|
| 36 | { | 
|---|
| 37 |   this->init(); | 
|---|
| 38 |  | 
|---|
| 39 |   this->source = source; | 
|---|
| 40 |  | 
|---|
| 41 |   if (root) | 
|---|
| 42 |     this->loadParams(root); | 
|---|
| 43 |   else | 
|---|
| 44 |     this->setTexture("maps/aim.png"); | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | /** | 
|---|
| 48 |  * destroys a Aim | 
|---|
| 49 | */ | 
|---|
| 50 | Aim::~Aim () | 
|---|
| 51 | { | 
|---|
| 52 | /*  if (this->text != NULL) | 
|---|
| 53 |     delete this->text;*/ | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | /** | 
|---|
| 57 |  * initializes the Aim | 
|---|
| 58 |  */ | 
|---|
| 59 | void Aim::init() | 
|---|
| 60 | { | 
|---|
| 61 |   this->setClassID(CL_CROSSHAIR, "Aim"); | 
|---|
| 62 |   this->setName("Aim"); | 
|---|
| 63 |  | 
|---|
| 64 |   this->setLayer(E2D_LAYER_TOP); | 
|---|
| 65 |   this->setRotationSpeed(30.0* (float)rand()/RAND_MAX + 10.0); | 
|---|
| 66 |   this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0); | 
|---|
| 67 |  | 
|---|
| 68 |   this->setBindNode(this); | 
|---|
| 69 |   this->source = NULL; | 
|---|
| 70 |  | 
|---|
| 71 |   this->range = 1000; | 
|---|
| 72 |   this->angle = M_PI_4; | 
|---|
| 73 |   this->targetGroup = OM_GROUP_01; | 
|---|
| 74 |   this->anim = new tAnimation<Aim>(this, &Aim::setSize); | 
|---|
| 75 |   this->anim->setInfinity(ANIM_INF_CONSTANT); | 
|---|
| 76 |   this->anim->addKeyFrame(500, .3, ANIM_LINEAR); | 
|---|
| 77 |   this->anim->addKeyFrame(100, .2, ANIM_LINEAR); | 
|---|
| 78 |   this->anim->addKeyFrame(50, .01, ANIM_LINEAR); | 
|---|
| 79 |  | 
|---|
| 80 |  | 
|---|
| 81 | /*  this->text = new Text(); | 
|---|
| 82 |   this->text->setLayer(this->getLayer()); | 
|---|
| 83 |   this->text->setParent2D(this); | 
|---|
| 84 |   this->text->setRelCoor2D(10, -50); | 
|---|
| 85 |   this->text->setParentMode2D(E2D_PARENT_MOVEMENT); | 
|---|
| 86 |   this->text->setText("Testing");*/ | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | void Aim::loadParams(const TiXmlElement* root) | 
|---|
| 90 | { | 
|---|
| 91 |   PNode::loadParams(root); | 
|---|
| 92 |  | 
|---|
| 93 |   LoadParam(root, "texture", this, Aim, setTexture) | 
|---|
| 94 |       .describe("the texture-file to load onto the Aim"); | 
|---|
| 95 |  | 
|---|
| 96 |   LoadParam(root, "size", this, Aim, setSize) | 
|---|
| 97 |       .describe("the size of the Aim in Pixels"); | 
|---|
| 98 |  | 
|---|
| 99 |   LoadParam(root, "rotation-speed", this, Aim, setRotationSpeed) | 
|---|
| 100 |       .describe("the Speed with which the Aim should rotate"); | 
|---|
| 101 |  | 
|---|
| 102 |   LoadParam(root, "target-group", this, Aim, setTargetGroupS); | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | void Aim::searchTarget() | 
|---|
| 106 | { | 
|---|
| 107 |   ObjectManager::EntityList::iterator entity; | 
|---|
| 108 |   //printf("%d\n", this->targetGroup); | 
|---|
| 109 |   for (entity = State::getObjectManager()->getObjectList(this->targetGroup).begin(); | 
|---|
| 110 |        entity != State::getObjectManager()->getObjectList(this->targetGroup).end(); | 
|---|
| 111 |        entity ++) | 
|---|
| 112 |   { | 
|---|
| 113 |     diffVec = ( (*entity)->getAbsCoor() - this->source->getAbsCoor() ); | 
|---|
| 114 |  | 
|---|
| 115 |     if ( diffVec.len() < range )//&&  acos( (this->source->getAbsDirX()).dot(diffVec)/(diffVec.len() * (this->source->getAbsDirX()).len() ) )  < angle) | 
|---|
| 116 |     { | 
|---|
| 117 |       //if (this->getParent() != (*entity)) | 
|---|
| 118 |       { | 
|---|
| 119 |         printf("found target::: %d %s::%s\n", (*entity)->getOMListNumber(), (*entity)->getClassCName(), (*entity)->getCName()); | 
|---|
| 120 |         this->anim->replay(); | 
|---|
| 121 |         this->setParentSoft(*entity, 5); | 
|---|
| 122 |         return; | 
|---|
| 123 |       } | 
|---|
| 124 |     } | 
|---|
| 125 |   } | 
|---|
| 126 |  | 
|---|
| 127 |    //if no target found: | 
|---|
| 128 |    this->setParent(PNode::getNullParent()); | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | void Aim::setTargetGroupS(const std::string& groupName) | 
|---|
| 132 | { | 
|---|
| 133 |   OM_LIST id = ObjectManager::StringToOMList(groupName); | 
|---|
| 134 |   if (id != OM_NULL) | 
|---|
| 135 |     this->setTargetGroup(id); | 
|---|
| 136 |   else | 
|---|
| 137 |     PRINTF(2)("List %s not found for targetting\n", groupName.c_str()); | 
|---|
| 138 | } | 
|---|
| 139 |  | 
|---|
| 140 | /** | 
|---|
| 141 |  * @brief sets the size of the Aim. | 
|---|
| 142 |  * @param size the size in pixels | 
|---|
| 143 |  */ | 
|---|
| 144 | void Aim::setSize(float size) | 
|---|
| 145 | { | 
|---|
| 146 |   this->setSize2D(size/2, size/2); | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | /** | 
|---|
| 150 |  * sets the material to load | 
|---|
| 151 |  * @param textureFile The texture-file to load onto the crosshair | 
|---|
| 152 |  */ | 
|---|
| 153 | void Aim::setTexture(const std::string& textureFile) | 
|---|
| 154 | { | 
|---|
| 155 |   this->material.setDiffuseMap(textureFile); | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | /** | 
|---|
| 159 |  * ticks the Aim | 
|---|
| 160 |  * @param dt the time to ticks | 
|---|
| 161 |  */ | 
|---|
| 162 | void Aim::tick(float dt) | 
|---|
| 163 | { | 
|---|
| 164 |   // let the crosshair rotate | 
|---|
| 165 |   this->shiftDir2D(dt * rotationSpeed); | 
|---|
| 166 |  | 
|---|
| 167 | //   char outputText[100]; | 
|---|
| 168 | //   sprintf(outputText, "%s - distance: %f\n", this->getParent()->getName(), (this->source->getAbsCoor() - this->getAbsCoor()).len()); | 
|---|
| 169 | //   this->text->setText(outputText); | 
|---|
| 170 |  | 
|---|
| 171 |  | 
|---|
| 172 | //  if (this->source->getAbsCoor().x > this->getAbsCoor().x ) | 
|---|
| 173 |   diffVec = ( this->getAbsCoor() - this->source->getAbsCoor() ); | 
|---|
| 174 | //only look for target if the aim hasn`t locked a target yet or if the actual target is out of range | 
|---|
| 175 |    if(this->getParent() == PNode::getNullParent() || | 
|---|
| 176 |       diffVec.len() > range )// || | 
|---|
| 177 |      //( acos( (this->source->getAbsDirX()).dot(diffVec)/(diffVec.len() * (this->source->getAbsDirX()).len() ) ) > angle)) | 
|---|
| 178 |     { | 
|---|
| 179 |      this->setParentSoft(PNode::getNullParent(),5); | 
|---|
| 180 |      this->searchTarget(); | 
|---|
| 181 |     } | 
|---|
| 182 |  | 
|---|
| 183 | //   float z = 0.0f; | 
|---|
| 184 | //   glReadPixels ((int)this->getAbsCoor2D().x, | 
|---|
| 185 | //                  GraphicsEngine::getInstance()->getResolutionY()-(int)this->getAbsCoor2D().y-1, | 
|---|
| 186 | //                  1, | 
|---|
| 187 | //                  1, | 
|---|
| 188 | //                  GL_DEPTH_COMPONENT, | 
|---|
| 189 | //                  GL_FLOAT, | 
|---|
| 190 | //                  &z); | 
|---|
| 191 | // | 
|---|
| 192 | // | 
|---|
| 193 | //   GLdouble objX=.0, objY=.0, objZ=.0; | 
|---|
| 194 | //   gluUnProject(this->getAbsCoor2D().x, | 
|---|
| 195 | //                GraphicsEngine::getInstance()->getResolutionY()-this->getAbsCoor2D().y-1, | 
|---|
| 196 | //                .99,  // z | 
|---|
| 197 | //                GraphicsEngine::modMat, | 
|---|
| 198 | //                GraphicsEngine::projMat, | 
|---|
| 199 | //                GraphicsEngine::viewPort, | 
|---|
| 200 | //                &objX, | 
|---|
| 201 | //                &objY, | 
|---|
| 202 | //                &objZ ); | 
|---|
| 203 | //aa | 
|---|
| 204 | //   this->setAbsCoor(objX, objY, objZ); | 
|---|
| 205 | } | 
|---|
| 206 |  | 
|---|
| 207 | /** | 
|---|
| 208 |  * draws the crosshair | 
|---|
| 209 |  */ | 
|---|
| 210 | void Aim::draw() const | 
|---|
| 211 | { | 
|---|
| 212 |  | 
|---|
| 213 |  if( this->getParent() != PNode::getNullParent() ) | 
|---|
| 214 |   { | 
|---|
| 215 |   glPushMatrix(); | 
|---|
| 216 |   glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); | 
|---|
| 217 |  | 
|---|
| 218 |   glRotatef(this->getAbsDir2D(), 0,0,1); | 
|---|
| 219 |   this->material.select(); | 
|---|
| 220 |   glBegin(GL_TRIANGLE_STRIP); | 
|---|
| 221 |   glTexCoord2f(0, 0); | 
|---|
| 222 |   glVertex2f(-this->getSizeX2D(), -this->getSizeY2D()); | 
|---|
| 223 |   glTexCoord2f(1, 0); | 
|---|
| 224 |   glVertex2f(this->getSizeX2D(), -this->getSizeY2D()); | 
|---|
| 225 |   glTexCoord2f(0, 1); | 
|---|
| 226 |   glVertex2f(-this->getSizeX2D(), this->getSizeY2D()); | 
|---|
| 227 |   glTexCoord2f(1, 1); | 
|---|
| 228 |   glVertex2f(this->getSizeX2D(), this->getSizeY2D()); | 
|---|
| 229 |   glEnd(); | 
|---|
| 230 |   glPopMatrix(); | 
|---|
| 231 |   } | 
|---|
| 232 |  | 
|---|
| 233 | } | 
|---|