Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2105 was 2037, checked in by adrian, 20 years ago

orxonox/branches/burli: Repeating ground grid (plus player-centered view for testing)

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, -17.0 + DataTank::yOffset, 14.0 + 5*cos(DataTank::yOffset/50*3.141), DataTank::xOffset, 0.0 + DataTank::yOffset, 0.0, 0.0, 1.0, 0.0);
180  /* first draw all players */
181  playerList* tmpPlayer = lastPlayer;
182  Player* player = tmpPlayer->player;
183  while( tmpPlayer != null ) 
184    {
185      (*tmpPlayer->player).drawPlayer();
186      tmpPlayer = tmpPlayer->next;
187    }
188  /* second draw all npcs */
189  npcList* tmpNPC = lastNPC;
190  while( tmpNPC != null )
191    {
192      (*tmpNPC->npc).drawNPC();
193      tmpNPC = tmpNPC->next;
194    }
195
196  /* now draw the rest of the world: environement */
197  envList* tmpEnv = lastEnv;
198  while( tmpEnv != null )
199    {
200      (*tmpEnv->env).drawEnvironment();
201      tmpEnv = tmpEnv->next;
202    }
203 
204  /* draw the repeating ground grid  */
205  glColor3f(0.0, 1.0, 0.0); 
206  glBegin(GL_LINES);
207  float drawx, drawy;
208  int offsetx, offsety;
209  float s1, s2;
210 
211  for (int y = 0; y < GRIDLENGTH; y++)
212  {
213        drawy = y + floor(DataTank::yOffset) - GRIDLENGTH / 2;
214        offsety = (int)floor(DataTank::yOffset) + y;
215        if (offsety < 0)
216        {
217          offsety = GRIDLENGTH - ((-offsety) % GRIDLENGTH);
218        }
219       
220        for (int x = 0; x < GRIDWIDTH; x++)
221        {
222          drawx = x + floor(DataTank::xOffset) - GRIDWIDTH / 2;
223          offsetx = (int)floor(DataTank::xOffset) + x;
224          if (offsetx < 0)
225          {
226                offsetx = GRIDWIDTH - ((-offsetx) % GRIDWIDTH);
227          }
228                 
229          /* horizontal */
230          /* not simplified! */
231          s1 = surface[(offsetx+GRIDWIDTH   )%GRIDWIDTH][offsety%GRIDLENGTH];
232          s2 = surface[(offsetx+GRIDWIDTH+1)%GRIDWIDTH][offsety%GRIDLENGTH];
233          glVertex3f(drawx, drawy, s1);
234          glVertex3f(drawx+1, drawy, s2);
235         
236          /* vertical */
237          s1 = surface[offsetx%GRIDWIDTH][(offsety+GRIDLENGTH  )%GRIDLENGTH];
238          s2 = surface[offsetx%GRIDWIDTH][(offsety+GRIDLENGTH+1)%GRIDLENGTH];
239          glVertex3f(drawx, drawy, s1);
240          glVertex3f(drawx, drawy+1, s2);
241        }
242  }
243  glEnd();
244
245  //primitiveMove+=0.07;
246  DataTank::yOffset += step;
247 
248  tmpPlayer = lastPlayer;
249  while( tmpPlayer != null ) 
250    {
251      tmpPlayer->player->yCor += step;
252      /* just for testing: alway have the player in the center */
253      DataTank::xOffset = (20 * DataTank::xOffset + (float)tmpPlayer->player->xCor) / 21.0;
254      tmpPlayer = tmpPlayer->next;
255    }
256 
257}
258
259
260void World::initEnvironement()
261{
262 double rx;
263 for (int y = 0; y < GRIDLENGTH; y++)
264 {
265    for (int x = 0; x < GRIDWIDTH; x++)
266        {
267          /*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);*/
268         rx = double(x - GRIDWIDTH/2 + GRIDWIDTH / 20)/GRIDWIDTH*12;
269         surface[x][y] = -exp(-abs(rx))*rx*5 + cos(y*3.141/6)*0.6-3;
270         surface[x][y] += (x%8 && y%12) ? 5 : 0; 
271         surface[x][y] -= (x%3 && y%3) ? 0.3 : 0; 
272        }       
273  }
274}
275
276
277void World::setWorldStep(float step)
278{
279  this->step = step;
280  cout << "setting speed to " << step << endl;
281}
282
283
284
285/**
286   \brief Updates the world and all its objects
287   
288   Calculates the new state of the world. User-input and AI of
289   the enemies are accounted for.
290*/
291void World::updateWorld(void) 
292{
293 
294
295}
296
297
298/* collision detection */
299/* fix: bad efficency: stupid brute force */
300
301void World::detectCollision() 
302{
303  //cout << "World::detectCollision" << endl;
304  float xOff, yOff, zOff, radius;
305  npcList* tmpNPC, *tmpRef;
306
307  //cout << "World::detectCollsions" << endl;
308  /* first: check if any player's shoots trigger a collision */
309  playerList* tmpPlayer = lastPlayer;
310  Player* player = tmpPlayer->player;
311  int state;
312  while( tmpPlayer != null ) 
313    {
314      tmpNPC = lastNPC;
315      while( tmpNPC != null )
316        {
317          //cout << "npc != null" << endl;
318          radius = tmpNPC->npc->collisionRadius;
319          //cout << "worki" << endl;
320          ShootLaser::shoot* shoota = tmpPlayer->player->shootLaser->lastShoot;
321          while( shoota != null )
322            {
323              xOff = shoota->xCor - tmpNPC->npc->xCor;
324              yOff = shoota->yCor - tmpNPC->npc->yCor;
325              zOff = shoota->zCor - tmpNPC->npc->zCor;
326              if ( sqrt(xOff*xOff + yOff*yOff + zOff*zOff) < radius ) 
327                {
328                  //cout << "COLLISION " << endl;
329                  int state = tmpNPC->npc->hit();
330                  /* state is a value that marks if the ship dies or not */
331                  /* if state == 0 the ship dies and we have to remove it */
332                  /*
333                  if ( state == 0 ) {
334                    tmpRef = tmpNPC;
335                    tmpNPC = tmpNPC->next;
336                    removeNPC(tmpRef->npc);
337                    break;
338                  }
339                  */
340                }
341              shoota = shoota->next;
342            }
343          //cout << "changing npc..." << endl;
344          tmpNPC = tmpNPC->next;
345          //cout << "..changing npc done" << endl;
346        }
347      //cout << "changing play..." << endl;
348      tmpPlayer = tmpPlayer->next;
349      //cout << "changing play done" << endl;
350    }
351
352  //cout << "World::detectCollisions middle" << endl;
353
354  /* second: check if any player hits an enemy */
355  tmpPlayer = lastPlayer;
356  while( tmpPlayer != null ) 
357    {
358      tmpNPC = lastNPC;
359      while( tmpNPC != null )
360        {
361          radius = tmpNPC->npc->collisionRadius + tmpPlayer->player->collisionRadius;
362          xOff = tmpPlayer->player->xCor - tmpNPC->npc->xCor;
363          yOff = tmpPlayer->player->yCor - tmpNPC->npc->yCor;
364          zOff = tmpPlayer->player->zCor - tmpNPC->npc->zCor;
365          if ( sqrt(xOff*xOff + yOff*yOff + zOff*zOff) < radius ) {
366            //cout << "COLLISION " << endl;
367            tmpNPC->npc->hit();
368          }
369         
370          tmpNPC = tmpNPC->next;
371        }
372     
373      tmpPlayer = tmpPlayer->next;
374    }
375 
376 
377
378  /* third: check if any enemy shoots a player */
379
380  //cout << "World::detectCollisions end" << endl;
381}
382
383
384
385/**
386   \brief Routine for testing purposes.
387   
388   testing, testing, testing...
389*/
390void World::testThaTest(void) 
391{
392  cout << "World::testThaTest() called" << endl;
393  /* test addPlayer */
394  cout << "addPlayer test..." << endl;
395  playerList* pl = lastPlayer;
396  while ( pl != null )
397    {
398      cout << "player " << pl->number << " was found" << endl;
399      pl = pl->next;
400    }
401
402  /* test addNPC */
403  cout << "addNPC test..." << endl;
404  npcList* nl = lastNPC;
405  while ( nl != null )
406    {
407      cout << "npc " << nl->number << " was found" << endl;
408      nl = nl->next;
409    }
410
411
412  /* test addEnv */
413  cout << "addEnv test..." << endl;
414  envList* en = lastEnv;
415  while ( en != null )
416    {
417      cout << "env " << en->number << " was found" << endl;
418      en = en->next;
419    }
420
421  /* test drawWorld() */
422}
Note: See TracBrowser for help on using the repository browser.