Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/chris/src/world.cc @ 1983

Last change on this file since 1983 was 1982, checked in by chris, 20 years ago

orxonox/branches/chris: merged trunk into my branch, moved new files into new folder

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