Component Based Design (C# Game Engine)

1k Views Asked by At

I'm currently designing a 2D Game Engine in c#, using GDI+ and I've come across a horrible design flaw in my code.

The idea is that my game engine consists of multiple screens, each screen contains multiple game objects and each game object contains multiple components (Just like unity does). Each game object could contain different components compared to the previous game object.

For example, lets say I have a ball that bounces. I would need a physics component, and a transform component.

However, I've realised that some components, rely on other components to work.

In the case I mentioned above the physics component, needs to access the transform component to update and know the balls position, rotation, etc.

So at the moment, my base component class contains a reference to the GameObject it is currently attached too, so that any component can access any other component in the game object.

I just wanted to know whether or not this was the best approach towards my component based engine.

Below is a script for the Sprite Component, which needs to access the game objects position.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameEngine.Components
{
    /// <summary>
    /// 
    /// </summary>
    /// <seealso cref="GameEngine.Components.Component" />
    /// <seealso cref="System.IDisposable" />
    public class Sprite : Component, IDisposable
    {
        /// <summary>
        /// The bitmap the graphics engine renders
        /// </summary>
        private Bitmap bitmap;

        /// <summary>
        /// Initializes a new instance of the <see cref="Sprite"/> class.
        /// </summary>
        /// <param name="bitmap">The bitmap.</param>
        public Sprite(Bitmap bitmap)
        {
            this.bitmap = bitmap;

            this.Events.OnRenderFrame += Events_OnRenderFrame;
        }

        /// <summary>
        /// Handles the OnRenderFrame event of the Events control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RenderEventArgs"/> instance containing the event data.</param>
        private void Events_OnRenderFrame(object sender, RenderEventArgs e)
        {
            // Uses the game objects transform component, to get the position of the object
            e.GraphicsEngine.DrawSprite(this, GameObject.GetComponentOfType<Transform>().Position);
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            bitmap.Dispose();
        }
    }
}
1

There are 1 best solutions below

0
On BEST ANSWER

However, I've realised that some components, rely on other components to work.

In the case I mentioned above the physics component, needs to access the transform component to update and know the balls position, rotation, etc.

Yes its true, but it is not the problem. It is a requirement which cannot be eliminated as some time two or more compoennts work together to achieve an objective. But you can make it more sensible through adding dependent component from your code as Require Component attribute usually use in Unity.

You should need to keeping mind that beside several advantages of this component based model complete independce cannot be achived.

The components should be appropriate for deployment into any suitable environments, thus they should possess minimal dependencies related to other components[They should be, but cannot possible in every circumstances like in your case or unity game-engin]. This ensures that the deployment of a particular component do not affect the whole system in any way.(more)

Note: you have right to disagree with the answer or provide more relent answer that ensure decoupling.