| 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 "load_param.h" | 
|---|
| 21 | #include "graphics_engine.h" | 
|---|
| 22 | #include "state.h" | 
|---|
| 23 | #include "list.h" | 
|---|
| 24 | #include "material.h" | 
|---|
| 25 | #include "t_animation.h" | 
|---|
| 26 | #include "text.h" | 
|---|
| 27 |  | 
|---|
| 28 | #include "world_entity.h" | 
|---|
| 29 |  | 
|---|
| 30 | using namespace std; | 
|---|
| 31 |  | 
|---|
| 32 |  | 
|---|
| 33 | /** | 
|---|
| 34 |  * standart constructor | 
|---|
| 35 |  */ | 
|---|
| 36 | Aim::Aim (PNode* source, const TiXmlElement* root) | 
|---|
| 37 | { | 
|---|
| 38 |   this->init(); | 
|---|
| 39 |  | 
|---|
| 40 |   this->source = source; | 
|---|
| 41 |  | 
|---|
| 42 |   if (root) | 
|---|
| 43 |     this->loadParams(root); | 
|---|
| 44 |   else | 
|---|
| 45 |     this->setTexture("maps/aim.png"); | 
|---|
| 46 | } | 
|---|
| 47 |  | 
|---|
| 48 | /** | 
|---|
| 49 |  * destroys a Aim | 
|---|
| 50 | */ | 
|---|
| 51 | Aim::~Aim () | 
|---|
| 52 | { | 
|---|
| 53 |   if (this->material) | 
|---|
| 54 |     delete this->material; | 
|---|
| 55 |  | 
|---|
| 56 |   if (this->text != NULL) | 
|---|
| 57 |     delete this->text; | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | /** | 
|---|
| 61 |  * initializes the Aim | 
|---|
| 62 |  */ | 
|---|
| 63 | void Aim::init() | 
|---|
| 64 | { | 
|---|
| 65 |   this->setClassID(CL_CROSSHAIR, "Aim"); | 
|---|
| 66 |   this->setName("Aim"); | 
|---|
| 67 |  | 
|---|
| 68 |   this->setLayer(E2D_LAYER_TOP); | 
|---|
| 69 |   this->setRotationSpeed(30.0* (float)rand()/RAND_MAX + 10.0); | 
|---|
| 70 |   this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0); | 
|---|
| 71 |  | 
|---|
| 72 |   this->setBindNode(this); | 
|---|
| 73 |   this->material = new Material; | 
|---|
| 74 |   this->source = NULL; | 
|---|
| 75 |  | 
|---|
| 76 |   this->anim = new tAnimation<Aim>(this, &Aim::setSize); | 
|---|
| 77 |   this->anim->setInfinity(ANIM_INF_CONSTANT); | 
|---|
| 78 |   this->anim->addKeyFrame(500, .3, ANIM_LINEAR); | 
|---|
| 79 |   this->anim->addKeyFrame(100, .2, ANIM_LINEAR); | 
|---|
| 80 |   this->anim->addKeyFrame(50, .01, ANIM_LINEAR); | 
|---|
| 81 |  | 
|---|
| 82 |   this->text = new Text(); | 
|---|
| 83 |   this->text->setLayer(this->getLayer()); | 
|---|
| 84 |   this->text->setParent2D(this); | 
|---|
| 85 |   this->text->setRelCoor2D(10, -50); | 
|---|
| 86 |   this->text->setParentMode2D(E2D_PARENT_MOVEMENT); | 
|---|
| 87 |   this->text->setText("Testing"); | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | void Aim::loadParams(const TiXmlElement* root) | 
|---|
| 91 | { | 
|---|
| 92 |   static_cast<PNode*>(this)->loadParams(root); | 
|---|
| 93 |  | 
|---|
| 94 |   LoadParam(root, "texture", this, Aim, setTexture) | 
|---|
| 95 |       .describe("the texture-file to load onto the Aim"); | 
|---|
| 96 |  | 
|---|
| 97 |   LoadParam(root, "size", this, Aim, setSize) | 
|---|
| 98 |       .describe("the size of the Aim in Pixels"); | 
|---|
| 99 |  | 
|---|
| 100 |   LoadParam(root, "rotation-speed", this, Aim, setRotationSpeed) | 
|---|
| 101 |       .describe("the Speed with which the Aim should rotate"); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | void Aim::searchTarget(float range) | 
|---|
| 105 | { | 
|---|
| 106 |   //FIXME// | 
|---|
| 107 | /*  tIterator<WorldEntity>* iterator = State::getWorldEntityList()->getIterator(); | 
|---|
| 108 |   WorldEntity* entity = iterator->firstElement(); | 
|---|
| 109 |   while (likely(entity != NULL)) | 
|---|
| 110 |   { | 
|---|
| 111 |     if (entity->isA(CL_NPC) && this->source->getAbsCoor().x < entity->getAbsCoor().x && (this->source->getAbsCoor() - entity->getAbsCoor()).len() < range) | 
|---|
| 112 |     { | 
|---|
| 113 |       if (this->getParent() != entity) | 
|---|
| 114 |       { | 
|---|
| 115 |         this->anim->replay(); | 
|---|
| 116 |         this->setParentSoft(entity, 5); | 
|---|
| 117 |       } | 
|---|
| 118 |       delete iterator; | 
|---|
| 119 |       return; | 
|---|
| 120 |     } | 
|---|
| 121 |     entity = iterator->nextElement(); | 
|---|
| 122 |   } | 
|---|
| 123 |  | 
|---|
| 124 |   delete iterator;*/ | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 |  | 
|---|
| 128 |  | 
|---|
| 129 | /** | 
|---|
| 130 |  * sets the size of the Aim. | 
|---|
| 131 |  * @param size the size in pixels | 
|---|
| 132 |  */ | 
|---|
| 133 | void Aim::setSize(float size) | 
|---|
| 134 | { | 
|---|
| 135 |   this->setSize2D(size/2, size/2); | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | /** | 
|---|
| 139 |  * sets the material to load | 
|---|
| 140 |  * @param textureFile The texture-file to load onto the crosshair | 
|---|
| 141 |  */ | 
|---|
| 142 | void Aim::setTexture(const char* textureFile) | 
|---|
| 143 | { | 
|---|
| 144 |   this->material->setDiffuseMap(textureFile); | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | /** | 
|---|
| 148 |  * ticks the Aim | 
|---|
| 149 |  * @param dt the time to ticks | 
|---|
| 150 |  */ | 
|---|
| 151 | void Aim::tick(float dt) | 
|---|
| 152 | { | 
|---|
| 153 |   // let the crosshair rotate | 
|---|
| 154 |   this->shiftDir2D(dt * rotationSpeed); | 
|---|
| 155 |  | 
|---|
| 156 |   char outputText[100]; | 
|---|
| 157 |   sprintf(outputText, "%s - distance: %f\n", this->getParent()->getName(), (this->source->getAbsCoor() - this->getAbsCoor()).len()); | 
|---|
| 158 |   this->text->setText(outputText); | 
|---|
| 159 |  | 
|---|
| 160 |  | 
|---|
| 161 |   if (this->source->getAbsCoor().x > this->getAbsCoor().x ) | 
|---|
| 162 |     this->searchTarget(1000); | 
|---|
| 163 | //   float z = 0.0f; | 
|---|
| 164 | //   glReadPixels ((int)this->getAbsCoor2D().x, | 
|---|
| 165 | //                  GraphicsEngine::getInstance()->getResolutionY()-(int)this->getAbsCoor2D().y-1, | 
|---|
| 166 | //                  1, | 
|---|
| 167 | //                  1, | 
|---|
| 168 | //                  GL_DEPTH_COMPONENT, | 
|---|
| 169 | //                  GL_FLOAT, | 
|---|
| 170 | //                  &z); | 
|---|
| 171 | // | 
|---|
| 172 | // | 
|---|
| 173 | //   GLdouble objX=.0, objY=.0, objZ=.0; | 
|---|
| 174 | //   gluUnProject(this->getAbsCoor2D().x, | 
|---|
| 175 | //                GraphicsEngine::getInstance()->getResolutionY()-this->getAbsCoor2D().y-1, | 
|---|
| 176 | //                .99,  // z | 
|---|
| 177 | //                GraphicsEngine::modMat, | 
|---|
| 178 | //                GraphicsEngine::projMat, | 
|---|
| 179 | //                GraphicsEngine::viewPort, | 
|---|
| 180 | //                &objX, | 
|---|
| 181 | //                &objY, | 
|---|
| 182 | //                &objZ ); | 
|---|
| 183 | // | 
|---|
| 184 | //   this->setAbsCoor(objX, objY, objZ); | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | /** | 
|---|
| 188 |  * draws the crosshair | 
|---|
| 189 |  */ | 
|---|
| 190 | void Aim::draw() const | 
|---|
| 191 | { | 
|---|
| 192 |  | 
|---|
| 193 |   glPushMatrix(); | 
|---|
| 194 |   glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); | 
|---|
| 195 |  | 
|---|
| 196 |   glRotatef(this->getAbsDir2D(), 0,0,1); | 
|---|
| 197 |   this->material->select(); | 
|---|
| 198 |   glBegin(GL_TRIANGLE_STRIP); | 
|---|
| 199 |   glTexCoord2f(0, 0); | 
|---|
| 200 |   glVertex2f(-this->getSizeX2D(), -this->getSizeY2D()); | 
|---|
| 201 |   glTexCoord2f(1, 0); | 
|---|
| 202 |   glVertex2f(this->getSizeX2D(), -this->getSizeY2D()); | 
|---|
| 203 |   glTexCoord2f(0, 1); | 
|---|
| 204 |   glVertex2f(-this->getSizeX2D(), this->getSizeY2D()); | 
|---|
| 205 |   glTexCoord2f(1, 1); | 
|---|
| 206 |   glVertex2f(this->getSizeX2D(), this->getSizeY2D()); | 
|---|
| 207 |   glEnd(); | 
|---|
| 208 |   glPopMatrix(); | 
|---|
| 209 |  | 
|---|
| 210 | } | 
|---|