Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged steering branch to presentation2 branch

File size: 7.3 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 "infos/PlayerInfo.h"
40#include "overlays/OrxonoxOverlay.h"
41#include "graphics/Camera.h"
42#include "sound/SoundManager.h"
43#include "Scene.h"
44
45namespace orxonox
46{
47    SetConsoleCommand(NewHumanController, changeMode,          false).keybindMode(KeybindMode::OnPress);
48
49    CreateUnloadableFactory(NewHumanController);
50
51    NewHumanController* NewHumanController::localController_s = 0;
52
53    NewHumanController::NewHumanController(BaseObject* creator) : HumanController(creator)
54    {
55        RegisterObject(NewHumanController);
56
57        overlaySize_ = 0.08;
58        controlMode_ = 0;
59
60        crossHairOverlay_ = new OrxonoxOverlay(this);
61        crossHairOverlay_->setBackgroundMaterial("Orxonox/Crosshair3");
62        crossHairOverlay_->setSize(Vector2(overlaySize_, overlaySize_));
63        crossHairOverlay_->show();
64
65        // HACK: Define which objects are targetable when considering the creator of an orxonox::Model
66        this->targetMask_.exclude(ClassByString("BaseObject"));
67        this->targetMask_.include(ClassByString("WorldEntity"));
68        this->targetMask_.exclude(ClassByString("Projectile"));
69
70        NewHumanController::localController_s = this;
71    }
72
73    NewHumanController::~NewHumanController()
74    {
75        if (this->isInitialized())
76        {
77        }
78    }
79
80    void NewHumanController::tick(float dt)
81    {
82        crossHairOverlay_->setPosition(Vector2(static_cast<float>(this->currentYaw_)/2*-1+.5-overlaySize_/2, static_cast<float>(this->currentPitch_)/2*-1+.5-overlaySize_/2));
83
84        HumanController::tick(dt);
85    }
86
87    /*void NewHumanController::tick(float dt)
88    {
89        if (GameMode::playsSound() && NewHumanController::localController_s && NewHumanController::localController_s->controllableEntity_)
90        {
91            // Update sound listener
92            Camera* camera = NewHumanController::localController_s->controllableEntity_->getCamera();
93            if (camera)
94            {
95                SoundManager::getInstance().setListenerPosition(camera->getWorldPosition());
96                SoundManager::getInstance().setListenerOrientation(camera->getWorldOrientation());
97            }
98            else
99                COUT(3) << "NewHumanController, Warning: Using a ControllableEntity without Camera" << std::endl;
100        }
101    }*/
102   
103    void NewHumanController::doFire(unsigned int firemode)
104    {
105        //if (HumanController::localController_s && HumanController::localController_s->controllableEntity_) {
106
107/*
108        // Get results, create a node/entity on the position
109        for ( itr = result.begin(); itr != result.end(); itr++ )
110        {
111            if (itr->movable && itr->movable->getName() == "Head")
112            {
113                soundMgr->StopSound( &jaguarSoundChannel );
114                soundMgr->PlaySound( jaguarSound, headNode, &jaguarSoundChannel );
115                break;
116            } // if
117        }
118*/
119
120        HumanController::localController_s->getControllableEntity()->fire(firemode);
121    }
122
123    Vector3 NewHumanController::getTarget()
124    {
125        Ogre::RaySceneQuery * rsq = HumanController::localController_s->getControllableEntity()->getScene()->getSceneManager()->createRayQuery(Ogre::Ray());
126
127        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);
128
129        rsq->setRay(mouseRay);
130        rsq->setSortByDistance(true);
131
132        /*
133        Distance of objects:
134        ignore everything under 200 maybe even take 1000 as min distance to shoot at
135
136        shots are regularly traced and are entities!!!!!!!!! this is the biggest problem
137        they vanish only after a distance of 10'000
138        */
139
140
141        Ogre::RaySceneQueryResult& result = rsq->execute();
142
143        Ogre::RaySceneQueryResult::iterator itr;
144        for (itr = result.begin(); itr != result.end(); ++itr)
145        {
146            if (itr->movable->isInScene() && itr->movable->getMovableType() == "Entity" && itr->distance > 500)
147            {
148                // Try to cast the user pointer
149                WorldEntity* wePtr = dynamic_cast<WorldEntity*>(itr->movable->getUserObject());
150                if (wePtr)
151                {
152                    BaseObject* creator = wePtr->getCreator();
153                    if (this->targetMask_.isExcluded(creator->getIdentifier()))
154                        continue;
155                }
156
157                itr->movable->getParentSceneNode()->showBoundingBox(true);
158                //std::cout << itr->movable->getParentSceneNode()->_getDerivedPosition() << endl;
159                return mouseRay.getOrigin() + mouseRay.getDirection() * itr->distance; //or itr->movable->getParentSceneNode()->_getDerivedPosition()
160            }
161
162        }
163
164        return mouseRay.getOrigin() + mouseRay.getDirection() * 1200;
165
166        //return this->controllableEntity_->getWorldPosition() + (this->controllableEntity_->getWorldOrientation() * Vector3::NEGATIVE_UNIT_Z * 2000);
167        //return this->controllableEntity_->getWorldPosition() + (this->controllableEntity_->getCamera()->getOgreCamera()->getOrientation() * Vector3::NEGATIVE_UNIT_Z);
168    }
169
170    void NewHumanController::yaw(const Vector2& value)
171    {
172//         SUPER(NewHumanController, yaw, value);
173        if (this->controlMode_ == 0)
174            HumanController::yaw(value);
175       
176        this->currentYaw_ = value.x;
177        //std::cout << "Y: " << static_cast<float>(this->currentPitch_) << " X: " << static_cast<float>(this->currentYaw_) << endl;
178    }
179
180    void NewHumanController::pitch(const Vector2& value)
181    {
182//         SUPER(NewHumanController, pitch, value);
183        if (this->controlMode_ == 0)
184            HumanController::pitch(value);
185
186        this->currentPitch_ = value.x;
187        //std::cout << "Y: " << static_cast<float>(this->currentPitch_) << " X: " << static_cast<float>(this->currentYaw_) << endl;
188    }
189
190    void NewHumanController::changeMode() {
191        if (NewHumanController::localController_s->controlMode_ == 0)
192            NewHumanController::localController_s->controlMode_ = 1;
193        else
194            NewHumanController::localController_s->controlMode_ = 0;
195    }
196}
Note: See TracBrowser for help on using the repository browser.