| 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 | * Gani Aliguzhinov |
|---|
| 24 | * Co-authors: |
|---|
| 25 | * ... |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | #include "MasterController.h" |
|---|
| 30 | #include "controllers/ActionpointController.h" |
|---|
| 31 | namespace orxonox |
|---|
| 32 | { |
|---|
| 33 | |
|---|
| 34 | RegisterClass(MasterController); |
|---|
| 35 | |
|---|
| 36 | //Leaders share the fact that they have Wingmans |
|---|
| 37 | MasterController::MasterController(Context* context) : Controller(context) |
|---|
| 38 | { |
|---|
| 39 | RegisterObject(MasterController); |
|---|
| 40 | this->controllers_.clear(); |
|---|
| 41 | this->numberOfTicksPassedSinceLastActionCall_ = 0; |
|---|
| 42 | this->indexOfCurrentController_ = 0; |
|---|
| 43 | this->ticks_ = 0; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | MasterController::~MasterController() |
|---|
| 47 | { |
|---|
| 48 | this->controllers_.clear(); |
|---|
| 49 | } |
|---|
| 50 | /*HACK*/ |
|---|
| 51 | //the whole idea is a hack |
|---|
| 52 | void MasterController::tick(float dt) |
|---|
| 53 | { |
|---|
| 54 | if (!this->isActive()) |
|---|
| 55 | return; |
|---|
| 56 | ++this->ticks_; |
|---|
| 57 | if (this->ticks_ == 1) |
|---|
| 58 | { |
|---|
| 59 | //fill the vector in the first tick |
|---|
| 60 | for (ActionpointController* controller : ObjectList<ActionpointController>()) |
|---|
| 61 | { |
|---|
| 62 | //----0ptr?---- |
|---|
| 63 | if (!controller) |
|---|
| 64 | continue; |
|---|
| 65 | this->controllers_.push_back(controller); |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | else |
|---|
| 69 | { |
|---|
| 70 | if (this->controllers_.empty()) |
|---|
| 71 | return; |
|---|
| 72 | |
|---|
| 73 | //iterate over vecotr with the index, keep index in boundaries |
|---|
| 74 | if (this->indexOfCurrentController_ >= this->controllers_.size()) |
|---|
| 75 | { |
|---|
| 76 | this->indexOfCurrentController_ = 0; |
|---|
| 77 | } |
|---|
| 78 | //each 9 ticks index is incremented |
|---|
| 79 | if (this->numberOfTicksPassedSinceLastActionCall_ >= 9) |
|---|
| 80 | { |
|---|
| 81 | this->numberOfTicksPassedSinceLastActionCall_ = 0; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | if (this->numberOfTicksPassedSinceLastActionCall_ > 0) |
|---|
| 85 | { |
|---|
| 86 | if (this->numberOfTicksPassedSinceLastActionCall_ == 3) |
|---|
| 87 | { |
|---|
| 88 | //check if 0ptr |
|---|
| 89 | if (!this->controllers_.at(this->indexOfCurrentController_)) |
|---|
| 90 | { |
|---|
| 91 | this->controllers_.erase(this->controllers_.begin() + this->indexOfCurrentController_); |
|---|
| 92 | return; |
|---|
| 93 | } |
|---|
| 94 | //call maneuver for current index |
|---|
| 95 | this->controllers_.at(this->indexOfCurrentController_)->maneuver(); |
|---|
| 96 | } |
|---|
| 97 | else if (this->numberOfTicksPassedSinceLastActionCall_ == 6) |
|---|
| 98 | { |
|---|
| 99 | //check if 0ptr |
|---|
| 100 | if (!this->controllers_.at(this->indexOfCurrentController_)) |
|---|
| 101 | { |
|---|
| 102 | this->controllers_.erase(this->controllers_.begin() + this->indexOfCurrentController_); |
|---|
| 103 | return; |
|---|
| 104 | } |
|---|
| 105 | //call canFire for current index |
|---|
| 106 | this->controllers_.at(this->indexOfCurrentController_)->bShooting_ = this->controllers_.at(this->indexOfCurrentController_)->canFire(); |
|---|
| 107 | } |
|---|
| 108 | ++this->numberOfTicksPassedSinceLastActionCall_; |
|---|
| 109 | } |
|---|
| 110 | else |
|---|
| 111 | { |
|---|
| 112 | //check if 0ptr |
|---|
| 113 | if (!this->controllers_.at(this->indexOfCurrentController_)) |
|---|
| 114 | { |
|---|
| 115 | this->controllers_.erase(this->controllers_.begin() + this->indexOfCurrentController_); |
|---|
| 116 | return; |
|---|
| 117 | } |
|---|
| 118 | //call action for current index |
|---|
| 119 | this->controllers_.at(this->indexOfCurrentController_)->action(); |
|---|
| 120 | |
|---|
| 121 | //bCopyOrientation makes ship oscillate like crazy if set to true all the time.s |
|---|
| 122 | this->controllers_.at(this->indexOfCurrentController_)->bCopyOrientation_ = this->ticks_ % 3 == 0; |
|---|
| 123 | |
|---|
| 124 | ++this->numberOfTicksPassedSinceLastActionCall_; |
|---|
| 125 | ++this->indexOfCurrentController_; |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | } |
|---|
| 129 | } |
|---|