| 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 |  *      Sven Stucki | 
|---|
| 24 |  *   Co-authors: | 
|---|
| 25 |  *      ... | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 | #include "DockingController.h" | 
|---|
| 30 |  | 
|---|
| 31 | #include <cmath> | 
|---|
| 32 |  | 
|---|
| 33 | #include "infos/PlayerInfo.h" | 
|---|
| 34 | #include "worldentities/ControllableEntity.h" | 
|---|
| 35 | #include "Dock.h" | 
|---|
| 36 | #include "core/CoreIncludes.h" | 
|---|
| 37 |  | 
|---|
| 38 | namespace orxonox | 
|---|
| 39 | { | 
|---|
| 40 |     RegisterClass(DockingController); | 
|---|
| 41 |  | 
|---|
| 42 |     DockingController::DockingController(Context* context) : ArtificialController(context) | 
|---|
| 43 |     { | 
|---|
| 44 |         RegisterObject(DockingController); | 
|---|
| 45 |  | 
|---|
| 46 |         this->dock_ = nullptr; | 
|---|
| 47 |         this->player_ = nullptr; | 
|---|
| 48 |         this->entity_ = nullptr; | 
|---|
| 49 |     } | 
|---|
| 50 |  | 
|---|
| 51 |     DockingController::~DockingController() | 
|---|
| 52 |     { | 
|---|
| 53 |  | 
|---|
| 54 |     } | 
|---|
| 55 |  | 
|---|
| 56 |     void DockingController::tick(float dt) | 
|---|
| 57 |     { | 
|---|
| 58 |         ControllableEntity* entity = this->getControllableEntity(); | 
|---|
| 59 |         if (!entity) | 
|---|
| 60 |             return; | 
|---|
| 61 |  | 
|---|
| 62 |         float distance = (this->dock_->getWorldPosition() - entity->getPosition()).length(); | 
|---|
| 63 |         Vector2 coord = get2DViewCoordinates(     // I don't understand this too | 
|---|
| 64 |             entity->getPosition(), | 
|---|
| 65 |             entity->getOrientation() * WorldEntity::FRONT, | 
|---|
| 66 |             entity->getOrientation() * WorldEntity::UP, | 
|---|
| 67 |             dock_->getWorldPosition() | 
|---|
| 68 |         ); | 
|---|
| 69 |  | 
|---|
| 70 |         // adjust direction of spaceship | 
|---|
| 71 |         if (distance > 10) | 
|---|
| 72 |         { | 
|---|
| 73 |             entity->rotateYaw(-1.0f * 0.8f * clamp(coord.x * 10, -1.0f, 1.0f)); | 
|---|
| 74 |             entity->rotatePitch(0.8f * clamp(coord.y * 10, -1.0f, 1.0f)); | 
|---|
| 75 |         } | 
|---|
| 76 |  | 
|---|
| 77 |         /*// adjust speed | 
|---|
| 78 |         if (distance < 200 && entity->getVelocity().squaredLength() > dock->getVelocity().squaredLength()) | 
|---|
| 79 |             entity->moveFrontBack(0.2f); | 
|---|
| 80 |         else | 
|---|
| 81 |             entity->moveFrontBack(0.8f);*/ | 
|---|
| 82 |  | 
|---|
| 83 |         entity->moveFrontBack(0.5f * log(distance/10.0f)); | 
|---|
| 84 |  | 
|---|
| 85 |         if (distance < 20) | 
|---|
| 86 |             this->positionReached(); | 
|---|
| 87 |  | 
|---|
| 88 |         SUPER(DockingController, tick, dt); | 
|---|
| 89 |     } | 
|---|
| 90 |  | 
|---|
| 91 |     void DockingController::takeControl(bool docking) | 
|---|
| 92 |     { | 
|---|
| 93 |         this->docking_ = docking; | 
|---|
| 94 |  | 
|---|
| 95 |         this->entity_ = this->player_->getControllableEntity(); | 
|---|
| 96 |         assert(this->entity_); | 
|---|
| 97 |  | 
|---|
| 98 |         if (docking) | 
|---|
| 99 |         { | 
|---|
| 100 |             orxout(verbose, context::docking) << "DockingController::takeControl Taking over control." << endl; | 
|---|
| 101 |  | 
|---|
| 102 |             this->entity_->setDestroyWhenPlayerLeft(false); | 
|---|
| 103 |             this->player_->pauseControl(); | 
|---|
| 104 |             this->entity_->setController(this); | 
|---|
| 105 |             this->setControllableEntity(this->entity_); | 
|---|
| 106 |         } | 
|---|
| 107 |     } | 
|---|
| 108 |  | 
|---|
| 109 |     void DockingController::positionReached() | 
|---|
| 110 |     { | 
|---|
| 111 |         orxout(verbose, context::docking) << "DockingController::positionReached() called." << endl; | 
|---|
| 112 |  | 
|---|
| 113 |         assert(this->player_); | 
|---|
| 114 |         assert(this->dock_); | 
|---|
| 115 |  | 
|---|
| 116 |         // stop spaceship | 
|---|
| 117 |         this->entity_->setPosition(this->dock_->getWorldPosition()); | 
|---|
| 118 |         this->entity_->setVelocity(0, 0, 0); | 
|---|
| 119 |         this->entity_->setOrientation(this->dock_->getWorldOrientation()); | 
|---|
| 120 |  | 
|---|
| 121 |         // give control back to player | 
|---|
| 122 |         this->player_->startControl(this->entity_); | 
|---|
| 123 |         this->setActive(false); | 
|---|
| 124 |         this->controllableEntity_ = nullptr; | 
|---|
| 125 |  | 
|---|
| 126 |         if (this->docking_) | 
|---|
| 127 |             this->dock_->dockingAnimationFinished(this->player_); | 
|---|
| 128 |         /*else | 
|---|
| 129 |             dock->undockingAnimationFinished(player);*/ | 
|---|
| 130 |  | 
|---|
| 131 |         this->destroy(); | 
|---|
| 132 |     } | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|