Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6606 was 6606, checked in by snellen, 18 years ago

aim.cc, targeting_turret.cc and aiming turret.cc updated

File size: 5.5 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 "material.h"
24#include "t_animation.h"
25#include "text.h"
26
27#include "world_entity.h"
28
29using namespace std;
30
31
32/**
33 * standart constructor
34 */
35Aim::Aim (PNode* source, const TiXmlElement* root)
36{
37  this->init();
38
39  this->source = source;
40
41  if (root)
42    this->loadParams(root);
43  else
44    this->setTexture("maps/aim.png");
45}
46
47/**
48 * destroys a Aim
49*/
50Aim::~Aim ()
51{
52  if (this->material)
53    delete this->material;
54
55/*  if (this->text != NULL)
56    delete this->text;*/
57}
58
59/**
60 * initializes the Aim
61 */
62void Aim::init()
63{
64  this->setClassID(CL_CROSSHAIR, "Aim");
65  this->setName("Aim");
66
67  this->setLayer(E2D_LAYER_TOP);
68  this->setRotationSpeed(30.0* (float)rand()/RAND_MAX + 10.0);
69  this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0);
70
71  this->setBindNode(this);
72  this->material = new Material;
73  this->source = NULL;
74
75  this->anim = new tAnimation<Aim>(this, &Aim::setSize);
76  this->anim->setInfinity(ANIM_INF_CONSTANT);
77  this->anim->addKeyFrame(500, .3, ANIM_LINEAR);
78  this->anim->addKeyFrame(100, .2, ANIM_LINEAR);
79  this->anim->addKeyFrame(50, .01, ANIM_LINEAR);
80
81/*  this->text = new Text();
82  this->text->setLayer(this->getLayer());
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(root, "texture", this, Aim, setTexture)
94      .describe("the texture-file to load onto the Aim");
95
96  LoadParam(root, "size", this, Aim, setSize)
97      .describe("the size of the Aim in Pixels");
98
99  LoadParam(root, "rotation-speed", this, Aim, setRotationSpeed)
100      .describe("the Speed with which the Aim should rotate");
101}
102
103void Aim::searchTarget(float range, float angle)
104{
105  std::list<WorldEntity*>::iterator entity;
106  Vector diffVec(0.0, 0.0, 0.0);
107  diffVec = ( this->getAbsCoor() - this->source->getAbsCoor() );
108
109//only look for target if the aim hasn`t locked a target yet or if the actual target is out of range
110  if( this == PNode::getNullParent() || diffVec.len() > range  || ( acos( (this->source->getAbsDirX()).dot(diffVec)/(diffVec.len() * (this->source->getAbsDirX()).len() ) ) > angle && (this->source->getAbsDirX()).dot(diffVec) > 0))
111  for (entity = State::getObjectManager()->getObjectList(OM_GROUP_00).begin();
112       entity != State::getObjectManager()->getObjectList(OM_GROUP_00).end();
113       entity ++)
114  {
115    diffVec = ( (*entity)->getAbsCoor() - this->source->getAbsCoor() );
116
117    if ( diffVec.len() < range  &&  acos( (this->source->getAbsDirX()).dot(diffVec)/(diffVec.len() * (this->source->getAbsDirX()).len() ) ) < angle && (this->source->getAbsDirX()).dot(diffVec) > 0)
118    {
119      if (this->getParent() != (*entity))
120      {
121        this->anim->replay();
122        this->setParentSoft(*entity, 5);
123        return;
124      }
125    }
126  }
127//if no target found:
128  this->setParent(PNode::getNullParent());
129}
130
131
132
133/**
134 * @brief sets the size of the Aim.
135 * @param size the size in pixels
136 */
137void Aim::setSize(float size)
138{
139  this->setSize2D(size/2, size/2);
140}
141
142/**
143 * sets the material to load
144 * @param textureFile The texture-file to load onto the crosshair
145 */
146void Aim::setTexture(const char* textureFile)
147{
148  this->material->setDiffuseMap(textureFile);
149}
150
151/**
152 * ticks the Aim
153 * @param dt the time to ticks
154 */
155void Aim::tick(float dt)
156{
157  // let the crosshair rotate
158  this->shiftDir2D(dt * rotationSpeed);
159
160//   char outputText[100];
161//   sprintf(outputText, "%s - distance: %f\n", this->getParent()->getName(), (this->source->getAbsCoor() - this->getAbsCoor()).len());
162//   this->text->setText(outputText);
163
164
165//  if (this->source->getAbsCoor().x > this->getAbsCoor().x )
166//    this->searchTarget(1000,M_PI_2);
167//   float z = 0.0f;
168//   glReadPixels ((int)this->getAbsCoor2D().x,
169//                  GraphicsEngine::getInstance()->getResolutionY()-(int)this->getAbsCoor2D().y-1,
170//                  1,
171//                  1,
172//                  GL_DEPTH_COMPONENT,
173//                  GL_FLOAT,
174//                  &z);
175//
176//
177//   GLdouble objX=.0, objY=.0, objZ=.0;
178//   gluUnProject(this->getAbsCoor2D().x,
179//                GraphicsEngine::getInstance()->getResolutionY()-this->getAbsCoor2D().y-1,
180//                .99,  // z
181//                GraphicsEngine::modMat,
182//                GraphicsEngine::projMat,
183//                GraphicsEngine::viewPort,
184//                &objX,
185//                &objY,
186//                &objZ );
187//
188//   this->setAbsCoor(objX, objY, objZ);
189}
190
191/**
192 * draws the crosshair
193 */
194void Aim::draw() const
195{
196
197  glPushMatrix();
198  glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0);
199
200  glRotatef(this->getAbsDir2D(), 0,0,1);
201  this->material->select();
202  glBegin(GL_TRIANGLE_STRIP);
203  glTexCoord2f(0, 0);
204  glVertex2f(-this->getSizeX2D(), -this->getSizeY2D());
205  glTexCoord2f(1, 0);
206  glVertex2f(this->getSizeX2D(), -this->getSizeY2D());
207  glTexCoord2f(0, 1);
208  glVertex2f(-this->getSizeX2D(), this->getSizeY2D());
209  glTexCoord2f(1, 1);
210  glVertex2f(this->getSizeX2D(), this->getSizeY2D());
211  glEnd();
212  glPopMatrix();
213
214}
Note: See TracBrowser for help on using the repository browser.