/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Grauer co-programmer: ... */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON #include "aim.h" #include "load_param.h" #include "graphics_engine.h" #include "state.h" #include "list.h" #include "material.h" #include "t_animation.h" #include "text.h" #include "world_entity.h" using namespace std; /** * standart constructor */ Aim::Aim (PNode* source, const TiXmlElement* root) { this->init(); this->source = source; if (root) this->loadParams(root); else this->setTexture("maps/aim.png"); } /** * destroys a Aim */ Aim::~Aim () { if (this->material) delete this->material; if (this->text != NULL) delete this->text; } /** * initializes the Aim */ void Aim::init() { this->setClassID(CL_CROSSHAIR, "Aim"); this->setName("Aim"); this->setLayer(E2D_LAYER_TOP); this->setRotationSpeed(30.0* (float)rand()/RAND_MAX + 10.0); this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0); this->setBindNode(this); this->material = new Material; this->source = NULL; this->anim = new tAnimation(this, &Aim::setSize); this->anim->setInfinity(ANIM_INF_CONSTANT); this->anim->addKeyFrame(500, .3, ANIM_LINEAR); this->anim->addKeyFrame(100, .2, ANIM_LINEAR); this->anim->addKeyFrame(50, .01, ANIM_LINEAR); this->text = new Text(); this->text->setLayer(this->getLayer()); this->text->setParent2D(this); this->text->setRelCoor2D(10, -50); this->text->setParentMode2D(E2D_PARENT_MOVEMENT); this->text->setText("Testing"); } void Aim::loadParams(const TiXmlElement* root) { static_cast(this)->loadParams(root); LoadParam(root, "texture", this, Aim, setTexture) .describe("the texture-file to load onto the Aim"); LoadParam(root, "size", this, Aim, setSize) .describe("the size of the Aim in Pixels"); LoadParam(root, "rotation-speed", this, Aim, setRotationSpeed) .describe("the Speed with which the Aim should rotate"); } void Aim::searchTarget(float range) { //FIXME// /* tIterator* iterator = State::getWorldEntityList()->getIterator(); WorldEntity* entity = iterator->firstElement(); while (likely(entity != NULL)) { if (entity->isA(CL_NPC) && this->source->getAbsCoor().x < entity->getAbsCoor().x && (this->source->getAbsCoor() - entity->getAbsCoor()).len() < range) { if (this->getParent() != entity) { this->anim->replay(); this->setParentSoft(entity, 5); } delete iterator; return; } entity = iterator->nextElement(); } delete iterator;*/ } /** * sets the size of the Aim. * @param size the size in pixels */ void Aim::setSize(float size) { this->setSize2D(size/2, size/2); } /** * sets the material to load * @param textureFile The texture-file to load onto the crosshair */ void Aim::setTexture(const char* textureFile) { this->material->setDiffuseMap(textureFile); } /** * ticks the Aim * @param dt the time to ticks */ void Aim::tick(float dt) { // let the crosshair rotate this->shiftDir2D(dt * rotationSpeed); char outputText[100]; sprintf(outputText, "%s - distance: %f\n", this->getParent()->getName(), (this->source->getAbsCoor() - this->getAbsCoor()).len()); this->text->setText(outputText); if (this->source->getAbsCoor().x > this->getAbsCoor().x ) this->searchTarget(1000); // float z = 0.0f; // glReadPixels ((int)this->getAbsCoor2D().x, // GraphicsEngine::getInstance()->getResolutionY()-(int)this->getAbsCoor2D().y-1, // 1, // 1, // GL_DEPTH_COMPONENT, // GL_FLOAT, // &z); // // // GLdouble objX=.0, objY=.0, objZ=.0; // gluUnProject(this->getAbsCoor2D().x, // GraphicsEngine::getInstance()->getResolutionY()-this->getAbsCoor2D().y-1, // .99, // z // GraphicsEngine::modMat, // GraphicsEngine::projMat, // GraphicsEngine::viewPort, // &objX, // &objY, // &objZ ); // // this->setAbsCoor(objX, objY, objZ); } /** * draws the crosshair */ void Aim::draw() const { glPushMatrix(); glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0); glRotatef(this->getAbsDir2D(), 0,0,1); this->material->select(); glBegin(GL_TRIANGLE_STRIP); glTexCoord2f(0, 0); glVertex2f(-this->getSizeX2D(), -this->getSizeY2D()); glTexCoord2f(1, 0); glVertex2f(this->getSizeX2D(), -this->getSizeY2D()); glTexCoord2f(0, 1); glVertex2f(-this->getSizeX2D(), this->getSizeY2D()); glTexCoord2f(1, 1); glVertex2f(this->getSizeX2D(), this->getSizeY2D()); glEnd(); glPopMatrix(); }