Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/world_entities/src/world_entities/weapons/aim.cc @ 5560

Last change on this file since 5560 was 5560, checked in by bensch, 18 years ago

orxonox/branches/world_entities: AimingTurrets zoom in to the selected Entities… looks quite good, if i may say so, but it is quite buggy

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