Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/core/world.cc @ 1919

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

orxonox/trunk: gc modified, player moves with world, collision detection works

File size: 7.2 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}
36
37
38World::~World () {}
39
40
41/**
42   \brief Add Player
43   \param player A reference to the new player object
44   
45   Add a new Player to the game. Player has to be initialised previously
46*/
47bool World::addPlayer(Player* player) 
48{
49  playerList* listMember = new playerList;
50  listMember->player = player;
51  if ( lastPlayer != null ) 
52    {
53      listMember->number = lastPlayer->number + 1;
54      listMember->next = lastPlayer;
55    }
56  else 
57    {
58      listMember->number = 0;
59      listMember->next = null;
60    }
61  lastPlayer = listMember;
62}
63
64
65/**
66   \brief Remove Player
67   \param player A reference to the new npc object
68   
69   Remove a new Player to the game.
70*/
71bool World::removePlayer(Player* player) {
72  cout << "World::removeNPC not implemented yet" << endl;
73}
74
75Player* World::getLocalPlayer() 
76{
77  return localPlayer;
78}
79
80
81/**
82   \brief Add Non-Player-Character
83   \param player A reference to the new npc object
84   
85   Add a new Non-Player-Character to the game. Player has to be initialised previously
86*/
87bool World::addNPC(NPC* npc) 
88{
89  npcList* listMember = new npcList;
90  listMember->npc = npc;
91  if ( lastNPC != null ) 
92    {
93      listMember->number = lastNPC->number + 1;
94      listMember->next = lastNPC;
95    }
96  else 
97    {
98      listMember->number = 0;
99      listMember->next = null;
100    }
101  lastNPC = listMember;
102}
103
104
105/**
106   \brief Add environmental object
107   \param player A reference to the new env object
108   
109   Add a new Environment to the world. Env has to be initialised before.
110*/
111bool World::addEnv(Environment* env) 
112{
113  envList* listMember = new envList;
114  listMember->env = env;
115  if ( lastEnv != null ) 
116    {
117      listMember->number = lastEnv->number + 1;
118      listMember->next = lastEnv;
119    }
120  else 
121    {
122      listMember->number = 0;
123      listMember->next = null;
124    }
125  lastEnv = listMember;
126}
127
128
129
130
131
132/**
133   \brief Remove Non-Player Character
134   \param player A reference to the new npc object
135   
136   Remove a new Non-Player-Character to the game.
137*/
138bool World::removeNPC(NPC* npc) {
139  cout << "World::removeNPC not implemented yet" << endl;
140}
141
142
143
144/**
145   \brief Draws the World and all Objects contained
146   
147   Calls the draw function of all: Objects, Players, Environement. This is the core of all graphics here.
148*/
149void World::drawWorld(void) 
150{
151  glLoadIdentity();
152  gluLookAt(0.0, -14.0 + DataTank::yOffset, 15.0, 0.0, 0.0 + DataTank::yOffset, 0.0, 0.0, 1.0, 0.0);
153  /* first draw all players */
154  playerList* tmpPlayer = lastPlayer;
155  Player* player = tmpPlayer->player;
156  while( tmpPlayer != null ) 
157    {
158      (*tmpPlayer->player).drawPlayer();
159      tmpPlayer = tmpPlayer->next;
160    }
161  /* second draw all npcs */
162  npcList* tmpNPC = lastNPC;
163  while( tmpNPC != null )
164    {
165      (*tmpNPC->npc).drawNPC();
166      tmpNPC = tmpNPC->next;
167    }
168
169
170  /* now draw the rest of the world: environement */
171  envList* tmpEnv = lastEnv;
172  while( tmpEnv != null )
173    {
174      (*tmpEnv->env).drawEnvironment();
175      tmpEnv = tmpEnv->next;
176    }
177 
178 
179  /* draw the ground grid  */
180  glColor3f(0.0, 1.0, 0.0); 
181  glBegin(GL_LINES);
182  /* for the moment, we've got only pseudo moving ground */
183  for (int y = 0; y < 60; y += 2)
184    {
185      for (int x = 0; x < 60; x += 2)
186        {
187          glVertex3f((float)(x - 30), (float)(y - 30), surface[x][y]);
188          glVertex3f((float)(x - 28), (float)(y - 30), surface[x+2][y]);
189        }
190    }
191 glEnd();
192 //glPopMatrix();
193 
194  glBegin(GL_LINES);
195  for (int x = 0; x < 60; x += 2)
196    {
197      for (int y = 0; y < 60; y += 2)
198        {
199          glVertex3f((float)(x - 30), (float)(y - 30), surface[x][y]);
200          glVertex3f((float)(x - 30), (float)(y - 28), surface[x][y+2]);
201        }
202    }
203  glEnd();
204
205  //primitiveMove+=0.07;
206  DataTank::yOffset += 0.07;
207
208  tmpPlayer = lastPlayer;
209  while( tmpPlayer != null ) 
210    {
211      tmpPlayer->player->yCor += 0.07;
212      tmpPlayer = tmpPlayer->next;
213    }
214
215 
216}
217
218
219void World::initEnvironement()
220{
221
222 for (int x = 0; x < 60; x += 2)
223    {
224      for (int y = 0; y < 60; y += 2)
225        {
226          surface[x][y] = 0;
227        }
228    }
229}
230
231
232/**
233   \brief Updates the world and all its objects
234   
235   Calculates the new state of the world. User-input and AI of
236   the enemies are accounted for.
237*/
238void World::updateWorld(void) 
239{
240 
241
242}
243
244
245/* collision detection */
246/* fix: bad efficency: stupid brute force */
247
248void World::detectCollision() 
249{
250  //cout << "World::detectCollision" << endl;
251  float xOff, yOff, zOff, radius;
252  npcList* tmpNPC;
253
254  //cout << "World::detectCollsions" << endl;
255  /* first: check if any player's shoots trigger a collision */
256  playerList* tmpPlayer = lastPlayer;
257  Player* player = tmpPlayer->player;
258  while( tmpPlayer != null ) 
259    {
260      tmpNPC = lastNPC;
261      while( tmpNPC != null )
262        {
263          radius = tmpNPC->npc->collisionRadius;
264          ShootLaser::shoot* shoota = tmpPlayer->player->shootLaser->lastShoot;
265          while( shoota != null )
266            {
267              xOff = shoota->xCor - tmpNPC->npc->xCor;
268              yOff = shoota->yCor - tmpNPC->npc->yCor;
269              zOff = shoota->zCor - tmpNPC->npc->zCor;
270              if ( sqrt(xOff*xOff + yOff*yOff + zOff*zOff) < radius )
271                cout << "COLLISION " << endl;
272              shoota = shoota->next;
273            }
274          tmpNPC = tmpNPC->next;
275        }
276     
277      tmpPlayer = tmpPlayer->next;
278    }
279
280  /* second: check if any player hits an enemy */
281
282 
283  tmpPlayer = lastPlayer;
284  while( tmpPlayer != null ) 
285    {
286      tmpNPC = lastNPC;
287      while( tmpNPC != null )
288        {
289          radius = tmpNPC->npc->collisionRadius + tmpPlayer->player->collisionRadius;
290          xOff = tmpPlayer->player->xCor - tmpNPC->npc->xCor;
291          yOff = tmpPlayer->player->yCor - tmpNPC->npc->yCor;
292          zOff = tmpPlayer->player->zCor - tmpNPC->npc->zCor;
293          if ( sqrt(xOff*xOff + yOff*yOff + zOff*zOff) < radius )
294            cout << "COLLISION " << endl;
295         
296          tmpNPC = tmpNPC->next;
297        }
298     
299      tmpPlayer = tmpPlayer->next;
300    }
301 
302 
303
304  /* third: check if any enemy shoots a player */
305
306  //cout << "World::detectCollisions end" << endl;
307}
308
309
310
311/**
312   \brief Routine for testing purposes.
313   
314   testing, testing, testing...
315*/
316void World::testThaTest(void) 
317{
318  cout << "World::testThaTest() called" << endl;
319  /* test addPlayer */
320  cout << "addPlayer test..." << endl;
321  playerList* pl = lastPlayer;
322  while ( pl != null )
323    {
324      cout << "player " << pl->number << " was found" << endl;
325      pl = pl->next;
326    }
327
328  /* test addNPC */
329  cout << "addNPC test..." << endl;
330  npcList* nl = lastNPC;
331  while ( nl != null )
332    {
333      cout << "npc " << nl->number << " was found" << endl;
334      nl = nl->next;
335    }
336
337
338  /* test addEnv */
339  cout << "addEnv test..." << endl;
340  envList* en = lastEnv;
341  while ( en != null )
342    {
343      cout << "env " << en->number << " was found" << endl;
344      en = en->next;
345    }
346
347  /* test drawWorld() */
348}
Note: See TracBrowser for help on using the repository browser.