[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 | } |
---|
[10761] | 51 | // void WingmanController::chooseManeuver() |
---|
| 52 | // { |
---|
| 53 | // if (this->maneuverType_ == ManeuverType::NONE) |
---|
| 54 | // switch (this->maneuverType_ ) |
---|
| 55 | // { |
---|
| 56 | // case ManeuverType::NONE: |
---|
| 57 | // { |
---|
[10678] | 58 | |
---|
[10761] | 59 | // break; |
---|
| 60 | // } |
---|
| 61 | // case ManeuverType::NEUTRAL: |
---|
| 62 | // { |
---|
| 63 | |
---|
| 64 | // break; |
---|
| 65 | // } |
---|
| 66 | // case ManeuverType::OFFENSIVE: |
---|
| 67 | // { |
---|
| 68 | |
---|
| 69 | // break; |
---|
| 70 | // } |
---|
| 71 | // case ManeuverType::DEFENSIVE: |
---|
| 72 | // { |
---|
| 73 | |
---|
| 74 | // break; |
---|
| 75 | // } |
---|
| 76 | // } |
---|
| 77 | // if (!this->myWingman_) |
---|
| 78 | // return; |
---|
| 79 | // Vector3* targetRelativePositionOfWingman; |
---|
| 80 | // switch (this->formationMode_){ |
---|
| 81 | // case FormationMode::WALL: |
---|
| 82 | // { |
---|
| 83 | // targetRelativePositionOfWingman = new Vector3 (-400, 0, 0); |
---|
| 84 | // break; |
---|
| 85 | // } |
---|
| 86 | // case FormationMode::FINGER4: |
---|
| 87 | // { |
---|
| 88 | // targetRelativePositionOfWingman = new Vector3 (-400, 0, -200); |
---|
| 89 | // break; |
---|
| 90 | // } |
---|
| 91 | // case FormationMode::VEE: |
---|
| 92 | // { |
---|
| 93 | // break; |
---|
| 94 | // } |
---|
| 95 | // case FormationMode::DIAMOND: |
---|
| 96 | // { |
---|
| 97 | // targetRelativePositionOfWingman = new Vector3 (400, -200, 0); |
---|
| 98 | // break; |
---|
| 99 | // } |
---|
| 100 | // } |
---|
| 101 | // Quaternion orient = this->getControllableEntity()->getWorldOrientation(); |
---|
| 102 | |
---|
| 103 | // Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + |
---|
| 104 | // (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman))); |
---|
| 105 | |
---|
| 106 | // myWingman_->setTargetOrientation(orient); |
---|
| 107 | // myWingman_->setTargetPosition(targetAbsolutePositionOfWingman); |
---|
| 108 | |
---|
| 109 | // } |
---|
| 110 | |
---|
[10731] | 111 | void WingmanController::tick(float dt) |
---|
| 112 | { |
---|
[10762] | 113 | |
---|
[10731] | 114 | |
---|
| 115 | if (!this->isActive()) |
---|
| 116 | return; |
---|
[10761] | 117 | if (!this->target_) |
---|
| 118 | { |
---|
| 119 | //stay in formation |
---|
| 120 | } |
---|
| 121 | else |
---|
| 122 | { |
---|
[10762] | 123 | |
---|
[10761] | 124 | } |
---|
[10731] | 125 | if (this->bHasTargetPosition_) |
---|
| 126 | { |
---|
| 127 | this->moveToTargetPosition(); |
---|
| 128 | } |
---|
| 129 | |
---|
[10780] | 130 | if (this->bShooting_) |
---|
| 131 | doFire(); |
---|
[10731] | 132 | |
---|
| 133 | SUPER(WingmanController, tick, dt); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | void WingmanController::action() |
---|
| 137 | { |
---|
| 138 | if (!this->myLeader_) |
---|
| 139 | { |
---|
| 140 | CommonController* newLeader = findNewLeader(); |
---|
| 141 | this->myLeader_ = newLeader; |
---|
| 142 | if (newLeader) |
---|
| 143 | orxout(internal_error) << "new Leader set" << endl; |
---|
| 144 | else |
---|
[10759] | 145 | { |
---|
| 146 | //orxout(internal_error) << "0 leader" << endl; |
---|
| 147 | |
---|
| 148 | } |
---|
[10731] | 149 | |
---|
| 150 | } |
---|
| 151 | else |
---|
| 152 | { |
---|
| 153 | |
---|
| 154 | } |
---|
[10762] | 155 | if (canFire()) |
---|
[10780] | 156 | this->bShooting_ = true; |
---|
| 157 | else |
---|
| 158 | this->bShooting_ = false; |
---|
| 159 | |
---|
[10731] | 160 | } |
---|
| 161 | |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | |
---|
[10717] | 165 | CommonController* WingmanController::findNewLeader() |
---|
| 166 | { |
---|
| 167 | |
---|
| 168 | if (!this->getControllableEntity()) |
---|
[10722] | 169 | return 0; |
---|
[10717] | 170 | |
---|
[10722] | 171 | CommonController* closestLeader = 0; |
---|
| 172 | float minDistance = std::numeric_limits<float>::infinity(); |
---|
| 173 | |
---|
[10719] | 174 | for (ObjectList<CommonController>::iterator it = ObjectList<CommonController>::begin(); it; ++it) |
---|
[10717] | 175 | { |
---|
[10722] | 176 | //0ptr? |
---|
[10731] | 177 | if (!it || |
---|
[10759] | 178 | (it->getRank() != Rank::SECTIONLEADER && it->getRank() != Rank::DIVISIONLEADER) || |
---|
[10731] | 179 | !(it->getControllableEntity())) |
---|
[10722] | 180 | continue; |
---|
[10717] | 181 | //same team? |
---|
[10722] | 182 | if (this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam()) |
---|
[10717] | 183 | continue; |
---|
| 184 | //is equal to this? |
---|
[10719] | 185 | if (it->getControllableEntity() == this->getControllableEntity()) |
---|
[10717] | 186 | continue; |
---|
| 187 | |
---|
[10719] | 188 | float distance = (it->getControllableEntity()->getPosition() - this->getControllableEntity()->getPosition()).length(); |
---|
[10722] | 189 | if (distance < minDistance && !(it->hasWingman())) |
---|
| 190 | { |
---|
| 191 | closestLeader = *it; |
---|
| 192 | minDistance = distance; |
---|
| 193 | } |
---|
[10725] | 194 | |
---|
[10717] | 195 | } |
---|
[10722] | 196 | if (closestLeader) |
---|
| 197 | { |
---|
| 198 | if (closestLeader->setWingman(this)) |
---|
| 199 | return closestLeader; |
---|
| 200 | } |
---|
| 201 | return 0; |
---|
[10717] | 202 | } |
---|
[10722] | 203 | |
---|
[10719] | 204 | void WingmanController::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 205 | { |
---|
| 206 | SUPER(WingmanController, XMLPort, xmlelement, mode); |
---|
| 207 | |
---|
| 208 | //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); |
---|
| 209 | } |
---|
| 210 | |
---|
[10678] | 211 | |
---|
[10729] | 212 | |
---|
[10678] | 213 | } |
---|