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