0
\$\begingroup\$

My Game become more complex with each new Level or Map that I design. The game is an basic rpg game like Pokemon.

For now I have one class that organize all Maps but different maps have special features and different Monsters.

Better use for each Map an own Class and have duplicated code or is there a specific pattern for this?

Edit: My Problem: I can load the new map and play but all maps have different monsters and different events. I can't put all of them in the Game class and check all the time everything.

my class diagram in simple: enter image description here

\$\endgroup\$
  • \$\begingroup\$ Levels and maps are normally treated as data, not code. Can you clarify what's leading you to consider making a class for each level/map? There are probably ways to achieve this in a data-driven fashion, so you don't need to recompile every time you want to change a level. \$\endgroup\$ – DMGregory Dec 26 '16 at 18:50
  • \$\begingroup\$ The Game class is a static class and just load the map data, let the hero move and more. \$\endgroup\$ – Lirf Dec 26 '16 at 19:22
1
\$\begingroup\$

The problem you have can be solved by Inheritence. Make a base Map class with overridable functions. If you need to, you can extend the Map class for specific behaviors. You don't need to "duplicate code" this way, since you can call a function called Foo() from a child class which will ALSO call Foo() from the parent class.

Should I use a specific class for each map behavior?

In my opinion, yes. If your maps have mostly similar behaviors except for a few things, you can and should use inheritance to your benefit. It's also great for organizing map behaviors and keeping you sane, and your code will look cleaner.

As DMGregory has noted, maps are usually JUST data, so creating a new class for a specific ordering of tiles and objects is not necessary. An array or an XML file can do that for you. Your Map class should ONLY be in charge of doing things with the data, not holding onto the data in the editor. You should have a separate class or file with your map data in it and hand it to your Map class, which will render it and control the update loop. Try not to let you map do too many things at once, though. Because of this, it may be pertinent to rename your Map class to MapController or something similar, so that it's clear what exactly it does. Then you can extend for specific behaviors beyond that like SwampController or DesertController

\$\endgroup\$
  • \$\begingroup\$ I probably can't get around to use specific class for each map behavior. \$\endgroup\$ – Lirf Dec 26 '16 at 19:10
  • \$\begingroup\$ @Lirf Are you saying you can't make specific classes for each map or that you NEED to use a specific class for each map? \$\endgroup\$ – Premier Bromanov Dec 26 '16 at 19:35
  • \$\begingroup\$ I edit my question and put a class diagram. pls check \$\endgroup\$ – Lirf Dec 26 '16 at 19:56
  • 1
    \$\begingroup\$ In many games you can generalize certain map features so far that they can be expressed by data in the map file. Even functionality as complex as NPC behaviors are usually implemented with scripting languages, and that script code is part of the map files. \$\endgroup\$ – Philipp Dec 26 '16 at 20:24
  • \$\begingroup\$ @Lirf If that's the case, the Game class should probably not be loading the monsters and events. You should probably separate that functionaility and let a Map class, or another new class, handle that behavior and use the Game class for character movement and updating objects every frame. \$\endgroup\$ – Premier Bromanov Dec 27 '16 at 1:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.