[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); |
---|
[6149] | 49 | SetConsoleCommand(NewHumanController, accelerate, false).keybindMode(KeybindMode::OnPress); |
---|
| 50 | SetConsoleCommand(NewHumanController, decelerate, false).keybindMode(KeybindMode::OnPress); |
---|
[6091] | 51 | |
---|
[5979] | 52 | CreateUnloadableFactory(NewHumanController); |
---|
| 53 | |
---|
[6091] | 54 | NewHumanController* NewHumanController::localController_s = 0; |
---|
| 55 | |
---|
[6122] | 56 | NewHumanController::NewHumanController(BaseObject* creator) |
---|
| 57 | : HumanController(creator) |
---|
| 58 | , crossHairOverlay_(NULL) |
---|
[6195] | 59 | , centerOverlay_(NULL) |
---|
[5979] | 60 | { |
---|
| 61 | RegisterObject(NewHumanController); |
---|
[6001] | 62 | |
---|
[6055] | 63 | overlaySize_ = 0.08; |
---|
| 64 | controlMode_ = 0; |
---|
[6149] | 65 | acceleration_ = 0; |
---|
[6033] | 66 | |
---|
[6149] | 67 | //currentPitch_ = 1; |
---|
| 68 | //currentYaw_ = 1; |
---|
| 69 | |
---|
[6122] | 70 | if (GameMode::showsGraphics()) |
---|
| 71 | { |
---|
| 72 | crossHairOverlay_ = new OrxonoxOverlay(this); |
---|
| 73 | crossHairOverlay_->setBackgroundMaterial("Orxonox/Crosshair3"); |
---|
| 74 | crossHairOverlay_->setSize(Vector2(overlaySize_, overlaySize_)); |
---|
[6195] | 75 | crossHairOverlay_->hide(); |
---|
| 76 | |
---|
| 77 | centerOverlay_ = new OrxonoxOverlay(this); |
---|
| 78 | centerOverlay_->setBackgroundMaterial("Orxonox/CenterOverlay"); |
---|
| 79 | centerOverlay_->setSize(Vector2(overlaySize_ * 2.5, overlaySize_ * 2.5)); |
---|
| 80 | centerOverlay_->setPosition(Vector2(0.5 - overlaySize_*2.5/2.0, 0.5 - overlaySize_*2.5/2.0));\ |
---|
| 81 | centerOverlay_->hide(); |
---|
[6122] | 82 | } |
---|
[6045] | 83 | |
---|
[6058] | 84 | // HACK: Define which objects are targetable when considering the creator of an orxonox::Model |
---|
[6055] | 85 | this->targetMask_.exclude(ClassByString("BaseObject")); |
---|
| 86 | this->targetMask_.include(ClassByString("WorldEntity")); |
---|
| 87 | this->targetMask_.exclude(ClassByString("Projectile")); |
---|
[6091] | 88 | |
---|
| 89 | NewHumanController::localController_s = this; |
---|
[6195] | 90 | |
---|
| 91 | //HumanController::localController_s->getControllableEntity()->getCamera()->setDrag(true); |
---|
[5979] | 92 | } |
---|
| 93 | |
---|
| 94 | NewHumanController::~NewHumanController() |
---|
| 95 | { |
---|
[6055] | 96 | if (this->isInitialized()) |
---|
[5981] | 97 | { |
---|
[6122] | 98 | if (this->crossHairOverlay_) |
---|
| 99 | this->crossHairOverlay_->destroy(); |
---|
[5981] | 100 | } |
---|
[5979] | 101 | } |
---|
| 102 | |
---|
[6055] | 103 | void NewHumanController::tick(float dt) |
---|
| 104 | { |
---|
[6122] | 105 | if (GameMode::showsGraphics()) |
---|
[6111] | 106 | { |
---|
[6122] | 107 | if( this->controllableEntity_ && !this->controllableEntity_->isInMouseLook() ) |
---|
| 108 | { |
---|
[6157] | 109 | this->updateTarget(); |
---|
[6195] | 110 | if ( !controlPaused_ ) { |
---|
| 111 | this->crossHairOverlay_->setPosition(Vector2(static_cast<float>(this->currentYaw_)/2*-1+.5-overlaySize_/2, static_cast<float>(this->currentPitch_)/2*-1+.5-overlaySize_/2)); |
---|
| 112 | this->crossHairOverlay_->show(); |
---|
| 113 | this->centerOverlay_->show(); |
---|
| 114 | } |
---|
[6122] | 115 | } |
---|
[6195] | 116 | else { |
---|
[6122] | 117 | this->crossHairOverlay_->hide(); |
---|
[6195] | 118 | this->centerOverlay_->hide(); |
---|
| 119 | } |
---|
[6149] | 120 | |
---|
| 121 | if ( this->acceleration_ > 0 ) |
---|
[6173] | 122 | { |
---|
| 123 | /* |
---|
| 124 | if (this->controllableEntity_ && this->controllableEntity_->getEngine()) { |
---|
| 125 | std::cout << this->controllableEntity_->getEngine()->getAccelerationFront() << endl; |
---|
| 126 | } |
---|
| 127 | */ |
---|
| 128 | if ( this->accelerating_ ) |
---|
| 129 | HumanController::moveFrontBack(Vector2(1, 0)); |
---|
| 130 | else |
---|
| 131 | HumanController::moveFrontBack(Vector2(this->acceleration_, 0)); |
---|
| 132 | this->accelerating_ = false; |
---|
| 133 | //HumanController::moveFrontBack(Vector2(clamp(this->acceleration_ + this->currentAcceleration_, 0.0f, 1.0f), 0)); |
---|
| 134 | } |
---|
[6111] | 135 | } |
---|
[6001] | 136 | |
---|
| 137 | HumanController::tick(dt); |
---|
| 138 | } |
---|
| 139 | |
---|
[5993] | 140 | /*void NewHumanController::tick(float dt) |
---|
[5979] | 141 | { |
---|
| 142 | if (GameMode::playsSound() && NewHumanController::localController_s && NewHumanController::localController_s->controllableEntity_) |
---|
| 143 | { |
---|
| 144 | // Update sound listener |
---|
| 145 | Camera* camera = NewHumanController::localController_s->controllableEntity_->getCamera(); |
---|
| 146 | if (camera) |
---|
| 147 | { |
---|
| 148 | SoundManager::getInstance().setListenerPosition(camera->getWorldPosition()); |
---|
| 149 | SoundManager::getInstance().setListenerOrientation(camera->getWorldOrientation()); |
---|
| 150 | } |
---|
| 151 | else |
---|
| 152 | COUT(3) << "NewHumanController, Warning: Using a ControllableEntity without Camera" << std::endl; |
---|
| 153 | } |
---|
[5993] | 154 | }*/ |
---|
| 155 | |
---|
[6033] | 156 | void NewHumanController::doFire(unsigned int firemode) |
---|
| 157 | { |
---|
| 158 | //if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) { |
---|
| 159 | |
---|
| 160 | /* |
---|
[6055] | 161 | // Get results, create a node/entity on the position |
---|
| 162 | for ( itr = result.begin(); itr != result.end(); itr++ ) |
---|
| 163 | { |
---|
| 164 | if (itr->movable && itr->movable->getName() == "Head") |
---|
| 165 | { |
---|
| 166 | soundMgr->StopSound( &jaguarSoundChannel ); |
---|
| 167 | soundMgr->PlaySound( jaguarSound, headNode, &jaguarSoundChannel ); |
---|
| 168 | break; |
---|
| 169 | } // if |
---|
| 170 | } |
---|
[6033] | 171 | */ |
---|
| 172 | |
---|
[6157] | 173 | if (firemode == 1 && this->controlMode_ == 1) |
---|
| 174 | { |
---|
[6143] | 175 | //unlocked steering, steer on right mouse click |
---|
[6144] | 176 | HumanController::yaw(Vector2(this->currentYaw_, 0)); |
---|
| 177 | HumanController::pitch(Vector2(this->currentPitch_, 0)); |
---|
[6143] | 178 | } |
---|
[6157] | 179 | else |
---|
[6143] | 180 | HumanController::localController_s->getControllableEntity()->fire(firemode); |
---|
| 181 | |
---|
[6045] | 182 | } |
---|
| 183 | |
---|
[6111] | 184 | void NewHumanController::updateTarget() |
---|
[6055] | 185 | { |
---|
| 186 | Ogre::RaySceneQuery * rsq = HumanController::localController_s->getControllableEntity()->getScene()->getSceneManager()->createRayQuery(Ogre::Ray()); |
---|
[6033] | 187 | |
---|
[6058] | 188 | 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] | 189 | |
---|
[6055] | 190 | rsq->setRay(mouseRay); |
---|
| 191 | rsq->setSortByDistance(true); |
---|
[6033] | 192 | |
---|
[6055] | 193 | /* |
---|
| 194 | Distance of objects: |
---|
| 195 | ignore everything under 200 maybe even take 1000 as min distance to shoot at |
---|
[6033] | 196 | |
---|
[6055] | 197 | shots are regularly traced and are entities!!!!!!!!! this is the biggest problem |
---|
| 198 | they vanish only after a distance of 10'000 |
---|
| 199 | */ |
---|
[6045] | 200 | |
---|
| 201 | |
---|
[6055] | 202 | Ogre::RaySceneQueryResult& result = rsq->execute(); |
---|
[6111] | 203 | Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity()); |
---|
[6033] | 204 | |
---|
[6055] | 205 | Ogre::RaySceneQueryResult::iterator itr; |
---|
| 206 | for (itr = result.begin(); itr != result.end(); ++itr) |
---|
| 207 | { |
---|
| 208 | if (itr->movable->isInScene() && itr->movable->getMovableType() == "Entity" && itr->distance > 500) |
---|
[6045] | 209 | { |
---|
[6055] | 210 | // Try to cast the user pointer |
---|
| 211 | WorldEntity* wePtr = dynamic_cast<WorldEntity*>(itr->movable->getUserObject()); |
---|
| 212 | if (wePtr) |
---|
| 213 | { |
---|
[6114] | 214 | // go through all parents of object and look wheter they are Sightable or not |
---|
| 215 | bool isSightable = false; |
---|
| 216 | WorldEntity* parent = wePtr->getParent(); |
---|
| 217 | while( parent ) |
---|
| 218 | { |
---|
| 219 | if (this->targetMask_.isExcluded(parent->getIdentifier())) |
---|
| 220 | { |
---|
| 221 | parent = parent->getParent(); |
---|
| 222 | continue; |
---|
| 223 | } |
---|
| 224 | else |
---|
| 225 | { |
---|
| 226 | isSightable = true; |
---|
| 227 | break; |
---|
| 228 | } |
---|
| 229 | } |
---|
| 230 | if ( !isSightable ) |
---|
[6055] | 231 | continue; |
---|
[6045] | 232 | } |
---|
[6143] | 233 | |
---|
| 234 | if ( this->getControllableEntity() && this->getControllableEntity()->getTarget() != wePtr ) |
---|
| 235 | { |
---|
[6116] | 236 | this->getControllableEntity()->setTarget(wePtr); |
---|
[6143] | 237 | } |
---|
[6091] | 238 | |
---|
[6143] | 239 | if( pawn ) |
---|
| 240 | { |
---|
| 241 | pawn->setAimPosition( mouseRay.getOrigin() + mouseRay.getDirection() * itr->distance ); |
---|
| 242 | } |
---|
| 243 | |
---|
[6195] | 244 | //itr->movable->getParentSceneNode()->showBoundingBox(true); |
---|
[6091] | 245 | //std::cout << itr->movable->getParentSceneNode()->_getDerivedPosition() << endl; |
---|
[6111] | 246 | //return mouseRay.getOrigin() + mouseRay.getDirection() * itr->distance; //or itr->movable->getParentSceneNode()->_getDerivedPosition() |
---|
| 247 | return; |
---|
[6045] | 248 | } |
---|
[6091] | 249 | |
---|
[6055] | 250 | } |
---|
[6111] | 251 | if ( pawn ) |
---|
| 252 | { |
---|
| 253 | pawn->setAimPosition( mouseRay.getOrigin() + mouseRay.getDirection() * 1200 ); |
---|
| 254 | } |
---|
[6143] | 255 | |
---|
| 256 | if( this->getControllableEntity() && this->getControllableEntity()->getTarget() != 0 ) |
---|
| 257 | this->getControllableEntity()->setTarget( 0 ); |
---|
[6111] | 258 | |
---|
[6033] | 259 | |
---|
[6091] | 260 | //return this->controllableEntity_->getWorldPosition() + (this->controllableEntity_->getWorldOrientation() * Vector3::NEGATIVE_UNIT_Z * 2000); |
---|
[6058] | 261 | //return this->controllableEntity_->getWorldPosition() + (this->controllableEntity_->getCamera()->getOgreCamera()->getOrientation() * Vector3::NEGATIVE_UNIT_Z); |
---|
[6033] | 262 | } |
---|
| 263 | |
---|
[6149] | 264 | void NewHumanController::frontback(const Vector2& value) |
---|
| 265 | { |
---|
[6173] | 266 | this->accelerating_ = true; |
---|
[6149] | 267 | |
---|
[6173] | 268 | //if (this->acceleration_ == 0) |
---|
[6149] | 269 | HumanController::frontback(value); |
---|
| 270 | } |
---|
| 271 | |
---|
[5993] | 272 | void NewHumanController::yaw(const Vector2& value) |
---|
| 273 | { |
---|
| 274 | // SUPER(NewHumanController, yaw, value); |
---|
[6091] | 275 | if (this->controlMode_ == 0) |
---|
| 276 | HumanController::yaw(value); |
---|
[6143] | 277 | |
---|
[5993] | 278 | this->currentYaw_ = value.x; |
---|
[5979] | 279 | } |
---|
[6055] | 280 | |
---|
[5993] | 281 | void NewHumanController::pitch(const Vector2& value) |
---|
[5981] | 282 | { |
---|
[5993] | 283 | // SUPER(NewHumanController, pitch, value); |
---|
[6091] | 284 | if (this->controlMode_ == 0) |
---|
| 285 | HumanController::pitch(value); |
---|
| 286 | |
---|
[5993] | 287 | this->currentPitch_ = value.x; |
---|
[5981] | 288 | } |
---|
[6091] | 289 | |
---|
[6149] | 290 | void NewHumanController::changeMode() |
---|
| 291 | { |
---|
[6111] | 292 | if (NewHumanController::localController_s && NewHumanController::localController_s->controlMode_ == 0) |
---|
| 293 | { |
---|
| 294 | if (NewHumanController::localController_s->controllableEntity_ && !NewHumanController::localController_s->controllableEntity_->isInMouseLook() ) |
---|
| 295 | NewHumanController::localController_s->controlMode_ = 1; |
---|
| 296 | } |
---|
[6091] | 297 | else |
---|
| 298 | NewHumanController::localController_s->controlMode_ = 0; |
---|
| 299 | } |
---|
[6143] | 300 | |
---|
[6115] | 301 | void NewHumanController::changedControllableEntity() |
---|
| 302 | { |
---|
| 303 | this->controlMode_ = 0; |
---|
| 304 | this->currentYaw_ = 0; |
---|
| 305 | this->currentPitch_ = 0; |
---|
| 306 | } |
---|
[6149] | 307 | |
---|
| 308 | void NewHumanController::accelerate() |
---|
| 309 | { |
---|
[6157] | 310 | if ( NewHumanController::localController_s ) |
---|
| 311 | { |
---|
[6173] | 312 | NewHumanController::localController_s->acceleration_ = clamp(NewHumanController::localController_s->acceleration_ + 0.2f, 0.00f, 1.0f); |
---|
[6149] | 313 | } |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | void NewHumanController::decelerate() |
---|
| 317 | { |
---|
[6157] | 318 | if ( NewHumanController::localController_s ) |
---|
| 319 | { |
---|
[6173] | 320 | NewHumanController::localController_s->acceleration_ = clamp(NewHumanController::localController_s->acceleration_ - 0.1f, 0.0f, 1.0f); |
---|
[6149] | 321 | } |
---|
| 322 | } |
---|
[6195] | 323 | |
---|
| 324 | void NewHumanController::doResumeControl() { |
---|
| 325 | this->controlPaused_ = false; |
---|
| 326 | this->crossHairOverlay_->show(); |
---|
| 327 | this->centerOverlay_->show(); |
---|
| 328 | } |
---|
| 329 | |
---|
| 330 | void NewHumanController::doPauseControl() { |
---|
| 331 | this->controlPaused_ = true; |
---|
| 332 | this->crossHairOverlay_->hide(); |
---|
| 333 | this->centerOverlay_->hide(); |
---|
| 334 | } |
---|
[5979] | 335 | } |
---|