Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/gametypes/Pong.cc @ 2885

Last change on this file since 2885 was 2885, checked in by landauf, 15 years ago

Extended PongAI:

  • Random prediction errors depend on the vertical ball-speed
  • Fixed a bug: Position correction was also affected by the reaction delay. Now it works instantly.
  • Added oscillation avoidance system (the already implemented hysteresis avoidance system failed at low framerates)

Additionally fixed auto-respawn in Pong Gametype.

  • Property svn:eol-style set to native
File size: 5.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#include "OrxonoxStableHeaders.h"
30#include "Pong.h"
31
32#include "core/CoreIncludes.h"
33#include "core/ConfigValueIncludes.h"
34#include "core/Executor.h"
35#include "objects/worldentities/Model.h"
36#include "objects/worldentities/PongCenterpoint.h"
37#include "objects/worldentities/PongBall.h"
38#include "objects/worldentities/PongBat.h"
39#include "objects/infos/HumanPlayer.h"
40#include "objects/infos/PongBot.h"
41#include "objects/controllers/PongAI.h"
42
43namespace orxonox
44{
45    CreateUnloadableFactory(Pong);
46
47    Pong::Pong(BaseObject* creator) : Deathmatch(creator)
48    {
49        RegisterObject(Pong);
50
51        this->center_ = 0;
52        this->ball_ = 0;
53        this->bat_[0] = 0;
54        this->bat_[1] = 0;
55
56        this->starttimer_.setTimer(1.0, false, this, createExecutor(createFunctor(&Pong::startBall)));
57        this->starttimer_.stopTimer();
58
59        this->botclass_ = Class(PongBot);
60    }
61
62    void Pong::start()
63    {
64        if (this->center_)
65        {
66            if (!this->ball_)
67            {
68                this->ball_ = new PongBall(this->center_);
69                this->ball_->addTemplate(this->center_->getBalltemplate());
70            }
71
72            this->center_->attach(this->ball_);
73            this->ball_->setPosition(0, 0, 0);
74            this->ball_->setFieldDimension(this->center_->getFieldDimension());
75            this->ball_->setSpeed(0);
76            this->ball_->setBatLength(this->center_->getBatLength());
77
78            if (!this->bat_[0])
79            {
80                this->bat_[0] = new PongBat(this->center_);
81                this->bat_[0]->addTemplate(this->center_->getBattemplate());
82            }
83            if (!this->bat_[1])
84            {
85                this->bat_[1] = new PongBat(this->center_);
86                this->bat_[1]->addTemplate(this->center_->getBattemplate());
87            }
88
89            this->center_->attach(this->bat_[0]);
90            this->center_->attach(this->bat_[1]);
91            this->bat_[0]->setPosition(-this->center_->getFieldDimension().x / 2, 0, 0);
92            this->bat_[1]->setPosition( this->center_->getFieldDimension().x / 2, 0, 0);
93            this->bat_[0]->yaw(Degree(-90));
94            this->bat_[1]->yaw(Degree(90));
95            this->bat_[0]->setSpeed(this->center_->getBatSpeed());
96            this->bat_[1]->setSpeed(this->center_->getBatSpeed());
97            this->bat_[0]->setFieldHeight(this->center_->getFieldDimension().y);
98            this->bat_[1]->setFieldHeight(this->center_->getFieldDimension().y);
99            this->bat_[0]->setLength(this->center_->getBatLength());
100            this->bat_[1]->setLength(this->center_->getBatLength());
101
102            this->ball_->setBats(this->bat_);
103        }
104        else
105        {
106            COUT(1) << "Error: No Centerpoint specified." << std::endl;
107        }
108
109        this->starttimer_.startTimer();
110
111
112        bool temp = this->bForceSpawn_;
113        this->bForceSpawn_ = true;
114
115        Deathmatch::start();
116
117        this->bForceSpawn_ = temp;
118    }
119
120    void Pong::end()
121    {
122        if (this->ball_)
123        {
124            delete this->ball_;
125            this->ball_ = 0;
126        }
127
128        Deathmatch::end();
129    }
130
131    void Pong::spawnPlayer(PlayerInfo* player)
132    {
133        if (!this->bat_[0]->getPlayer())
134        {
135            player->startControl(this->bat_[0]);
136            this->players_[player].state_ = PlayerState::Alive;
137        }
138        else if (!this->bat_[1]->getPlayer())
139        {
140            player->startControl(this->bat_[1]);
141            this->players_[player].state_ = PlayerState::Alive;
142        }
143        else
144            return;
145
146        if (player && player->getController() && player->getController()->isA(Class(PongAI)))
147        {
148            PongAI* ai = dynamic_cast<PongAI*>(player->getController());
149            ai->setPongBall(this->ball_);
150        }
151    }
152
153    void Pong::playerScored(PlayerInfo* player)
154    {
155        Deathmatch::playerScored(player);
156
157        if (this->center_)
158        {
159            this->center_->fireEvent();
160        }
161
162        if (this->ball_)
163        {
164            this->ball_->setPosition(Vector3::ZERO);
165            this->ball_->setVelocity(Vector3::ZERO);
166            this->ball_->setSpeed(0);
167        }
168
169        if (this->bat_[0] && this->bat_[1])
170        {
171            this->bat_[0]->setPosition(-this->center_->getFieldDimension().x / 2, 0, 0);
172            this->bat_[1]->setPosition( this->center_->getFieldDimension().x / 2, 0, 0);
173        }
174
175        this->starttimer_.startTimer();
176    }
177
178    void Pong::startBall()
179    {
180        if (this->ball_ && this->center_)
181            this->ball_->setSpeed(this->center_->getBallSpeed());
182    }
183}
Note: See TracBrowser for help on using the repository browser.