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