| 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: Christian Meyer |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | #include "world.h" |
|---|
| 18 | #include "world_entity.h" |
|---|
| 19 | #include "collision.h" |
|---|
| 20 | #include "track.h" |
|---|
| 21 | #include "player.h" |
|---|
| 22 | #include "command_node.h" |
|---|
| 23 | #include "camera.h" |
|---|
| 24 | |
|---|
| 25 | using namespace std; |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | /** |
|---|
| 29 | \brief create a new World |
|---|
| 30 | |
|---|
| 31 | This creates a new empty world! |
|---|
| 32 | */ |
|---|
| 33 | World::World () |
|---|
| 34 | { |
|---|
| 35 | entities = new List<WorldEntity>(); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | /** |
|---|
| 39 | \brief remove the World from memory |
|---|
| 40 | */ |
|---|
| 41 | World::~World () |
|---|
| 42 | { |
|---|
| 43 | unload (); |
|---|
| 44 | delete entities; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | \brief checks for collisions |
|---|
| 49 | |
|---|
| 50 | This method runs through all WorldEntities known to the world and checks for collisions |
|---|
| 51 | between them. In case of collisions the collide() method of the corresponding entities |
|---|
| 52 | is called. |
|---|
| 53 | */ |
|---|
| 54 | void World::collide () |
|---|
| 55 | { |
|---|
| 56 | List<WorldEntity> *a, *b; |
|---|
| 57 | WorldEntity *aobj, *bobj; |
|---|
| 58 | |
|---|
| 59 | a = entities->get_next(); |
|---|
| 60 | |
|---|
| 61 | while( a != NULL) |
|---|
| 62 | { |
|---|
| 63 | aobj = a->get_object(); |
|---|
| 64 | if( aobj->bCollide && aobj->collisioncluster != NULL) |
|---|
| 65 | { |
|---|
| 66 | b = a->get_next(); |
|---|
| 67 | while( b != NULL ) |
|---|
| 68 | { |
|---|
| 69 | bobj = b->get_object(); |
|---|
| 70 | if( bobj->bCollide && bobj->collisioncluster != NULL ) |
|---|
| 71 | { |
|---|
| 72 | unsigned long ahitflg, bhitflg; |
|---|
| 73 | if( check_collision ( &aobj->place, aobj->collisioncluster, |
|---|
| 74 | &ahitflg, &bobj->place, bobj->collisioncluster, |
|---|
| 75 | &bhitflg) ); |
|---|
| 76 | { |
|---|
| 77 | aobj->collide (bobj, ahitflg, bhitflg); |
|---|
| 78 | bobj->collide (aobj, bhitflg, ahitflg); |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | b = b->get_next(); |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | a = a->get_next(); |
|---|
| 85 | } |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | /** |
|---|
| 89 | \brief runs through all entities calling their draw() methods |
|---|
| 90 | */ |
|---|
| 91 | void World::draw () |
|---|
| 92 | { |
|---|
| 93 | // draw geometry |
|---|
| 94 | |
|---|
| 95 | // draw entities |
|---|
| 96 | List<WorldEntity> *l; |
|---|
| 97 | WorldEntity* entity; |
|---|
| 98 | |
|---|
| 99 | l = entities->get_next(); |
|---|
| 100 | while( l != NULL ) |
|---|
| 101 | { |
|---|
| 102 | entity = l->get_object(); |
|---|
| 103 | if( entity->bDraw ) entity->draw(); |
|---|
| 104 | l = l->get_next(); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | // draw debug coord system |
|---|
| 109 | glLoadIdentity(); |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | glBegin(GL_LINES); |
|---|
| 113 | |
|---|
| 114 | for( float x = -128.0; x < 128.0; x += 25.0) |
|---|
| 115 | { |
|---|
| 116 | for( float y = -128.0; y < 128.0; y += 25.0) |
|---|
| 117 | { |
|---|
| 118 | glColor3f(1,0,0); |
|---|
| 119 | glVertex3f(x,y,-128.0); |
|---|
| 120 | glVertex3f(x,y,0.0); |
|---|
| 121 | glColor3f(0.5,0,0); |
|---|
| 122 | glVertex3f(x,y,0.0); |
|---|
| 123 | glVertex3f(x,y,128.0); |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | for( float y = -128.0; y < 128.0; y += 25.0) |
|---|
| 127 | { |
|---|
| 128 | for( float z = -128.0; z < 128.0; z += 25.0) |
|---|
| 129 | { |
|---|
| 130 | glColor3f(0,1,0); |
|---|
| 131 | glVertex3f(-128.0,y,z); |
|---|
| 132 | glVertex3f(0.0,y,z); |
|---|
| 133 | glColor3f(0,0.5,0); |
|---|
| 134 | glVertex3f(0.0,y,z); |
|---|
| 135 | glVertex3f(128.0,y,z); |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | for( float x = -128.0; x < 128.0; x += 25.0) |
|---|
| 139 | { |
|---|
| 140 | for( float z = -128.0; z < 128.0; z += 25.0) |
|---|
| 141 | { |
|---|
| 142 | glColor3f(0,0,1); |
|---|
| 143 | glVertex3f(x,-128.0,z); |
|---|
| 144 | glVertex3f(x,0.0,z); |
|---|
| 145 | glColor3f(0,0,0.5); |
|---|
| 146 | glVertex3f(x,0.0,z); |
|---|
| 147 | glVertex3f(x,128.0,z); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | //draw track |
|---|
| 153 | glColor3f(0,1,1); |
|---|
| 154 | for( int i = 0; i < tracklen; i++) |
|---|
| 155 | { |
|---|
| 156 | glVertex3f(pathnodes[i].x,pathnodes[i].y,pathnodes[i].z); |
|---|
| 157 | glVertex3f(pathnodes[(i+1)%tracklen].x,pathnodes[(i+1)%tracklen].y,pathnodes[(i+1)%tracklen].z); |
|---|
| 158 | } |
|---|
| 159 | glEnd(); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | /** |
|---|
| 163 | \brief updates Placements and notifies entities when they left the |
|---|
| 164 | world |
|---|
| 165 | |
|---|
| 166 | This runs trough all WorldEntities and maps Locations to Placements |
|---|
| 167 | if they are bound, checks whether they left the level boundaries |
|---|
| 168 | and calls appropriate functions. |
|---|
| 169 | */ |
|---|
| 170 | void World::update () |
|---|
| 171 | { |
|---|
| 172 | List<WorldEntity> *l; |
|---|
| 173 | WorldEntity* entity; |
|---|
| 174 | Location* loc; |
|---|
| 175 | Placement* plc; |
|---|
| 176 | Uint32 t; |
|---|
| 177 | |
|---|
| 178 | l = entities->get_next(); |
|---|
| 179 | while( l != NULL ) |
|---|
| 180 | { |
|---|
| 181 | entity = l->get_object(); |
|---|
| 182 | |
|---|
| 183 | if( !entity->isFree() ) |
|---|
| 184 | { |
|---|
| 185 | loc = entity->get_location(); |
|---|
| 186 | plc = entity->get_placement(); |
|---|
| 187 | t = loc->part; |
|---|
| 188 | |
|---|
| 189 | /* check if entity has still a legal track-id */ |
|---|
| 190 | if( t >= tracklen ) |
|---|
| 191 | { |
|---|
| 192 | printf("An entity is out of the game area\n"); |
|---|
| 193 | entity->left_world (); |
|---|
| 194 | } |
|---|
| 195 | else |
|---|
| 196 | { |
|---|
| 197 | while( track[t].map_coords( loc, plc) ) |
|---|
| 198 | { |
|---|
| 199 | track[t].post_leave (entity); |
|---|
| 200 | if( loc->part >= tracklen ) |
|---|
| 201 | { |
|---|
| 202 | printf("An entity has left the game area\n"); |
|---|
| 203 | entity->left_world (); |
|---|
| 204 | break; |
|---|
| 205 | } |
|---|
| 206 | track[loc->part].post_enter (entity); |
|---|
| 207 | } |
|---|
| 208 | } |
|---|
| 209 | } |
|---|
| 210 | else |
|---|
| 211 | { |
|---|
| 212 | /* TO DO: implement check whether this particular free entity |
|---|
| 213 | is out of the game area |
|---|
| 214 | TO DO: call function to notify the entity that it left |
|---|
| 215 | the game area |
|---|
| 216 | */ |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | l = l->get_next(); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | /** |
|---|
| 225 | \brief relays the passed time since the last frame to entities and Track parts |
|---|
| 226 | \param deltaT: the time passed since the last frame in milliseconds |
|---|
| 227 | */ |
|---|
| 228 | void World::time_slice (Uint32 deltaT) |
|---|
| 229 | { |
|---|
| 230 | List<WorldEntity> *l; |
|---|
| 231 | WorldEntity* entity; |
|---|
| 232 | float seconds = deltaT; |
|---|
| 233 | |
|---|
| 234 | seconds /= 1000; |
|---|
| 235 | |
|---|
| 236 | l = entities->get_next(); |
|---|
| 237 | while( l != NULL) |
|---|
| 238 | { |
|---|
| 239 | entity = l->get_object(); |
|---|
| 240 | entity->tick (seconds); |
|---|
| 241 | l = l->get_next(); |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | for( int i = 0; i < tracklen; i++) track[i].tick (seconds); |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | /** |
|---|
| 248 | \brief removes level data from memory |
|---|
| 249 | */ |
|---|
| 250 | void World::unload() |
|---|
| 251 | { |
|---|
| 252 | if( pathnodes) delete []pathnodes; |
|---|
| 253 | if( track) delete []pathnodes; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | /** |
|---|
| 257 | \brief loads a simple level for testing purposes |
|---|
| 258 | */ |
|---|
| 259 | void World::load_debug_level() |
|---|
| 260 | { |
|---|
| 261 | // create some path nodes |
|---|
| 262 | pathnodes = new Vector[6]; |
|---|
| 263 | pathnodes[0] = Vector(0, 0, 0); |
|---|
| 264 | pathnodes[1] = Vector(-100, 40, 0); |
|---|
| 265 | pathnodes[2] = Vector(-100, 140, 0); |
|---|
| 266 | pathnodes[3] = Vector(0, 180, 0); |
|---|
| 267 | pathnodes[4] = Vector(100, 140, 0); |
|---|
| 268 | pathnodes[5] = Vector(100, 40, 0); |
|---|
| 269 | |
|---|
| 270 | // create the tracks |
|---|
| 271 | tracklen = 6; |
|---|
| 272 | track = new Track[6]; |
|---|
| 273 | for( int i = 0; i < tracklen; i++) |
|---|
| 274 | { |
|---|
| 275 | track[i] = Track( i, (i+1)%tracklen, &pathnodes[i], &pathnodes[(i+1)%tracklen]); |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | // create a player |
|---|
| 279 | WorldEntity* myPlayer = (WorldEntity*) spawn<Player>(); |
|---|
| 280 | |
|---|
| 281 | // bind input |
|---|
| 282 | Orxonox *orx = Orxonox::getInstance(); |
|---|
| 283 | orx->get_localinput()->bind (myPlayer); |
|---|
| 284 | |
|---|
| 285 | // bind camera |
|---|
| 286 | orx->get_camera()->bind (myPlayer); |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | /** |
|---|
| 290 | \brief calls the correct mapping function to convert a given "look at"-Location to a |
|---|
| 291 | Camera Placement |
|---|
| 292 | */ |
|---|
| 293 | void World::calc_camera_pos (Location* loc, Placement* plc) |
|---|
| 294 | { |
|---|
| 295 | track[loc->part].map_camera (loc, plc); |
|---|
| 296 | } |
|---|