[9156] | 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: |
---|
[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) |
---|
[9156] | 37 | { |
---|
| 38 | this->init(); |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * destroys a AimingSystem |
---|
| 44 | */ |
---|
| 45 | AimingSystem::~AimingSystem () |
---|
| 46 | {} |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | /** |
---|
| 50 | * initializes the AimingSystem |
---|
| 51 | */ |
---|
| 52 | void AimingSystem::init() |
---|
| 53 | { |
---|
| 54 | this->setClassID(CL_CROSSHAIR, "AimingSystem"); |
---|
| 55 | this->setName("AimingSystem"); |
---|
[9166] | 56 | |
---|
| 57 | this->loadModel("models/guns/targeting_system_body.obj"); |
---|
[9156] | 58 | |
---|
[9168] | 59 | // registering default reactions: |
---|
| 60 | this->subscribeReaction(CREngine::CR_OBJECT_DAMAGE, CL_WORLD_ENTITY); |
---|
[9156] | 61 | } |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | |
---|
[9160] | 65 | /** |
---|
| 66 | * get back the nearest target |
---|
[9163] | 67 | * @returns the nerest target |
---|
[9160] | 68 | */ |
---|
[9166] | 69 | WorldEntity* AimingSystem::getNearestTarget() |
---|
[9160] | 70 | { |
---|
| 71 | if( this->selectionList.size() == 0) |
---|
| 72 | return NULL; |
---|
| 73 | else if( this->selectionList.size() == 1) |
---|
| 74 | return this->selectionList.back(); |
---|
[9156] | 75 | |
---|
[9168] | 76 | WorldEntity* nearestEntity = NULL; |
---|
[9163] | 77 | float distance = 0.0f; |
---|
| 78 | float smalestDistance = this->range * 5.0f; |
---|
[9160] | 79 | |
---|
[9163] | 80 | |
---|
[9160] | 81 | for( int i = 0; i < this->selectionList.size(); i++) |
---|
| 82 | { |
---|
[9163] | 83 | distance = fabs((this->getAbsCoor() - this->selectionList[i]->getAbsCoor()).len()); |
---|
| 84 | if( distance < smalestDistance) |
---|
| 85 | { |
---|
| 86 | nearestEntity = this->selectionList[i]; |
---|
| 87 | smalestDistance = distance; |
---|
| 88 | } |
---|
| 89 | } |
---|
[9160] | 90 | |
---|
[9168] | 91 | if( nearestEntity != this->owner) |
---|
| 92 | return nearestEntity; |
---|
| 93 | else return NULL; |
---|
[9160] | 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
[9156] | 97 | /** |
---|
[9160] | 98 | * called when an object is "selected" |
---|
| 99 | * @param damage damage to be dealt |
---|
| 100 | * @param killer the entity |
---|
| 101 | */ |
---|
| 102 | void AimingSystem::hit(float damage, WorldEntity* killer) |
---|
| 103 | { |
---|
| 104 | this->selectionList.push_back(killer); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | /** |
---|
[9156] | 110 | * ticks the AimingSystem |
---|
| 111 | * @param dt the time to ticks |
---|
| 112 | */ |
---|
| 113 | void AimingSystem::tick(float dt) |
---|
| 114 | { |
---|
[9163] | 115 | |
---|
[9168] | 116 | // this->selectionList.clear(); |
---|
[9156] | 117 | } |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | * draws the crosshair |
---|
| 122 | */ |
---|
| 123 | void AimingSystem::draw() const |
---|
| 124 | { |
---|
[9168] | 125 | WorldEntity::draw(); |
---|
| 126 | |
---|
| 127 | if( this->getOBBTree () != NULL) |
---|
| 128 | this->getOBBTree()->drawBV(0, 1); |
---|
[9156] | 129 | } |
---|