Changeset 2068 in orxonox.OLD for orxonox/branches/chris/src/world.cc
- Timestamp:
- Jul 5, 2004, 10:43:49 AM (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
orxonox/branches/chris/src/world.cc
r2058 r2068 12 12 ### File Specific: 13 13 main-programmer: Patrick Boenzli 14 co-programmer: 14 co-programmer: Christian Meyer 15 15 */ 16 16 … … 18 18 #include <stdlib.h> 19 19 #include <cmath> 20 #include <GL/glut.h>21 20 22 21 #include "npc.h" … … 39 38 This creates a new empty world! 40 39 */ 41 World::World () { 42 lastPlayer = null; 43 lastNPC = null; 44 lastEnv = null; 45 primitiveMove = 0; 46 step = 0; 40 World::World () 41 { 42 entities = new List<WorldEntitites>(); 47 43 } 48 44 49 45 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) 46 World::~World () 60 47 { 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; 48 delete entities; 74 49 } 75 50 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; 51 template<class T> T* World::spawn<T>(Location* loc, WorldEntity* owner) 52 { 53 T* entity = new T(); 54 entities->add ((WorldEntity*)entity, LIST_ADD_NEXT); 55 return entity; 85 56 } 86 57 87 Player* World::getLocalPlayer() 58 World::collide () 88 59 { 89 return localPlayer; 60 List<WorldEntity> *a, *b; 61 WorldEntity *aobj, *bobj; 62 63 a = entities->get_next(); 64 65 while( a != NULL) 66 { 67 aobj = a->get_object(); 68 if( aobj->bCollide && aobj->collisioncluster != NULL) 69 { 70 b = a->get_next(); 71 while( b != NULL) 72 { 73 bobj = b->get_object(); 74 if( bobj->bCollide && bobj->collisioncluster != NULL) 75 { 76 Uint32 ahitflg, bhitflg; 77 if check_collision ( aobj->place, aobj->collisioncluster, &ahitflg, bobj->place, bobj->collisioncluster, &bhitflg); 78 { 79 aobj->collide (bobj, ahitflg, bhitflg); 80 bobj->collide (aobj, bhitflg, ahitflg); 81 } 82 } 83 b = b->get_next(); 84 } 85 } 86 a = a->get_next(); 87 } 90 88 } 91 89 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) 90 void World::draw () 100 91 { 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 else109 { 110 listMember->number = 0;111 listMember->next = null;112 } 113 lastNPC = listMember; 92 // draw geometry 93 94 // draw entities 95 List<WorldEntity> *l; 96 WorldEntity* entity; 97 98 l = entities->get_next(); 99 while( l != NULL) 100 { 101 entity = l->get_object(); 102 if( entity->bDraw) entity->draw(); 103 l = l->get_next(); 104 } 114 105 } 115 106 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; 107 void World::update () 108 { 150 109 } 151 110 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) 111 void World::time_slice (Uint32 deltaT) 161 112 { 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 else170 { 171 listMember->number = 0;172 listMember->next = null;173 } 174 lastEnv = listMember; 113 List<WorldEntity> *l; 114 WorldEntity* entity; 115 float seconds = deltaT; 116 117 seconds /= 1000; 118 119 l = entities->get_next(); 120 while( l != NULL) 121 { 122 entity = l->get_object(); 123 entity->tick (seconds); 124 l = l->get_next(); 125 } 175 126 } 176 177 178 179 180 /**181 \brief Draws the World and all Objects contained182 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 objects277 278 Calculates the new state of the world. User-input and AI of279 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 }
Note: See TracChangeset
for help on using the changeset viewer.