Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6116 was 6116, checked in by scheusso, 15 years ago

this should prevent unneccessary network traffic

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