Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6223 was 6223, checked in by scheusso, 14 years ago

fixed some uninitialised variables

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