Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6151 was 6149, checked in by wirthmi, 15 years ago

Introduced new feature: permanent speed

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