Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

targetting turrets are firing all the time now, but the hack is out :/

File size: 6.0 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
[7193]20#include "util/loading/load_param.h"
[4781]21#include "graphics_engine.h"
22#include "state.h"
[4832]23#include "material.h"
[5750]24#include "t_animation.h"
25#include "text.h"
[4781]26
[5750]27#include "world_entity.h"
28
[1853]29
[1856]30
[9406]31
[3245]32/**
[4832]33 * standart constructor
34 */
[5750]35Aim::Aim (PNode* source, const TiXmlElement* root)
[4832]36{
37  this->init();
38
[5750]39  this->source = source;
40
[4832]41  if (root)
42    this->loadParams(root);
43  else
44    this->setTexture("maps/aim.png");
45}
46
47/**
[5556]48 * destroys a Aim
[3245]49*/
[5556]50Aim::~Aim ()
[3365]51{
[6305]52/*  if (this->text != NULL)
53    delete this->text;*/
[4832]54}
55
56/**
[5556]57 * initializes the Aim
[4832]58 */
[5556]59void Aim::init()
[4832]60{
[5556]61  this->setClassID(CL_CROSSHAIR, "Aim");
62  this->setName("Aim");
[4320]63
[5398]64  this->setLayer(E2D_LAYER_TOP);
[5750]65  this->setRotationSpeed(30.0* (float)rand()/RAND_MAX + 10.0);
[4832]66  this->setSize(GraphicsEngine::getInstance()->getResolutionX()/10.0);
[4830]67
[5750]68  this->setBindNode(this);
69  this->source = NULL;
70
[6724]71  this->range = 10000;
72  this->angle = M_PI_4;
[9518]73  this->targetGroup = OM_GROUP_01;
[5750]74  this->anim = new tAnimation<Aim>(this, &Aim::setSize);
75  this->anim->setInfinity(ANIM_INF_CONSTANT);
76  this->anim->addKeyFrame(500, .3, ANIM_LINEAR);
77  this->anim->addKeyFrame(100, .2, ANIM_LINEAR);
78  this->anim->addKeyFrame(50, .01, ANIM_LINEAR);
79
[6760]80
[6305]81/*  this->text = new Text();
[5779]82  this->text->setLayer(this->getLayer());
[5750]83  this->text->setParent2D(this);
84  this->text->setRelCoor2D(10, -50);
85  this->text->setParentMode2D(E2D_PARENT_MOVEMENT);
[6305]86  this->text->setText("Testing");*/
[3365]87}
[1853]88
[5556]89void Aim::loadParams(const TiXmlElement* root)
[3543]90{
[6512]91  PNode::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");
[9516]101
102  LoadParam(root, "target-group", this, Aim, setTargetGroupS);
[3543]103}
[4779]104
[6637]105void Aim::searchTarget()
[5750]106{
[7370]107  ObjectManager::EntityList::iterator entity;
[9516]108  //printf("%d\n", this->targetGroup);
[6222]109
[9516]110  for (entity = State::getObjectManager()->getObjectList(this->targetGroup).begin();
111       entity != State::getObjectManager()->getObjectList(this->targetGroup).end();
[6222]112       entity ++)
[5750]113  {
[6637]114    diffVec = ( (*entity)->getAbsCoor() - this->source->getAbsCoor() );
115
[9516]116    if ( diffVec.len() < range )//&&  acos( (this->source->getAbsDirX()).dot(diffVec)/(diffVec.len() * (this->source->getAbsDirX()).len() ) )  < angle)
[5750]117    {
[6724]118      //if (this->getParent() != (*entity))
[5750]119      {
[9516]120        printf("found target::: %d %s::%s\n", (*entity)->getOMListNumber(), (*entity)->getClassCName(), (*entity)->getCName());
[5750]121        this->anim->replay();
[9516]122        this->setParent/*Soft*/(*entity/*, 5 */);
[6222]123        return;
[5750]124      }
125    }
126  }
[6724]127
128   //if no target found:
129   this->setParent(PNode::getNullParent());
[9516]130}
[6724]131
[9516]132void Aim::setTargetGroupS(const std::string& groupName)
133{
134  OM_LIST id = ObjectManager::StringToOMList(groupName);
135  if (id != OM_NULL)
136    this->setTargetGroup(id);
137  else
138    PRINTF(2)("List %s not found for targetting\n", groupName.c_str());
[5750]139}
140
[4832]141/**
[6305]142 * @brief sets the size of the Aim.
[4832]143 * @param size the size in pixels
144 */
[5556]145void Aim::setSize(float size)
[4832]146{
[5378]147  this->setSize2D(size/2, size/2);
148}
[4832]149
150/**
151 * sets the material to load
152 * @param textureFile The texture-file to load onto the crosshair
153 */
[7221]154void Aim::setTexture(const std::string& textureFile)
[4832]155{
[9516]156  this->material.setDiffuseMap(textureFile);
[4832]157}
158
159/**
[5556]160 * ticks the Aim
[4832]161 * @param dt the time to ticks
162 */
[5556]163void Aim::tick(float dt)
[4832]164{
[4834]165  // let the crosshair rotate
[5378]166  this->shiftDir2D(dt * rotationSpeed);
[4781]167
[6305]168//   char outputText[100];
169//   sprintf(outputText, "%s - distance: %f\n", this->getParent()->getName(), (this->source->getAbsCoor() - this->getAbsCoor()).len());
170//   this->text->setText(outputText);
[4832]171
172
[6637]173//  if (this->source->getAbsCoor().x > this->getAbsCoor().x )
[6724]174  diffVec = ( this->getAbsCoor() - this->source->getAbsCoor() );
175//only look for target if the aim hasn`t locked a target yet or if the actual target is out of range
[6760]176   if(this->getParent() == PNode::getNullParent() ||
[9516]177      diffVec.len() > range )// ||
178     //( acos( (this->source->getAbsDirX()).dot(diffVec)/(diffVec.len() * (this->source->getAbsDirX()).len() ) ) > angle))
[6724]179    {
[6760]180     this->setParentSoft(PNode::getNullParent(),5);
[6637]181     this->searchTarget();
[6724]182    }
183
[5750]184//   float z = 0.0f;
185//   glReadPixels ((int)this->getAbsCoor2D().x,
186//                  GraphicsEngine::getInstance()->getResolutionY()-(int)this->getAbsCoor2D().y-1,
187//                  1,
188//                  1,
189//                  GL_DEPTH_COMPONENT,
190//                  GL_FLOAT,
191//                  &z);
192//
193//
194//   GLdouble objX=.0, objY=.0, objZ=.0;
195//   gluUnProject(this->getAbsCoor2D().x,
196//                GraphicsEngine::getInstance()->getResolutionY()-this->getAbsCoor2D().y-1,
197//                .99,  // z
198//                GraphicsEngine::modMat,
199//                GraphicsEngine::projMat,
200//                GraphicsEngine::viewPort,
201//                &objX,
202//                &objY,
203//                &objZ );
[6760]204//aa
[5750]205//   this->setAbsCoor(objX, objY, objZ);
[4834]206}
207
208/**
209 * draws the crosshair
210 */
[5556]211void Aim::draw() const
[4834]212{
[5750]213
[6724]214 if( this->getParent() != PNode::getNullParent() )
215  {
[4955]216  glPushMatrix();
[5378]217  glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0);
[4830]218
[5378]219  glRotatef(this->getAbsDir2D(), 0,0,1);
[9516]220  this->material.select();
[4832]221  glBegin(GL_TRIANGLE_STRIP);
222  glTexCoord2f(0, 0);
[5378]223  glVertex2f(-this->getSizeX2D(), -this->getSizeY2D());
[4832]224  glTexCoord2f(1, 0);
[5378]225  glVertex2f(this->getSizeX2D(), -this->getSizeY2D());
[4832]226  glTexCoord2f(0, 1);
[5378]227  glVertex2f(-this->getSizeX2D(), this->getSizeY2D());
[4832]228  glTexCoord2f(1, 1);
[5378]229  glVertex2f(this->getSizeX2D(), this->getSizeY2D());
[4832]230  glEnd();
[4955]231  glPopMatrix();
[6724]232  }
[4830]233
[4781]234}
Note: See TracBrowser for help on using the repository browser.