Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/tags/0.1-pre-alpha-3/core/world.cc @ 9249

Last change on this file since 9249 was 2242, checked in by dave, 20 years ago

orxonox/branches/dave: habe wieder mal was gemacht:)

File size: 8.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
22using namespace std;
23
24
25/**
26   \brief Create a new World
27   
28   This creates a new empty world!
29*/
30World::World () {
31  lastPlayer = null;
32  lastNPC = null;
33  lastEnv = null;
34  primitiveMove = 0;
35  step = 0;
36}
37
38
39World::~World () {}
40
41
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;
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    }
62  lastPlayer = listMember;
63}
64
65
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}
75
76Player* World::getLocalPlayer() 
77{
78  return localPlayer;
79}
80
81
82/**
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/**
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/**
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
167
168
169/**
170   \brief Draws the World and all Objects contained
171   
172   Calls the draw function of all: Objects, Players, Environement. This is the core of all graphics here.
173*/
174void World::drawWorld(float Zahl1,float Zahl2) 
175{
176  glLoadIdentity();
177  gluLookAt(0.0, -14.0 + DataTank::yOffset, 15.0, 0.0, 0.0 + DataTank::yOffset, 0.0, 0.0, 1.0, 0.0);
178
179  //glRotatef(Zahl1,1.0,0.0,0.0);
180  //glRotatef(Zahl2,0.0,1.0,0.0);
181 
182  /* first draw all players */
183  playerList* tmpPlayer = lastPlayer;
184  Player* player = tmpPlayer->player;
185  while( tmpPlayer != null ) 
186    {
187      (*tmpPlayer->player).drawPlayer((float)Zahl1,(float)Zahl2);
188      tmpPlayer = tmpPlayer->next;
189    }
190  /* second draw all npcs */
191  npcList* tmpNPC = lastNPC;
192  while( tmpNPC != null )
193    {
194      (*tmpNPC->npc).drawNPC();
195      tmpNPC = tmpNPC->next;
196    }
197
198  /* now draw the rest of the world: environement */
199  envList* tmpEnv = lastEnv;
200  while( tmpEnv != null )
201    {
202      (*tmpEnv->env).drawEnvironment();
203      tmpEnv = tmpEnv->next;
204    }
205 
206  /* draw the ground grid  */
207  glColor3f(0.3, 0.3, 0.0); 
208 // glBegin(GL_LINES);
209  /* for the moment, we've got only pseudo moving ground
210  for (int y = 0; y < 60; y += 2)
211    {
212      for (int x = 0; x < 60; x += 2)
213        {
214          glVertex3f((float)(x - 30), (float)(y - 30), surface[x][y]);
215          glVertex3f((float)(x - 28), (float)(y - 30), surface[x+2][y]);
216        }
217    }
218  glEnd();
219 
220  glBegin(GL_LINES);
221  for (int x = 0; x < 60; x += 2)
222    {
223      for (int y = 0; y < 60; y += 2)
224        {
225          glVertex3f((float)(x - 30), (float)(y - 30), surface[x][y]);
226          glVertex3f((float)(x - 30), (float)(y - 28), surface[x][y+2]);
227        }
228    }
229  glEnd();*/
230
231  //primitiveMove+=0.07;
232  DataTank::yOffset += step;
233
234  tmpPlayer = lastPlayer;
235  while( tmpPlayer != null ) 
236    {
237      tmpPlayer->player->yCor += step;
238      tmpPlayer = tmpPlayer->next;
239    }
240
241 
242}
243
244
245void World::initEnvironement()
246{
247
248 for (int x = 0; x < 60; x += 2)
249    {
250      for (int y = 0; y < 60; y += 2)
251        {
252          surface[x][y] = 0;
253        }
254    }
255}
256
257
258void World::setWorldStep(float step)
259{
260  this->step = step;
261  cout << "setting speed to " << step << endl;
262}
263
264
265
266/**
267   \brief Updates the world and all its objects
268   
269   Calculates the new state of the world. User-input and AI of
270   the enemies are accounted for.
271*/
272void World::updateWorld(void) 
273{
274 
275
276}
277
278
279/* collision detection */
280/* fix: bad efficency: stupid brute force */
281
282void World::detectCollision() 
283{
284  //cout << "World::detectCollision" << endl;
285  float xOff, yOff, zOff, radius;
286  npcList* tmpNPC, *tmpRef;
287
288  //cout << "World::detectCollsions" << endl;
289  /* first: check if any player's shoots trigger a collision */
290  playerList* tmpPlayer = lastPlayer;
291  Player* player = tmpPlayer->player;
292  int state;
293  while( tmpPlayer != null ) 
294    {
295      tmpNPC = lastNPC;
296      while( tmpNPC != null )
297        {
298          //cout << "npc != null" << endl;
299          radius = tmpNPC->npc->collisionRadius;
300          //cout << "worki" << endl;
301          ShootLaser::shoot* shoota = tmpPlayer->player->shootLaser->lastShoot;
302          while( shoota != null )
303            {
304              xOff = shoota->xCor - tmpNPC->npc->xCor;
305              yOff = shoota->yCor - tmpNPC->npc->yCor;
306              zOff = shoota->zCor - tmpNPC->npc->zCor;
307              if ( sqrt(xOff*xOff + yOff*yOff + zOff*zOff) < radius ) 
308                {
309                  //cout << "COLLISION " << endl;
310                  int state = tmpNPC->npc->hit();
311                  /* state is a value that marks if the ship dies or not */
312                  /* if state == 0 the ship dies and we have to remove it */
313                  /*
314                  if ( state == 0 ) {
315                    tmpRef = tmpNPC;
316                    tmpNPC = tmpNPC->next;
317                    removeNPC(tmpRef->npc);
318                    break;
319                  }
320                  */
321                }
322              shoota = shoota->next;
323            }
324          //cout << "changing npc..." << endl;
325          tmpNPC = tmpNPC->next;
326          //cout << "..changing npc done" << endl;
327        }
328      //cout << "changing play..." << endl;
329      tmpPlayer = tmpPlayer->next;
330      //cout << "changing play done" << endl;
331    }
332
333  //cout << "World::detectCollisions middle" << endl;
334
335  /* second: check if any player hits an enemy */
336  tmpPlayer = lastPlayer;
337  while( tmpPlayer != null ) 
338    {
339      tmpNPC = lastNPC;
340      while( tmpNPC != null )
341        {
342          radius = tmpNPC->npc->collisionRadius + tmpPlayer->player->collisionRadius;
343          xOff = tmpPlayer->player->xCor - tmpNPC->npc->xCor;
344          yOff = tmpPlayer->player->yCor - tmpNPC->npc->yCor;
345          zOff = tmpPlayer->player->zCor - tmpNPC->npc->zCor;
346          if ( sqrt(xOff*xOff + yOff*yOff + zOff*zOff) < radius ) {
347            //cout << "COLLISION " << endl;
348            tmpNPC->npc->hit();
349          }
350         
351          tmpNPC = tmpNPC->next;
352        }
353     
354      tmpPlayer = tmpPlayer->next;
355    }
356 
357 
358
359  /* third: check if any enemy shoots a player */
360
361  //cout << "World::detectCollisions end" << endl;
362}
363
364
365
366/**
367   \brief Routine for testing purposes.
368   
369   testing, testing, testing...
370*/
371void World::testThaTest(void) 
372{
373  cout << "World::testThaTest() called" << endl;
374  /* test addPlayer */
375  cout << "addPlayer test..." << endl;
376  playerList* pl = lastPlayer;
377  while ( pl != null )
378    {
379      cout << "player " << pl->number << " was found" << endl;
380      pl = pl->next;
381    }
382
383  /* test addNPC */
384  cout << "addNPC test..." << endl;
385  npcList* nl = lastNPC;
386  while ( nl != null )
387    {
388      cout << "npc " << nl->number << " was found" << endl;
389      nl = nl->next;
390    }
391
392
393  /* test addEnv */
394  cout << "addEnv test..." << endl;
395  envList* en = lastEnv;
396  while ( en != null )
397    {
398      cout << "env " << en->number << " was found" << endl;
399      en = en->next;
400    }
401
402  /* test drawWorld() */
403}
Note: See TracBrowser for help on using the repository browser.