Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial2/src/modules/pong/PongBat.cc @ 9270

Last change on this file since 9270 was 8108, checked in by dafrick, 14 years ago

Merging changes from tetris branch into trunk, since they are also useful, there.

  • Property svn:eol-style set to native
File size: 4.8 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 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file PongBat.cc
31    @brief Implementation of the PongBat class.
32*/
33
34#include "PongBat.h"
35
36#include "core/CoreIncludes.h"
37#include "core/XMLPort.h"
38
39namespace orxonox
40{
41    CreateFactory(PongBat);
42
43    /**
44    @brief
45        Constructor. Registers and initializes the object.
46    */
47    PongBat::PongBat(BaseObject* creator) : ControllableEntity(creator)
48    {
49        RegisterObject(PongBat);
50
51        this->movement_ = 0;
52        this->bMoveLocal_ = false;
53        this->speed_ = 60;
54        this->length_ = 0.25;
55        this->fieldHeight_ = 100;
56        this->bSteadiedPosition_ = false;
57
58        this->registerVariables();
59    }
60
61    /**
62    @brief
63        Registers variables to be synchronized over the network.
64    */
65    void PongBat::registerVariables()
66    {
67        registerVariable(this->speed_);
68        registerVariable(this->fieldHeight_);
69        registerVariable(this->length_);
70    }
71
72    /**
73    @brief
74        Is called each tick.
75        Moves the bat.
76    @param dt
77        The time since last tick.
78    */
79    void PongBat::tick(float dt)
80    {
81        // If the bat is controlled (but not over the network).
82        if (this->hasLocalController())
83        {
84            if (this->movement_ != 0)
85            {
86                // The absolute value of the movement is restricted to be lesser or equal than the speed of the bat.
87                this->movement_ = clamp(this->movement_, -1.0f, 1.0f) * this->speed_;
88
89                // If moveRightLeft() is used the movement is dependento on wehther it is the right or the left bat, so, it is i.e. dependent on the orientation of the bat.
90                if (this->bMoveLocal_)
91                    this->setVelocity(this->getOrientation() * Vector3(this->movement_, 0, 0));
92                else
93                    this->setVelocity(0, 0, this->movement_);
94
95                this->movement_ = 0;
96                this->bSteadiedPosition_ = false;
97            }
98            // If there is no movement but the position has not been steadied, the velocity is set to zero and the position is reaffirmed.
99            else if (!this->bSteadiedPosition_)
100            {
101                // To ensure network synchronicity
102                this->setVelocity(0, 0, 0);
103                this->setPosition(this->getPosition());
104                this->bSteadiedPosition_ = true;
105            }
106        }
107
108        SUPER(PongBat, tick, dt);
109
110        // Restrict the position of the bats, for them to always be between the upper and lower delimiters. i.e. the bats stall if they reach the upper or lower boundary.
111        Vector3 position = this->getPosition();
112        if (position.z > this->fieldHeight_ / 2 - this->fieldHeight_ * this->length_ / 2)
113            position.z = this->fieldHeight_ / 2 - this->fieldHeight_ * this->length_ / 2;
114        if (position.z < -this->fieldHeight_ / 2 + this->fieldHeight_ * this->length_ / 2)
115            position.z = -this->fieldHeight_ / 2 + this->fieldHeight_ * this->length_ / 2;
116        if (position != this->getPosition())
117        {
118            this->setPosition(position);
119            this->setVelocity( Vector3::ZERO );
120        }
121    }
122
123    /**
124    @brief
125        Overloaded the function to steer the bat up and down.
126    @param value
127        A vector whose first component is the inverse direction in which we want to steer the bat.
128    */
129    void PongBat::moveFrontBack(const Vector2& value)
130    {
131        this->bMoveLocal_ = false;
132        this->movement_ = -value.x;
133    }
134
135    /**
136    @brief
137        Overloaded the function to steer the bat up and down.
138    @param value
139        A vector whose first component is the direction in which we wnat to steer the bat.
140    */
141    void PongBat::moveRightLeft(const Vector2& value)
142    {
143        this->bMoveLocal_ = true;
144        this->movement_ = value.x;
145    }
146
147    /**
148    @brief
149        Is called when the player changed.
150    */
151    void PongBat::changedPlayer()
152    {
153        this->setVelocity(0, 0, 0);
154    }
155}
Note: See TracBrowser for help on using the repository browser.