Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/orxonox/controllers/NewHumanController.cc @ 6279

Last change on this file since 6279 was 6279, checked in by scheusso, 16 years ago

overlay fix in NHC

  • Property svn:eol-style set to native
File size: 18.1 KB
RevLine 
[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
[6256]31#include <cmath>
[6033]32#include <OgreRay.h>
33#include <OgreSceneQuery.h>
34#include <OgreCamera.h>
35#include <OgreSceneManager.h>
[5981]36
[5979]37#include "core/CoreIncludes.h"
38#include "core/ConsoleCommand.h"
39#include "worldentities/ControllableEntity.h"
[6111]40#include "worldentities/pawns/Pawn.h"
[5979]41#include "infos/PlayerInfo.h"
[6055]42#include "overlays/OrxonoxOverlay.h"
[5979]43#include "graphics/Camera.h"
44#include "sound/SoundManager.h"
45#include "Scene.h"
46
47namespace orxonox
48{
[6091]49    SetConsoleCommand(NewHumanController, changeMode,          false).keybindMode(KeybindMode::OnPress);
[6149]50    SetConsoleCommand(NewHumanController, accelerate,          false).keybindMode(KeybindMode::OnPress);
51    SetConsoleCommand(NewHumanController, decelerate,          false).keybindMode(KeybindMode::OnPress);
[6210]52    SetConsoleCommand(NewHumanController, unfire,              true).keybindMode(KeybindMode::OnRelease);
[6091]53
[5979]54    CreateUnloadableFactory(NewHumanController);
55
[6091]56    NewHumanController* NewHumanController::localController_s = 0;
57
[6122]58    NewHumanController::NewHumanController(BaseObject* creator)
59        : HumanController(creator)
60        , crossHairOverlay_(NULL)
[6195]61        , centerOverlay_(NULL)
[5979]62    {
63        RegisterObject(NewHumanController);
[6001]64
[6055]65        overlaySize_ = 0.08;
[6210]66        arrowsSize_ = 0.4;
[6055]67        controlMode_ = 0;
[6149]68        acceleration_ = 0;
[6236]69        accelerating_ = false;
[6210]70        firemode_ = -1;
71        showArrows_ = true;
[6236]72        showOverlays_ = false;
[6033]73
[6149]74        //currentPitch_ = 1;
75        //currentYaw_ = 1;
76
[6122]77        if (GameMode::showsGraphics())
78        {
79            crossHairOverlay_ = new OrxonoxOverlay(this);
80            crossHairOverlay_->setBackgroundMaterial("Orxonox/Crosshair3");
81            crossHairOverlay_->setSize(Vector2(overlaySize_, overlaySize_));
[6195]82            crossHairOverlay_->hide();
[6210]83            //crossHairOverlay_->setAspectCorrection(true); not working
[6195]84
85            centerOverlay_ = new OrxonoxOverlay(this);
86            centerOverlay_->setBackgroundMaterial("Orxonox/CenterOverlay");
87            centerOverlay_->setSize(Vector2(overlaySize_ * 2.5, overlaySize_ * 2.5));
88            centerOverlay_->setPosition(Vector2(0.5 - overlaySize_*2.5/2.0, 0.5 - overlaySize_*2.5/2.0));\
89            centerOverlay_->hide();
[6210]90
91            if (showArrows_)
92            {
93                arrowsOverlay1_ = new OrxonoxOverlay(this);
[6212]94                arrowsOverlay1_->setBackgroundMaterial("Orxonox/CenterOverlay");
[6210]95                arrowsOverlay1_->setSize(Vector2(0.02727, 0.36 * arrowsSize_));
96                arrowsOverlay1_->setPickPoint(Vector2(0.5, 0.5));
97                arrowsOverlay1_->setPosition(Vector2(0.5, 0.5));
98                arrowsOverlay1_->hide();
99   
100                arrowsOverlay2_ = new OrxonoxOverlay(this);
[6212]101                arrowsOverlay2_->setBackgroundMaterial("Orxonox/CenterOverlay");
[6210]102                arrowsOverlay2_->setSize(Vector2(0.02727, 0.59 * arrowsSize_));
103                arrowsOverlay2_->setPickPoint(Vector2(0.5, 0.5));
104                arrowsOverlay2_->setPosition(Vector2(0.5, 0.5));
105                arrowsOverlay2_->hide();
106   
107                arrowsOverlay3_ = new OrxonoxOverlay(this);
[6212]108                arrowsOverlay3_->setBackgroundMaterial("Orxonox/CenterOverlay");
[6210]109                arrowsOverlay3_->setSize(Vector2(0.02727, 0.77 * arrowsSize_));
110                arrowsOverlay3_->setPickPoint(Vector2(0.5, 0.5));
111                arrowsOverlay3_->setPosition(Vector2(0.5, 0.5));
112                arrowsOverlay3_->hide();
113   
114                arrowsOverlay4_ = new OrxonoxOverlay(this);
[6212]115                arrowsOverlay4_->setBackgroundMaterial("Orxonox/CenterOverlay");
[6210]116                arrowsOverlay4_->setSize(Vector2(0.02727, arrowsSize_));
117                arrowsOverlay4_->setPickPoint(Vector2(0.5, 0.5));
118                arrowsOverlay4_->setPosition(Vector2(0.5, 0.5));
119                arrowsOverlay4_->hide();
120            }
[6122]121        }
[6045]122
[6058]123        // HACK: Define which objects are targetable when considering the creator of an orxonox::Model
[6055]124        this->targetMask_.exclude(ClassByString("BaseObject"));
125        this->targetMask_.include(ClassByString("WorldEntity"));
126        this->targetMask_.exclude(ClassByString("Projectile"));
[6091]127
128        NewHumanController::localController_s = this;
[6195]129
[6236]130        controlPaused_ = false;
[6210]131
[6195]132//HumanController::localController_s->getControllableEntity()->getCamera()->setDrag(true);
[5979]133    }
134
135    NewHumanController::~NewHumanController()
136    {
[6055]137        if (this->isInitialized())
[5981]138        {
[6122]139            if (this->crossHairOverlay_)
140                this->crossHairOverlay_->destroy();
[6210]141            if (this->centerOverlay_)
142                this->centerOverlay_->destroy();
143
144            if (showArrows_)
145            {
146                if (this->arrowsOverlay1_)
147                    this->arrowsOverlay1_->destroy();
148                if (this->arrowsOverlay2_)
149                    this->arrowsOverlay2_->destroy();
150                if (this->arrowsOverlay3_)
151                    this->arrowsOverlay3_->destroy();
152                if (this->arrowsOverlay4_)
153                    this->arrowsOverlay4_->destroy();
154            }
[5981]155        }
[5979]156    }
157
[6055]158    void NewHumanController::tick(float dt)
159    {
[6122]160        if (GameMode::showsGraphics())
[6111]161        {
[6210]162
[6122]163            if( this->controllableEntity_ && !this->controllableEntity_->isInMouseLook() )
164            {
[6279]165                if (this->getControllableEntity() && this->getControllableEntity()->getIdentifier()->getName() == "SpaceShip")
166                    this->showOverlays();
[6157]167                this->updateTarget();
[6195]168                if ( !controlPaused_ ) {
169                    this->crossHairOverlay_->setPosition(Vector2(static_cast<float>(this->currentYaw_)/2*-1+.5-overlaySize_/2, static_cast<float>(this->currentPitch_)/2*-1+.5-overlaySize_/2));
[6210]170
171                    if ( this->controlMode_ == 0 || ( this->controlMode_ == 1 && this->firemode_ == 1 ) )
[6236]172                    {
173                        if ( this->showOverlays_ )
174                            alignArrows();
175                    }
[6210]176                    else
177                        hideArrows();
[6195]178                }
[6122]179            }
[6279]180            else
181                this->hideOverlays();
[6210]182
[6149]183            if ( this->acceleration_ > 0 )
[6173]184            {
185/*
186if (this->controllableEntity_ && this->controllableEntity_->getEngine()) {
187    std::cout << this->controllableEntity_->getEngine()->getAccelerationFront() << endl;
188}
189*/
190                if ( this->accelerating_ )
191                    HumanController::moveFrontBack(Vector2(1, 0));
192                else
193                    HumanController::moveFrontBack(Vector2(this->acceleration_, 0)); 
194                this->accelerating_ = false;
195                //HumanController::moveFrontBack(Vector2(clamp(this->acceleration_ + this->currentAcceleration_, 0.0f, 1.0f), 0));
196            }
[6111]197        }
[6001]198
199        HumanController::tick(dt);
200    }
201
[5993]202    /*void NewHumanController::tick(float dt)
[5979]203    {
204        if (GameMode::playsSound() && NewHumanController::localController_s && NewHumanController::localController_s->controllableEntity_)
205        {
206            // Update sound listener
207            Camera* camera = NewHumanController::localController_s->controllableEntity_->getCamera();
208            if (camera)
209            {
210                SoundManager::getInstance().setListenerPosition(camera->getWorldPosition());
211                SoundManager::getInstance().setListenerOrientation(camera->getWorldOrientation());
212            }
213            else
214                COUT(3) << "NewHumanController, Warning: Using a ControllableEntity without Camera" << std::endl;
215        }
[5993]216    }*/
217   
[6033]218    void NewHumanController::doFire(unsigned int firemode)
219    {
220        //if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) {
221
222/*
[6055]223        // Get results, create a node/entity on the position
224        for ( itr = result.begin(); itr != result.end(); itr++ )
225        {
226            if (itr->movable && itr->movable->getName() == "Head")
227            {
228                soundMgr->StopSound( &jaguarSoundChannel );
229                soundMgr->PlaySound( jaguarSound, headNode, &jaguarSoundChannel );
230                break;
231            } // if
232        }
[6033]233*/
234
[6210]235        this->firemode_ = firemode;
236
[6157]237        if (firemode == 1 && this->controlMode_ == 1)
238        {
[6143]239            //unlocked steering, steer on right mouse click
[6144]240            HumanController::yaw(Vector2(this->currentYaw_, 0));
241            HumanController::pitch(Vector2(this->currentPitch_, 0));
[6143]242        }
[6157]243        else
[6143]244            HumanController::localController_s->getControllableEntity()->fire(firemode);
245
[6045]246    }
247
[6210]248    void NewHumanController::unfire()
249    {
250        if (NewHumanController::localController_s)
251            NewHumanController::localController_s->doUnfire();
252    }
253
254    void NewHumanController::doUnfire()
255    {
256        this->firemode_ = -1;
257        hideArrows();
258    }
259
[6111]260    void NewHumanController::updateTarget()
[6055]261    {
262        Ogre::RaySceneQuery * rsq = HumanController::localController_s->getControllableEntity()->getScene()->getSceneManager()->createRayQuery(Ogre::Ray());
[6033]263
[6058]264        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]265
[6055]266        rsq->setRay(mouseRay);
267        rsq->setSortByDistance(true);
[6033]268
[6055]269        /*
270        Distance of objects:
271        ignore everything under 200 maybe even take 1000 as min distance to shoot at
[6033]272
[6055]273        shots are regularly traced and are entities!!!!!!!!! this is the biggest problem
274        they vanish only after a distance of 10'000
275        */
[6045]276
277
[6055]278        Ogre::RaySceneQueryResult& result = rsq->execute();
[6111]279        Pawn* pawn = orxonox_cast<Pawn*>(this->getControllableEntity());
[6033]280
[6055]281        Ogre::RaySceneQueryResult::iterator itr;
282        for (itr = result.begin(); itr != result.end(); ++itr)
283        {
284            if (itr->movable->isInScene() && itr->movable->getMovableType() == "Entity" && itr->distance > 500)
[6045]285            {
[6055]286                // Try to cast the user pointer
287                WorldEntity* wePtr = dynamic_cast<WorldEntity*>(itr->movable->getUserObject());
288                if (wePtr)
289                {
[6114]290                    // go through all parents of object and look wheter they are Sightable or not
291                    bool isSightable = false;
292                    WorldEntity* parent = wePtr->getParent();
293                    while( parent )
294                    {
295                        if (this->targetMask_.isExcluded(parent->getIdentifier()))
296                        {
297                            parent = parent->getParent();
298                            continue;
299                        }
300                        else
301                        {
302                            isSightable = true;
303                            break;
304                        }
305                    }
306                    if ( !isSightable )
[6055]307                        continue;
[6045]308                }
[6143]309
310                if ( this->getControllableEntity() && this->getControllableEntity()->getTarget() != wePtr )
311                {
[6116]312                    this->getControllableEntity()->setTarget(wePtr);
[6143]313                }
[6091]314
[6143]315                if( pawn )
316                {
317                    pawn->setAimPosition( mouseRay.getOrigin() + mouseRay.getDirection() * itr->distance );
318                }
319
[6195]320                //itr->movable->getParentSceneNode()->showBoundingBox(true);
[6091]321                //std::cout << itr->movable->getParentSceneNode()->_getDerivedPosition() << endl;
[6111]322                //return mouseRay.getOrigin() + mouseRay.getDirection() * itr->distance; //or itr->movable->getParentSceneNode()->_getDerivedPosition()
323                return;
[6045]324            }
[6091]325
[6055]326        }
[6111]327        if ( pawn )
328        {
329            pawn->setAimPosition( mouseRay.getOrigin() + mouseRay.getDirection() * 1200 );
330        }
[6143]331
332        if( this->getControllableEntity() && this->getControllableEntity()->getTarget() != 0 )
333            this->getControllableEntity()->setTarget( 0 );
[6111]334   
[6033]335
[6091]336        //return this->controllableEntity_->getWorldPosition() + (this->controllableEntity_->getWorldOrientation() * Vector3::NEGATIVE_UNIT_Z * 2000);
[6058]337        //return this->controllableEntity_->getWorldPosition() + (this->controllableEntity_->getCamera()->getOgreCamera()->getOrientation() * Vector3::NEGATIVE_UNIT_Z);
[6033]338    }
339
[6149]340    void NewHumanController::frontback(const Vector2& value)
341    {
[6173]342        this->accelerating_ = true;
[6149]343
[6173]344        //if (this->acceleration_ == 0)
[6149]345            HumanController::frontback(value);
346    }
347
[5993]348    void NewHumanController::yaw(const Vector2& value)
349    {
350//         SUPER(NewHumanController, yaw, value);
[6210]351        if (this->controlMode_ == 0 || ( this->controllableEntity_ && this->controllableEntity_->isInMouseLook() ) )
[6091]352            HumanController::yaw(value);
[6143]353
[5993]354        this->currentYaw_ = value.x;
[5979]355    }
[6055]356
[5993]357    void NewHumanController::pitch(const Vector2& value)
[5981]358    {
[5993]359//         SUPER(NewHumanController, pitch, value);
[6210]360        if (this->controlMode_ == 0 || ( this->controllableEntity_ && this->controllableEntity_->isInMouseLook() ) )
[6091]361            HumanController::pitch(value);
362
[5993]363        this->currentPitch_ = value.x;
[5981]364    }
[6091]365
[6149]366    void NewHumanController::changeMode()
367    {
[6111]368        if (NewHumanController::localController_s && NewHumanController::localController_s->controlMode_ == 0)
369        {
370                NewHumanController::localController_s->controlMode_ = 1;
[6210]371                NewHumanController::localController_s->hideArrows();
[6111]372        }
[6091]373        else
374            NewHumanController::localController_s->controlMode_ = 0;
375    }
[6143]376
[6115]377    void NewHumanController::changedControllableEntity()
378    {
379        this->controlMode_ = 0;
380        this->currentYaw_ = 0;
381        this->currentPitch_ = 0;
[6236]382        if (this->getControllableEntity() && this->getControllableEntity()->getIdentifier()->getName() == "SpaceShip")
383        {
384            this->showOverlays_ = true;
385            if( !this->controlPaused_ )
386            {
387                this->showOverlays();
388                this->alignArrows();
389            }
390        }
391        else
392        {
393            this->showOverlays_ = false;
394            this->hideOverlays();
395        }
[6115]396    }
[6149]397
398    void NewHumanController::accelerate()
399    {
[6157]400        if ( NewHumanController::localController_s )
401        {
[6173]402            NewHumanController::localController_s->acceleration_ = clamp(NewHumanController::localController_s->acceleration_ + 0.2f, 0.00f, 1.0f);
[6149]403        }
404    }
405
406    void NewHumanController::decelerate()
407    {
[6157]408        if ( NewHumanController::localController_s )
409        {
[6173]410            NewHumanController::localController_s->acceleration_ = clamp(NewHumanController::localController_s->acceleration_ - 0.1f, 0.0f, 1.0f);
[6149]411        }
412    }
[6195]413
414    void NewHumanController::doResumeControl() {
415        this->controlPaused_ = false;
[6236]416        if( this->showOverlays_ ) {
417            this->showOverlays();
418        }
[6195]419    }
420
421    void NewHumanController::doPauseControl() {
422        this->controlPaused_ = true;
[6236]423       
424        this->hideOverlays();
[6195]425    }
[6210]426
427    void NewHumanController::alignArrows() {
428        if (showArrows_) {
429            hideArrows();
430   
431            float distance = sqrt(pow(static_cast<float>(this->currentYaw_)/2*-1,2) + pow(static_cast<float>(this->currentPitch_)/2*-1,2));
432   
433            if ( distance > 0.04 && distance <= 0.59 * arrowsSize_ / 2.0 ) {
434                this->arrowsOverlay1_->setRotation(Degree(-90 + -1.0 * atan2(static_cast<float>(this->currentPitch_)/2*-1, static_cast<float>(this->currentYaw_)/2*-1) / (2.0f * Ogre::Math::PI) * 360.0f));
435   
436                this->arrowsOverlay1_->show();
437            }
438            else if ( distance > 0.59 * arrowsSize_ / 2.0 && distance <= 0.77 * arrowsSize_ / 2.0 ) {
439                this->arrowsOverlay2_->setRotation(Degree(-90 + -1.0 * atan2(static_cast<float>(this->currentPitch_)/2*-1, static_cast<float>(this->currentYaw_)/2*-1) / (2.0f * Ogre::Math::PI) * 360.0f));
440   
441                this->arrowsOverlay2_->show();
442            }
443            else if ( distance > 0.77 * arrowsSize_ / 2.0 && distance <= arrowsSize_ / 2.0 ) {
444                this->arrowsOverlay3_->setRotation(Degree(-90 + -1.0 * atan2(static_cast<float>(this->currentPitch_)/2*-1, static_cast<float>(this->currentYaw_)/2*-1) / (2.0f * Ogre::Math::PI) * 360.0f));
445   
446                this->arrowsOverlay3_->show();
447            }
448            else if ( distance > arrowsSize_ / 2.0 ) {
449                this->arrowsOverlay4_->setRotation(Degree(-90 + -1.0 * atan2(static_cast<float>(this->currentPitch_)/2*-1, static_cast<float>(this->currentYaw_)/2*-1) / (2.0f * Ogre::Math::PI) * 360.0f));
450   
451                this->arrowsOverlay4_->show();
452            }
453        }
454    }
455
[6236]456    void NewHumanController::showOverlays() {
457        this->crossHairOverlay_->show();
458        this->centerOverlay_->show();
459       
460        if (showArrows_) {
461            this->arrowsOverlay1_->show();
462            this->arrowsOverlay2_->show();
463            this->arrowsOverlay3_->show();
464            this->arrowsOverlay4_->show();
465        }
466    }
467
468    void NewHumanController::hideOverlays() {
469        this->crossHairOverlay_->hide();
470        this->centerOverlay_->hide();
471       
472        this->hideArrows();
473    }
474
[6210]475    void NewHumanController::hideArrows() {
476        if (showArrows_) {
477            this->arrowsOverlay1_->hide();
478            this->arrowsOverlay2_->hide();
479            this->arrowsOverlay3_->hide();
480            this->arrowsOverlay4_->hide();
481        }
482    }
[5979]483}
Note: See TracBrowser for help on using the repository browser.