[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; |
---|
[10759] | 43 | this->rank_ = Rank::WINGMAN; |
---|
[10731] | 44 | |
---|
[10678] | 45 | } |
---|
| 46 | |
---|
| 47 | WingmanController::~WingmanController() |
---|
| 48 | { |
---|
[10725] | 49 | |
---|
[10678] | 50 | } |
---|
| 51 | |
---|
[10731] | 52 | void WingmanController::tick(float dt) |
---|
| 53 | { |
---|
| 54 | //------------------------------------------------------- |
---|
[10759] | 55 | /* if (this->target_) |
---|
| 56 | { |
---|
| 57 | this->aimAtTarget(); |
---|
| 58 | this->doFire(); |
---|
| 59 | this->bShooting_ = true; |
---|
| 60 | }*/ |
---|
[10731] | 61 | |
---|
| 62 | if (!this->isActive()) |
---|
| 63 | return; |
---|
| 64 | //--------------------------Stay in formation-------------------------- |
---|
| 65 | if (this->bHasTargetPosition_) |
---|
| 66 | { |
---|
| 67 | //targetPosition_ and targetOrientation_ are set by the Leader in its action() |
---|
| 68 | this->moveToTargetPosition(); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | //--------------------------Attack same target as the Leader-------------------------- |
---|
| 72 | |
---|
| 73 | /*if (this->target_) |
---|
| 74 | { |
---|
| 75 | this->aimAtTarget(); |
---|
| 76 | this->doFire(); |
---|
| 77 | } |
---|
| 78 | */ |
---|
| 79 | |
---|
| 80 | //orxout(internal_error) << "I am " << this << endl; |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | SUPER(WingmanController, tick, dt); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | void WingmanController::action() |
---|
| 87 | { |
---|
| 88 | if (!this->myLeader_) |
---|
| 89 | { |
---|
| 90 | CommonController* newLeader = findNewLeader(); |
---|
| 91 | this->myLeader_ = newLeader; |
---|
| 92 | if (newLeader) |
---|
| 93 | orxout(internal_error) << "new Leader set" << endl; |
---|
| 94 | else |
---|
[10759] | 95 | { |
---|
| 96 | //orxout(internal_error) << "0 leader" << endl; |
---|
| 97 | |
---|
| 98 | } |
---|
[10731] | 99 | |
---|
| 100 | } |
---|
| 101 | else |
---|
| 102 | { |
---|
| 103 | |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | |
---|
[10717] | 110 | CommonController* WingmanController::findNewLeader() |
---|
| 111 | { |
---|
| 112 | |
---|
| 113 | if (!this->getControllableEntity()) |
---|
[10722] | 114 | return 0; |
---|
[10717] | 115 | |
---|
[10722] | 116 | CommonController* closestLeader = 0; |
---|
| 117 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
| 118 | |
---|
[10719] | 119 | for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it) |
---|
[10717] | 120 | { |
---|
[10722] | 121 | //0ptr? |
---|
[10731] | 122 | if (!it || |
---|
[10759] | 123 | (it->getRank() != Rank::SECTIONLEADER && it->getRank() != Rank::DIVISIONLEADER) || |
---|
[10731] | 124 | !(it->getControllableEntity())) |
---|
[10722] | 125 | continue; |
---|
[10717] | 126 | //same team? |
---|
[10722] | 127 | if (this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam()) |
---|
[10717] | 128 | continue; |
---|
| 129 | //is equal to this? |
---|
[10719] | 130 | if (it->getControllableEntity() == this->getControllableEntity()) |
---|
[10717] | 131 | continue; |
---|
| 132 | |
---|
[10719] | 133 | float distance = (it->getControllableEntity()->getPosition() - this->getControllableEntity()->getPosition()).length(); |
---|
[10722] | 134 | if (distance < minDistance && !(it->hasWingman())) |
---|
| 135 | { |
---|
| 136 | closestLeader = *it; |
---|
| 137 | minDistance = distance; |
---|
| 138 | } |
---|
[10725] | 139 | |
---|
[10717] | 140 | } |
---|
[10722] | 141 | if (closestLeader) |
---|
| 142 | { |
---|
| 143 | if (closestLeader->setWingman(this)) |
---|
| 144 | return closestLeader; |
---|
| 145 | } |
---|
| 146 | return 0; |
---|
[10717] | 147 | } |
---|
[10722] | 148 | |
---|
[10719] | 149 | void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 150 | { |
---|
| 151 | SUPER(WingmanController, XMLPort, xmlelement, mode); |
---|
| 152 | |
---|
| 153 | //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); |
---|
| 154 | } |
---|
| 155 | |
---|
[10678] | 156 | |
---|
[10729] | 157 | |
---|
[10678] | 158 | } |
---|