Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: simple-aiming function, with visible targetting-system

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