Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/burli/src/world.cc @ 2595

Last change on this file since 2595 was 2551, checked in by patrick, 20 years ago

orxonox/trunk: minor changes - enhanced sc controll, fixed uncontrolled rotation effect, added some debug outputs for testing purposes, reformatted some src files from win style but not all

File size: 9.4 KB
Line 
1
2/*
3   orxonox - the future of 3D-vertical-scrollers
4
5   Copyright (C) 2004 orx
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2, or (at your option)
10   any later version.
11
12   ### File Specific:
13   main-programmer: Patrick Boenzli
14   co-programmer:
15*/
16
17
18#include "world.h"
19
20#include <iostream>
21#include <stdlib.h>
22#include <math.h>
23
24using namespace std;
25
26
27/**
28   \brief Create a new World
29   
30   This creates a new empty world!
31*/
32World::World () {
33  lastPlayer = null;
34  lastNPC = null;
35  lastEnv = null;
36  primitiveMove = 0;
37  step = 0;
38}
39
40
41World::~World () {}
42
43
44/**
45   \brief Add Player
46   \param player A reference to the new player object
47   
48   Add a new Player to the game. Player has to be initialised previously
49*/
50bool World::addPlayer(Player* player) 
51{
52  playerList* listMember = new playerList;
53  listMember->player = player;
54  if ( lastPlayer != null ) 
55    {
56      listMember->number = lastPlayer->number + 1;
57      listMember->next = lastPlayer;
58    }
59  else 
60    {
61      listMember->number = 0;
62      listMember->next = null;
63    }
64  lastPlayer = listMember;
65}
66
67
68/**
69   \brief Remove Player
70   \param player A reference to the new npc object
71   
72   Remove a new Player to the game.
73*/
74bool World::removePlayer(Player* player) {
75  cout << "World::removeNPC not implemented yet" << endl;
76}
77
78Player* World::getLocalPlayer() 
79{
80  return localPlayer;
81}
82
83
84/**
85   \brief Add Non-Player-Character
86   \param player A reference to the new npc object
87   
88   Add a new Non-Player-Character to the game. Player has to be initialised previously
89*/
90bool World::addNPC(NPC* npc) 
91{
92  npcList* listMember = new npcList;
93  listMember->npc = npc;
94  if ( lastNPC != null ) 
95    {
96      listMember->number = lastNPC->number + 1;
97      listMember->next = lastNPC;
98    }
99  else 
100    {
101      listMember->number = 0;
102      listMember->next = null;
103    }
104  lastNPC = listMember;
105}
106
107
108/**
109   \brief Remove Non-Player Character
110   \param player A reference to the new npc object
111   
112   Remove a new Non-Player-Character to the game.
113*/
114bool World::removeNPC(NPC* npc) {
115
116  npcList* npcRef = lastNPC;
117  npcList* lastRef = lastNPC;
118  while ( npcRef != null ) 
119    {
120      if ( npcRef->npc == npc ) {
121        cout << "found" << endl;
122        if ( npcRef == lastRef ) {
123          lastNPC = lastNPC->next;
124          delete npcRef;
125          npcRef = lastNPC;
126          lastRef = lastNPC;
127        }
128        else {
129          lastRef->next = npcRef->next;
130          delete npcRef;
131          npcRef = lastRef->next;
132        }
133        cout << "killed ..." << endl;
134      }
135      else {
136        lastRef = npcRef;
137        npcRef = npcRef->next;
138      }
139    }
140  cout << "npc left" << endl;
141}
142
143
144
145/**
146   \brief Add environmental object
147   \param player A reference to the new env object
148   
149   Add a new Environment to the world. Env has to be initialised before.
150*/
151bool World::addEnv(Environment* env) 
152{
153  envList* listMember = new envList;
154  listMember->env = env;
155  if ( lastEnv != null ) 
156    {
157      listMember->number = lastEnv->number + 1;
158      listMember->next = lastEnv;
159    }
160  else 
161    {
162      listMember->number = 0;
163      listMember->next = null;
164    }
165  lastEnv = listMember;
166}
167
168
169
170
171/**
172   \brief Draws the World and all Objects contained
173   
174   Calls the draw function of all: Objects, Players, Environement. This is the core of all graphics here.
175*/
176void World::drawWorld(void) 
177{
178  glLoadIdentity();
179  gluLookAt(2*sin(DataTank::yOffset/90*3.141)+DataTank::xOffset, 
180        -17.0 + DataTank::yOffset, 14.0 + 5*cos(DataTank::yOffset/50*3.141), 
181        DataTank::xOffset, 0.0 + DataTank::yOffset, 
182        0.0, 0.0, 1.0, 0.0);
183  /* first draw all players */
184  playerList* tmpPlayer = lastPlayer;
185  Player* player = tmpPlayer->player;
186  while( tmpPlayer != null ) 
187    {
188      (*tmpPlayer->player).drawPlayer();
189      tmpPlayer = tmpPlayer->next;
190    }
191  /* second draw all npcs */
192  npcList* tmpNPC = lastNPC;
193  while( tmpNPC != null )
194    {
195      (*tmpNPC->npc).drawNPC();
196      tmpNPC = tmpNPC->next;
197    }
198
199  /* now draw the rest of the world: environement */
200  envList* tmpEnv = lastEnv;
201  while( tmpEnv != null )
202    {
203      (*tmpEnv->env).drawEnvironment();
204      tmpEnv = tmpEnv->next;
205    }
206 
207  /* draw the repeating ground grid  */
208  glColor3f(0.0, 1.0, 0.0); 
209  glBegin(GL_LINES);
210  float drawx, drawy;
211  int offsetx, offsety;
212  float s1, s2;
213 
214  for (int y = 0; y < GRIDLENGTH; y++)
215  {
216        drawy = y + floor(DataTank::yOffset) - GRIDLENGTH / 2;
217        offsety = (int)floor(DataTank::yOffset) + y;
218        if (offsety < 0)
219        {
220          offsety = GRIDLENGTH - ((-offsety) % GRIDLENGTH);
221        }
222       
223        for (int x = 0; x < GRIDWIDTH; x++)
224        {
225          drawx = x + floor(DataTank::xOffset) - GRIDWIDTH / 2;
226          offsetx = (int)floor(DataTank::xOffset) + x;
227          if (offsetx < 0)
228          {
229                offsetx = GRIDWIDTH - ((-offsetx) % GRIDWIDTH);
230          }
231                 
232          /* horizontal */
233          /* not simplified! */
234          s1 = surface[(offsetx+GRIDWIDTH   )%GRIDWIDTH][offsety%GRIDLENGTH];
235          s2 = surface[(offsetx+GRIDWIDTH+1)%GRIDWIDTH][offsety%GRIDLENGTH];
236          glVertex3f(drawx, drawy, s1);
237          glVertex3f(drawx+1, drawy, s2);
238         
239          /* vertical */
240          s1 = surface[offsetx%GRIDWIDTH][(offsety+GRIDLENGTH  )%GRIDLENGTH];
241          s2 = surface[offsetx%GRIDWIDTH][(offsety+GRIDLENGTH+1)%GRIDLENGTH];
242          glVertex3f(drawx, drawy, s1);
243          glVertex3f(drawx, drawy+1, s2);
244        }
245  }
246  glEnd();
247
248  //primitiveMove+=0.07;
249  DataTank::yOffset += step;
250 
251  tmpPlayer = lastPlayer;
252  while( tmpPlayer != null ) 
253    {
254      tmpPlayer->player->yCor += step;
255      /* just for testing: alway have the player in the center */
256      DataTank::xOffset = (20 * DataTank::xOffset + (float)tmpPlayer->player->xCor) / 21.0;
257      tmpPlayer = tmpPlayer->next;
258    }
259 
260}
261
262
263void World::initEnvironement()
264{
265 double rx;
266 for (int y = 0; y < GRIDLENGTH; y++)
267 {
268    for (int x = 0; x < GRIDWIDTH; x++)
269        {
270          /*surface[x][y] = cos(y*3.141/8 )*0.7 * (0.6 + sin(x*3.141/5 + phase)*cos(y*3.141/5*3) * 0.4);*/
271         rx = double(x - GRIDWIDTH/2 + GRIDWIDTH / 20)/GRIDWIDTH*12;
272         surface[x][y] = -exp(-abs(rx))*rx*5 + cos(y*3.141/6)*0.6-3;
273         surface[x][y] += (x%8 && y%12) ? 5 : 0; 
274         surface[x][y] -= (x%3 && y%3) ? 0.3 : 0; 
275        }       
276  }
277}
278
279
280void World::setWorldStep(float step)
281{
282  this->step = step;
283  cout << "setting speed to " << step << endl;
284}
285
286
287
288/**
289   \brief Updates the world and all its objects
290   
291   Calculates the new state of the world. User-input and AI of
292   the enemies are accounted for.
293*/
294void World::updateWorld(void) 
295{
296 
297
298}
299
300
301/* collision detection */
302/* fix: bad efficency: stupid brute force */
303
304void World::detectCollision() 
305{
306  //cout << "World::detectCollision" << endl;
307  float xOff, yOff, zOff, radius;
308  npcList* tmpNPC, *tmpRef;
309
310  //cout << "World::detectCollsions" << endl;
311  /* first: check if any player's shoots trigger a collision */
312  playerList* tmpPlayer = lastPlayer;
313  Player* player = tmpPlayer->player;
314  int state;
315  while( tmpPlayer != null ) 
316    {
317      tmpNPC = lastNPC;
318      while( tmpNPC != null )
319        {
320          //cout << "npc != null" << endl;
321          radius = tmpNPC->npc->collisionRadius;
322          //cout << "worki" << endl;
323          ShootLaser::shoot* shoota = tmpPlayer->player->shootLaser->lastShoot;
324          while( shoota != null )
325            {
326              xOff = shoota->xCor - tmpNPC->npc->xCor;
327              yOff = shoota->yCor - tmpNPC->npc->yCor;
328              zOff = shoota->zCor - tmpNPC->npc->zCor;
329              if ( sqrt(xOff*xOff + yOff*yOff + zOff*zOff) < radius ) 
330                {
331                  //cout << "COLLISION " << endl;
332                  int state = tmpNPC->npc->hit();
333                  /* state is a value that marks if the ship dies or not */
334                  /* if state == 0 the ship dies and we have to remove it */
335                  /*
336                  if ( state == 0 ) {
337                    tmpRef = tmpNPC;
338                    tmpNPC = tmpNPC->next;
339                    removeNPC(tmpRef->npc);
340                    break;
341                  }
342                  */
343                }
344              shoota = shoota->next;
345            }
346          //cout << "changing npc..." << endl;
347          tmpNPC = tmpNPC->next;
348          //cout << "..changing npc done" << endl;
349        }
350      //cout << "changing play..." << endl;
351      tmpPlayer = tmpPlayer->next;
352      //cout << "changing play done" << endl;
353    }
354
355  //cout << "World::detectCollisions middle" << endl;
356
357  /* second: check if any player hits an enemy */
358  tmpPlayer = lastPlayer;
359  while( tmpPlayer != null ) 
360    {
361      tmpNPC = lastNPC;
362      while( tmpNPC != null )
363        {
364          radius = tmpNPC->npc->collisionRadius + tmpPlayer->player->collisionRadius;
365          xOff = tmpPlayer->player->xCor - tmpNPC->npc->xCor;
366          yOff = tmpPlayer->player->yCor - tmpNPC->npc->yCor;
367          zOff = tmpPlayer->player->zCor - tmpNPC->npc->zCor;
368          if ( sqrt(xOff*xOff + yOff*yOff + zOff*zOff) < radius ) {
369            //cout << "COLLISION " << endl;
370            tmpNPC->npc->hit();
371          }
372         
373          tmpNPC = tmpNPC->next;
374        }
375     
376      tmpPlayer = tmpPlayer->next;
377    }
378 
379 
380
381  /* third: check if any enemy shoots a player */
382
383  //cout << "World::detectCollisions end" << endl;
384}
385
386
387
388/**
389   \brief Routine for testing purposes.
390   
391   testing, testing, testing...
392*/
393void World::testThaTest(void) 
394{
395  cout << "World::testThaTest() called" << endl;
396  /* test addPlayer */
397  cout << "addPlayer test..." << endl;
398  playerList* pl = lastPlayer;
399  while ( pl != null )
400    {
401      cout << "player " << pl->number << " was found" << endl;
402      pl = pl->next;
403    }
404
405  /* test addNPC */
406  cout << "addNPC test..." << endl;
407  npcList* nl = lastNPC;
408  while ( nl != null )
409    {
410      cout << "npc " << nl->number << " was found" << endl;
411      nl = nl->next;
412    }
413
414
415  /* test addEnv */
416  cout << "addEnv test..." << endl;
417  envList* en = lastEnv;
418  while ( en != null )
419    {
420      cout << "env " << en->number << " was found" << endl;
421      en = en->next;
422    }
423
424  /* test drawWorld() */
425}
Note: See TracBrowser for help on using the repository browser.