Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/weapons/aim.cc @ 6054

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

orxonox/trunk: multiple new Reparenting modes in PNode.
Testing the stuff in GuidedMissile
Projectile has a PNode as reference not as pointer
some minor weapon changes

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