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