/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Patrick Boenzli co-programmer: Christian Meyer */ #include "world.h" #include "world_entity.h" #include "collision.h" #include "track.h" #include "player.h" #include "command_node.h" #include "camera.h" #include "environment.h" #include "p_node.h" #include "null_parent.h" using namespace std; /** \brief create a new World This creates a new empty world! */ World::World (char* name) { this->worldName = name; this->debugWorldNr = -1; this->entities = new tList(); } World::World (int worldID) { this->debugWorldNr = worldID; this->worldName = NULL; this->entities = new tList(); } /** \brief remove the World from memory */ World::~World () { printf("World::~World() - deleting current world\n"); CommandNode* cn = Orxonox::getInstance()->getLocalInput(); cn->unbind(this->localPlayer); cn->reset(); this->localCamera->destroy(); WorldEntity* entity = entities->enumerate(); while( entity != NULL ) { entity->destroy(); entity = entities->nextElement(); } this->entities->destroy(); /* FIX the parent list has to be cleared - not possible if we got the old list also*/ //this->nullParent->destroy (); delete this->entities; delete this->localCamera; /* this->localPlayer hasn't to be deleted explicitly, it is contained in entities*/ } ErrorMessage World::init() { this->bPause = false; CommandNode* cn = Orxonox::getInstance()->getLocalInput(); cn->addToWorld(this); cn->enable(true); /* this is only for test purposes */ this->debug (); } ErrorMessage World::start() { printf("World::start() - starting current World: nr %i\n", this->debugWorldNr); this->bQuitOrxonox = false; this->bQuitCurrentGame = false; this->mainLoop(); } ErrorMessage World::stop() { printf("World::stop() - got stop signal\n"); this->bQuitCurrentGame = true; } ErrorMessage World::pause() { this->isPaused = true; } ErrorMessage World::resume() { this->isPaused = false; } void World::destroy() { } void World::load() { if(this->debugWorldNr != -1) { switch(this->debugWorldNr) { /* this loads the hard-coded debug world. this only for simplicity and will be removed by a reald world-loader, which interprets a world-file. if you want to add an own debug world, just add a case DEBUG_WORLD_[nr] and make whatever you want... */ case DEBUG_WORLD_0: { this->nullParent = new NullParent (); // create some path nodes this->pathnodes = new Vector[6]; this->pathnodes[0] = Vector(0, 0, 0); this->pathnodes[1] = Vector(1000, 0, 0); // this->pathnodes[2] = Vector(-100, 140, 0); // this->pathnodes[3] = Vector(0, 180, 0); // this->pathnodes[4] = Vector(100, 140, 0); // this->pathnodes[5] = Vector(100, 40, 0); // create the tracks this->tracklen = 2; this->track = new Track[2]; for( int i = 0; i < this->tracklen; i++) { this->track[i] = Track( i, (i+1)%this->tracklen, &this->pathnodes[i], &this->pathnodes[(i+1)%this->tracklen]); } // !\todo old track-system has to be removed // create a player WorldEntity* myPlayer = new Player(); this->spawn(myPlayer); this->localPlayer = myPlayer; // bind input Orxonox *orx = Orxonox::getInstance(); orx->getLocalInput()->bind (myPlayer); // bind camera this->localCamera = new Camera(this); this->getCamera()->bind (myPlayer); /* Placement* plc = new Placement; plc->r = Vector(100, 10, 10); plc->w = Quaternion(); WorldEntity* env = new Environment(); this->spawn(env, plc); */ break; } case DEBUG_WORLD_1: { this->nullParent = new NullParent (); // create some path nodes this->pathnodes = new Vector[6]; this->pathnodes[0] = Vector(0, 0, 0); this->pathnodes[1] = Vector(20, 10, 10); this->pathnodes[2] = Vector(40, 0, 10); this->pathnodes[3] = Vector(60, 10, 0); this->pathnodes[4] = Vector(80, 20, 10); this->pathnodes[5] = Vector(30, 50, 0); // create the tracks this->tracklen = 6; this->track = new Track[6]; for( int i = 0; i < this->tracklen; i++) { this->track[i] = Track( i, (i+1)%this->tracklen, &this->pathnodes[i], &this->pathnodes[(i+1)%this->tracklen]); } // create a player WorldEntity* myPlayer = new Player(); this->spawn(myPlayer); this->localPlayer = myPlayer; // bind input Orxonox *orx = Orxonox::getInstance(); orx->getLocalInput()->bind (myPlayer); // bind camera this->localCamera = new Camera(this); this->getCamera()->bind (myPlayer); break; } default: printf("World::load() - no world with ID %i found", this->debugWorldNr ); } } else if(this->worldName != NULL) { } // initialize debug coord system objectList = glGenLists(1); glNewList (objectList, GL_COMPILE); glLoadIdentity(); glColor3f(1.0,0,0); glBegin(GL_QUADS); int sizeX = 100; int sizeY = 80; float length = 1000; float width = 200; float widthX = float (length /sizeX); float widthY = float (width /sizeY); float height [sizeX][sizeY]; Vector normal_vectors[sizeX][sizeY]; for ( int i = 0; inextElement(); if( aobj->bCollide && aobj->collisioncluster != NULL) { b = a->nextElement(); while( b != NULL ) { bobj = b->nextElement(); if( bobj->bCollide && bobj->collisioncluster != NULL ) { unsigned long ahitflg, bhitflg; if( check_collision ( &aobj->place, aobj->collisioncluster, &ahitflg, &bobj->place, bobj->collisioncluster, &bhitflg) ); { aobj->collide (bobj, ahitflg, bhitflg); bobj->collide (aobj, bhitflg, ahitflg); } } b = b->nextElement(); } } a = a->enumerate(); } */ } /** \brief runs through all entities calling their draw() methods */ void World::draw () { // draw geometry // draw entities WorldEntity* entity; entity = this->entities->enumerate(); while( entity != NULL ) { if( entity->bDraw ) entity->draw(); entity = this->entities->nextElement(); } // draw debug coord system glCallList (objectList); } /** \brief updates Placements and notifies entities when they left the world This runs trough all WorldEntities and maps Locations to Placements if they are bound, checks whether they left the level boundaries and calls appropriate functions. */ void World::update () { //List *l; WorldEntity* entity; Location* loc; Placement* plc; Uint32 t; // l = entities->enumerate(); entity = this->entities->enumerate(); while( entity != NULL ) { if( !entity->isFree() ) { loc = entity->getLocation(); plc = entity->getPlacement(); t = loc->part; /* check if entity has still a legal track-id */ if( t >= tracklen ) { printf("An entity is out of the game area\n"); entity->leftWorld (); } else { while( track[t].mapCoords( loc, plc) ) { track[t].postLeave (entity); if( loc->part >= tracklen ) { printf("An entity has left the game area\n"); entity->leftWorld (); break; } track[loc->part].postEnter (entity); } } } else { /* \todo: implement check whether this particular free entity is out of the game area \todo: call function to notify the entity that it left the game area */ } entity = entities->nextElement(); } } /** \brief relays the passed time since the last frame to entities and Track parts \param deltaT: the time passed since the last frame in milliseconds */ void World::timeSlice (Uint32 deltaT) { //List *l; WorldEntity* entity; float seconds = deltaT / 1000.0; entity = entities->enumerate(); while( entity != NULL) { entity->tick (seconds); entity = entities->nextElement(); } //for( int i = 0; i < tracklen; i++) track[i].tick (seconds); } /** \brief removes level data from memory */ void World::unload() { if( pathnodes) delete []pathnodes; if( track) delete []pathnodes; } /** \brief calls the correct mapping function to convert a given "look at"-Location to a Camera Placement */ void World::calcCameraPos (Location* loc, Placement* plc) { track[loc->part].mapCamera (loc, plc); } void World::setTrackLen(Uint32 len) { this->tracklen = len; } int World::getTrackLen() { return this->tracklen; } /** \brief function to put your own debug stuff into it. it can display informations about the current class/procedure */ void World::debug() { printf ("World::debug() - starting debug\n"); PNode* p1 = new NullParent (); PNode* p2 = new PNode (new Vector(2, 2, 2), p1); PNode* p3 = new PNode (new Vector(4, 4, 4), p1); PNode* p4 = new PNode (new Vector(6, 6, 6), p2); p1->debug (); p2->debug (); p3->debug (); p4->debug (); p1->shiftCoor (new Vector(-1, -1, -1)); printf("World::debug() - shift\n"); p1->debug (); p2->debug (); p3->debug (); p4->debug (); p1->update (1); printf ("World::debug() - update\n"); p1->debug (); p2->debug (); p3->debug (); p4->debug (); p2->shiftCoor (new Vector(-1, -1, -1)); p1->update (2); p1->debug (); p2->debug (); p3->debug (); p4->debug (); p2->setAbsCoor (new Vector(1,2,3)); p1->update (2); p1->debug (); p2->debug (); p3->debug (); p4->debug (); p1->destroy (); /* WorldEntity* entity; printf("counting all entities\n"); printf("World::debug() - enumerate()\n"); entity = entities->enumerate(); while( entity != NULL ) { if( entity->bDraw ) printf("got an entity\n"); entity = entities->nextElement(); } */ } /* \brief main loop of the world: executing all world relevant function in this loop we synchronize (if networked), handle input events, give the heart-beat to all other member-entities of the world (tick to player, enemies etc.), checking for collisions drawing everything to the screen. */ void World::mainLoop() { this->lastFrame = SDL_GetTicks(); printf("World::mainLoop() - Entering main loop\n"); while( !this->bQuitOrxonox && !this->bQuitCurrentGame) /* \todo implement pause */ { // Network synchronize(); // Process input handleInput(); if( this->bQuitCurrentGame || this->bQuitOrxonox) { printf("World::mainLoop() - leaving loop earlier...\n"); break; } // Process time timeSlice(); // Process collision collision(); // Draw display(); for(int i = 0; i < 10000000; i++) {} } printf("World::mainLoop() - Exiting the main loop\n"); } /** \brief synchronize local data with remote data */ void World::synchronize () { // Get remote input // Update synchronizables } /** \brief run all input processing the command node is the central input event dispatcher. the node uses the even-queue from sdl and has its own event-passing-queue. */ void World::handleInput () { // localinput CommandNode* cn = Orxonox::getInstance()->getLocalInput(); cn->process(); // remoteinput } /** \brief advance the timeline this calculates the time used to process one frame (with all input handling, drawing, etc) the time is mesured in ms and passed to all world-entities and other classes that need a heart-beat. */ void World::timeSlice () { Uint32 currentFrame = SDL_GetTicks(); if(!this->bPause) { Uint32 dt = currentFrame - this->lastFrame; if(dt > 0) { float fps = 1000/dt; printf("fps = %f\n", fps); } else { /* the frame-rate is limited to 100 frames per second, all other things are for nothing. */ printf("fps = 1000 - frame rate is adjusted\n"); SDL_Delay(10); dt = 10; } this->timeSlice (dt); this->update (); this->localCamera->timeSlice(dt); } this->lastFrame = currentFrame; } /** \brief compute collision detection */ void World::collision () { this->collide (); } /** \brief render the current frame clear all buffers and draw the world */ void World::display () { // clear buffer glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // set camera this->localCamera->apply (); // draw world this->draw(); // draw HUD // flip buffers SDL_GL_SwapBuffers(); } /** \brief give back active camera this passes back the actualy active camera \todo ability to define more than one camera or camera-places */ Camera* World::getCamera() { return this->localCamera; } /** \brief add and spawn a new entity to this world \param entity to be added */ void World::spawn(WorldEntity* entity) { Location zeroloc; Location* loc = NULL; WorldEntity* owner; if( this->nullParent != NULL) this->nullParent->addChild (entity); entities->add (entity); zeroloc.dist = 0; zeroloc.part = 0; zeroloc.pos = Vector(); zeroloc.rot = Quaternion(); loc = &zeroloc; entity->init (loc, owner); if (entity->bFree) { this->track[loc->part].mapCoords( loc, entity->getPlacement()); } entity->postSpawn (); } /** \brief add and spawn a new entity to this world \param entity to be added \param location where to add */ void World::spawn(WorldEntity* entity, Location* loc) { if( this->nullParent != NULL) this->nullParent->addChild (entity); Location zeroLoc; WorldEntity* owner; this->entities->add (entity); if( loc == NULL) { zeroLoc.dist = 0; zeroLoc.part = 0; zeroLoc.pos = Vector(); zeroLoc.rot = Quaternion(); loc = &zeroLoc; } entity->init (loc, owner); if (entity->bFree) { this->track[loc->part].mapCoords( loc, entity->getPlacement()); } entity->postSpawn (); //return entity; } /** \brief add and spawn a new entity to this world \param entity to be added \param place where to be added */ void World::spawn(WorldEntity* entity, Placement* plc) { if( this->nullParent != NULL) this->nullParent->addChild (entity); Placement zeroPlc; WorldEntity* owner; if( plc == NULL) { zeroPlc.r = Vector(); zeroPlc.w = Quaternion(); plc = &zeroPlc; } this->entities->add (entity); entity->init (plc, owner); entity->postSpawn (); //return entity; } /* \brief commands that the world must catch \returns false if not used by the world */ bool World::command(Command* cmd) { return false; }