Author Topic: How to deal with the hugantous Level class?  (Read 5418 times)

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
How to deal with the hugantous Level class?
« on: June 23, 2016, 07:06:56 AM »
In Teemu the Level class contains everything for every level theme which makes it really large. I have the same problem in Kaduria also. I think the obvious way to break up Level is inheriting from simple generic class to more complex Level types that have specific features. But are there other ways? I guess the level structure (terrain) generation could be detached from the main class, but it also has to have some kind of inheritance to different types. The problem is also that you would need to create some kind of "clue" map for game object generation rather than create objects with the terrain which is not very practical since objects are in the Level class.

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: How to deal with the hugantous Level class?
« Reply #1 on: June 25, 2016, 12:32:11 PM »
Inheritance route seems to work ok. It was actually quite easy to change it. All I did was change theme creation switch-case to a virtual function Create_Structure which chooses from Level (base class) themes and then started to inherit level types to derived classes. The first one is the island. Another change was the way levels are created, because you need to create Island type to call the proper virtual function obviously. Since there were only two 'new Level' calls it was easy to create a factory routine for them:

Code: [Select]
Level *Game_World::Get_New_Level_From_Factory(int gen_id, int set_id, int theme)
{
T_Level_Theme t(theme);
const int w=t.Get_Width();
const int h=t.Get_Height();

Level *rv;
switch (theme)
{
case thIsland: rv=new Island(gen_id, set_id, w, h); break;
default:
rv=new Level(gen_id, set_id, theme, w, h);
break;
}

return rv;
}