/* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: * jostoffe * Co-authors: * ... * */ #include "ArrowController.h" #include "HumanController.h" #include "worldentities/ControllableEntity.h" #include "core/CoreIncludes.h" #include "util/Math.h" namespace orxonox { RegisterClass(ArrowController); ArrowController::ArrowController(Context* context) : Controller(context) { RegisterObject(ArrowController); this->currentGPSPoint_ = 0; this->accuracy_ = 1000.0f; } ArrowController::~ArrowController() { for (WorldEntity* gpspoint : this->gpspoints_) { if(gpspoint) gpspoint->destroy(); } } //Set the distance you need to reach before the next waypoint will be selected void ArrowController::setAccuracy(float accuracy){ this->accuracy_ = accuracy; } float ArrowController::getAccuracy(){ return this->accuracy_; }; void ArrowController::addGPSPoint(WorldEntity* gpspoint) { this->gpspoints_.push_back(gpspoint); } WorldEntity* ArrowController::getGPSPoint(unsigned int index) const { if (index < this->gpspoints_.size()) return this->gpspoints_[index]; else return nullptr; } void ArrowController::XMLPort(Element& xmlelement, XMLPort::Mode mode) { SUPER(ArrowController, XMLPort, xmlelement, mode); XMLPortObject(ArrowController, WorldEntity, "gpspoints", addGPSPoint, getGPSPoint, xmlelement, mode); XMLPortParam(ArrowController, "accuracy", setAccuracy, getAccuracy, xmlelement, mode); } void ArrowController::tick(float dt) { if (!this->isActive()) return; if (this->gpspoints_.size() == 0 || !this->getControllableEntity()) return; //Set all waypoint to invisible at the beginning if (this->currentGPSPoint_ == 0){ for(unsigned int i = 0; i < this->gpspoints_.size(); i++ ) this->gpspoints_[i]->setVisible(false); } //Make the arrow inivisible as soon as you reached the last Waypoint, otherwise make the next waypoint visible if(currentGPSPoint_ >= gpspoints_.size()){ this->getControllableEntity()->setVisible(false); return; } else this->gpspoints_[this->currentGPSPoint_]->setVisible(true); //Set the next waypoint as target as soon as you reached the previous one if (this->gpspoints_[this->currentGPSPoint_]->getWorldPosition().squaredDistance(this->getControllableEntity()->getPosition()) <= this->accuracy_){ this->gpspoints_[this->currentGPSPoint_]->setVisible(false); this->currentGPSPoint_ = (this->currentGPSPoint_ + 1); return; } Vector3 target = gpspoints_[currentGPSPoint_]->getWorldPosition(); WorldEntity::TransformSpace trans = WorldEntity::TransformSpace::World; //Get the position and orientation of the Spaceship Vector3 spaceShipPosition = HumanController::getLocalControllerSingleton()->getControllableEntity()->getWorldPosition(); Quaternion spaceShipOrientation = HumanController::getLocalControllerSingleton()->getControllableEntity()->getOrientation(); //Calculate the new arrow position Vector3 ss_y = spaceShipOrientation.yAxis(); spaceShipPosition.x += 20 * ss_y.x; spaceShipPosition.y += 20 * ss_y.y; spaceShipPosition.z += 20 * ss_y.z; //Update Arrow position and orientation this->getControllableEntity()->setPosition(spaceShipPosition); this->getControllableEntity()->lookAt(target, trans, Vector3(0,0,1)); } }