[C++]Zmiana poziomu/manager sceny.

0

Witam,
chciałbym zrobić grę, w której będą zmieniać się poziomy. Wpadłem na pomysł, aby zrobić to tak:

int main(void) {
  //init
  //load assets

  while(true) {
    if(stan == cos_tam) {
      //draw level
    }
    if(stan == cos_innego) {
      //draw another level
    }
  }

  //unload assets
  //destroy window
  //close app
}

Tylko wydaje mi się to niezbyt optymalne, bo wszystkie assety trzeba załadować od razu przy uruchamianiu aplikacji.
Drugim pomysłem jest to:

 
int main(void) {
  //init
  //load assets for level one
  while(true) {
    //level1
  }
  //unload assets for level 1
  //load assets for level 2
  while(true) {
    //level2
  }
  //unload assets for level 2
  //destroy window
  //close app
}

To też nie wydaje mi się zbyt eleganckie... Jak wy radzicie sobie z tym w swoich projektach?

0
const unsigned MAX_LEVEL=100;

int main()
  {
   //init
   bool GameOver=false;
   unsigned L=0;
   while((++L<=MAX_LEVEL)&&(!GameOver))
     {
      //load assets for level(L)
      bool LevelDone=false;
      while((!GameOver)&&(!LevelDone))
        {
         //level(L)
        }
      //unload assets for level(L)
     }
  //destroy window
  //close app
  return 0;
 }
0

Ciekawa metoda, a da się jakoś inaczej?

0

Problemem tej metody jest brak możliwości wyboru konkretnego poziomu ;(

0

Dodajesz bool OnlyOneLevel i ustawiasz L na inną wartość niż 0.

0

A jakiś obiektowy przykład?

0
int main()
  {
   LeveList LL;
   LL<<Level(1,20,300);
   LL<<Level(2,25,350);
   LL.SetLevel(0);
   Game G(LL,ScreenAllegro());
   G.run();
   return 0;
  }
2

Na interfejsie byłoby tak:

// interfejs
class ILevel {
public:
  virtual void load(...) = 0;
  virtual void unload(...) = 0;
  virtual void run(...) = 0;
};

// tworzysz poziomy
class Level0 {
 void load(...) { blebleble; }
 void unload(...) { blebleble; }
 void run(...) { blebleble; }
};

class Level1 {
public:
  bleblebleble
};

itd blebleble


int main()
{
  // tworzysz instancje poziomów
  ILevel* levels[] = {
    new Level0, new Level1, new Level2, itd...
  };
  const int levels_num = sizeof(levels) / sizeof(levels[0]);
  
  int start_level = 0;

  // wykonujesz poziomy
  int i;
  for(i = start_level; i < levels_num; ++i)
  {
    levels[i]->load(...);
    levels[i]->run(...);
    levels[i]->unload(...);
  }
  
  // usuwasz instancje poziomów
  for(i = 0; i < levels_num; ++i) delete levels[i];
}

to samo tylko nie obiektowo, czyli w oparciu o tablicę pointerów do funkcji

void Level0(...) {
  // loading
  blebleble  
  
  // run
  blebleble

  // unloading
  blebleble
}

void Level1(...) {
  itd... blebleble
}

int main()
{
  typedef void(*LevelFunctionType)(...);
  LevelFunctionType levels[] = { Level0, Level1, itd }; // tablica pointerów to funkcji
  const int levels_num = sizeof(levels) / sizeof(levels[0]);

  int i, start_level = 0;
  for(i = start_level; i < levels_num; ++i)
    levels[i](); // wywołanie funkcji z tablicy pointerów
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1