Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/hoverHS15/src/modules/hover/Hover.cc @ 10894

Last change on this file since 10894 was 10894, checked in by meierman, 9 years ago

Flag generator implemented

File size: 8.7 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 *      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
50namespace 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        SUPER(Hover, tick, dt);
112
113
114
115
116        if(firstTick)
117        {
118            std::fill( g_Maze, g_Maze + NumCells * NumCells, 0 );
119            g_PtX=0;
120            g_PtY=0;
121            GenerateMaze();
122            MazeOut();
123            RenderMaze();
124            LevelOut();
125            firstTick = false;
126
127            for(int y=0; y<10; y++){
128                for(int x=0; x<10; x++){
129                    switch(levelcode[y][x]){
130                        case 1: new HoverWall(origin_->getContext(), x+1, 10-y, 1);
131                                break;
132                        case 3: new HoverWall(origin_->getContext(), x+1, 10-y, 1);
133                        case 2: new HoverWall(origin_->getContext(), x+1, 10-y, 0);
134                        default: break;
135                    }
136
137
138                   
139                }   
140            }
141
142
143            for ( int i = 0; i < 5; i++ )
144                flagVector.push_back(new HoverFlag(origin_->getContext(), rand()%10, rand()%10));
145           
146            //new HoverFlag(origin_->getContext()); //Rechts in Y Richtung
147            //new HoverWall(origin_->getContext(), 5, 6, 0); //Ueber in x richtung
148            //new HoverWall(origin_->getContext(), 5, 5, 0); //Ueber in x richtung
149        }
150        for ( int i = 0; i < flagVector.size(); i++ ){
151            if(flagVector[i]->getCollided()){
152                flagVector[i]->destroyLater();
153                flagVector.erase (flagVector.begin()+i);
154            }
155        }
156
157
158
159    }
160
161   
162
163    void Hover::start()
164    {
165
166        // Call start for the parent class.
167        Gametype::start();
168
169    }
170
171
172    void Hover::end()
173    {
174        // DON'T CALL THIS!
175        //      Deathmatch::end();
176        // It will misteriously crash the game!
177        // Instead startMainMenu, this won't crash.
178        GSLevel::startMainMenu();
179    }
180
181
182
183
184    ////////////////////////////////////////////////////////////////////////////
185
186
187    //                   0  1  2  3  4  5  6  7  8
188    //                      U  R     D           L
189    int Heading_X[9] = { 0, 0,+1, 0, 0, 0, 0, 0,-1 };
190    int Heading_Y[9] = { 0,-1, 0, 0,+1, 0, 0, 0, 0 };
191    int Mask[9]      = {
192                                0,
193                                eDirection_Down | eDirection_Down << 4,
194                                eDirection_Left | eDirection_Left << 4,
195                                0,
196                                eDirection_Up | eDirection_Up << 4,
197                                0,
198                                0,
199                                0,
200                                eDirection_Right | eDirection_Right << 4
201                            };
202
203
204    ////////////////////////////////////////////////////////////////////////////
205
206    bool Hover::IsDirValid( eDirection Dir )
207    {
208        int NewX = g_PtX + Heading_X[ Dir ];
209        int NewY = g_PtY + Heading_Y[ Dir ];
210
211        if ( !Dir || NewX < 0 || NewY < 0 || NewX >= NumCells || NewY >= NumCells ) return false;
212
213        return !g_Maze[ NewX + NumCells * NewY ];
214    }
215
216    eDirection Hover::GetDirection()
217    {
218        eDirection Dir = eDirection( 1 << RandomInt4() );
219
220        while ( true )
221        {
222            for ( int x = 0; x < 4; x++ )
223            {
224                if ( IsDirValid( Dir ) ) { return eDirection( Dir ); }
225
226                Dir = eDirection( Dir << 1 );
227
228                if ( Dir > eDirection_Left ) { Dir = eDirection_Up; }
229            }
230
231            Dir = eDirection( ( g_Maze[ CellIdx() ] & 0xf0 ) >> 4 );
232
233            // nowhere to go
234            if ( !Dir ) return eDirection_Invalid;
235
236            g_PtX += Heading_X[ Dir ];
237            g_PtY += Heading_Y[ Dir ];
238
239            Dir = eDirection( 1 << RandomInt4() );
240        }
241    }
242
243    void Hover::GenerateMaze()
244    {
245        int Cells = 0;
246
247        for ( eDirection Dir = GetDirection(); Dir != eDirection_Invalid; Dir = GetDirection() )
248        {
249            // a progress indicator, kind of
250           // if ( ++Cells % 1000 == 0 ) std::cout << ".";
251
252            g_Maze[ CellIdx() ] |= Dir;
253
254            g_PtX += Heading_X[ Dir ];
255            g_PtY += Heading_Y[ Dir ];
256
257            g_Maze[ CellIdx() ] = Mask[ Dir ];
258        }
259
260        std::cout << std::endl;
261    } 
262   
263
264    void Hover::MazeOut(){
265        for ( int y = 0; y < NumCells; y++ )
266        {
267            for ( int x = 0; x < NumCells; x++ )
268            {
269                char v = g_Maze[ y * NumCells + x ];
270                orxout()<<"[";
271                if ( ( v & eDirection_Up    ) ) orxout()<<"U";
272                else orxout()<<" ";
273                if ( ( v & eDirection_Right ) ) orxout()<<"R";
274                else orxout()<<" ";
275                if ( ( v & eDirection_Down  ) ) orxout()<<" ";
276                else orxout()<<" ";
277                if ( ( v & eDirection_Left  ) ) orxout()<<" ";
278                else orxout()<<" ";
279                orxout()<<"]";
280            }
281            orxout()<<endl;
282        }
283
284    }
285
286    void Hover::LevelOut(){
287        for ( int y = 0; y < NumCells; y++ )
288        {
289            for ( int x = 0; x < NumCells; x++ )
290            {
291                /*orxout()<<"[";
292                if ( levelcode[x][y] < 2) orxout()<<"U";
293                else orxout()<<" ";
294                if ( levelcode[x][y] % 2 == 0) orxout()<<"R";
295                else orxout()<<" ";
296
297                orxout()<<" ";
298                orxout()<<" ";
299                orxout()<<"]";*/
300
301                orxout()<<levelcode[x][y];
302            }
303            orxout()<<endl;
304        }
305
306
307
308    }
309
310    void Hover::RenderMaze()
311    {
312        for ( int y = 0; y < NumCells; y++ )
313        {
314            for ( int x = 0; x < NumCells; x++ )
315            {
316                char v = g_Maze[ y * NumCells + x ];
317
318                if ( !( v & eDirection_Up    ) && y >0) levelcode[y][x] |= 2;
319                if ( !( v & eDirection_Right ) && x <9) levelcode[y][x] |= 1;
320                //if ( !( v & eDirection_Down  ) && y>0) levelcode[x][y-1] += 2;
321                //if ( !( v & eDirection_Left  ) && x>0) levelcode[x-1][y] += 1;
322            }
323        }
324        for ( int y = 3; y < 7; y++ )
325        {
326            for ( int x = 3; x < 7; x++ )
327            {
328
329                if(y == 3 && x != 7)
330                    levelcode[y][x] &= 2;
331                else if (x == 7 && y != 3)
332                    levelcode[y][x] &= 1;
333                else if(x != 7)
334                    levelcode[y][x] = 0;
335            }
336        }
337
338    }
339
340
341
342}
Note: See TracBrowser for help on using the repository browser.