| 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 | #include <iostream> |
|---|
| 18 | #include <stdlib.h> |
|---|
| 19 | #include <cmath> |
|---|
| 20 | #include <GL/glut.h> |
|---|
| 21 | |
|---|
| 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 | |
|---|
| 30 | #include "world.h" |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | using namespace std; |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | \brief Create a new World |
|---|
| 38 | |
|---|
| 39 | This creates a new empty world! |
|---|
| 40 | */ |
|---|
| 41 | World::World () { |
|---|
| 42 | lastPlayer = null; |
|---|
| 43 | lastNPC = null; |
|---|
| 44 | lastEnv = null; |
|---|
| 45 | primitiveMove = 0; |
|---|
| 46 | step = 0; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | World::~World () {} |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 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; |
|---|
| 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 | } |
|---|
| 73 | lastPlayer = listMember; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 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 | } |
|---|
| 86 | |
|---|
| 87 | Player* World::getLocalPlayer() |
|---|
| 88 | { |
|---|
| 89 | return localPlayer; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | /** |
|---|
| 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 | /** |
|---|
| 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 | /** |
|---|
| 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 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | /** |
|---|
| 181 | \brief Draws the World and all Objects contained |
|---|
| 182 | |
|---|
| 183 | Calls the draw function of all: Objects, Players, Environement. This is the core of all graphics here. |
|---|
| 184 | */ |
|---|
| 185 | void World::drawWorld(void) |
|---|
| 186 | { |
|---|
| 187 | |
|---|
| 188 | glLoadIdentity(); |
|---|
| 189 | gluLookAt(0.0, -14.0 + DataTank::yOffset, 15.0, 0.0, 0.0 + DataTank::yOffset, 0.0, 0.0, 1.0, 0.0); |
|---|
| 190 | /* first draw all players */ |
|---|
| 191 | playerList* tmpPlayer = lastPlayer; |
|---|
| 192 | Player* player = tmpPlayer->player; |
|---|
| 193 | while( tmpPlayer != null ) |
|---|
| 194 | { |
|---|
| 195 | tmpPlayer->player->paint(); |
|---|
| 196 | tmpPlayer = tmpPlayer->next; |
|---|
| 197 | } |
|---|
| 198 | /* second draw all npcs */ |
|---|
| 199 | npcList* tmpNPC = lastNPC; |
|---|
| 200 | while( tmpNPC != null ) |
|---|
| 201 | { |
|---|
| 202 | (*tmpNPC->npc).paint(); |
|---|
| 203 | tmpNPC = tmpNPC->next; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | /* now draw the rest of the world: environement */ |
|---|
| 207 | envList* tmpEnv = lastEnv; |
|---|
| 208 | while( tmpEnv != null ) |
|---|
| 209 | { |
|---|
| 210 | (*tmpEnv->env).drawEnvironment(); |
|---|
| 211 | tmpEnv = tmpEnv->next; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | /* draw the ground grid */ |
|---|
| 215 | glColor3f(0.0, 1.0, 0.0); |
|---|
| 216 | glBegin(GL_LINES); |
|---|
| 217 | /* for the moment, we've got only pseudo moving ground */ |
|---|
| 218 | for (int y = 0; y < 60; y += 2) |
|---|
| 219 | { |
|---|
| 220 | for (int x = 0; x < 60; x += 2) |
|---|
| 221 | { |
|---|
| 222 | glVertex3f((float)(x - 30), (float)(y - 30), surface[x][y]); |
|---|
| 223 | glVertex3f((float)(x - 28), (float)(y - 30), surface[x+2][y]); |
|---|
| 224 | } |
|---|
| 225 | } |
|---|
| 226 | glEnd(); |
|---|
| 227 | |
|---|
| 228 | glBegin(GL_LINES); |
|---|
| 229 | for (int x = 0; x < 60; x += 2) |
|---|
| 230 | { |
|---|
| 231 | for (int y = 0; y < 60; y += 2) |
|---|
| 232 | { |
|---|
| 233 | glVertex3f((float)(x - 30), (float)(y - 30), surface[x][y]); |
|---|
| 234 | glVertex3f((float)(x - 30), (float)(y - 28), surface[x][y+2]); |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | glEnd(); |
|---|
| 238 | |
|---|
| 239 | //primitiveMove+=0.07; |
|---|
| 240 | DataTank::yOffset += step; |
|---|
| 241 | |
|---|
| 242 | tmpPlayer = lastPlayer; |
|---|
| 243 | while( tmpPlayer != null ) |
|---|
| 244 | { |
|---|
| 245 | tmpPlayer->player->yCor += step; |
|---|
| 246 | tmpPlayer = tmpPlayer->next; |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | void World::initEnvironement() |
|---|
| 254 | { |
|---|
| 255 | |
|---|
| 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 | } |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | void World::setWorldStep(float step) |
|---|
| 267 | { |
|---|
| 268 | //cout << "World::setWorldStep(" << step << ");" << endl; |
|---|
| 269 | this->step = step; |
|---|
| 270 | //cout << "setting speed to " << step << endl; |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 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 | |
|---|
| 288 | /* collision detection */ |
|---|
| 289 | /* fix: bad efficency: stupid brute force */ |
|---|
| 290 | |
|---|
| 291 | void World::detectCollision() |
|---|
| 292 | { |
|---|
| 293 | //cout << "World::detectCollision" << endl; |
|---|
| 294 | float xOff, yOff, zOff, radius; |
|---|
| 295 | npcList* tmpNPC, *tmpRef; |
|---|
| 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; |
|---|
| 301 | int state; |
|---|
| 302 | while( tmpPlayer != null ) |
|---|
| 303 | { |
|---|
| 304 | tmpNPC = lastNPC; |
|---|
| 305 | while( tmpNPC != null ) |
|---|
| 306 | { |
|---|
| 307 | //cout << "npc != null" << endl; |
|---|
| 308 | radius = tmpNPC->npc->collisionRadius; |
|---|
| 309 | //cout << "worki" << endl; |
|---|
| 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; |
|---|
| 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 | } |
|---|
| 331 | shoota = shoota->next; |
|---|
| 332 | } |
|---|
| 333 | //cout << "changing npc..." << endl; |
|---|
| 334 | tmpNPC = tmpNPC->next; |
|---|
| 335 | //cout << "..changing npc done" << endl; |
|---|
| 336 | } |
|---|
| 337 | //cout << "changing play..." << endl; |
|---|
| 338 | tmpPlayer = tmpPlayer->next; |
|---|
| 339 | //cout << "changing play done" << endl; |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | //cout << "World::detectCollisions middle" << endl; |
|---|
| 343 | |
|---|
| 344 | /* second: check if any player hits an enemy */ |
|---|
| 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; |
|---|
| 355 | if ( sqrt(xOff*xOff + yOff*yOff + zOff*zOff) < radius ) { |
|---|
| 356 | //cout << "COLLISION " << endl; |
|---|
| 357 | tmpNPC->npc->hit(); |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | tmpNPC = tmpNPC->next; |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | tmpPlayer = tmpPlayer->next; |
|---|
| 364 | } |
|---|
| 365 | |
|---|
| 366 | |
|---|
| 367 | |
|---|
| 368 | /* third: check if any enemy shoots a player */ |
|---|
| 369 | |
|---|
| 370 | //cout << "World::detectCollisions end" << endl; |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | /** |
|---|
| 376 | \brief Routine for testing purposes. |
|---|
| 377 | |
|---|
| 378 | testing, testing, testing... |
|---|
| 379 | */ |
|---|
| 380 | void World::testThaTest(void) |
|---|
| 381 | { |
|---|
| 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 | } |
|---|
| 391 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 411 | /* test drawWorld() */ |
|---|
| 412 | } |
|---|