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 | * ... |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file Tetris.cc |
---|
31 | @brief Implementation of the Tetris class. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "Tetris.h" |
---|
35 | |
---|
36 | #include "core/CoreIncludes.h" |
---|
37 | #include "core/EventIncludes.h" |
---|
38 | #include "core/command/Executor.h" |
---|
39 | |
---|
40 | #include "gamestates/GSLevel.h" |
---|
41 | |
---|
42 | #include "TetrisCenterpoint.h" |
---|
43 | #include "TetrisStone.h" |
---|
44 | #include "infos/PlayerInfo.h" |
---|
45 | |
---|
46 | namespace orxonox |
---|
47 | { |
---|
48 | |
---|
49 | CreateUnloadableFactory(Tetris); |
---|
50 | |
---|
51 | /** |
---|
52 | @brief |
---|
53 | Constructor. Registers and initializes the object. |
---|
54 | */ |
---|
55 | Tetris::Tetris(BaseObject* creator) : Deathmatch(creator) |
---|
56 | { |
---|
57 | RegisterObject(Tetris); |
---|
58 | |
---|
59 | this->activeStone_ = NULL; |
---|
60 | |
---|
61 | // Pre-set the timer, but don't start it yet. |
---|
62 | this->starttimer_.setTimer(1.0, false, createExecutor(createFunctor(&Tetris::startStone, this))); |
---|
63 | this->starttimer_.stopTimer(); |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | @brief |
---|
68 | Destructor. Cleans up, if initialized. |
---|
69 | */ |
---|
70 | Tetris::~Tetris() |
---|
71 | { |
---|
72 | if (this->isInitialized()) |
---|
73 | this->cleanup(); |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | @brief |
---|
78 | Cleans up the Gametype. |
---|
79 | */ |
---|
80 | void Tetris::cleanup() |
---|
81 | { |
---|
82 | |
---|
83 | } |
---|
84 | |
---|
85 | void Tetris::tick(float dt) |
---|
86 | { |
---|
87 | SUPER(Tetris, tick, dt); |
---|
88 | |
---|
89 | TetrisStone* stone = this->activeStone_; |
---|
90 | if(stone != NULL) |
---|
91 | { |
---|
92 | // Get the current position of the active stone |
---|
93 | Vector3 position = stone->getPosition(); |
---|
94 | |
---|
95 | if(position.x < this->center_->getStoneSize()/2.0) //!< If the stone touches the left edge of the level |
---|
96 | position.x = this->center_->getStoneSize()/2.0; |
---|
97 | else if(position.x > (this->center_->getWidth()-0.5)*this->center_->getStoneSize()) //!< If the stone touches the right edge of the level |
---|
98 | position.x = (this->center_->getWidth()-0.5)*this->center_->getStoneSize(); |
---|
99 | |
---|
100 | if(!this->correctStonePos(stone)) //!< If the stone touches another stone |
---|
101 | { |
---|
102 | stone->setVelocity(Vector3::ZERO); |
---|
103 | this->createStone(); |
---|
104 | this->startStone(); |
---|
105 | } |
---|
106 | |
---|
107 | if(position.y < this->center_->getStoneSize()/2.0) //!< If the stone has reached the bottom of the level |
---|
108 | { |
---|
109 | position.y = this->center_->getStoneSize()/2.0; |
---|
110 | stone->setVelocity(Vector3::ZERO); |
---|
111 | this->createStone(); |
---|
112 | this->startStone(); |
---|
113 | } |
---|
114 | |
---|
115 | stone->setPosition(position); |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | @brief |
---|
121 | Starts the Tetris minigame. |
---|
122 | */ |
---|
123 | void Tetris::start() |
---|
124 | { |
---|
125 | if (this->center_ != NULL) // There needs to be a TetrisCenterpoint, i.e. the area the game takes place. |
---|
126 | { |
---|
127 | // Create the first stone. |
---|
128 | this->createStone(); |
---|
129 | } |
---|
130 | else // If no centerpoint was specified, an error is thrown and the level is exited. |
---|
131 | { |
---|
132 | COUT(1) << "Error: No Centerpoint specified." << std::endl; |
---|
133 | GSLevel::startMainMenu(); |
---|
134 | return; |
---|
135 | } |
---|
136 | |
---|
137 | // Start the timer. After it has expired the ball is started. |
---|
138 | this->starttimer_.startTimer(); |
---|
139 | |
---|
140 | // Set variable to temporarily force the player to spawn. |
---|
141 | bool temp = this->bForceSpawn_; |
---|
142 | this->bForceSpawn_ = true; |
---|
143 | |
---|
144 | // Call start for the parent class. |
---|
145 | Deathmatch::start(); |
---|
146 | |
---|
147 | // Reset the variable. |
---|
148 | this->bForceSpawn_ = temp; |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | @brief |
---|
153 | Ends the Tetris minigame. |
---|
154 | */ |
---|
155 | void Tetris::end() |
---|
156 | { |
---|
157 | this->cleanup(); |
---|
158 | |
---|
159 | // Call end for the parent class. |
---|
160 | Deathmatch::end(); |
---|
161 | } |
---|
162 | |
---|
163 | /** |
---|
164 | @brief |
---|
165 | Spawns player. |
---|
166 | */ |
---|
167 | void Tetris::spawnPlayersIfRequested() |
---|
168 | { |
---|
169 | // Spawn a human player. |
---|
170 | for (std::map<PlayerInfo*, Player>::iterator it = this->players_.begin(); it != this->players_.end(); ++it) |
---|
171 | if (it->first->isHumanPlayer() && (it->first->isReadyToSpawn() || this->bForceSpawn_)) |
---|
172 | this->spawnPlayer(it->first); |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | @brief |
---|
177 | Spawns the input player. |
---|
178 | @param player |
---|
179 | The player to be spawned. |
---|
180 | */ |
---|
181 | void Tetris::spawnPlayer(PlayerInfo* player) |
---|
182 | { |
---|
183 | assert(player); |
---|
184 | |
---|
185 | if(this->player_ != NULL) |
---|
186 | { |
---|
187 | this->player_ = player; |
---|
188 | this->players_[player].state_ = PlayerState::Alive; |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | @brief |
---|
194 | Starts the first stone. |
---|
195 | */ |
---|
196 | void Tetris::startStone(void) |
---|
197 | { |
---|
198 | if(this->player_ == NULL) |
---|
199 | return; |
---|
200 | |
---|
201 | if(this->activeStone_ != NULL) |
---|
202 | this->player_->stopControl(); |
---|
203 | |
---|
204 | // Make the last stone to be created the active stone. |
---|
205 | this->activeStone_ = this->stones_.back(); |
---|
206 | |
---|
207 | this->player_->startControl(this->activeStone_); |
---|
208 | this->activeStone_->setVelocity(0.0f, -this->center_->getStoneSpeed(), 0.0f); |
---|
209 | } |
---|
210 | |
---|
211 | /** |
---|
212 | @brief |
---|
213 | Creates a new stone. |
---|
214 | */ |
---|
215 | void Tetris::createStone(void) |
---|
216 | { |
---|
217 | // Create a new stone and add it to the list of stones. |
---|
218 | TetrisStone* stone = new TetrisStone(this->center_); |
---|
219 | this->stones_.push_back(stone); |
---|
220 | |
---|
221 | // Apply the stone template to the stone. |
---|
222 | stone->addTemplate(this->center_->getStoneTemplate()); |
---|
223 | |
---|
224 | // Attach the stone to the Centerpoint and set the position of the stone to be at the top middle. |
---|
225 | this->center_->attach(stone); |
---|
226 | float xPos = (this->center_->getWidth()/2 + ((this->center_->getWidth() % 2)*2-1)/2.0)*this->center_->getStoneSize(); |
---|
227 | float yPos = (this->center_->getHeight()-0.5)*this->center_->getStoneSize(); |
---|
228 | stone->setPosition(xPos, yPos, 0.0f); |
---|
229 | } |
---|
230 | |
---|
231 | /** |
---|
232 | @brief |
---|
233 | Validate the stone position. |
---|
234 | @return |
---|
235 | Returns whether the supplied stone is in the correct position. |
---|
236 | */ |
---|
237 | bool Tetris::correctStonePos(TetrisStone* stone) |
---|
238 | { |
---|
239 | for(std::vector<TetrisStone*>::const_iterator it = this->stones_.begin(); it != this->stones_.end(); ++it) |
---|
240 | { |
---|
241 | TetrisStone* currentStone = it->_Ptr(); //!< Gives access to the current stone in the list |
---|
242 | Vector3 currentStonePosition = it->_Ptr()->getPosition(); //!< Saves the position of the currentStone |
---|
243 | Vector3 stonePosition = stone->getPosition(); //!< Saves the position of the supplied stone |
---|
244 | |
---|
245 | // @TODO: Use the TetrisStone member functions to check both stones for an overlap. |
---|
246 | // Also make sure to correct the stone position accordingly. |
---|
247 | // |
---|
248 | // This case applies if the stones overlap completely |
---|
249 | //if((stonePosition.x == currentStonePosition.x) && (stonePosition.y == currentStonePosition.y)) |
---|
250 | // This case applies if the stones overlap partially vertically |
---|
251 | //if(stonePosition.y - stone->getHeight()/2 < currentStonePosition.y + currentStone->getHeight()/2) |
---|
252 | |
---|
253 | |
---|
254 | } |
---|
255 | } |
---|
256 | |
---|
257 | /** |
---|
258 | @brief |
---|
259 | Get the player. |
---|
260 | @return |
---|
261 | Returns a pointer to the player. If there is no player, NULL is returned. |
---|
262 | */ |
---|
263 | PlayerInfo* Tetris::getPlayer(void) const |
---|
264 | { |
---|
265 | return this->player_; |
---|
266 | } |
---|
267 | |
---|
268 | } |
---|