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 | * Florian Zinggeler |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file Hover.cc |
---|
31 | @brief Implementation of the Hover class. |
---|
32 | */ |
---|
33 | |
---|
34 | //#include "orxonox/worldentities/pawns/SpaceShip.h" |
---|
35 | #include "Hover.h" |
---|
36 | |
---|
37 | #include "HoverWall.h" |
---|
38 | #include "HoverFlag.h" |
---|
39 | #include "core/CoreIncludes.h" |
---|
40 | |
---|
41 | #include <iostream> |
---|
42 | #include <string> |
---|
43 | #include <time.h> |
---|
44 | #include <stdlib.h> |
---|
45 | #include <memory.h> |
---|
46 | #include <stdint.h> |
---|
47 | #include <fstream> |
---|
48 | #include <vector> |
---|
49 | |
---|
50 | namespace orxonox |
---|
51 | { |
---|
52 | bool firstTick = true; |
---|
53 | int levelcode[10][10] = |
---|
54 | { |
---|
55 | { 0,0,0,0,0,0,0,0,0,0 }, |
---|
56 | { 0,0,0,0,0,0,0,0,0,0 }, |
---|
57 | { 0,0,0,0,0,0,0,0,0,0 }, |
---|
58 | { 0,0,0,0,0,0,0,0,0,0 }, |
---|
59 | { 0,0,0,0,0,0,0,0,0,0 }, |
---|
60 | { 0,0,0,0,0,0,0,0,0,0 }, |
---|
61 | { 0,0,0,0,0,0,0,0,0,0 }, |
---|
62 | { 0,0,0,0,0,0,0,0,0,0 }, |
---|
63 | { 0,0,0,0,0,0,0,0,0,0 }, |
---|
64 | { 0,0,0,0,0,0,0,0,0,0 } |
---|
65 | }; |
---|
66 | |
---|
67 | const int NumCells = 10; |
---|
68 | unsigned char* g_Maze = new unsigned char[ NumCells* NumCells ]; |
---|
69 | |
---|
70 | // current traversing position |
---|
71 | int g_PtX; |
---|
72 | int g_PtY; |
---|
73 | |
---|
74 | // return the current index in g_Maze |
---|
75 | int Hover::CellIdx() |
---|
76 | { |
---|
77 | return g_PtX + NumCells * g_PtY; |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | int Hover::RandomInt() |
---|
82 | { |
---|
83 | return (rand() % NumCells); |
---|
84 | } |
---|
85 | |
---|
86 | int Hover::RandomInt4() |
---|
87 | { |
---|
88 | return (rand() % 4); |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | RegisterUnloadableClass(Hover); |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | |
---|
97 | |
---|
98 | |
---|
99 | |
---|
100 | Hover::Hover(Context* context) : Gametype(context) |
---|
101 | { |
---|
102 | |
---|
103 | RegisterObject(Hover); |
---|
104 | this->setHUDTemplate("HoverHUD"); |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | void Hover::tick(float dt) |
---|
110 | { |
---|
111 | |
---|
112 | SUPER(Hover, tick, dt); |
---|
113 | |
---|
114 | |
---|
115 | |
---|
116 | |
---|
117 | if(firstTick) |
---|
118 | { |
---|
119 | |
---|
120 | std::fill( g_Maze, g_Maze + NumCells * NumCells, 0 ); |
---|
121 | g_PtX=0; |
---|
122 | g_PtY=0; |
---|
123 | GenerateMaze(); |
---|
124 | MazeOut(); |
---|
125 | RenderMaze(); |
---|
126 | LevelOut(); |
---|
127 | firstTick = false; |
---|
128 | |
---|
129 | //Outer Walls |
---|
130 | for(int i = 0; i<10; i++){ |
---|
131 | new HoverWall(origin_->getContext(), 0, i+1, 1); |
---|
132 | new HoverWall(origin_->getContext(), 10, i+1, 1); |
---|
133 | new HoverWall(origin_->getContext(), i+1, 0, 2); |
---|
134 | new HoverWall(origin_->getContext(), i+1, 10, 2); |
---|
135 | } |
---|
136 | |
---|
137 | for(int y=0; y<10; y++){ |
---|
138 | for(int x=0; x<10; x++){ |
---|
139 | switch(levelcode[y][x]){ |
---|
140 | case 1: new HoverWall(origin_->getContext(), x+1, 10-y, 1); |
---|
141 | break; |
---|
142 | case 3: new HoverWall(origin_->getContext(), x+1, 10-y, 1); |
---|
143 | case 2: new HoverWall(origin_->getContext(), x+1, 10-y, 0); |
---|
144 | default: break; |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | for ( int i = 0; i < 5; i++ ) |
---|
154 | flagVector.push_back(new HoverFlag(origin_->getContext(), rand()%10, rand()%10)); |
---|
155 | |
---|
156 | Flags_ = flagVector.size(); |
---|
157 | |
---|
158 | //new HoverFlag(origin_->getContext()); //Rechts in Y Richtung |
---|
159 | //new HoverWall(origin_->getContext(), 5, 6, 0); //Ueber in x richtung |
---|
160 | //new HoverWall(origin_->getContext(), 5, 5, 0); //Ueber in x richtung |
---|
161 | } |
---|
162 | for ( int i = 0; i < flagVector.size(); i++ ){ |
---|
163 | if(flagVector[i]->getCollided()){ |
---|
164 | flagVector[i]->destroyLater(); |
---|
165 | flagVector.erase (flagVector.begin()+i); |
---|
166 | } |
---|
167 | } |
---|
168 | Flags_ = flagVector.size(); |
---|
169 | |
---|
170 | |
---|
171 | |
---|
172 | } |
---|
173 | |
---|
174 | int Hover::getFlags() |
---|
175 | { |
---|
176 | |
---|
177 | // Call start for the parent class. |
---|
178 | return Flags_; |
---|
179 | } |
---|
180 | |
---|
181 | void Hover::start() |
---|
182 | { |
---|
183 | |
---|
184 | // Call start for the parent class. |
---|
185 | Gametype::start(); |
---|
186 | |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | void Hover::end() |
---|
191 | { |
---|
192 | // DON'T CALL THIS! |
---|
193 | // Deathmatch::end(); |
---|
194 | // It will misteriously crash the game! |
---|
195 | // Instead startMainMenu, this won't crash. |
---|
196 | GSLevel::startMainMenu(); |
---|
197 | } |
---|
198 | |
---|
199 | |
---|
200 | |
---|
201 | |
---|
202 | //////////////////////////////////////////////////////////////////////////// |
---|
203 | |
---|
204 | |
---|
205 | // 0 1 2 3 4 5 6 7 8 |
---|
206 | // U R D L |
---|
207 | int Heading_X[9] = { 0, 0,+1, 0, 0, 0, 0, 0,-1 }; |
---|
208 | int Heading_Y[9] = { 0,-1, 0, 0,+1, 0, 0, 0, 0 }; |
---|
209 | int Mask[9] = { |
---|
210 | 0, |
---|
211 | eDirection_Down | eDirection_Down << 4, |
---|
212 | eDirection_Left | eDirection_Left << 4, |
---|
213 | 0, |
---|
214 | eDirection_Up | eDirection_Up << 4, |
---|
215 | 0, |
---|
216 | 0, |
---|
217 | 0, |
---|
218 | eDirection_Right | eDirection_Right << 4 |
---|
219 | }; |
---|
220 | |
---|
221 | |
---|
222 | //////////////////////////////////////////////////////////////////////////// |
---|
223 | |
---|
224 | bool Hover::IsDirValid( eDirection Dir ) |
---|
225 | { |
---|
226 | int NewX = g_PtX + Heading_X[ Dir ]; |
---|
227 | int NewY = g_PtY + Heading_Y[ Dir ]; |
---|
228 | |
---|
229 | if ( !Dir || NewX < 0 || NewY < 0 || NewX >= NumCells || NewY >= NumCells ) return false; |
---|
230 | |
---|
231 | return !g_Maze[ NewX + NumCells * NewY ]; |
---|
232 | } |
---|
233 | |
---|
234 | eDirection Hover::GetDirection() |
---|
235 | { |
---|
236 | eDirection Dir = eDirection( 1 << RandomInt4() ); |
---|
237 | |
---|
238 | while ( true ) |
---|
239 | { |
---|
240 | for ( int x = 0; x < 4; x++ ) |
---|
241 | { |
---|
242 | if ( IsDirValid( Dir ) ) { return eDirection( Dir ); } |
---|
243 | |
---|
244 | Dir = eDirection( Dir << 1 ); |
---|
245 | |
---|
246 | if ( Dir > eDirection_Left ) { Dir = eDirection_Up; } |
---|
247 | } |
---|
248 | |
---|
249 | Dir = eDirection( ( g_Maze[ CellIdx() ] & 0xf0 ) >> 4 ); |
---|
250 | |
---|
251 | // nowhere to go |
---|
252 | if ( !Dir ) return eDirection_Invalid; |
---|
253 | |
---|
254 | g_PtX += Heading_X[ Dir ]; |
---|
255 | g_PtY += Heading_Y[ Dir ]; |
---|
256 | |
---|
257 | Dir = eDirection( 1 << RandomInt4() ); |
---|
258 | } |
---|
259 | } |
---|
260 | |
---|
261 | void Hover::GenerateMaze() |
---|
262 | { |
---|
263 | int Cells = 0; |
---|
264 | |
---|
265 | for ( eDirection Dir = GetDirection(); Dir != eDirection_Invalid; Dir = GetDirection() ) |
---|
266 | { |
---|
267 | // a progress indicator, kind of |
---|
268 | // if ( ++Cells % 1000 == 0 ) std::cout << "."; |
---|
269 | |
---|
270 | g_Maze[ CellIdx() ] |= Dir; |
---|
271 | |
---|
272 | g_PtX += Heading_X[ Dir ]; |
---|
273 | g_PtY += Heading_Y[ Dir ]; |
---|
274 | |
---|
275 | g_Maze[ CellIdx() ] = Mask[ Dir ]; |
---|
276 | } |
---|
277 | |
---|
278 | std::cout << std::endl; |
---|
279 | } |
---|
280 | |
---|
281 | |
---|
282 | void Hover::MazeOut(){ |
---|
283 | for ( int y = 0; y < NumCells; y++ ) |
---|
284 | { |
---|
285 | for ( int x = 0; x < NumCells; x++ ) |
---|
286 | { |
---|
287 | char v = g_Maze[ y * NumCells + x ]; |
---|
288 | orxout()<<"["; |
---|
289 | if ( ( v & eDirection_Up ) ) orxout()<<"U"; |
---|
290 | else orxout()<<" "; |
---|
291 | if ( ( v & eDirection_Right ) ) orxout()<<"R"; |
---|
292 | else orxout()<<" "; |
---|
293 | if ( ( v & eDirection_Down ) ) orxout()<<" "; |
---|
294 | else orxout()<<" "; |
---|
295 | if ( ( v & eDirection_Left ) ) orxout()<<" "; |
---|
296 | else orxout()<<" "; |
---|
297 | orxout()<<"]"; |
---|
298 | } |
---|
299 | orxout()<<endl; |
---|
300 | } |
---|
301 | |
---|
302 | } |
---|
303 | |
---|
304 | void Hover::LevelOut(){ |
---|
305 | for ( int y = 0; y < NumCells; y++ ) |
---|
306 | { |
---|
307 | for ( int x = 0; x < NumCells; x++ ) |
---|
308 | { |
---|
309 | /*orxout()<<"["; |
---|
310 | if ( levelcode[x][y] < 2) orxout()<<"U"; |
---|
311 | else orxout()<<" "; |
---|
312 | if ( levelcode[x][y] % 2 == 0) orxout()<<"R"; |
---|
313 | else orxout()<<" "; |
---|
314 | |
---|
315 | orxout()<<" "; |
---|
316 | orxout()<<" "; |
---|
317 | orxout()<<"]";*/ |
---|
318 | |
---|
319 | orxout()<<levelcode[x][y]; |
---|
320 | } |
---|
321 | orxout()<<endl; |
---|
322 | } |
---|
323 | |
---|
324 | |
---|
325 | |
---|
326 | } |
---|
327 | |
---|
328 | void Hover::RenderMaze() |
---|
329 | { |
---|
330 | for ( int y = 0; y < NumCells; y++ ) |
---|
331 | { |
---|
332 | for ( int x = 0; x < NumCells; x++ ) |
---|
333 | { |
---|
334 | char v = g_Maze[ y * NumCells + x ]; |
---|
335 | |
---|
336 | if ( !( v & eDirection_Up ) && y >0) levelcode[y][x] |= 2; |
---|
337 | if ( !( v & eDirection_Right ) && x <9) levelcode[y][x] |= 1; |
---|
338 | //if ( !( v & eDirection_Down ) && y>0) levelcode[x][y-1] += 2; |
---|
339 | //if ( !( v & eDirection_Left ) && x>0) levelcode[x-1][y] += 1; |
---|
340 | } |
---|
341 | } |
---|
342 | for ( int y = 3; y < 7; y++ ) |
---|
343 | { |
---|
344 | for ( int x = 3; x < 7; x++ ) |
---|
345 | { |
---|
346 | |
---|
347 | if(y == 3 && x != 7) |
---|
348 | levelcode[y][x] &= 2; |
---|
349 | else if (x == 7 && y != 3) |
---|
350 | levelcode[y][x] &= 1; |
---|
351 | else if(x != 7) |
---|
352 | levelcode[y][x] = 0; |
---|
353 | } |
---|
354 | } |
---|
355 | |
---|
356 | } |
---|
357 | |
---|
358 | |
---|
359 | |
---|
360 | } |
---|