Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/fps/src/orxonox/worldentities/pawns/FpsPlayer.cc @ 6882

Last change on this file since 6882 was 6882, checked in by edwind, 14 years ago

commit by ed

File size: 7.4 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
36
37#include "core/CoreIncludes.h"
38#include "core/ConfigValueIncludes.h"
39#include "core/Template.h"
40#include "core/XMLPort.h"
41#include "items/Engine.h"
42
43#include <cmath>
44
45namespace orxonox
46{
47    const float orientationGain = 100;
48    const float jumpvalue = 300;
49    CreateFactory(FpsPlayer);
50
51    FpsPlayer::FpsPlayer(BaseObject* creator) : Pawn(creator)
52    {
53        RegisterObject(FpsPlayer);
54        this->speed_ = 200;
55        this->localVelocity_ = Vector3::ZERO;
56/*
57 *        this->primaryThrust_  = 100;
58 *        this->auxilaryThrust_ =  30;
59 *        this->rotationThrust_ =  10;
60 *
61 *        this->localLinearAcceleration_.setValue(0, 0, 0);
62 *        this->localAngularAcceleration_.setValue(0, 0, 0);
63 *        this->bBoost_ = false;
64 *        this->bPermanentBoost_ = false;
65 *        this->steering_ = Vector3::ZERO;
66*/     
67
68
69        this->bInvertYAxis_ = false;
70
71        this->setDestroyWhenPlayerLeft(true);
72
73        // FpsPlayer is always a physical object per default
74        // Be aware of this call: The collision type legality check will not reach derived classes!
75        this->setCollisionType(WorldEntity::Dynamic);
76        // Get notification about collisions
77        this->enableCollisionCallback();
78
79        this->setConfigValues();
80        this->registerVariables();
81       
82    }
83
84    FpsPlayer::~FpsPlayer()
85    {
86    }
87
88    void FpsPlayer::XMLPort(Element& xmlelement, XMLPort::Mode mode)
89    {
90        SUPER(FpsPlayer, XMLPort, xmlelement, mode);
91       
92        XMLPortParamVariable(FpsPlayer, "primaryThrust",  primaryThrust_,  xmlelement, mode);
93        XMLPortParamVariable(FpsPlayer, "auxilaryThrust", auxilaryThrust_, xmlelement, mode);
94        XMLPortParamVariable(FpsPlayer, "rotationThrust", rotationThrust_, xmlelement, mode);
95    }
96
97    void FpsPlayer::registerVariables()
98    {
99        registerVariable(this->primaryThrust_,  VariableDirection::ToClient);
100        registerVariable(this->auxilaryThrust_, VariableDirection::ToClient);
101        registerVariable(this->rotationThrust_, VariableDirection::ToClient);
102    }
103
104    void FpsPlayer::setConfigValues()
105    {
106        SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = targetting down).");
107    }
108
109    bool FpsPlayer::isCollisionTypeLegal(WorldEntity::CollisionType type) const
110    {
111        if (type != WorldEntity::Dynamic)
112        {
113            CCOUT(1) << "Error: Cannot tell a FpsPlayer not to be dynamic! Ignoring." << std::endl;
114            assert(false); // Only in debug mode
115            return false;
116        }
117        else
118            return true;
119    }
120
121    void FpsPlayer::tick(float dt)
122    {
123        if (this->hasLocalController())
124        {
125            this->setOrientation(savedOrientation_);
126           
127            thistickboost=false;
128           
129            float localSpeedSquared = this->localVelocity_.squaredLength();
130            float localSpeed;
131            if (localSpeedSquared > 1.0)
132                localSpeed = this->speed_ / sqrtf(localSpeedSquared);
133            else
134                localSpeed = this->speed_;
135
136            this->localVelocity_.x *= localSpeed;
137            this->localVelocity_.z *= localSpeed;
138            Vector3 temp = this->getOrientation() * this->localVelocity_;
139            if(localVelocity_.y==jumpvalue) this->setVelocity(Vector3(temp.x, temp.y + this->getVelocity().y, temp.z));
140            else this->setVelocity(Vector3(temp.x, this->getVelocity().y, temp.z));
141            this->localVelocity_.x = 0;
142            this->localVelocity_.y = 0;
143            this->localVelocity_.z = 0;
144
145            if (!this->isInMouseLook())
146            {
147                this->yaw(Radian(this->yaw_ * this->getMouseLookSpeed()),WorldEntity::Parent);
148                //this->pitch(Radian(this->pitch_ * this->getMouseLookSpeed()));
149                Radian pitch=this->cameraPositionRootNode_->getOrientation().getPitch();
150                if( pitch<Radian(1.5707) && pitch>Radian(-1.5707) ) this->cameraPositionRootNode_->pitch(Radian(this->pitch_ * this->getMouseLookSpeed()));
151                else if(pitch<Radian(-1.5707)){
152                        if(this->pitch_>0.0) this->cameraPositionRootNode_->pitch(Radian(this->pitch_ * this->getMouseLookSpeed()));
153                        else if(pitch<Radian(-1.571)) this->cameraPositionRootNode_->pitch(-pitch+Radian(-1.570796));
154                }
155                else if(pitch>Radian(1.5707)){
156                        if(this->pitch_<0.0) this->cameraPositionRootNode_->pitch(Radian(this->pitch_ * this->getMouseLookSpeed()));
157                        else if(pitch>Radian(1.571)) this->cameraPositionRootNode_->pitch(-pitch+Radian(1.570796));
158                }
159                 
160               
161 //               this->roll(Radian(this->roll_ * this->getMouseLookSpeed()));
162            }
163
164            this->yaw_ = this->pitch_ = this->roll_ = 0;
165           
166            //Quaternion q=this->getOrientation();
167            //if( q.y<0.99 ) this->setOrientation(q.w, q.x, 1.0, q.z);
168            this->setAngularVelocity(0.0, 0.0, 0.0);
169            savedOrientation_=this->getOrientation();
170        }
171
172        SUPER(FpsPlayer, tick, dt);
173    }
174
175    void FpsPlayer::setPlayer(PlayerInfo* player)
176    {
177        ControllableEntity::setPlayer(player);
178
179//        this->setSyncMode(ObjectDirection::ToClient);
180    }
181
182    void FpsPlayer::startLocalHumanControl()
183    {
184        ControllableEntity::startLocalHumanControl();
185    }
186   
187    void FpsPlayer::moveFrontBack(const Vector2& value)
188    {
189        this->localVelocity_.z -= value.x;
190    }
191
192
193    void FpsPlayer::moveRightLeft(const Vector2& value)
194    {
195        this->localVelocity_.x += value.x;
196    }
197
198    void FpsPlayer::moveUpDown(const Vector2& value)
199    {
200        //this->localVelocity_.y += value.x;
201    }
202
203    void FpsPlayer::rotateYaw(const Vector2& value)
204    {
205        this->yaw_ += value.y;
206
207        ControllableEntity::rotateYaw(value);
208    }
209
210    void FpsPlayer::rotatePitch(const Vector2& value)
211    {
212        this->pitch_ += value.y;
213
214        ControllableEntity::rotatePitch(value);
215    }
216
217    void FpsPlayer::rotateRoll(const Vector2& value)
218    {
219        this->roll_ += value.y;
220
221        ControllableEntity::rotateRoll(value);
222    }
223
224    void FpsPlayer::fire()
225    {
226    }
227   
228    void FpsPlayer::boost()                                     //acctually jump
229    {
230        if(isfloor) { 
231                if(!thistickboost) this->localVelocity_.y = jumpvalue;
232                //this->physicalBody_->applyCentralImpulse(btVector3(0, jumpvalue, 0));
233                thistickboost=true;
234                isfloor=false;
235        }
236    }
237
238    bool FpsPlayer::collidesAgainst(WorldEntity* otherObject, btManifoldPoint& contactPoint)
239    {
240        if(contactPoint.m_normalWorldOnB.y() > 0.6) isfloor=true;
241        else isfloor=false;
242       
243        return false;
244    }
245   
246}
Note: See TracBrowser for help on using the repository browser.