Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/world.cc @ 2380

Last change on this file since 2380 was 2190, checked in by bensch, 20 years ago

orxonox/trunk: merged and copied all files from branches/chris into trunk. it all seems to be in propper order.

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