I am trying to use a component based pattern on my new project. The last projects I did the old fashioned way. Base class and derive from that until your brain hurts. This time I wanted to do it on a new way with a component approach.
Component Base Summary: The idea is that you build components and compose your objects out of different components e.g. the Player class holds GraphicComponent, PhysicComponent, InputComponent, CameraComponent, ScriptComponent and so on, but an decoration object like a plant only has GraphicComponent and maybe PhysicComponent.
I like this approach. I tried to implement it but I have a little understanding issue.
As far as I understand it, each component has the logic for itself in it, and barely know of other components. But this is the part I don't understand: Let's say we have a RenderComponent which holds the logic for rendering. But each concrete object like a player, a plant an UI-element and so on has to be rendered on a completly different way. The player hast special effects and different animations, the plant is only a sprite and the ui element is a combination of both. Do I put the logic for every different rendering in the RenderComponent all at once e.g. RenderPlayer()
, RenderPlant()
, RenderUIElement()
which does not make sense to me or do I make an Interface out of the RenderComponent and create a specific RenderComponent for each concrete objects like PlayerRenderComponent : IRenderComponenent
?
Update
After reading the Answer from Nick, I did a quick UML-likish diagram to clearify if I got the idea/structure right. The diagram is just an example with a pseudo UML-like diagram an some pseudo example constelations. The Idea is, that the concrete composition objects holds and treats their component as a data structure. They set it, alter it, and read from it to do it's logics. They also can compare Components of other concrete objects for convience and provice their components to higher systems (like managers)
Update 2 After some discussion and a bit of confusion due opposites arguments, I started to google with new keywords I got from the comments and found an article which helped me understand how the system is glued together.