[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 |
---|
[2068] | 14 | co-programmer: Christian Meyer |
---|
[1853] | 15 | */ |
---|
| 16 | |
---|
| 17 | #include "world.h" |
---|
[2101] | 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" |
---|
[1853] | 24 | |
---|
[1856] | 25 | using namespace std; |
---|
[1853] | 26 | |
---|
| 27 | |
---|
[1858] | 28 | /** |
---|
| 29 | \brief Create a new World |
---|
| 30 | |
---|
| 31 | This creates a new empty world! |
---|
| 32 | */ |
---|
[2068] | 33 | World::World () |
---|
[1855] | 34 | { |
---|
[2101] | 35 | entities = new List<WorldEntity>(); |
---|
[1855] | 36 | } |
---|
| 37 | |
---|
| 38 | |
---|
[2068] | 39 | World::~World () |
---|
[1872] | 40 | { |
---|
[2080] | 41 | unload (); |
---|
[2068] | 42 | delete entities; |
---|
[1872] | 43 | } |
---|
[1858] | 44 | |
---|
[2101] | 45 | void World::collide () |
---|
[1858] | 46 | { |
---|
[2068] | 47 | List<WorldEntity> *a, *b; |
---|
| 48 | WorldEntity *aobj, *bobj; |
---|
| 49 | |
---|
| 50 | a = entities->get_next(); |
---|
| 51 | |
---|
| 52 | while( a != NULL) |
---|
[1883] | 53 | { |
---|
[2068] | 54 | aobj = a->get_object(); |
---|
| 55 | if( aobj->bCollide && aobj->collisioncluster != NULL) |
---|
| 56 | { |
---|
| 57 | b = a->get_next(); |
---|
| 58 | while( b != NULL) |
---|
| 59 | { |
---|
| 60 | bobj = b->get_object(); |
---|
| 61 | if( bobj->bCollide && bobj->collisioncluster != NULL) |
---|
| 62 | { |
---|
[2101] | 63 | unsigned long ahitflg, bhitflg; |
---|
| 64 | if( check_collision ( &aobj->place, aobj->collisioncluster, &ahitflg, &bobj->place, bobj->collisioncluster, &bhitflg)); |
---|
[2068] | 65 | { |
---|
| 66 | aobj->collide (bobj, ahitflg, bhitflg); |
---|
| 67 | bobj->collide (aobj, bhitflg, ahitflg); |
---|
| 68 | } |
---|
| 69 | } |
---|
| 70 | b = b->get_next(); |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | a = a->get_next(); |
---|
[1883] | 74 | } |
---|
| 75 | } |
---|
[1879] | 76 | |
---|
[2068] | 77 | void World::draw () |
---|
[1883] | 78 | { |
---|
[2068] | 79 | // draw geometry |
---|
| 80 | |
---|
| 81 | // draw entities |
---|
| 82 | List<WorldEntity> *l; |
---|
| 83 | WorldEntity* entity; |
---|
| 84 | |
---|
| 85 | l = entities->get_next(); |
---|
| 86 | while( l != NULL) |
---|
| 87 | { |
---|
| 88 | entity = l->get_object(); |
---|
| 89 | if( entity->bDraw) entity->draw(); |
---|
| 90 | l = l->get_next(); |
---|
[1883] | 91 | } |
---|
[1858] | 92 | } |
---|
| 93 | |
---|
[2068] | 94 | void World::update () |
---|
[1931] | 95 | { |
---|
[2080] | 96 | List<WorldEntity> *l; |
---|
| 97 | WorldEntity* entity; |
---|
| 98 | Location* loc; |
---|
| 99 | Placement* plc; |
---|
| 100 | Uint32 t; |
---|
| 101 | |
---|
| 102 | l = entities->get_next(); |
---|
| 103 | while( l != NULL) |
---|
| 104 | { |
---|
| 105 | entity = l->get_object(); |
---|
| 106 | |
---|
[2101] | 107 | if( !entity->isFree()) |
---|
[2080] | 108 | { |
---|
| 109 | loc = entity->get_location(); |
---|
| 110 | plc = entity->get_placement(); |
---|
| 111 | t = loc->part; |
---|
| 112 | |
---|
| 113 | if( t >= tracklen) |
---|
| 114 | { |
---|
| 115 | printf("An entity is out of the game area\n"); |
---|
| 116 | entity->left_world (); |
---|
| 117 | } |
---|
| 118 | else |
---|
| 119 | { |
---|
| 120 | while( track[t].map_coords( loc, plc)) |
---|
| 121 | { |
---|
[2101] | 122 | track[t].post_leave (entity); |
---|
[2080] | 123 | if( loc->part >= tracklen) |
---|
| 124 | { |
---|
| 125 | printf("An entity has left the game area\n"); |
---|
| 126 | entity->left_world (); |
---|
| 127 | break; |
---|
| 128 | } |
---|
[2101] | 129 | track[loc->part].post_enter (entity); |
---|
[2080] | 130 | } |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | else |
---|
| 134 | { |
---|
| 135 | // TO DO: implement check whether this particular free entity is out of the game area |
---|
| 136 | // TO DO: call function to notify the entity that it left the game area |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | l = l->get_next(); |
---|
| 140 | } |
---|
| 141 | |
---|
[1931] | 142 | } |
---|
| 143 | |
---|
[2068] | 144 | void World::time_slice (Uint32 deltaT) |
---|
[1858] | 145 | { |
---|
[2068] | 146 | List<WorldEntity> *l; |
---|
| 147 | WorldEntity* entity; |
---|
| 148 | float seconds = deltaT; |
---|
| 149 | |
---|
| 150 | seconds /= 1000; |
---|
| 151 | |
---|
| 152 | l = entities->get_next(); |
---|
| 153 | while( l != NULL) |
---|
| 154 | { |
---|
| 155 | entity = l->get_object(); |
---|
| 156 | entity->tick (seconds); |
---|
| 157 | l = l->get_next(); |
---|
[1899] | 158 | } |
---|
[2080] | 159 | |
---|
[2101] | 160 | for( int i = 0; i < tracklen; i++) track[i].tick (seconds); |
---|
[2080] | 161 | } |
---|
| 162 | |
---|
| 163 | void World::unload() |
---|
| 164 | { |
---|
| 165 | if( pathnodes) delete []pathnodes; |
---|
| 166 | if( track) delete []pathnodes; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | void World::load_debug_level() |
---|
| 170 | { |
---|
| 171 | // create some path nodes |
---|
| 172 | pathnodes = new Vector[6]; |
---|
| 173 | pathnodes[0] = Vector(0, 0, 0); |
---|
| 174 | pathnodes[1] = Vector(-100, 40, 0); |
---|
| 175 | pathnodes[2] = Vector(-100, 140, 0); |
---|
| 176 | pathnodes[3] = Vector(0, 180, 0); |
---|
| 177 | pathnodes[4] = Vector(100, 140, 0); |
---|
| 178 | pathnodes[5] = Vector(100, 40, 0); |
---|
| 179 | |
---|
| 180 | // create the tracks |
---|
| 181 | tracklen = 6; |
---|
| 182 | track = new Track[6]; |
---|
| 183 | for( int i = 0; i < tracklen; i++) |
---|
| 184 | { |
---|
| 185 | track[i] = Track( i, (i+1)%tracklen, &pathnodes[i], &pathnodes[(i+1)%tracklen]); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | // create a player |
---|
[2101] | 189 | WorldEntity* myPlayer = (WorldEntity*) spawn<Player>(); |
---|
[2080] | 190 | |
---|
| 191 | // bind input |
---|
[2096] | 192 | Orxonox *orx = Orxonox::getInstance(); |
---|
| 193 | orx->get_localinput()->bind (myPlayer); |
---|
| 194 | |
---|
[2080] | 195 | // bind camera |
---|
[2096] | 196 | orx->get_camera()->bind (myPlayer); |
---|
[2101] | 197 | } |
---|
[2104] | 198 | |
---|
| 199 | void World::calc_camera_pos (Location* loc, Placement* plc) |
---|
| 200 | { |
---|
| 201 | track[loc->part].map_camera (loc, plc); |
---|
| 202 | } |
---|