[10678] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * Dominik Solenicki |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "WingmanController.h" |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | namespace orxonox |
---|
| 33 | { |
---|
| 34 | |
---|
| 35 | RegisterClass(WingmanController); |
---|
[10729] | 36 | |
---|
| 37 | |
---|
[10717] | 38 | WingmanController::WingmanController(Context* context) : CommonController(context) |
---|
[10678] | 39 | { |
---|
| 40 | RegisterObject(WingmanController); |
---|
[10719] | 41 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&WingmanController::action, this))); |
---|
[10725] | 42 | this->myLeader_ = 0; |
---|
[10731] | 43 | this->rank_ = WINGMAN; |
---|
| 44 | |
---|
[10678] | 45 | } |
---|
| 46 | |
---|
| 47 | WingmanController::~WingmanController() |
---|
| 48 | { |
---|
[10725] | 49 | |
---|
[10678] | 50 | } |
---|
| 51 | |
---|
[10731] | 52 | void WingmanController::tick(float dt) |
---|
| 53 | { |
---|
| 54 | //------------------------------------------------------- |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | if (!this->isActive()) |
---|
| 58 | return; |
---|
| 59 | //--------------------------Stay in formation-------------------------- |
---|
| 60 | if (this->bHasTargetPosition_) |
---|
| 61 | { |
---|
| 62 | //targetPosition_ and targetOrientation_ are set by the Leader in its action() |
---|
| 63 | this->moveToTargetPosition(); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | //--------------------------Attack same target as the Leader-------------------------- |
---|
| 67 | |
---|
| 68 | /*if (this->target_) |
---|
| 69 | { |
---|
| 70 | this->aimAtTarget(); |
---|
| 71 | this->doFire(); |
---|
| 72 | } |
---|
| 73 | */ |
---|
| 74 | |
---|
| 75 | //orxout(internal_error) << "I am " << this << endl; |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | SUPER(WingmanController, tick, dt); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | void WingmanController::action() |
---|
| 82 | { |
---|
| 83 | if (!this->myLeader_) |
---|
| 84 | { |
---|
| 85 | CommonController* newLeader = findNewLeader(); |
---|
| 86 | this->myLeader_ = newLeader; |
---|
| 87 | if (newLeader) |
---|
| 88 | orxout(internal_error) << "new Leader set" << endl; |
---|
| 89 | else |
---|
| 90 | orxout(internal_error) << "0 leader" << endl; |
---|
| 91 | |
---|
| 92 | } |
---|
| 93 | else |
---|
| 94 | { |
---|
| 95 | |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | |
---|
[10717] | 102 | CommonController* WingmanController::findNewLeader() |
---|
| 103 | { |
---|
| 104 | |
---|
| 105 | if (!this->getControllableEntity()) |
---|
[10722] | 106 | return 0; |
---|
[10717] | 107 | |
---|
[10722] | 108 | CommonController* closestLeader = 0; |
---|
| 109 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
| 110 | |
---|
[10719] | 111 | for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it) |
---|
[10717] | 112 | { |
---|
[10722] | 113 | //0ptr? |
---|
[10731] | 114 | if (!it || |
---|
| 115 | (it->getRank() != SECTIONLEADER && it->getRank() != DIVISIONLEADER) || |
---|
| 116 | !(it->getControllableEntity())) |
---|
[10722] | 117 | continue; |
---|
[10717] | 118 | //same team? |
---|
[10722] | 119 | if (this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam()) |
---|
[10717] | 120 | continue; |
---|
| 121 | //is equal to this? |
---|
[10719] | 122 | if (it->getControllableEntity() == this->getControllableEntity()) |
---|
[10717] | 123 | continue; |
---|
| 124 | |
---|
[10719] | 125 | float distance = (it->getControllableEntity()->getPosition() - this->getControllableEntity()->getPosition()).length(); |
---|
[10722] | 126 | if (distance < minDistance && !(it->hasWingman())) |
---|
| 127 | { |
---|
| 128 | closestLeader = *it; |
---|
| 129 | minDistance = distance; |
---|
| 130 | } |
---|
[10725] | 131 | |
---|
[10717] | 132 | } |
---|
[10722] | 133 | if (closestLeader) |
---|
| 134 | { |
---|
| 135 | if (closestLeader->setWingman(this)) |
---|
| 136 | return closestLeader; |
---|
| 137 | } |
---|
| 138 | return 0; |
---|
[10717] | 139 | } |
---|
[10722] | 140 | |
---|
[10719] | 141 | void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 142 | { |
---|
| 143 | SUPER(WingmanController, XMLPort, xmlelement, mode); |
---|
| 144 | |
---|
| 145 | //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); |
---|
| 146 | } |
---|
| 147 | |
---|
[10678] | 148 | |
---|
[10729] | 149 | |
---|
[10678] | 150 | } |
---|