[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 |
---|
[2190] | 14 | co-programmer: Christian Meyer |
---|
[1853] | 15 | */ |
---|
| 16 | |
---|
[2190] | 17 | #include "world.h" |
---|
| 18 | #include "world_entity.h" |
---|
| 19 | #include "collision.h" |
---|
| 20 | #include "track.h" |
---|
[2036] | 21 | #include "player.h" |
---|
[2190] | 22 | #include "command_node.h" |
---|
| 23 | #include "camera.h" |
---|
[2036] | 24 | |
---|
[1856] | 25 | using namespace std; |
---|
[1853] | 26 | |
---|
| 27 | |
---|
[1858] | 28 | /** |
---|
[2551] | 29 | \brief create a new World |
---|
| 30 | |
---|
| 31 | This creates a new empty world! |
---|
[1858] | 32 | */ |
---|
[2190] | 33 | World::World () |
---|
[1855] | 34 | { |
---|
[2551] | 35 | entities = new List<WorldEntity>(); |
---|
[1855] | 36 | } |
---|
| 37 | |
---|
[1858] | 38 | /** |
---|
[2551] | 39 | \brief remove the World from memory |
---|
[1858] | 40 | */ |
---|
[2190] | 41 | World::~World () |
---|
[1872] | 42 | { |
---|
[2551] | 43 | unload (); |
---|
| 44 | delete entities; |
---|
[1872] | 45 | } |
---|
[1858] | 46 | |
---|
[1855] | 47 | /** |
---|
[2551] | 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. |
---|
[1858] | 53 | */ |
---|
[2190] | 54 | void World::collide () |
---|
[1858] | 55 | { |
---|
[2551] | 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) |
---|
[2190] | 65 | { |
---|
[2551] | 66 | b = a->get_next(); |
---|
| 67 | while( b != NULL ) |
---|
| 68 | { |
---|
| 69 | bobj = b->get_object(); |
---|
| 70 | if( bobj->bCollide && bobj->collisioncluster != NULL ) |
---|
[2190] | 71 | { |
---|
[2551] | 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 | } |
---|
[2190] | 80 | } |
---|
[2551] | 81 | b = b->get_next(); |
---|
| 82 | } |
---|
[2190] | 83 | } |
---|
[2551] | 84 | a = a->get_next(); |
---|
| 85 | } |
---|
[1858] | 86 | } |
---|
| 87 | |
---|
| 88 | /** |
---|
[2551] | 89 | \brief runs through all entities calling their draw() methods |
---|
[1931] | 90 | */ |
---|
[2190] | 91 | void World::draw () |
---|
[2077] | 92 | { |
---|
[2551] | 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) |
---|
[2190] | 117 | { |
---|
[2551] | 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); |
---|
[1931] | 124 | } |
---|
[2551] | 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) |
---|
[2190] | 129 | { |
---|
[2551] | 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); |
---|
[2190] | 136 | } |
---|
[2551] | 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) |
---|
[2190] | 141 | { |
---|
[2551] | 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); |
---|
[2190] | 148 | } |
---|
[2551] | 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(); |
---|
[1931] | 160 | } |
---|
| 161 | |
---|
| 162 | /** |
---|
[2551] | 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. |
---|
[1883] | 169 | */ |
---|
[2190] | 170 | void World::update () |
---|
[1883] | 171 | { |
---|
[2551] | 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) ) |
---|
[2190] | 198 | { |
---|
[2551] | 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); |
---|
[2190] | 207 | } |
---|
[2551] | 208 | } |
---|
[2190] | 209 | } |
---|
[2551] | 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 | |
---|
[1883] | 222 | } |
---|
| 223 | |
---|
[2077] | 224 | /** |
---|
[2551] | 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 |
---|
[2077] | 227 | */ |
---|
[2190] | 228 | void World::time_slice (Uint32 deltaT) |
---|
[2077] | 229 | { |
---|
[2551] | 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); |
---|
[2077] | 245 | } |
---|
[1883] | 246 | |
---|
[2190] | 247 | /** |
---|
[2551] | 248 | \brief removes level data from memory |
---|
[1858] | 249 | */ |
---|
[2190] | 250 | void World::unload() |
---|
[1858] | 251 | { |
---|
[2551] | 252 | if( pathnodes) delete []pathnodes; |
---|
| 253 | if( track) delete []pathnodes; |
---|
[1883] | 254 | } |
---|
[1879] | 255 | |
---|
[2190] | 256 | /** |
---|
[2551] | 257 | \brief loads a simple level for testing purposes |
---|
[1858] | 258 | */ |
---|
[2190] | 259 | void World::load_debug_level() |
---|
[1858] | 260 | { |
---|
[2551] | 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 |
---|
[2190] | 282 | Orxonox *orx = Orxonox::getInstance(); |
---|
| 283 | orx->get_localinput()->bind (myPlayer); |
---|
[1900] | 284 | |
---|
[2551] | 285 | // bind camera |
---|
| 286 | orx->get_camera()->bind (myPlayer); |
---|
[1899] | 287 | } |
---|
| 288 | |
---|
[2190] | 289 | /** |
---|
[2551] | 290 | \brief calls the correct mapping function to convert a given "look at"-Location to a |
---|
| 291 | Camera Placement |
---|
[1855] | 292 | */ |
---|
[2190] | 293 | void World::calc_camera_pos (Location* loc, Placement* plc) |
---|
[1855] | 294 | { |
---|
[2190] | 295 | track[loc->part].map_camera (loc, plc); |
---|
[1855] | 296 | } |
---|