Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6256 was 6256, checked in by rgrieder, 15 years ago

Removed debug output.

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