Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/world_entities: smoother aiming, and also displaying Distance (this is not too nice, but quite good to explain a few Things …

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