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 | |
---|
22 | using namespace std; |
---|
23 | |
---|
24 | |
---|
25 | /** |
---|
26 | \brief Create a new World |
---|
27 | |
---|
28 | This creates a new empty world! |
---|
29 | */ |
---|
30 | World::World () { |
---|
31 | lastPlayer = null; |
---|
32 | lastNPC = null; |
---|
33 | lastEnv = null; |
---|
34 | primitiveMove = 0; |
---|
35 | step = 0; |
---|
36 | } |
---|
37 | |
---|
38 | |
---|
39 | World::~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 | */ |
---|
48 | bool 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 | */ |
---|
72 | bool World::removePlayer(Player* player) { |
---|
73 | cout << "World::removeNPC not implemented yet" << endl; |
---|
74 | } |
---|
75 | |
---|
76 | Player* 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 | */ |
---|
88 | bool 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 | */ |
---|
112 | bool 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 | */ |
---|
149 | bool 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 | */ |
---|
174 | void World::drawWorld(void) |
---|
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 | /* first draw all players */ |
---|
179 | playerList* tmpPlayer = lastPlayer; |
---|
180 | Player* player = tmpPlayer->player; |
---|
181 | while( tmpPlayer != null ) |
---|
182 | { |
---|
183 | (*tmpPlayer->player).drawPlayer(); |
---|
184 | tmpPlayer = tmpPlayer->next; |
---|
185 | } |
---|
186 | /* second draw all npcs */ |
---|
187 | npcList* tmpNPC = lastNPC; |
---|
188 | while( tmpNPC != null ) |
---|
189 | { |
---|
190 | (*tmpNPC->npc).drawNPC(); |
---|
191 | tmpNPC = tmpNPC->next; |
---|
192 | } |
---|
193 | |
---|
194 | /* now draw the rest of the world: environement */ |
---|
195 | envList* tmpEnv = lastEnv; |
---|
196 | while( tmpEnv != null ) |
---|
197 | { |
---|
198 | (*tmpEnv->env).drawEnvironment(); |
---|
199 | tmpEnv = tmpEnv->next; |
---|
200 | } |
---|
201 | |
---|
202 | /* draw the ground grid */ |
---|
203 | glColor3f(0.0, 1.0, 0.0); |
---|
204 | glBegin(GL_LINES); |
---|
205 | /* for the moment, we've got only pseudo moving ground */ |
---|
206 | for (int y = 0; y < 60; y += 2) |
---|
207 | { |
---|
208 | for (int x = 0; x < 60; x += 2) |
---|
209 | { |
---|
210 | glVertex3f((float)(x - 30), (float)(y - 30), surface[x][y]); |
---|
211 | glVertex3f((float)(x - 28), (float)(y - 30), surface[x+2][y]); |
---|
212 | } |
---|
213 | } |
---|
214 | glEnd(); |
---|
215 | |
---|
216 | glBegin(GL_LINES); |
---|
217 | for (int x = 0; x < 60; x += 2) |
---|
218 | { |
---|
219 | for (int y = 0; y < 60; y += 2) |
---|
220 | { |
---|
221 | glVertex3f((float)(x - 30), (float)(y - 30), surface[x][y]); |
---|
222 | glVertex3f((float)(x - 30), (float)(y - 28), surface[x][y+2]); |
---|
223 | } |
---|
224 | } |
---|
225 | glEnd(); |
---|
226 | |
---|
227 | //primitiveMove+=0.07; |
---|
228 | DataTank::yOffset += step; |
---|
229 | |
---|
230 | tmpPlayer = lastPlayer; |
---|
231 | while( tmpPlayer != null ) |
---|
232 | { |
---|
233 | tmpPlayer->player->yCor += step; |
---|
234 | tmpPlayer = tmpPlayer->next; |
---|
235 | } |
---|
236 | |
---|
237 | |
---|
238 | } |
---|
239 | |
---|
240 | |
---|
241 | void World::initEnvironement() |
---|
242 | { |
---|
243 | |
---|
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 | } |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | void World::setWorldStep(float step) |
---|
255 | { |
---|
256 | this->step = step; |
---|
257 | cout << "setting speed to " << step << endl; |
---|
258 | } |
---|
259 | |
---|
260 | |
---|
261 | |
---|
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 | */ |
---|
268 | void World::updateWorld(void) |
---|
269 | { |
---|
270 | |
---|
271 | |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | /* collision detection */ |
---|
276 | /* fix: bad efficency: stupid brute force */ |
---|
277 | |
---|
278 | void World::detectCollision() |
---|
279 | { |
---|
280 | //cout << "World::detectCollision" << endl; |
---|
281 | float xOff, yOff, zOff, radius; |
---|
282 | npcList* tmpNPC, *tmpRef; |
---|
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; |
---|
288 | int state; |
---|
289 | while( tmpPlayer != null ) |
---|
290 | { |
---|
291 | tmpNPC = lastNPC; |
---|
292 | while( tmpNPC != null ) |
---|
293 | { |
---|
294 | //cout << "npc != null" << endl; |
---|
295 | radius = tmpNPC->npc->collisionRadius; |
---|
296 | //cout << "worki" << endl; |
---|
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; |
---|
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 | } |
---|
318 | shoota = shoota->next; |
---|
319 | } |
---|
320 | //cout << "changing npc..." << endl; |
---|
321 | tmpNPC = tmpNPC->next; |
---|
322 | //cout << "..changing npc done" << endl; |
---|
323 | } |
---|
324 | //cout << "changing play..." << endl; |
---|
325 | tmpPlayer = tmpPlayer->next; |
---|
326 | //cout << "changing play done" << endl; |
---|
327 | } |
---|
328 | |
---|
329 | //cout << "World::detectCollisions middle" << endl; |
---|
330 | |
---|
331 | /* second: check if any player hits an enemy */ |
---|
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; |
---|
342 | if ( sqrt(xOff*xOff + yOff*yOff + zOff*zOff) < radius ) { |
---|
343 | //cout << "COLLISION " << endl; |
---|
344 | tmpNPC->npc->hit(); |
---|
345 | } |
---|
346 | |
---|
347 | tmpNPC = tmpNPC->next; |
---|
348 | } |
---|
349 | |
---|
350 | tmpPlayer = tmpPlayer->next; |
---|
351 | } |
---|
352 | |
---|
353 | |
---|
354 | |
---|
355 | /* third: check if any enemy shoots a player */ |
---|
356 | |
---|
357 | //cout << "World::detectCollisions end" << endl; |
---|
358 | } |
---|
359 | |
---|
360 | |
---|
361 | |
---|
362 | /** |
---|
363 | \brief Routine for testing purposes. |
---|
364 | |
---|
365 | testing, testing, testing... |
---|
366 | */ |
---|
367 | void World::testThaTest(void) |
---|
368 | { |
---|
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 | } |
---|
378 | |
---|
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 | |
---|
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 | |
---|
398 | /* test drawWorld() */ |
---|
399 | } |
---|