[10008] | 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 | * Martin Mueller |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[10031] | 29 | #include "TurretController.h" |
---|
| 30 | #include "worldentities/pawns/Pawn.h" |
---|
| 31 | #include "Turret.h" |
---|
[10008] | 32 | |
---|
| 33 | namespace orxonox |
---|
| 34 | { |
---|
| 35 | RegisterClass(TurretController); |
---|
| 36 | |
---|
| 37 | TurretController::TurretController(Context* context) : ArtificialController(context) |
---|
| 38 | { |
---|
| 39 | RegisterObject(TurretController); |
---|
[10044] | 40 | |
---|
| 41 | this->once_ = false; |
---|
| 42 | |
---|
[10008] | 43 | } |
---|
| 44 | |
---|
| 45 | TurretController::~TurretController() |
---|
| 46 | { |
---|
| 47 | |
---|
| 48 | } |
---|
| 49 | |
---|
[10031] | 50 | void TurretController::searchTarget() |
---|
| 51 | { |
---|
| 52 | Turret* turret = orxonox_cast<Turret*>(this->getControllableEntity()); |
---|
[10044] | 53 | if(target_ && turret->isInRange(target_->getWorldPosition())) |
---|
[10031] | 54 | { |
---|
| 55 | return; |
---|
| 56 | } |
---|
| 57 | else |
---|
| 58 | { |
---|
| 59 | this->forgetTarget(); |
---|
[10044] | 60 | turret->setTarget(0); |
---|
[10031] | 61 | } |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | ControllableEntity* parent = orxonox_cast<ControllableEntity*>(turret->getParent()); |
---|
| 65 | if(parent) |
---|
| 66 | { |
---|
| 67 | Pawn* parenttarget = orxonox_cast<Pawn*>(parent->getTarget()); |
---|
[10044] | 68 | if(parenttarget && turret->isInRange(parenttarget->getWorldPosition())) |
---|
[10031] | 69 | { |
---|
| 70 | this->setTarget(parenttarget); |
---|
| 71 | turret->setTarget(parenttarget); |
---|
| 72 | return; |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | for (ObjectList<Pawn>::iterator it = ObjectList<Pawn>::begin(); it != ObjectList<Pawn>::end(); ++it) |
---|
| 77 | { |
---|
| 78 | Pawn* entity = orxonox_cast<Pawn*>(*it); |
---|
[10044] | 79 | if (this->FormationController::sameTeam(this->getControllableEntity(), entity, this->getGametype())) |
---|
[10031] | 80 | continue; |
---|
| 81 | |
---|
[10044] | 82 | if(turret->isInRange(entity->getWorldPosition())) |
---|
[10031] | 83 | { |
---|
| 84 | this->setTarget(entity); |
---|
| 85 | turret->setTarget(entity); |
---|
| 86 | break; |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
[10042] | 91 | bool TurretController::isLookingAtTargetNew(float angle) const |
---|
| 92 | { |
---|
| 93 | return (getAngle(this->getControllableEntity()->getWorldPosition(), this->getControllableEntity()->getWorldOrientation() * WorldEntity::FRONT, this->target_->getWorldPosition()) < angle); |
---|
| 94 | } |
---|
| 95 | |
---|
[10008] | 96 | void TurretController::tick(float dt) |
---|
| 97 | { |
---|
[10044] | 98 | if (!this->isActive() || !this->getControllableEntity()) |
---|
| 99 | return; |
---|
[10039] | 100 | |
---|
| 101 | |
---|
[10044] | 102 | if(!this->once_) |
---|
| 103 | { |
---|
| 104 | if(this->getTeam() != -1) |
---|
| 105 | { |
---|
| 106 | orxout(internal_warning) << "Turret: Team already set, may result in undesired behaviour" << endl; |
---|
| 107 | } |
---|
| 108 | else |
---|
| 109 | { |
---|
| 110 | //Make sure the turret is in the same team as the parent |
---|
| 111 | ControllableEntity* parent = orxonox_cast<ControllableEntity*> (this->getControllableEntity()->getParent()); |
---|
| 112 | if(parent) |
---|
| 113 | { |
---|
| 114 | Controller* parentcontroller = parent->getController(); |
---|
| 115 | if(parentcontroller) |
---|
| 116 | { |
---|
| 117 | this->setTeam(parentcontroller->getTeam()); |
---|
| 118 | } |
---|
| 119 | else |
---|
| 120 | { |
---|
| 121 | this->setTeam(parent->getTeam()); |
---|
| 122 | } |
---|
| 123 | this->getControllableEntity()->setTeam(parent->getTeam()); |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | this->once_ = true; |
---|
| 127 | } |
---|
[10039] | 128 | |
---|
[10044] | 129 | this->searchTarget(); |
---|
| 130 | if(target_) |
---|
| 131 | { |
---|
| 132 | Turret* turret = orxonox_cast<Turret*> (this->getControllableEntity()); |
---|
| 133 | this->aimAtTarget(); |
---|
| 134 | turret->aimAtPosition(target_->getWorldPosition()); |
---|
| 135 | if(this->isLookingAtTargetNew(Degree(5).valueRadians())) |
---|
| 136 | { |
---|
| 137 | this->getControllableEntity()->fire(0); |
---|
| 138 | orxout() << 42 << endl; |
---|
| 139 | } |
---|
| 140 | } |
---|
[10008] | 141 | } |
---|
| 142 | } |
---|