Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation3/src/orxonox/worldentities/pawns/FpsPlayer.cc @ 7073

Last change on this file since 7073 was 7073, checked in by landauf, 14 years ago

merged fps branch to presentation3

  • Property svn:eol-style set to native
File size: 8.7 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() && this->mesh_.getEntity())
96            this->detachOgreObject(this->mesh_.getEntity());
97    }
98
99    void FpsPlayer::XMLPort(Element& xmlelement, XMLPort::Mode mode)
100    {
101        SUPER(FpsPlayer, XMLPort, xmlelement, mode);
102       
103        XMLPortParamVariable(FpsPlayer, "primaryThrust",  primaryThrust_,  xmlelement, mode);
104        XMLPortParamVariable(FpsPlayer, "auxilaryThrust", auxilaryThrust_, xmlelement, mode);
105        XMLPortParamVariable(FpsPlayer, "rotationThrust", rotationThrust_, xmlelement, mode);
106        XMLPortParam(FpsPlayer, "weapon", setMeshSource, getMeshSource, xmlelement, mode);
107    }
108
109    void FpsPlayer::registerVariables()
110    {
111        registerVariable(this->primaryThrust_,  VariableDirection::ToClient);
112        registerVariable(this->auxilaryThrust_, VariableDirection::ToClient);
113        registerVariable(this->rotationThrust_, VariableDirection::ToClient);
114        registerVariable(this->weaponMashName_);
115    }
116   
117   
118
119    void FpsPlayer::setConfigValues()
120    {
121        SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = targetting down).");
122    }
123
124    bool FpsPlayer::isCollisionTypeLegal(WorldEntity::CollisionType type) const
125    {
126        if (type != WorldEntity::Dynamic)
127        {
128            CCOUT(1) << "Error: Cannot tell a FpsPlayer not to be dynamic! Ignoring." << std::endl;
129            assert(false); // Only in debug mode
130            return false;
131        }
132        else
133            return true;
134    }
135
136    void FpsPlayer::tick(float dt)
137    {
138        if (this->hasLocalController())
139        {
140            this->setOrientation(savedOrientation_);
141           
142            thisTickBoost_=false;
143           
144            float localSpeedSquared = this->localVelocity_.squaredLength();
145            float localSpeed;
146            if (localSpeedSquared > 1.0)
147                localSpeed = this->speed_ / sqrtf(localSpeedSquared);
148            else
149                localSpeed = this->speed_;
150
151            this->localVelocity_.x *= localSpeed;
152            this->localVelocity_.z *= localSpeed;
153            Vector3 temp = this->getOrientation() * this->localVelocity_;
154            if(localVelocity_.y==jumpValue_) this->setVelocity(Vector3(temp.x, temp.y + this->getVelocity().y, temp.z));
155            else this->setVelocity(Vector3(temp.x, this->getVelocity().y, temp.z));
156            this->localVelocity_.x = 0;
157            this->localVelocity_.y = 0;
158            this->localVelocity_.z = 0;
159
160            if (!this->isInMouseLook())
161            {
162                this->yaw(Radian(this->yaw_ * this->getMouseLookSpeed()), WorldEntity::Parent);
163               
164                Radian pitch=this->cameraPositionRootNode_->getOrientation().getPitch();
165                if( pitch<Radian(1.5707) && pitch>Radian(-1.5707) ) {
166                        this->cameraPositionRootNode_->pitch(Radian(this->pitch_ * this->getMouseLookSpeed()));
167                        }
168                else if(pitch<Radian(-1.5707)){
169                        if(this->pitch_>0.0) {
170                                this->cameraPositionRootNode_->pitch(Radian(this->pitch_ * this->getMouseLookSpeed()));
171                                }
172                        else if(pitch<Radian(-1.571)){
173                                this->cameraPositionRootNode_->pitch(-pitch+Radian(-1.570796));
174                                }
175                }
176                else if(pitch>Radian(1.5707)){
177                        if(this->pitch_<0.0) {
178                                this->cameraPositionRootNode_->pitch(Radian(this->pitch_ * this->getMouseLookSpeed()));
179                                }
180                        else if(pitch>Radian(1.571)){ 
181                                this->cameraPositionRootNode_->pitch(-pitch+Radian(1.570796));
182                                }
183                }
184                this->weaponNode_->setOrientation(this->cameraPositionRootNode_->getOrientation());
185               
186            }
187
188            this->yaw_ = this->pitch_ = this->roll_ = 0;
189           
190            this->setAngularVelocity(0.0, 0.0, 0.0);
191            savedOrientation_=this->getOrientation();
192        }
193
194        SUPER(FpsPlayer, tick, dt);
195    }
196   
197    void FpsPlayer::changedMesh()
198    {
199        if (GameMode::showsGraphics())
200        {
201            if (this->mesh_.getEntity())
202                this->weaponNode_->detachObject(this->mesh_.getEntity());
203
204            this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_);
205
206            if (this->mesh_.getEntity())
207            {
208                this->weaponNode_->attachObject(this->mesh_.getEntity());
209            }
210        }
211    }
212
213    void FpsPlayer::setPlayer(PlayerInfo* player)
214    {
215        ControllableEntity::setPlayer(player);
216
217//        this->setSyncMode(ObjectDirection::ToClient);
218    }
219
220    void FpsPlayer::startLocalHumanControl()
221    {
222        ControllableEntity::startLocalHumanControl();
223    }
224   
225    void FpsPlayer::moveFrontBack(const Vector2& value)
226    {
227        this->localVelocity_.z -= value.x;
228    }
229
230
231    void FpsPlayer::moveRightLeft(const Vector2& value)
232    {
233        this->localVelocity_.x += value.x;
234    }
235
236    void FpsPlayer::moveUpDown(const Vector2& value)
237    {
238        //this->localVelocity_.y += value.x;
239    }
240
241    void FpsPlayer::rotateYaw(const Vector2& value)
242    {
243        this->yaw_ += value.y;
244
245        ControllableEntity::rotateYaw(value);
246    }
247
248    void FpsPlayer::rotatePitch(const Vector2& value)
249    {
250        this->pitch_ += value.y;
251
252        ControllableEntity::rotatePitch(value);
253    }
254
255    void FpsPlayer::rotateRoll(const Vector2& value)
256    {
257        this->roll_ += value.y;
258
259        ControllableEntity::rotateRoll(value);
260    }
261
262    void FpsPlayer::fire()
263    {
264    }
265   
266    void FpsPlayer::boost()                                     //acctually jump
267    {
268        if(isFloor_) { 
269                if(!thisTickBoost_) this->localVelocity_.y = jumpValue_;
270                //this->physicalBody_->applyCentralImpulse(btVector3(0, jumpvalue, 0));
271                thisTickBoost_=true;
272                isFloor_=false;
273        }
274    }
275
276    bool FpsPlayer::collidesAgainst(WorldEntity* otherObject, btManifoldPoint& contactPoint)
277    {
278        if(contactPoint.m_normalWorldOnB.y() > 0.6) isFloor_=true;
279        else isFloor_=false;
280       
281        return false;
282    }
283   
284    void FpsPlayer::addedWeaponPack(WeaponPack* wPack)
285    {
286        for (size_t i = 0; i < wPack->getNumWeapons(); ++i)
287        {
288            Weapon* weapon = wPack->getWeapon(i);
289            if (weapon->getWeaponSlot())
290            {
291                weapon->getWeaponSlot()->removeWeapon();
292                weapon->detachFromParent();
293                weapon->attachToNode(this->weaponNode_);
294            }
295        }
296    }
297}
Note: See TracBrowser for help on using the repository browser.