/* * 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: * Fabian 'x3n' Landau * Co-authors: * Dominik Solenicki * */ #include "SectionController.h" namespace orxonox { RegisterClass(SectionController); SectionController::SectionController(Context* context) : LeaderController(context) { RegisterObject(SectionController); this->setFormationMode(FormationMode::FINGER4); this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&SectionController::action, this))); this->myWingman_ = 0; this->myDivisionLeader_ = 0; this->rank_ = Rank::SECTIONLEADER; orxout(internal_error) << this << "Was created" << endl; } SectionController::~SectionController() { } void SectionController::tick(float dt) { if (!this->isActive()) return; if (this->bHasTargetPosition_) { this->moveToTargetPosition(); } if (this->bShooting_) doFire(); SUPER(SectionController, tick, dt); } void SectionController::action() { //this->target_ = this->sectionTarget_; if (!myDivisionLeader_) { LeaderController* newDivisionLeader = findNewDivisionLeader(); this->myDivisionLeader_ = newDivisionLeader; if (newDivisionLeader) orxout(internal_error) << "new DivisionLeader set" << endl; else { } } setTargetPositionOfWingman(); if (this->target_ && this->myWingman_) this->myWingman_->setTarget(this->target_); if (canFire()) this->bShooting_ = true; else this->bShooting_ = false; } void SectionController::setTargetPositionOfWingman() { if (!this->myWingman_) return; Vector3* targetRelativePositionOfWingman; switch (this->formationMode_){ case FormationMode::WALL: { targetRelativePositionOfWingman = new Vector3 (-400, 0, 0); break; } case FormationMode::FINGER4: { targetRelativePositionOfWingman = new Vector3 (-400, 0, -200); break; } case FormationMode::VEE: { break; } case FormationMode::DIAMOND: { targetRelativePositionOfWingman = new Vector3 (400, -200, 0); break; } } Quaternion orient = this->getControllableEntity()->getWorldOrientation(); Vector3 targetAbsolutePositionOfWingman = ((this->getControllableEntity()->getWorldPosition()) + (this->getControllableEntity()->getWorldOrientation()* (*targetRelativePositionOfWingman))); myWingman_->setTargetOrientation(orient); myWingman_->setTargetPosition(targetAbsolutePositionOfWingman); } LeaderController* SectionController::findNewDivisionLeader() { if (!this->getControllableEntity()) return 0; LeaderController* closestLeader = 0; float minDistance = std::numeric_limits::infinity(); //go through all pawns for (ObjectList::iterator it = ObjectList::begin(); it; ++it) { //0ptr or not DivisionController? if (!(it) || !((it)->getRank() == Rank::DIVISIONLEADER) || !(it->getControllableEntity())) continue; //same team? if ((this->getControllableEntity()->getTeam() != (it)->getControllableEntity()->getTeam())) continue; //is equal to this? if (orxonox_cast(*it) == this->getControllableEntity()) continue; float distance = ((it)->getControllableEntity()->getPosition() - this->getControllableEntity()->getPosition()).length(); if (distance < minDistance && !(it->hasFollower())) { closestLeader = *it; minDistance = distance; } } if (closestLeader) { if (closestLeader->setFollower(this)) return closestLeader; } return 0; } bool SectionController::setWingman(CommonController* cwingman) { WeakPtr wingman = orxonox_cast(cwingman); if (!this->myWingman_) { this->myWingman_ = wingman; return true; } else { return false; } } bool SectionController::hasWingman() { if (this->myWingman_) return true; else return false; } void SectionController::XMLPort(Element& xmlelement, XMLPort::Mode mode) { SUPER(SectionController, XMLPort, xmlelement, mode); //XMLPortParam(SectionController, "target_", setTarget, getTarget, xmlelement, mode).defaultValues(100.0f); } }