Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/orxonox/worldentities/pawns/FpsPlayer.cc @ 8600

Last change on this file since 8600 was 7184, checked in by rgrieder, 14 years ago

Replaced mathematical constants with a common definition in Math.h.
Use math::pi, math::pi_d (double), math::e, etc. from now on.

  • Property svn:eol-style set to native
File size: 9.6 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 *      Cyrill Frei
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "FpsPlayer.h"
30
31#include <OgreSceneNode.h>
32#include <BulletDynamics/Dynamics/btRigidBody.h>
33#include <LinearMath/btVector3.h>
34#include <BulletCollision/NarrowPhaseCollision/btManifoldPoint.h>
35#include <OgreSceneManager.h>
36#include <OgreSceneNode.h>
37#include <OgreEntity.h>
38
39#include "core/CoreIncludes.h"
40#include "core/ConfigValueIncludes.h"
41#include "core/Template.h"
42#include "core/XMLPort.h"
43#include "items/Engine.h"
44#include "Scene.h"
45#include "weaponsystem/WeaponPack.h"
46#include "weaponsystem/WeaponSlot.h"
47#include "weaponsystem/Weapon.h"
48
49#include <cmath>
50
51namespace orxonox
52{
53    const float orientationGain_ = 100;
54    const float jumpValue_ = 300;
55    CreateFactory(FpsPlayer);
56
57    FpsPlayer::FpsPlayer(BaseObject* creator) : Pawn(creator)
58    {
59        RegisterObject(FpsPlayer);
60        this->speed_ = 200;
61        this->localVelocity_ = Vector3::ZERO;
62/*
63 *        this->primaryThrust_  = 100;
64 *        this->auxilaryThrust_ =  30;
65 *        this->rotationThrust_ =  10;
66 *
67 *        this->localLinearAcceleration_.setValue(0, 0, 0);
68 *        this->localAngularAcceleration_.setValue(0, 0, 0);
69 *        this->bBoost_ = false;
70 *        this->bPermanentBoost_ = false;
71 *        this->steering_ = Vector3::ZERO;
72*/
73
74
75        this->bInvertYAxis_ = false;
76
77        this->setDestroyWhenPlayerLeft(true);
78
79        // FpsPlayer is always a physical object per default
80        // Be aware of this call: The collision type legality check will not reach derived classes!
81        this->setCollisionType(WorldEntity::Dynamic);
82        // Get notification about collisions
83        this->enableCollisionCallback();
84
85        this->setConfigValues();
86        this->registerVariables();
87
88        //this->weaponNode = this->cameraPositionRootNode_;
89        this->weaponNode_ = this->getScene()->getRootSceneNode()->createChildSceneNode();
90        this->attachNode(this->weaponNode_);
91    }
92
93    FpsPlayer::~FpsPlayer()
94    {
95        if (this->isInitialized())
96        {
97            if (this->mesh_.getEntity())
98                this->detachOgreObject(this->mesh_.getEntity());
99
100            if (this->weaponNode_ && this->getScene()->getSceneManager())
101                this->getScene()->getSceneManager()->destroySceneNode(this->weaponNode_->getName());
102        }
103    }
104
105    void FpsPlayer::XMLPort(Element& xmlelement, XMLPort::Mode mode)
106    {
107        SUPER(FpsPlayer, XMLPort, xmlelement, mode);
108
109        XMLPortParamVariable(FpsPlayer, "primaryThrust",  primaryThrust_,  xmlelement, mode);
110        XMLPortParamVariable(FpsPlayer, "auxilaryThrust", auxilaryThrust_, xmlelement, mode);
111        XMLPortParamVariable(FpsPlayer, "rotationThrust", rotationThrust_, xmlelement, mode);
112        XMLPortParam(FpsPlayer, "weapon", setMeshSource, getMeshSource, xmlelement, mode);
113    }
114
115    void FpsPlayer::registerVariables()
116    {
117        registerVariable(this->primaryThrust_,  VariableDirection::ToClient);
118        registerVariable(this->auxilaryThrust_, VariableDirection::ToClient);
119        registerVariable(this->rotationThrust_, VariableDirection::ToClient);
120        registerVariable(this->weaponMashName_, VariableDirection::ToClient);
121    }
122
123
124
125    void FpsPlayer::setConfigValues()
126    {
127        SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = targetting down).");
128    }
129
130    bool FpsPlayer::isCollisionTypeLegal(WorldEntity::CollisionType type) const
131    {
132        if (type != WorldEntity::Dynamic)
133        {
134            CCOUT(1) << "Error: Cannot tell a FpsPlayer not to be dynamic! Ignoring." << std::endl;
135            assert(false); // Only in debug mode
136            return false;
137        }
138        else
139            return true;
140    }
141
142    void FpsPlayer::tick(float dt)
143    {
144        if (this->hasLocalController())
145        {
146            this->setOrientation(savedOrientation_);
147
148            this->thisTickBoost_ = false;
149
150            float localSpeedSquared = this->localVelocity_.squaredLength();
151            float localSpeed;
152            if (localSpeedSquared > 1.0)
153                localSpeed = this->speed_ / sqrtf(localSpeedSquared);
154            else
155                localSpeed = this->speed_;
156
157            this->localVelocity_.x *= localSpeed;
158            this->localVelocity_.z *= localSpeed;
159            Vector3 temp = this->getOrientation() * this->localVelocity_;
160            if (localVelocity_.y == jumpValue_)
161                this->setVelocity(Vector3(temp.x, temp.y + this->getVelocity().y, temp.z));
162            else
163                this->setVelocity(Vector3(temp.x, this->getVelocity().y, temp.z));
164            this->localVelocity_.x = 0;
165            this->localVelocity_.y = 0;
166            this->localVelocity_.z = 0;
167
168            if (!this->isInMouseLook())
169            {
170                this->yaw(Radian(this->yaw_ * this->getMouseLookSpeed()), WorldEntity::Parent);
171
172                Radian pitch = this->cameraPositionRootNode_->getOrientation().getPitch();
173                if (pitch < Radian(math::pi_2) && pitch > Radian(-math::pi_2))
174                {
175                    this->cameraPositionRootNode_->pitch(Radian(this->pitch_ * this->getMouseLookSpeed()));
176                }
177                else if (pitch < Radian(-math::pi_2))
178                {
179                    if (this->pitch_ > 0.0f)
180                        this->cameraPositionRootNode_->pitch(Radian(this->pitch_ * this->getMouseLookSpeed()));
181                    else if (pitch < Radian(-math::pi_2))
182                        this->cameraPositionRootNode_->pitch(-pitch + Radian(-math::pi_2));
183                }
184                else if (pitch > Radian(math::pi_2))
185                {
186                    if (this->pitch_ < 0.0f)
187                        this->cameraPositionRootNode_->pitch(Radian(this->pitch_ * this->getMouseLookSpeed()));
188                    else if (pitch > Radian(math::pi_2))
189                        this->cameraPositionRootNode_->pitch(-pitch + Radian(math::pi_2));
190                }
191                this->weaponNode_->setOrientation(this->cameraPositionRootNode_->getOrientation());
192            }
193
194            this->yaw_ = this->pitch_ = this->roll_ = 0;
195
196            this->setAngularVelocity(0.0, 0.0, 0.0);
197            this->savedOrientation_ = this->getOrientation();
198        }
199
200        SUPER(FpsPlayer, tick, dt);
201    }
202
203    void FpsPlayer::changedMesh()
204    {
205        if (GameMode::showsGraphics())
206        {
207            if (this->mesh_.getEntity())
208                this->weaponNode_->detachObject(this->mesh_.getEntity());
209
210            this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_);
211
212            if (this->mesh_.getEntity())
213            {
214                this->weaponNode_->attachObject(this->mesh_.getEntity());
215            }
216        }
217    }
218
219    void FpsPlayer::setPlayer(PlayerInfo* player)
220    {
221        ControllableEntity::setPlayer(player);
222
223//        this->setSyncMode(ObjectDirection::ToClient);
224    }
225
226    void FpsPlayer::startLocalHumanControl()
227    {
228        ControllableEntity::startLocalHumanControl();
229    }
230
231    void FpsPlayer::moveFrontBack(const Vector2& value)
232    {
233        this->localVelocity_.z -= value.x;
234    }
235
236
237    void FpsPlayer::moveRightLeft(const Vector2& value)
238    {
239        this->localVelocity_.x += value.x;
240    }
241
242    void FpsPlayer::moveUpDown(const Vector2& value)
243    {
244        //this->localVelocity_.y += value.x;
245    }
246
247    void FpsPlayer::rotateYaw(const Vector2& value)
248    {
249        this->yaw_ += value.y;
250
251        ControllableEntity::rotateYaw(value);
252    }
253
254    void FpsPlayer::rotatePitch(const Vector2& value)
255    {
256        this->pitch_ += value.y;
257
258        ControllableEntity::rotatePitch(value);
259    }
260
261    void FpsPlayer::rotateRoll(const Vector2& value)
262    {
263        this->roll_ += value.y;
264
265        ControllableEntity::rotateRoll(value);
266    }
267
268    void FpsPlayer::fire()
269    {
270    }
271
272    void FpsPlayer::boost() //acctually jump
273    {
274        if (this->isFloor_)
275        {
276            if (!this->thisTickBoost_)
277                this->localVelocity_.y = jumpValue_;
278            //this->physicalBody_->applyCentralImpulse(btVector3(0, jumpvalue, 0));
279            this->thisTickBoost_ = true;
280            this->isFloor_ = false;
281        }
282    }
283
284    bool FpsPlayer::collidesAgainst(WorldEntity* otherObject, btManifoldPoint& contactPoint)
285    {
286        if (contactPoint.m_normalWorldOnB.y() > 0.6)
287            this->isFloor_ = true;
288        else
289            this->isFloor_ = false;
290
291        return false;
292    }
293
294    void FpsPlayer::addedWeaponPack(WeaponPack* wPack)
295    {
296        for (size_t i = 0; i < wPack->getNumWeapons(); ++i)
297        {
298            Weapon* weapon = wPack->getWeapon(i);
299            if (weapon->getWeaponSlot())
300            {
301                weapon->getWeaponSlot()->removeWeapon();
302                weapon->detachFromParent();
303                weapon->attachToNode(this->weaponNode_);
304            }
305        }
306    }
307}
Note: See TracBrowser for help on using the repository browser.