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