| 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: Patrick Boenzli | 
|---|
| 13 |    co-programmer: ... | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON | 
|---|
| 17 |  | 
|---|
| 18 | #include "aiming_system.h" | 
|---|
| 19 |  | 
|---|
| 20 | #include "util/loading/load_param.h" | 
|---|
| 21 |  | 
|---|
| 22 | #include "state.h" | 
|---|
| 23 | #include "debug.h" | 
|---|
| 24 |  | 
|---|
| 25 | #include "aabb.h" | 
|---|
| 26 | #include "obb_tree.h" | 
|---|
| 27 |  | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 | ObjectListDefinition(AimingSystem); | 
|---|
| 31 |  | 
|---|
| 32 | /** | 
|---|
| 33 |  * standart constructor | 
|---|
| 34 |  */ | 
|---|
| 35 | AimingSystem::AimingSystem (WorldEntity* entity) | 
|---|
| 36 |   : WorldEntity() | 
|---|
| 37 | { | 
|---|
| 38 |   this->owner = entity; | 
|---|
| 39 |  | 
|---|
| 40 |   this->init(); | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 |  | 
|---|
| 44 | /** | 
|---|
| 45 |  * destroys a AimingSystem | 
|---|
| 46 | */ | 
|---|
| 47 | AimingSystem::~AimingSystem () | 
|---|
| 48 | {} | 
|---|
| 49 |  | 
|---|
| 50 |  | 
|---|
| 51 | /** | 
|---|
| 52 |  * initializes the AimingSystem | 
|---|
| 53 |  */ | 
|---|
| 54 | void AimingSystem::init() | 
|---|
| 55 | { | 
|---|
| 56 |   this->registerObject(this, AimingSystem::_objectList); | 
|---|
| 57 |   this->setName("AimingSystem"); | 
|---|
| 58 |  | 
|---|
| 59 |   //this->loadModel("models/guns/targeting_system_body2.obj"); | 
|---|
| 60 | //   this->loadModel("models/ships/fighter.obj"); | 
|---|
| 61 |  | 
|---|
| 62 |   // registering default reactions: | 
|---|
| 63 |   this->unsubscribeReaction(CREngine::CR_OBJECT_DAMAGE); | 
|---|
| 64 |   this->subscribeReaction(CREngine::CR_OBJECT_DAMAGE, WorldEntity::staticClassID()); | 
|---|
| 65 |  | 
|---|
| 66 |   this->range = 1000.0f; | 
|---|
| 67 |   this->sideLength = 2.0f; | 
|---|
| 68 |  | 
|---|
| 69 |   // new obb tree | 
|---|
| 70 |   this->obbTree = new OBBTree(); | 
|---|
| 71 |   this->obbTree->createBox(Vector(0.0f, 0.0f, 0.0f), Vector(this->range, this->sideLength, this->sideLength)); | 
|---|
| 72 |   this->setOBBTree(this->obbTree); | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 |  | 
|---|
| 76 |  | 
|---|
| 77 | /** | 
|---|
| 78 |  * get back the nearest target | 
|---|
| 79 |  * @returns the nerest target | 
|---|
| 80 |  */ | 
|---|
| 81 | WorldEntity* AimingSystem::getNearestTarget() | 
|---|
| 82 | { | 
|---|
| 83 |   if( this->selectionList.size() == 0) | 
|---|
| 84 |     return NULL; | 
|---|
| 85 |  | 
|---|
| 86 |  | 
|---|
| 87 |   WorldEntity* nearestEntity     = NULL; | 
|---|
| 88 |   float        distance          = 0.0f; | 
|---|
| 89 |   float        smalestDistance   = this->range * 5.0f; | 
|---|
| 90 |  | 
|---|
| 91 |  | 
|---|
| 92 |   for(unsigned int i = 0; i < this->selectionList.size(); i++) | 
|---|
| 93 |   { | 
|---|
| 94 |     distance = fabs((this->getAbsCoor() - this->selectionList[i]->getAbsCoor()).len()); | 
|---|
| 95 |     if( distance < smalestDistance) | 
|---|
| 96 |     { | 
|---|
| 97 |       nearestEntity = this->selectionList[i]; | 
|---|
| 98 |       smalestDistance = distance; | 
|---|
| 99 |     } | 
|---|
| 100 |   } | 
|---|
| 101 |  | 
|---|
| 102 |   PRINTF(0)("entity: %s\n", nearestEntity->getClassCName()); | 
|---|
| 103 |     return nearestEntity; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 |  | 
|---|
| 107 | /** | 
|---|
| 108 |  * called when an object is "selected" | 
|---|
| 109 |  *  @param damage damage to be dealt | 
|---|
| 110 |  *  @param killer the entity | 
|---|
| 111 |  */ | 
|---|
| 112 | void AimingSystem::hit(float damage, WorldEntity* killer) | 
|---|
| 113 | { | 
|---|
| 114 |   if( this->owner != killer) | 
|---|
| 115 |   { | 
|---|
| 116 |     //PRINTF(0)("real hit: %s\n", killer->getClassCName()); | 
|---|
| 117 |     this->selectionList.push_back(killer); | 
|---|
| 118 |   } | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 |  | 
|---|
| 122 |  | 
|---|
| 123 | /** | 
|---|
| 124 |  * ticks the AimingSystem | 
|---|
| 125 |  * @param dt the time to ticks | 
|---|
| 126 |  */ | 
|---|
| 127 | void AimingSystem::tick(float dt) | 
|---|
| 128 | { | 
|---|
| 129 |  | 
|---|
| 130 |  | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 |  | 
|---|
| 134 | /** | 
|---|
| 135 |  * draws the crosshair | 
|---|
| 136 |  */ | 
|---|
| 137 | void AimingSystem::draw() const | 
|---|
| 138 | { | 
|---|
| 139 |   WorldEntity::draw(); | 
|---|
| 140 |  | 
|---|
| 141 | //   glMatrixMode(GL_MODELVIEW); | 
|---|
| 142 | //   glPushMatrix(); | 
|---|
| 143 | // | 
|---|
| 144 | //   /* translate */ | 
|---|
| 145 | //   glTranslatef (this->getAbsCoor ().x, | 
|---|
| 146 | //                 this->getAbsCoor ().y, | 
|---|
| 147 | //                 this->getAbsCoor ().z); | 
|---|
| 148 | //   Vector tmpRot = this->getAbsDir().getSpacialAxis(); | 
|---|
| 149 | //   glRotatef (this->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); | 
|---|
| 150 | // | 
|---|
| 151 | //   this->obbTree->drawBV(0, 1); | 
|---|
| 152 | // | 
|---|
| 153 | //   glPopMatrix(); | 
|---|
| 154 |  | 
|---|
| 155 | } | 
|---|