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