[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" |
---|
[5559] | 23 | #include "list.h" |
---|
[4832] | 24 | #include "material.h" |
---|
[4781] | 25 | |
---|
[5559] | 26 | #include "world_entity.h" |
---|
| 27 | |
---|
[1856] | 28 | using namespace std; |
---|
[1853] | 29 | |
---|
[1856] | 30 | |
---|
[3245] | 31 | /** |
---|
[4832] | 32 | * standart constructor |
---|
| 33 | */ |
---|
[5556] | 34 | Aim::Aim (const TiXmlElement* root) |
---|
[4832] | 35 | { |
---|
| 36 | this->init(); |
---|
| 37 | |
---|
| 38 | if (root) |
---|
| 39 | this->loadParams(root); |
---|
| 40 | else |
---|
| 41 | this->setTexture("maps/aim.png"); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | /** |
---|
[5556] | 45 | * destroys a Aim |
---|
[3245] | 46 | */ |
---|
[5556] | 47 | Aim::~Aim () |
---|
[3365] | 48 | { |
---|
[4832] | 49 | if (this->material) |
---|
[5557] | 50 | delete this->material; |
---|
[4832] | 51 | } |
---|
| 52 | |
---|
| 53 | /** |
---|
[5556] | 54 | * initializes the Aim |
---|
[4832] | 55 | */ |
---|
[5556] | 56 | void Aim::init() |
---|
[4832] | 57 | { |
---|
[5556] | 58 | this->setClassID(CL_CROSSHAIR, "Aim"); |
---|
| 59 | this->setName("Aim"); |
---|
[4320] | 60 | |
---|
[5398] | 61 | this->setLayer(E2D_LAYER_TOP); |
---|
[5559] | 62 | this->setRotationSpeed(30.0* (float)rand()/RAND_MAX + 10.0); |
---|
[4832] | 63 | this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0); |
---|
[4830] | 64 | |
---|
[5559] | 65 | this->setBindNode(this); |
---|
[4832] | 66 | this->material = new Material; |
---|
[3365] | 67 | } |
---|
[1853] | 68 | |
---|
| 69 | |
---|
[5556] | 70 | void Aim::loadParams(const TiXmlElement* root) |
---|
[3543] | 71 | { |
---|
[4832] | 72 | static_cast<PNode*>(this)->loadParams(root); |
---|
[4830] | 73 | |
---|
[5556] | 74 | LoadParam<Aim>(root, "texture", this, &Aim::setTexture) |
---|
| 75 | .describe("the texture-file to load onto the Aim"); |
---|
[4831] | 76 | |
---|
[5556] | 77 | LoadParam<Aim>(root, "size", this, &Aim::setSize) |
---|
| 78 | .describe("the size of the Aim in Pixels"); |
---|
[4832] | 79 | |
---|
[5556] | 80 | LoadParam<Aim>(root, "rotation-speed", this, &Aim::setRotationSpeed) |
---|
| 81 | .describe("the Speed with which the Aim should rotate"); |
---|
[3543] | 82 | } |
---|
[4779] | 83 | |
---|
[5559] | 84 | void Aim::searchTarget(PNode* source) |
---|
| 85 | { |
---|
| 86 | tIterator<WorldEntity>* iterator = State::getWorldEntityList()->getIterator(); |
---|
| 87 | WorldEntity* entity = iterator->firstElement(); |
---|
| 88 | while (likely(entity != NULL)) |
---|
| 89 | { |
---|
| 90 | if (entity->isA(CL_NPC) && (source->getAbsCoor() - entity->getAbsCoor()).len() < 100) |
---|
| 91 | { |
---|
| 92 | this->setParent(entity); |
---|
| 93 | delete iterator; |
---|
| 94 | return; |
---|
| 95 | } |
---|
| 96 | entity = iterator->nextElement(); |
---|
| 97 | } |
---|
[4832] | 98 | |
---|
[5559] | 99 | delete iterator; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | |
---|
[4832] | 104 | /** |
---|
[5556] | 105 | * sets the size of the Aim. |
---|
[4832] | 106 | * @param size the size in pixels |
---|
| 107 | */ |
---|
[5556] | 108 | void Aim::setSize(float size) |
---|
[4832] | 109 | { |
---|
[5378] | 110 | this->setSize2D(size/2, size/2); |
---|
| 111 | } |
---|
[4832] | 112 | |
---|
| 113 | /** |
---|
| 114 | * sets the material to load |
---|
| 115 | * @param textureFile The texture-file to load onto the crosshair |
---|
| 116 | */ |
---|
[5556] | 117 | void Aim::setTexture(const char* textureFile) |
---|
[4832] | 118 | { |
---|
| 119 | this->material->setDiffuseMap(textureFile); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | /** |
---|
[5556] | 123 | * ticks the Aim |
---|
[4832] | 124 | * @param dt the time to ticks |
---|
| 125 | */ |
---|
[5556] | 126 | void Aim::tick(float dt) |
---|
[4832] | 127 | { |
---|
[4834] | 128 | // let the crosshair rotate |
---|
[5378] | 129 | this->shiftDir2D(dt * rotationSpeed); |
---|
[4781] | 130 | |
---|
[4832] | 131 | |
---|
[5559] | 132 | // float z = 0.0f; |
---|
| 133 | // glReadPixels ((int)this->getAbsCoor2D().x, |
---|
| 134 | // GraphicsEngine::getInstance()->getResolutionY()-(int)this->getAbsCoor2D().y-1, |
---|
| 135 | // 1, |
---|
| 136 | // 1, |
---|
| 137 | // GL_DEPTH_COMPONENT, |
---|
| 138 | // GL_FLOAT, |
---|
| 139 | // &z); |
---|
| 140 | // |
---|
| 141 | // |
---|
| 142 | // GLdouble objX=.0, objY=.0, objZ=.0; |
---|
| 143 | // gluUnProject(this->getAbsCoor2D().x, |
---|
| 144 | // GraphicsEngine::getInstance()->getResolutionY()-this->getAbsCoor2D().y-1, |
---|
| 145 | // .99, // z |
---|
| 146 | // GraphicsEngine::modMat, |
---|
| 147 | // GraphicsEngine::projMat, |
---|
| 148 | // GraphicsEngine::viewPort, |
---|
| 149 | // &objX, |
---|
| 150 | // &objY, |
---|
| 151 | // &objZ ); |
---|
| 152 | // |
---|
| 153 | // this->setAbsCoor(objX, objY, objZ); |
---|
[4834] | 154 | } |
---|
| 155 | |
---|
| 156 | /** |
---|
| 157 | * draws the crosshair |
---|
| 158 | */ |
---|
[5556] | 159 | void Aim::draw() const |
---|
[4834] | 160 | { |
---|
[4955] | 161 | glPushMatrix(); |
---|
[5378] | 162 | glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); |
---|
[4830] | 163 | |
---|
[5378] | 164 | glRotatef(this->getAbsDir2D(), 0,0,1); |
---|
[4832] | 165 | this->material->select(); |
---|
| 166 | glBegin(GL_TRIANGLE_STRIP); |
---|
| 167 | glTexCoord2f(0, 0); |
---|
[5378] | 168 | glVertex2f(-this->getSizeX2D(), -this->getSizeY2D()); |
---|
[4832] | 169 | glTexCoord2f(1, 0); |
---|
[5378] | 170 | glVertex2f(this->getSizeX2D(), -this->getSizeY2D()); |
---|
[4832] | 171 | glTexCoord2f(0, 1); |
---|
[5378] | 172 | glVertex2f(-this->getSizeX2D(), this->getSizeY2D()); |
---|
[4832] | 173 | glTexCoord2f(1, 1); |
---|
[5378] | 174 | glVertex2f(this->getSizeX2D(), this->getSizeY2D()); |
---|
[4832] | 175 | glEnd(); |
---|
[4955] | 176 | glPopMatrix(); |
---|
[4830] | 177 | |
---|
[4781] | 178 | } |
---|