Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tetris/src/modules/pong/PongBat.cc @ 8105

Last change on this file since 8105 was 8105, checked in by dafrick, 13 years ago

More documentation for Pong.

  • Property svn:eol-style set to native
File size: 4.5 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        //TODO detailed
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                //TODO What does this?
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            else if (!this->bSteadiedPosition_)
99            {
100                // To ensure network synchronicity
101                this->setVelocity(0, 0, 0);
102                this->setPosition(this->getPosition());
103                this->bSteadiedPosition_ = true;
104            }
105        }
106
107        SUPER(PongBat, tick, dt);
108
109        // 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.
110        Vector3 position = this->getPosition();
111        if (position.z > this->fieldHeight_ / 2 - this->fieldHeight_ * this->length_ / 2)
112            position.z = this->fieldHeight_ / 2 - this->fieldHeight_ * this->length_ / 2;
113        if (position.z < -this->fieldHeight_ / 2 + this->fieldHeight_ * this->length_ / 2)
114            position.z = -this->fieldHeight_ / 2 + this->fieldHeight_ * this->length_ / 2;
115        if (position != this->getPosition())
116        {
117            this->setPosition(position);
118            this->setVelocity( Vector3::ZERO );
119        }
120    }
121
122    /**
123    @brief
124        Overloaded the function to steer the bat up and down.
125    @param value
126        A vector whose first component is the inverse direction in which we want to steer the bat.
127    */
128    void PongBat::moveFrontBack(const Vector2& value)
129    {
130        this->bMoveLocal_ = false;
131        this->movement_ = -value.x;
132    }
133
134    /**
135    @brief
136        Overloaded the function to steer the bat up and down.
137    @param value
138        A vector whose first component is the direction in which we wnat to steer the bat.
139    */
140    void PongBat::moveRightLeft(const Vector2& value)
141    {
142        this->bMoveLocal_ = true;
143        this->movement_ = value.x;
144    }
145
146    /**
147    @brief
148        Is called when the player changed.
149    */
150    void PongBat::changedPlayer()
151    {
152        this->setVelocity(0, 0, 0);
153    }
154}
Note: See TracBrowser for help on using the repository browser.