| 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 | ObjectListDefinition(Aim); | 
|---|
| 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->text != NULL) | 
|---|
| 54 | delete this->text;*/ | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | /** | 
|---|
| 58 | * initializes the Aim | 
|---|
| 59 | */ | 
|---|
| 60 | void Aim::init() | 
|---|
| 61 | { | 
|---|
| 62 | this->registerObject(this, Aim::_objectList); | 
|---|
| 63 | this->setName("Aim"); | 
|---|
| 64 |  | 
|---|
| 65 | this->setLayer(E2D_LAYER_TOP); | 
|---|
| 66 | this->setRotationSpeed(30.0* (float)rand()/RAND_MAX + 10.0); | 
|---|
| 67 | this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0); | 
|---|
| 68 |  | 
|---|
| 69 | this->setBindNode(this); | 
|---|
| 70 | this->source = NULL; | 
|---|
| 71 |  | 
|---|
| 72 | this->range = 1000; | 
|---|
| 73 | this->angle = M_PI_4; | 
|---|
| 74 | this->targetGroup = OM_GROUP_01; | 
|---|
| 75 | this->anim = new tAnimation<Aim>(this, &Aim::setSize); | 
|---|
| 76 | this->anim->setInfinity(ANIM_INF_CONSTANT); | 
|---|
| 77 | this->anim->addKeyFrame(500, .3, ANIM_LINEAR); | 
|---|
| 78 | this->anim->addKeyFrame(100, .2, ANIM_LINEAR); | 
|---|
| 79 | this->anim->addKeyFrame(50, .01, ANIM_LINEAR); | 
|---|
| 80 |  | 
|---|
| 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 | PNode::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 | LoadParam(root, "target-group", this, Aim, setTargetGroupS); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | void Aim::searchTarget() | 
|---|
| 107 | { | 
|---|
| 108 | ObjectManager::EntityList::iterator entity; | 
|---|
| 109 | //printf("%d\n", this->targetGroup); | 
|---|
| 110 | for (entity = State::getObjectManager()->getEntityList(this->targetGroup).begin(); | 
|---|
| 111 | entity != State::getObjectManager()->getEntityList(this->targetGroup).end(); | 
|---|
| 112 | entity ++) | 
|---|
| 113 | { | 
|---|
| 114 | diffVec = ( (*entity)->getAbsCoor() - this->source->getAbsCoor() ); | 
|---|
| 115 |  | 
|---|
| 116 | if ( diffVec.len() < range )//&&  acos( (this->source->getAbsDirX()).dot(diffVec)/(diffVec.len() * (this->source->getAbsDirX()).len() ) )  < angle) | 
|---|
| 117 | { | 
|---|
| 118 | //if (this->getParent() != (*entity)) | 
|---|
| 119 | { | 
|---|
| 120 | //        printf("found target::: %d %s::%s\n", (*entity)->getOMListNumber(), (*entity)->getClassCName(), (*entity)->getCName()); | 
|---|
| 121 | this->anim->replay(); | 
|---|
| 122 | this->setParentSoft(*entity, 5); | 
|---|
| 123 | return; | 
|---|
| 124 | } | 
|---|
| 125 | } | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | //if no target found: | 
|---|
| 129 | this->setParent(PNode::getNullParent()); | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | void Aim::setTargetGroupS(const std::string& groupName) | 
|---|
| 133 | { | 
|---|
| 134 | OM_LIST id = ObjectManager::StringToOMList(groupName); | 
|---|
| 135 | if (id != OM_NULL) | 
|---|
| 136 | this->setTargetGroup(id); | 
|---|
| 137 | else | 
|---|
| 138 | PRINTF(2)("List %s not found for targetting\n", groupName.c_str()); | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | /** | 
|---|
| 142 | * @brief sets the size of the Aim. | 
|---|
| 143 | * @param size the size in pixels | 
|---|
| 144 | */ | 
|---|
| 145 | void Aim::setSize(float size) | 
|---|
| 146 | { | 
|---|
| 147 | this->setSize2D(size/2, size/2); | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | /** | 
|---|
| 151 | * sets the material to load | 
|---|
| 152 | * @param textureFile The texture-file to load onto the crosshair | 
|---|
| 153 | */ | 
|---|
| 154 | void Aim::setTexture(const std::string& textureFile) | 
|---|
| 155 | { | 
|---|
| 156 | this->material.setDiffuseMap(textureFile); | 
|---|
| 157 | } | 
|---|
| 158 |  | 
|---|
| 159 | /** | 
|---|
| 160 | * ticks the Aim | 
|---|
| 161 | * @param dt the time to ticks | 
|---|
| 162 | */ | 
|---|
| 163 | void Aim::tick(float dt) | 
|---|
| 164 | { | 
|---|
| 165 | // let the crosshair rotate | 
|---|
| 166 | this->shiftDir2D(dt * rotationSpeed); | 
|---|
| 167 |  | 
|---|
| 168 | //   char outputText[100]; | 
|---|
| 169 | //   sprintf(outputText, "%s - distance: %f\n", this->getParent()->getName(), (this->source->getAbsCoor() - this->getAbsCoor()).len()); | 
|---|
| 170 | //   this->text->setText(outputText); | 
|---|
| 171 |  | 
|---|
| 172 |  | 
|---|
| 173 | //  if (this->source->getAbsCoor().x > this->getAbsCoor().x ) | 
|---|
| 174 | diffVec = ( this->getAbsCoor() - this->source->getAbsCoor() ); | 
|---|
| 175 | //only look for target if the aim hasn`t locked a target yet or if the actual target is out of range | 
|---|
| 176 | if(this->getParent() == PNode::getNullParent() || | 
|---|
| 177 | diffVec.len() > range )// || | 
|---|
| 178 | //( acos( (this->source->getAbsDirX()).dot(diffVec)/(diffVec.len() * (this->source->getAbsDirX()).len() ) ) > angle)) | 
|---|
| 179 | { | 
|---|
| 180 | this->setParentSoft(PNode::getNullParent(),5); | 
|---|
| 181 | this->searchTarget(); | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | //   float z = 0.0f; | 
|---|
| 185 | //   glReadPixels ((int)this->getAbsCoor2D().x, | 
|---|
| 186 | //                  GraphicsEngine::getInstance()->getResolutionY()-(int)this->getAbsCoor2D().y-1, | 
|---|
| 187 | //                  1, | 
|---|
| 188 | //                  1, | 
|---|
| 189 | //                  GL_DEPTH_COMPONENT, | 
|---|
| 190 | //                  GL_FLOAT, | 
|---|
| 191 | //                  &z); | 
|---|
| 192 | // | 
|---|
| 193 | // | 
|---|
| 194 | //   GLdouble objX=.0, objY=.0, objZ=.0; | 
|---|
| 195 | //   gluUnProject(this->getAbsCoor2D().x, | 
|---|
| 196 | //                GraphicsEngine::getInstance()->getResolutionY()-this->getAbsCoor2D().y-1, | 
|---|
| 197 | //                .99,  // z | 
|---|
| 198 | //                GraphicsEngine::modMat, | 
|---|
| 199 | //                GraphicsEngine::projMat, | 
|---|
| 200 | //                GraphicsEngine::viewPort, | 
|---|
| 201 | //                &objX, | 
|---|
| 202 | //                &objY, | 
|---|
| 203 | //                &objZ ); | 
|---|
| 204 | //aa | 
|---|
| 205 | //   this->setAbsCoor(objX, objY, objZ); | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 | /** | 
|---|
| 209 | * draws the crosshair | 
|---|
| 210 | */ | 
|---|
| 211 | void Aim::draw() const | 
|---|
| 212 | { | 
|---|
| 213 |  | 
|---|
| 214 | if( this->getParent() != PNode::getNullParent() ) | 
|---|
| 215 | { | 
|---|
| 216 | glPushMatrix(); | 
|---|
| 217 | glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); | 
|---|
| 218 |  | 
|---|
| 219 | glRotatef(this->getAbsDir2D(), 0,0,1); | 
|---|
| 220 | this->material.select(); | 
|---|
| 221 | glBegin(GL_TRIANGLE_STRIP); | 
|---|
| 222 | glTexCoord2f(0, 0); | 
|---|
| 223 | glVertex2f(-this->getSizeX2D(), -this->getSizeY2D()); | 
|---|
| 224 | glTexCoord2f(1, 0); | 
|---|
| 225 | glVertex2f(this->getSizeX2D(), -this->getSizeY2D()); | 
|---|
| 226 | glTexCoord2f(0, 1); | 
|---|
| 227 | glVertex2f(-this->getSizeX2D(), this->getSizeY2D()); | 
|---|
| 228 | glTexCoord2f(1, 1); | 
|---|
| 229 | glVertex2f(this->getSizeX2D(), this->getSizeY2D()); | 
|---|
| 230 | glEnd(); | 
|---|
| 231 | glPopMatrix(); | 
|---|
| 232 | } | 
|---|
| 233 |  | 
|---|
| 234 | } | 
|---|