[5979] | 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: |
---|
[5981] | 23 | * Michael Wirth |
---|
[5979] | 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "NewHumanController.h" |
---|
| 30 | |
---|
[6033] | 31 | #include <OgreRay.h> |
---|
| 32 | #include <OgreSceneQuery.h> |
---|
| 33 | #include <OgreCamera.h> |
---|
| 34 | #include <OgreSceneManager.h> |
---|
[5981] | 35 | |
---|
[5979] | 36 | #include "core/CoreIncludes.h" |
---|
| 37 | #include "core/ConsoleCommand.h" |
---|
| 38 | #include "worldentities/ControllableEntity.h" |
---|
[6111] | 39 | #include "worldentities/pawns/Pawn.h" |
---|
[5979] | 40 | #include "infos/PlayerInfo.h" |
---|
[6055] | 41 | #include "overlays/OrxonoxOverlay.h" |
---|
[5979] | 42 | #include "graphics/Camera.h" |
---|
| 43 | #include "sound/SoundManager.h" |
---|
| 44 | #include "Scene.h" |
---|
| 45 | |
---|
| 46 | namespace orxonox |
---|
| 47 | { |
---|
[6091] | 48 | SetConsoleCommand(NewHumanController, changeMode, false).keybindMode(KeybindMode::OnPress); |
---|
| 49 | |
---|
[5979] | 50 | CreateUnloadableFactory(NewHumanController); |
---|
| 51 | |
---|
[6091] | 52 | NewHumanController* NewHumanController::localController_s = 0; |
---|
| 53 | |
---|
[6122] | 54 | NewHumanController::NewHumanController(BaseObject* creator) |
---|
| 55 | : HumanController(creator) |
---|
| 56 | , crossHairOverlay_(NULL) |
---|
[5979] | 57 | { |
---|
| 58 | RegisterObject(NewHumanController); |
---|
[6001] | 59 | |
---|
[6055] | 60 | overlaySize_ = 0.08; |
---|
| 61 | controlMode_ = 0; |
---|
[6033] | 62 | |
---|
[6122] | 63 | if (GameMode::showsGraphics()) |
---|
| 64 | { |
---|
| 65 | crossHairOverlay_ = new OrxonoxOverlay(this); |
---|
| 66 | crossHairOverlay_->setBackgroundMaterial("Orxonox/Crosshair3"); |
---|
| 67 | crossHairOverlay_->setSize(Vector2(overlaySize_, overlaySize_)); |
---|
| 68 | crossHairOverlay_->show(); |
---|
| 69 | } |
---|
[6045] | 70 | |
---|
[6058] | 71 | // HACK: Define which objects are targetable when considering the creator of an orxonox::Model |
---|
[6055] | 72 | this->targetMask_.exclude(ClassByString("BaseObject")); |
---|
| 73 | this->targetMask_.include(ClassByString("WorldEntity")); |
---|
| 74 | this->targetMask_.exclude(ClassByString("Projectile")); |
---|
[6091] | 75 | |
---|
| 76 | NewHumanController::localController_s = this; |
---|
[5979] | 77 | } |
---|
| 78 | |
---|
| 79 | NewHumanController::~NewHumanController() |
---|
| 80 | { |
---|
[6055] | 81 | if (this->isInitialized()) |
---|
[5981] | 82 | { |
---|
[6122] | 83 | if (this->crossHairOverlay_) |
---|
| 84 | this->crossHairOverlay_->destroy(); |
---|
[5981] | 85 | } |
---|
[5979] | 86 | } |
---|
| 87 | |
---|
[6055] | 88 | void NewHumanController::tick(float dt) |
---|
| 89 | { |
---|
[6122] | 90 | if (GameMode::showsGraphics()) |
---|
[6111] | 91 | { |
---|
[6122] | 92 | if( this->controllableEntity_ && !this->controllableEntity_->isInMouseLook() ) |
---|
| 93 | { |
---|
| 94 | this->crossHairOverlay_->setPosition(Vector2(static_cast<float>(this->currentYaw_)/2*-1+.5-overlaySize_/2, static_cast<float>(this->currentPitch_)/2*-1+.5-overlaySize_/2)); |
---|
| 95 | this->crossHairOverlay_->show(); |
---|
| 96 | } |
---|
| 97 | else |
---|
| 98 | this->crossHairOverlay_->hide(); |
---|
| 99 | // TODO: update aimPosition of Pawn |
---|
[6111] | 100 | } |
---|
[6001] | 101 | |
---|
| 102 | HumanController::tick(dt); |
---|
| 103 | } |
---|
| 104 | |
---|
[5993] | 105 | /*void NewHumanController::tick(float dt) |
---|
[5979] | 106 | { |
---|
| 107 | if (GameMode::playsSound() && NewHumanController::localController_s && NewHumanController::localController_s->controllableEntity_) |
---|
| 108 | { |
---|
| 109 | // Update sound listener |
---|
| 110 | Camera* camera = NewHumanController::localController_s->controllableEntity_->getCamera(); |
---|
| 111 | if (camera) |
---|
| 112 | { |
---|
| 113 | SoundManager::getInstance().setListenerPosition(camera->getWorldPosition()); |
---|
| 114 | SoundManager::getInstance().setListenerOrientation(camera->getWorldOrientation()); |
---|
| 115 | } |
---|
| 116 | else |
---|
| 117 | COUT(3) << "NewHumanController, Warning: Using a ControllableEntity without Camera" << std::endl; |
---|
| 118 | } |
---|
[5993] | 119 | }*/ |
---|
| 120 | |
---|
[6033] | 121 | void NewHumanController::doFire(unsigned int firemode) |
---|
| 122 | { |
---|
| 123 | //if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) { |
---|
| 124 | |
---|
| 125 | /* |
---|
[6055] | 126 | // Get results, create a node/entity on the position |
---|
| 127 | for ( itr = result.begin(); itr != result.end(); itr++ ) |
---|
| 128 | { |
---|
| 129 | if (itr->movable && itr->movable->getName() == "Head") |
---|
| 130 | { |
---|
| 131 | soundMgr->StopSound( &jaguarSoundChannel ); |
---|
| 132 | soundMgr->PlaySound( jaguarSound, headNode, &jaguarSoundChannel ); |
---|
| 133 | break; |
---|
| 134 | } // if |
---|
| 135 | } |
---|
[6033] | 136 | */ |
---|
[6111] | 137 | if( !NewHumanController::localController_s->getControllableEntity()->isInMouseLook() ) |
---|
| 138 | this->updateTarget(); |
---|
[6033] | 139 | |
---|
[6055] | 140 | HumanController::localController_s->getControllableEntity()->fire(firemode); |
---|
[6045] | 141 | } |
---|
| 142 | |
---|
[6111] | 143 | void NewHumanController::updateTarget() |
---|
[6055] | 144 | { |
---|
| 145 | Ogre::RaySceneQuery * rsq = HumanController::localController_s->getControllableEntity()->getScene()->getSceneManager()->createRayQuery(Ogre::Ray()); |
---|
[6033] | 146 | |
---|
[6058] | 147 | Ogre::Ray mouseRay = HumanController::localController_s->getControllableEntity()->getCamera()->getOgreCamera()->getCameraToViewportRay(static_cast<float>(this->currentYaw_)/2*-1+.5, static_cast<float>(this->currentPitch_)/2*-1+.5); |
---|
[6033] | 148 | |
---|
[6055] | 149 | rsq->setRay(mouseRay); |
---|
| 150 | rsq->setSortByDistance(true); |
---|
[6033] | 151 | |
---|
[6055] | 152 | /* |
---|
| 153 | Distance of objects: |
---|
| 154 | ignore everything under 200 maybe even take 1000 as min distance to shoot at |
---|
[6033] | 155 | |
---|
[6055] | 156 | shots are regularly traced and are entities!!!!!!!!! this is the biggest problem |
---|
| 157 | they vanish only after a distance of 10'000 |
---|
| 158 | */ |
---|
[6045] | 159 | |
---|
| 160 | |
---|
[6055] | 161 | Ogre::RaySceneQueryResult& result = rsq->execute(); |
---|
[6111] | 162 | Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity()); |
---|
[6033] | 163 | |
---|
[6055] | 164 | Ogre::RaySceneQueryResult::iterator itr; |
---|
| 165 | for (itr = result.begin(); itr != result.end(); ++itr) |
---|
| 166 | { |
---|
| 167 | if (itr->movable->isInScene() && itr->movable->getMovableType() == "Entity" && itr->distance > 500) |
---|
[6045] | 168 | { |
---|
[6055] | 169 | // Try to cast the user pointer |
---|
| 170 | WorldEntity* wePtr = dynamic_cast<WorldEntity*>(itr->movable->getUserObject()); |
---|
| 171 | if (wePtr) |
---|
| 172 | { |
---|
[6114] | 173 | // go through all parents of object and look wheter they are Sightable or not |
---|
| 174 | bool isSightable = false; |
---|
| 175 | WorldEntity* parent = wePtr->getParent(); |
---|
| 176 | while( parent ) |
---|
| 177 | { |
---|
| 178 | if (this->targetMask_.isExcluded(parent->getIdentifier())) |
---|
| 179 | { |
---|
| 180 | parent = parent->getParent(); |
---|
| 181 | continue; |
---|
| 182 | } |
---|
| 183 | else |
---|
| 184 | { |
---|
| 185 | isSightable = true; |
---|
| 186 | break; |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | if ( !isSightable ) |
---|
[6055] | 190 | continue; |
---|
[6045] | 191 | } |
---|
[6111] | 192 | |
---|
[6116] | 193 | if( this->getControllableEntity()->getTarget() != wePtr ) |
---|
| 194 | this->getControllableEntity()->setTarget(wePtr); |
---|
[6091] | 195 | |
---|
[6055] | 196 | itr->movable->getParentSceneNode()->showBoundingBox(true); |
---|
[6091] | 197 | //std::cout << itr->movable->getParentSceneNode()->_getDerivedPosition() << endl; |
---|
[6111] | 198 | //return mouseRay.getOrigin() + mouseRay.getDirection() * itr->distance; //or itr->movable->getParentSceneNode()->_getDerivedPosition() |
---|
| 199 | if ( pawn ) |
---|
| 200 | { |
---|
| 201 | pawn->setAimPosition( mouseRay.getOrigin() + mouseRay.getDirection() * itr->distance ); // or itr->movable->getParentSceneNode()->_getDerivedPosition() |
---|
| 202 | pawn->setTarget( wePtr ); |
---|
| 203 | } |
---|
| 204 | return; |
---|
[6045] | 205 | } |
---|
[6091] | 206 | |
---|
[6055] | 207 | } |
---|
[6111] | 208 | if ( pawn ) |
---|
| 209 | { |
---|
| 210 | pawn->setAimPosition( mouseRay.getOrigin() + mouseRay.getDirection() * 1200 ); |
---|
[6116] | 211 | if( this->getControllableEntity()->getTarget() != 0 ) |
---|
| 212 | pawn->setTarget( 0 ); |
---|
[6111] | 213 | } |
---|
| 214 | |
---|
[6033] | 215 | |
---|
[6091] | 216 | //return this->controllableEntity_->getWorldPosition() + (this->controllableEntity_->getWorldOrientation() * Vector3::NEGATIVE_UNIT_Z * 2000); |
---|
[6058] | 217 | //return this->controllableEntity_->getWorldPosition() + (this->controllableEntity_->getCamera()->getOgreCamera()->getOrientation() * Vector3::NEGATIVE_UNIT_Z); |
---|
[6033] | 218 | } |
---|
| 219 | |
---|
[5993] | 220 | void NewHumanController::yaw(const Vector2& value) |
---|
| 221 | { |
---|
| 222 | // SUPER(NewHumanController, yaw, value); |
---|
[6091] | 223 | if (this->controlMode_ == 0) |
---|
| 224 | HumanController::yaw(value); |
---|
[5993] | 225 | |
---|
| 226 | this->currentYaw_ = value.x; |
---|
[6033] | 227 | //std::cout << "Y: " << static_cast<float>(this->currentPitch_) << " X: " << static_cast<float>(this->currentYaw_) << endl; |
---|
[5979] | 228 | } |
---|
[6055] | 229 | |
---|
[5993] | 230 | void NewHumanController::pitch(const Vector2& value) |
---|
[5981] | 231 | { |
---|
[5993] | 232 | // SUPER(NewHumanController, pitch, value); |
---|
[6091] | 233 | if (this->controlMode_ == 0) |
---|
| 234 | HumanController::pitch(value); |
---|
| 235 | |
---|
[5993] | 236 | this->currentPitch_ = value.x; |
---|
[6033] | 237 | //std::cout << "Y: " << static_cast<float>(this->currentPitch_) << " X: " << static_cast<float>(this->currentYaw_) << endl; |
---|
[5981] | 238 | } |
---|
[6091] | 239 | |
---|
| 240 | void NewHumanController::changeMode() { |
---|
[6111] | 241 | if (NewHumanController::localController_s && NewHumanController::localController_s->controlMode_ == 0) |
---|
| 242 | { |
---|
| 243 | if (NewHumanController::localController_s->controllableEntity_ && !NewHumanController::localController_s->controllableEntity_->isInMouseLook() ) |
---|
| 244 | NewHumanController::localController_s->controlMode_ = 1; |
---|
| 245 | } |
---|
[6091] | 246 | else |
---|
| 247 | NewHumanController::localController_s->controlMode_ = 0; |
---|
| 248 | } |
---|
[6115] | 249 | |
---|
| 250 | void NewHumanController::changedControllableEntity() |
---|
| 251 | { |
---|
| 252 | this->controlMode_ = 0; |
---|
| 253 | this->currentYaw_ = 0; |
---|
| 254 | this->currentPitch_ = 0; |
---|
| 255 | } |
---|
[5979] | 256 | } |
---|