Alternatives to multiple inheritance for my architecture (NPCs in a Realtime Strategy game)?
Posted
by
Brettetete
on Programmers
See other posts from Programmers
or by Brettetete
Published on 2014-08-22T18:47:58Z
Indexed on
2014/08/22
22:32 UTC
Read the original article
Hit count: 269
Coding isn't that hard actually. The hard part is to write code that makes sense, is readable and understandable. So I want to get a better developer and create some solid architecture.
So I want to do create an architecture for NPCs in a video-game. It is a Realtime Strategy game like Starcraft, Age of Empires, Command & Conquers, etc etc.. So I'll have different kinds of NPCs. A NPC can have one to many abilities (methods) of these: Build()
, Farm()
and Attack()
.
Examples:
Worker can Build()
and Farm()
Warrior can Attack()
Citizen can Build()
, Farm()
and Attack()
Fisherman can Farm()
and Attack()
I hope everything is clear so far.
So now I do have my NPC Types and their abilities. But lets come to the technical / programmatical aspect.
What would be a good programmatic architecture for my different kinds of NPCs?
Okay I could have a base class. Actually I think this is a good way to stick with the DRY principle. So I can have methods like WalkTo(x,y)
in my base class since every NPC will be able to move. But now lets come to the real problem. Where do I implement my abilities? (remember: Build()
, Farm()
and Attack()
)
Since the abilities will consists of the same logic it would be annoying / break DRY principle to implement them for each NPC (Worker,Warrior, ..).
Okay I could implement the abilities within the base class. This would require some kind of logic that verifies if a NPC can use ability X. IsBuilder
, CanBuild
, .. I think it is clear what I want to express.
But I don't feel very well with this idea. This sounds like a bloated base class with too much functionality.
I do use C# as programming language. So multiple inheritance isn't an opinion here. Means: Having extra base classes like Fisherman : Farmer, Attacker
won't work.
© Programmers or respective owner