[6847] | 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 | * Oli Scheuss |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "DroneController.h" |
---|
| 30 | #include "worldentities/Drone.h" |
---|
| 31 | #include "util/Math.h" |
---|
| 32 | |
---|
| 33 | #include "core/CoreIncludes.h" |
---|
| 34 | #include "core/Executor.h" |
---|
| 35 | #include "worldentities/ControllableEntity.h" |
---|
| 36 | |
---|
| 37 | namespace orxonox |
---|
| 38 | { |
---|
| 39 | /** |
---|
| 40 | @brief |
---|
| 41 | Constructor. |
---|
| 42 | */ |
---|
| 43 | CreateFactory(DroneController); |
---|
| 44 | |
---|
| 45 | static const float ACTION_INTERVAL = 1.0f; |
---|
| 46 | |
---|
| 47 | DroneController::DroneController(BaseObject* creator) : ArtificialController(creator) |
---|
| 48 | { |
---|
| 49 | RegisterObject(DroneController); |
---|
| 50 | |
---|
| 51 | this->owner_ = 0; |
---|
| 52 | this->drone_ = 0; |
---|
| 53 | |
---|
| 54 | this->actionTimer_.setTimer(ACTION_INTERVAL, true, createExecutor(createFunctor(&DroneController::action, this))); |
---|
[6891] | 55 | |
---|
| 56 | this->owner_.setCallback(createFunctor(&DroneController::ownerDied, this)); |
---|
[6847] | 57 | } |
---|
| 58 | |
---|
| 59 | DroneController::~DroneController() |
---|
| 60 | { |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | void DroneController::setOwner(Pawn* owner){ |
---|
| 64 | this->owner_ = owner; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | void DroneController::setDrone(Drone* drone) |
---|
| 68 | { |
---|
| 69 | this->drone_ = drone; |
---|
| 70 | this->setControllableEntity(drone); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | void DroneController::action() |
---|
| 74 | { |
---|
| 75 | float random; |
---|
| 76 | float maxrand = 100.0f / ACTION_INTERVAL; |
---|
[7060] | 77 | float distanceToTargetSquared; |
---|
[6847] | 78 | |
---|
[7060] | 79 | if (target_) { |
---|
| 80 | const Vector3& locTargetDir = getDrone()->getOrientation().UnitInverse()*((getDrone()->getWorldPosition())-(target_->getWorldPosition())); //Vector from Drone To target out of drones local coordinate system |
---|
| 81 | distanceToTargetSquared = locTargetDir.squaredLength(); |
---|
| 82 | } |
---|
| 83 | |
---|
[6891] | 84 | random = rnd(maxrand); |
---|
[7060] | 85 | if ( random < 30 || (!this->target_) || distanceToTargetSquared > (this->getDrone()->getMaxShootingRange()*this->getDrone()->getMaxShootingRange())) |
---|
[6891] | 86 | this->searchNewTarget(); |
---|
[6991] | 87 | |
---|
[7060] | 88 | if (random < 50 && this->target_ && distanceToTargetSquared < (this->getDrone()->getMaxShootingRange()*this->getDrone()->getMaxShootingRange())) |
---|
[6918] | 89 | { |
---|
[6991] | 90 | this->isShooting_ = true; |
---|
[6918] | 91 | this->aimAtTarget(); |
---|
[6991] | 92 | drone_->rotateYaw(targetPosition_.x); //face target |
---|
| 93 | drone_->rotatePitch(targetPosition_.y); |
---|
[6918] | 94 | drone_->fire(0); |
---|
[6991] | 95 | this->isShooting_ = false; |
---|
[6918] | 96 | } |
---|
[6891] | 97 | |
---|
[6847] | 98 | } |
---|
| 99 | |
---|
[7038] | 100 | /** |
---|
[6847] | 101 | @brief |
---|
| 102 | The controlling happens here. This method defines what the controller has to do each tick. |
---|
| 103 | @param dt |
---|
| 104 | The duration of the tick. |
---|
| 105 | */ |
---|
| 106 | void DroneController::tick(float dt) |
---|
| 107 | { |
---|
[6991] | 108 | float maxDistanceSquared = this->getDrone()->getMaxDistanceToOwner()*this->getDrone()->getMaxDistanceToOwner(); |
---|
| 109 | float minDistanceSquared = this->getDrone()->getMinDistanceToOwner()*this->getDrone()->getMinDistanceToOwner(); |
---|
| 110 | if ((this->getDrone()->getWorldPosition() - this->getOwner()->getWorldPosition()).squaredLength() > maxDistanceSquared) { |
---|
[7060] | 111 | this->moveToPosition(this->getOwner()->getWorldPosition()); //fly towards owner |
---|
[6847] | 112 | } |
---|
[6991] | 113 | else if((this->getDrone()->getWorldPosition() - this->getOwner()->getWorldPosition()).squaredLength() < minDistanceSquared) { |
---|
[7060] | 114 | this->moveToPosition(-this->getOwner()->getWorldPosition()); //fly away from owner |
---|
[6991] | 115 | } |
---|
| 116 | else if(!isShooting_) { |
---|
| 117 | float random = rnd(2.0f); |
---|
| 118 | float randomSelection = rnd(6.0f); |
---|
| 119 | if((int)randomSelection==0) drone_->moveUpDown(random); |
---|
| 120 | else if((int)randomSelection==1) drone_->moveRightLeft(random); |
---|
| 121 | else if((int)randomSelection==2) drone_->moveFrontBack(random); |
---|
| 122 | else if((int)randomSelection==3) drone_->rotateYaw(random); |
---|
| 123 | else if((int)randomSelection==4) drone_->rotatePitch(random); |
---|
| 124 | else if((int)randomSelection==5) drone_->rotateRoll(random); |
---|
| 125 | } |
---|
[7035] | 126 | |
---|
[6847] | 127 | SUPER(AIController, tick, dt); |
---|
| 128 | |
---|
[6891] | 129 | } |
---|
[6847] | 130 | |
---|
[6891] | 131 | void DroneController::ownerDied() |
---|
| 132 | { |
---|
[7060] | 133 | // if (this->target_) { //Drone has some kind of Stockholm-Syndrom --> gets attached to Owners Killer |
---|
| 134 | // this->setOwner(target_); |
---|
| 135 | // this->searchNewTarget(); |
---|
| 136 | // } |
---|
[6891] | 137 | if (this->drone_) |
---|
| 138 | this->drone_->destroy(); |
---|
| 139 | else |
---|
| 140 | this->destroy(); |
---|
[6847] | 141 | } |
---|
| 142 | } |
---|