Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the proxy bache back with no conflicts

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
[9656]71  this->range = 1000;
[6724]72  this->angle = M_PI_4;
[9656]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");
[9656]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;
[9656]108  //printf("%d\n", this->targetGroup);
109  for (entity = State::getObjectManager()->getObjectList(this->targetGroup).begin();
110       entity != State::getObjectManager()->getObjectList(this->targetGroup).end();
[6222]111       entity ++)
[5750]112  {
[6637]113    diffVec = ( (*entity)->getAbsCoor() - this->source->getAbsCoor() );
114
[9656]115    if ( diffVec.len() < range )//&&  acos( (this->source->getAbsDirX()).dot(diffVec)/(diffVec.len() * (this->source->getAbsDirX()).len() ) )  < angle)
[5750]116    {
[6724]117      //if (this->getParent() != (*entity))
[5750]118      {
[9656]119        printf("found target::: %d %s::%s\n", (*entity)->getOMListNumber(), (*entity)->getClassCName(), (*entity)->getCName());
[5750]120        this->anim->replay();
[6222]121        this->setParentSoft(*entity, 5);
122        return;
[5750]123      }
124    }
125  }
[6724]126
127   //if no target found:
128   this->setParent(PNode::getNullParent());
[9656]129}
[6724]130
[9656]131void Aim::setTargetGroupS(const std::string& groupName)
132{
133  OM_LIST id = ObjectManager::StringToOMList(groupName);
134  if (id != OM_NULL)
135    this->setTargetGroup(id);
136  else
137    PRINTF(2)("List %s not found for targetting\n", groupName.c_str());
[5750]138}
139
[4832]140/**
[6305]141 * @brief sets the size of the Aim.
[4832]142 * @param size the size in pixels
143 */
[5556]144void Aim::setSize(float size)
[4832]145{
[5378]146  this->setSize2D(size/2, size/2);
147}
[4832]148
149/**
150 * sets the material to load
151 * @param textureFile The texture-file to load onto the crosshair
152 */
[7221]153void Aim::setTexture(const std::string& textureFile)
[4832]154{
[9656]155  this->material.setDiffuseMap(textureFile);
[4832]156}
157
158/**
[5556]159 * ticks the Aim
[4832]160 * @param dt the time to ticks
161 */
[5556]162void Aim::tick(float dt)
[4832]163{
[4834]164  // let the crosshair rotate
[5378]165  this->shiftDir2D(dt * rotationSpeed);
[4781]166
[6305]167//   char outputText[100];
168//   sprintf(outputText, "%s - distance: %f\n", this->getParent()->getName(), (this->source->getAbsCoor() - this->getAbsCoor()).len());
169//   this->text->setText(outputText);
[4832]170
171
[6637]172//  if (this->source->getAbsCoor().x > this->getAbsCoor().x )
[6724]173  diffVec = ( this->getAbsCoor() - this->source->getAbsCoor() );
174//only look for target if the aim hasn`t locked a target yet or if the actual target is out of range
[6760]175   if(this->getParent() == PNode::getNullParent() ||
[9656]176      diffVec.len() > range )// ||
177     //( acos( (this->source->getAbsDirX()).dot(diffVec)/(diffVec.len() * (this->source->getAbsDirX()).len() ) ) > angle))
[6724]178    {
[6760]179     this->setParentSoft(PNode::getNullParent(),5);
[6637]180     this->searchTarget();
[6724]181    }
182
[5750]183//   float z = 0.0f;
184//   glReadPixels ((int)this->getAbsCoor2D().x,
185//                  GraphicsEngine::getInstance()->getResolutionY()-(int)this->getAbsCoor2D().y-1,
186//                  1,
187//                  1,
188//                  GL_DEPTH_COMPONENT,
189//                  GL_FLOAT,
190//                  &z);
191//
192//
193//   GLdouble objX=.0, objY=.0, objZ=.0;
194//   gluUnProject(this->getAbsCoor2D().x,
195//                GraphicsEngine::getInstance()->getResolutionY()-this->getAbsCoor2D().y-1,
196//                .99,  // z
197//                GraphicsEngine::modMat,
198//                GraphicsEngine::projMat,
199//                GraphicsEngine::viewPort,
200//                &objX,
201//                &objY,
202//                &objZ );
[6760]203//aa
[5750]204//   this->setAbsCoor(objX, objY, objZ);
[4834]205}
206
207/**
208 * draws the crosshair
209 */
[5556]210void Aim::draw() const
[4834]211{
[5750]212
[6724]213 if( this->getParent() != PNode::getNullParent() )
214  {
[4955]215  glPushMatrix();
[5378]216  glTranslatef(this->getAbsCoor2D().x, this->getAbsCoor2D().y, 0);
[4830]217
[5378]218  glRotatef(this->getAbsDir2D(), 0,0,1);
[9656]219  this->material.select();
[4832]220  glBegin(GL_TRIANGLE_STRIP);
221  glTexCoord2f(0, 0);
[5378]222  glVertex2f(-this->getSizeX2D(), -this->getSizeY2D());
[4832]223  glTexCoord2f(1, 0);
[5378]224  glVertex2f(this->getSizeX2D(), -this->getSizeY2D());
[4832]225  glTexCoord2f(0, 1);
[5378]226  glVertex2f(-this->getSizeX2D(), this->getSizeY2D());
[4832]227  glTexCoord2f(1, 1);
[5378]228  glVertex2f(this->getSizeX2D(), this->getSizeY2D());
[4832]229  glEnd();
[4955]230  glPopMatrix();
[6724]231  }
[4830]232
[4781]233}
Note: See TracBrowser for help on using the repository browser.