Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: PNode compiles again
PNode is now handled with the erase function of stl, rather than remove (which is totally the wrong way when iterating through a list and delete elements)

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