Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5750 was 5750, checked in by bensch, 19 years ago

orxonox/trunk: merged the WorldEntities into the Trunk.
Merged with command:
svn merge branches/world_entities/ trunk/ -r5516:HEAD

conflics from world_entities changed in favor of branches/world_entity
all other conflict in favor of the trunk

File size: 4.9 KB
RevLine 
[4744]1/*
[1853]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.
[1855]10
11   ### File Specific:
[4779]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[5357]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON
[1853]17
[5556]18#include "aim.h"
[4781]19
[4832]20#include "load_param.h"
[4781]21#include "graphics_engine.h"
22#include "state.h"
[5750]23#include "list.h"
[4832]24#include "material.h"
[5750]25#include "t_animation.h"
26#include "text.h"
[4781]27
[5750]28#include "world_entity.h"
29
[1856]30using namespace std;
[1853]31
[1856]32
[3245]33/**
[4832]34 * standart constructor
35 */
[5750]36Aim::Aim (PNode* source, const TiXmlElement* root)
[4832]37{
38  this->init();
39
[5750]40  this->source = source;
41
[4832]42  if (root)
43    this->loadParams(root);
44  else
45    this->setTexture("maps/aim.png");
46}
47
48/**
[5556]49 * destroys a Aim
[3245]50*/
[5556]51Aim::~Aim ()
[3365]52{
[4832]53  if (this->material)
[5557]54    delete this->material;
[5750]55
56  if (this->text != NULL)
57    delete this->text;
[4832]58}
59
60/**
[5556]61 * initializes the Aim
[4832]62 */
[5556]63void Aim::init()
[4832]64{
[5556]65  this->setClassID(CL_CROSSHAIR, "Aim");
66  this->setName("Aim");
[4320]67
[5398]68  this->setLayer(E2D_LAYER_TOP);
[5750]69  this->setRotationSpeed(30.0* (float)rand()/RAND_MAX + 10.0);
[4832]70  this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0);
[4830]71
[5750]72  this->setBindNode(this);
[4832]73  this->material = new Material;
[5750]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");
[3365]87}
[1853]88
[5556]89void Aim::loadParams(const TiXmlElement* root)
[3543]90{
[4832]91  static_cast<PNode*>(this)->loadParams(root);
[4830]92
[5671]93  LoadParam(root, "texture", this, Aim, setTexture)
[5556]94      .describe("the texture-file to load onto the Aim");
[4831]95
[5671]96  LoadParam(root, "size", this, Aim, setSize)
[5556]97      .describe("the size of the Aim in Pixels");
[4832]98
[5671]99  LoadParam(root, "rotation-speed", this, Aim, setRotationSpeed)
[5556]100      .describe("the Speed with which the Aim should rotate");
[3543]101}
[4779]102
[5750]103void Aim::searchTarget(float range)
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) && this->source->getAbsCoor().x < entity->getAbsCoor().x && (this->source->getAbsCoor() - entity->getAbsCoor()).len() < range)
110    {
111      if (this->getParent() != entity)
112      {
113        this->anim->replay();
114        this->setParentSoft(entity, 5);
115      }
116      delete iterator;
117      return;
118    }
119    entity = iterator->nextElement();
120  }
[4832]121
[5750]122  delete iterator;
123}
124
125
126
[4832]127/**
[5556]128 * sets the size of the Aim.
[4832]129 * @param size the size in pixels
130 */
[5556]131void Aim::setSize(float size)
[4832]132{
[5378]133  this->setSize2D(size/2, size/2);
134}
[4832]135
136/**
137 * sets the material to load
138 * @param textureFile The texture-file to load onto the crosshair
139 */
[5556]140void Aim::setTexture(const char* textureFile)
[4832]141{
142  this->material->setDiffuseMap(textureFile);
143}
144
145/**
[5556]146 * ticks the Aim
[4832]147 * @param dt the time to ticks
148 */
[5556]149void Aim::tick(float dt)
[4832]150{
[4834]151  // let the crosshair rotate
[5378]152  this->shiftDir2D(dt * rotationSpeed);
[4781]153
[5750]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);
[4832]157
158
[5750]159  if (this->source->getAbsCoor().x > this->getAbsCoor().x )
160    this->searchTarget(1000);
161//   float z = 0.0f;
162//   glReadPixels ((int)this->getAbsCoor2D().x,
163//                  GraphicsEngine::getInstance()->getResolutionY()-(int)this->getAbsCoor2D().y-1,
164//                  1,
165//                  1,
166//                  GL_DEPTH_COMPONENT,
167//                  GL_FLOAT,
168//                  &z);
169//
170//
171//   GLdouble objX=.0, objY=.0, objZ=.0;
172//   gluUnProject(this->getAbsCoor2D().x,
173//                GraphicsEngine::getInstance()->getResolutionY()-this->getAbsCoor2D().y-1,
174//                .99,  // z
175//                GraphicsEngine::modMat,
176//                GraphicsEngine::projMat,
177//                GraphicsEngine::viewPort,
178//                &objX,
179//                &objY,
180//                &objZ );
181//
182//   this->setAbsCoor(objX, objY, objZ);
[4834]183}
184
185/**
186 * draws the crosshair
187 */
[5556]188void Aim::draw() const
[4834]189{
[5750]190
[4955]191  glPushMatrix();
[5378]192  glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0);
[4830]193
[5378]194  glRotatef(this->getAbsDir2D(), 0,0,1);
[4832]195  this->material->select();
196  glBegin(GL_TRIANGLE_STRIP);
197  glTexCoord2f(0, 0);
[5378]198  glVertex2f(-this->getSizeX2D(), -this->getSizeY2D());
[4832]199  glTexCoord2f(1, 0);
[5378]200  glVertex2f(this->getSizeX2D(), -this->getSizeY2D());
[4832]201  glTexCoord2f(0, 1);
[5378]202  glVertex2f(-this->getSizeX2D(), this->getSizeY2D());
[4832]203  glTexCoord2f(1, 1);
[5378]204  glVertex2f(this->getSizeX2D(), this->getSizeY2D());
[4832]205  glEnd();
[4955]206  glPopMatrix();
[4830]207
[4781]208}
Note: See TracBrowser for help on using the repository browser.