Answer the question
In order to leave comments, you need to log in
How to link two class hierarchies?
Perhaps expressed in one sentence, the question sounds wild. I'll try to make it clearer.
The game is divided into some kind of model and presentation. There is a base class for all material objects in the game: SpaceObject, it has descendants: Planet, Star, Ship, Asteroid, etc. As long as it all "lives" inside this very model, everything is fine. But as soon as there is a need to display an object, problems arise.
So, if an object appears in the same location as the player (there is a corresponding event), it is necessary to create its "alter ego" in the form of a GameObject of the game engine. The problem is in this code:
void InitializeSpaceObject(SpaceObject spaceObject)
{
if (spaceObject is Star)
InitializeStar(spaceObject as Star);
else if (spaceObject is Planet)
InitializePlanet(spaceObject as Planet);
else if (spaceObject is AsteroidField)
InitializeAsteroidField(spaceObject as AsteroidField);
else
throw new UnknownSpaceObjectTypeException("Cannot initialize SpaceObject of type " + spaceObject.GetType().ToString() + ": unknown type.");
}
Answer the question
In order to leave comments, you need to log in
If you have to live with the separation of architectural layers that you mentioned and cannot change anything: the first option "type" - "initializer" I see as the most convenient. You still have to add mapping when adding a new type of object.
In general, of course, you can continue the perversion and use reflection and make auto-mapping of the "model" classes onto the GameObject heirs. For example, look for all "model" classes in a certain namespace and look for the same ones in another (which, in turn, will be inherited from GameObject).
In general, in a sense, you go against the architecture of the engine itself, because. GameObjects are not only a "view" but also a model. I understand that you probably don't want to create heavy GameObjects when they don't seem to be needed yet (i.e. not in scope), but it's possible that if you don't add them to the scene, they won't load the engine.
In general, I have my doubts that such a separation into model and view when using Unity is a good practice.
Why not add an abstract Initialize method to the parent SpaceObject class, which you override in each child class, and then your code will be reduced to
void InitializeSpaceObject(SpaceObject spaceObject)
{
spaceObject.Initialize();
}
void InitializeSpaceObject(ISpaceObject spaceObject)
{
spaceObject.Initialize();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question