| 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 | *      jostoffe | 
|---|
| 24 | *   Co-authors: | 
|---|
| 25 | *      ... | 
|---|
| 26 | * | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | #include "ArrowController.h" | 
|---|
| 30 | #include "HumanController.h" | 
|---|
| 31 | #include "worldentities/ControllableEntity.h" | 
|---|
| 32 |  | 
|---|
| 33 | #include "core/CoreIncludes.h" | 
|---|
| 34 | #include "util/Math.h" | 
|---|
| 35 |  | 
|---|
| 36 | namespace orxonox | 
|---|
| 37 | { | 
|---|
| 38 |  | 
|---|
| 39 | RegisterClass(ArrowController); | 
|---|
| 40 |  | 
|---|
| 41 | ArrowController::ArrowController(Context* context) : Controller(context) | 
|---|
| 42 | { | 
|---|
| 43 |  | 
|---|
| 44 | RegisterObject(ArrowController); | 
|---|
| 45 |  | 
|---|
| 46 | this->currentGPSPoint_ = 0; | 
|---|
| 47 | this->accuracy_ = 1000.0f; | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | ArrowController::~ArrowController() | 
|---|
| 51 | { | 
|---|
| 52 | for (WorldEntity* gpspoint : this->gpspoints_) | 
|---|
| 53 | { | 
|---|
| 54 | if(gpspoint) | 
|---|
| 55 | gpspoint->destroy(); | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | //Set the distance you need to reach before the next waypoint will be selected | 
|---|
| 61 | void ArrowController::setAccuracy(float accuracy){ | 
|---|
| 62 | this->accuracy_ = accuracy; | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | float ArrowController::getAccuracy(){ | 
|---|
| 66 | return this->accuracy_; | 
|---|
| 67 | }; | 
|---|
| 68 |  | 
|---|
| 69 | void ArrowController::addGPSPoint(WorldEntity* gpspoint) | 
|---|
| 70 | { | 
|---|
| 71 | this->gpspoints_.push_back(gpspoint); | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | WorldEntity* ArrowController::getGPSPoint(unsigned int index) const | 
|---|
| 75 | { | 
|---|
| 76 | if (index < this->gpspoints_.size()) | 
|---|
| 77 | return this->gpspoints_[index]; | 
|---|
| 78 | else | 
|---|
| 79 | return nullptr; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | void ArrowController::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 83 | { | 
|---|
| 84 | SUPER(ArrowController, XMLPort, xmlelement, mode); | 
|---|
| 85 |  | 
|---|
| 86 | XMLPortObject(ArrowController, WorldEntity, "gpspoints", addGPSPoint, getGPSPoint,  xmlelement, mode); | 
|---|
| 87 | XMLPortParam(ArrowController, "accuracy", setAccuracy, getAccuracy, xmlelement, mode); | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | void ArrowController::tick(float dt) | 
|---|
| 91 | { | 
|---|
| 92 | if (!this->isActive()) | 
|---|
| 93 | return; | 
|---|
| 94 |  | 
|---|
| 95 | if (this->gpspoints_.size() == 0 || !this->getControllableEntity()) | 
|---|
| 96 | return; | 
|---|
| 97 | //Set all waypoint to invisible at the beginning | 
|---|
| 98 | if (this->currentGPSPoint_ == 0){ | 
|---|
| 99 | for(unsigned int i = 0; i < this->gpspoints_.size(); i++ ) | 
|---|
| 100 | this->gpspoints_[i]->setVisible(false); | 
|---|
| 101 | } | 
|---|
| 102 | //Make the arrow inivisible as soon as you reached the last Waypoint, otherwise make the next waypoint visible | 
|---|
| 103 | if(currentGPSPoint_ >= gpspoints_.size()){ | 
|---|
| 104 | this->getControllableEntity()->setVisible(false); | 
|---|
| 105 | return; | 
|---|
| 106 | } | 
|---|
| 107 | else this->gpspoints_[this->currentGPSPoint_]->setVisible(true); | 
|---|
| 108 |  | 
|---|
| 109 | //Set the next waypoint as target as soon as you reached the previous one | 
|---|
| 110 | if (this->gpspoints_[this->currentGPSPoint_]->getWorldPosition().squaredDistance(this->getControllableEntity()->getPosition()) <= this->accuracy_){ | 
|---|
| 111 | this->gpspoints_[this->currentGPSPoint_]->setVisible(false); | 
|---|
| 112 | this->currentGPSPoint_ = (this->currentGPSPoint_ + 1); | 
|---|
| 113 | return; | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | Vector3 target = gpspoints_[currentGPSPoint_]->getWorldPosition(); | 
|---|
| 117 | WorldEntity::TransformSpace trans = WorldEntity::TransformSpace::World; | 
|---|
| 118 |  | 
|---|
| 119 | //Get the position and orientation of the Spaceship | 
|---|
| 120 | Vector3 spaceShipPosition = HumanController::getLocalControllerSingleton()->getControllableEntity()->getWorldPosition(); | 
|---|
| 121 | Quaternion spaceShipOrientation = HumanController::getLocalControllerSingleton()->getControllableEntity()->getOrientation(); | 
|---|
| 122 |  | 
|---|
| 123 | //Calculate the new arrow position | 
|---|
| 124 | Vector3 ss_y = spaceShipOrientation.yAxis(); | 
|---|
| 125 | spaceShipPosition.x += 20 * ss_y.x; | 
|---|
| 126 | spaceShipPosition.y += 20 * ss_y.y; | 
|---|
| 127 | spaceShipPosition.z += 20 * ss_y.z; | 
|---|
| 128 |  | 
|---|
| 129 | //Update Arrow position and orientation | 
|---|
| 130 | this->getControllableEntity()->setPosition(spaceShipPosition); | 
|---|
| 131 | this->getControllableEntity()->lookAt(target, trans, Vector3(0,0,1)); | 
|---|
| 132 |  | 
|---|
| 133 |  | 
|---|
| 134 | } | 
|---|
| 135 | } | 
|---|