Inheritance vs file reading

Hi!

I have been working on a small 2d tower defense project and I'm going back and forth with which way to go when it comes to towers and monsters.

One way of doing it is to use inheritance, i.e Skeleton, Rat, Goblin as subclasses to Monster class and same with towers.

The other way is to have one Tower class and one Monster and then have files that holds information about those i.e a file called Goblin.xml that has health, speed, bounty, etc. for Goblin.


I did the entire thing as reading from files and it went really good, until it came to animation. Since each tower has different types of animations, textures, etc. I couldn't go for general programming. I bought these assets online and therefor can't change the textures.

My question is;
Is having each tower and each monster on a file or as a class? I.e Goblin.xml or Goblin.cpp/hpp?

Edit:
Also, anyone know what is usually used in larger games such as Diablo for instance? Do they have every monster/character class as a C++ class or do they read information from files?

Thanks in advance,
~Leona
Last edited on
Since each tower has different types of animations, textures, etc., I couldn't go for general programming

Why couldn't you add a "texture" entry to your file?

The problem with inheritance for game entities is that game entities are not often naturally hierarchical. There's a tendency to mix the traits of subclasses in ways that don't conform to any hierarchy.

Imagine a game with goblins, demons, and shamans. Pretend that the shamans are demons who raise other demons from the dead. So far a hierarchy might make sense, but what happens if there should also be goblin shamans, or some goblins borrow fire-resistance from the demons?

This isn't any good, because every time you create a new type that isn't neatly one kind of entity or another, you have to rework the inheritance hierarchy.

I've had some luck in the past with a structure somewhat like this:
http://www.cplusplus.com/forum/general/232926/
Which is pretty amenable to creating entities dynamically. You can use a file, if you want. One simple file format might list the entity name and then the components it contains. A sophisticated approach could even involve a scripting language.

Keep in mind that's just a toy example, and there are plenty of ways to do this depending on what you need. Do a search for "game component system" or "entity component system" - there are plenty of designs out there.
Last edited on
Thank you for the answer!

The thing with my textures for towers are like some of the towers have a texture spritesheet with the base only, and then other spritesheets for the weapon to be on top of the base. I.e I have a spritesheet with a stone tower, and then 8 spritesheets with the weapon facing 8 directions that need to be placed on top of the tower. Then some towers have one spritesheet for the entire thing, base and weapon. If all of the towers had the same texture structure it would be much easier to just create the same system for all and then just load the correct texture to the correct tower and everything would work smoothly.

I never heard of ECS so I'm definitely going to check that one up. Thanks for the advice!

~Leona
Topic archived. No new replies allowed.