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