Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Judihui it works

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