Skip to content

Component system

Julien Pires edited this page Aug 2, 2013 · 7 revisions

HomeModules overview ▸ Component system

Introduction

The component system provides tools to create data driven video games.

How it works

With a component system, application are data driven. This results in an architecture using aggregation over inheritance. A component is an aggregation of data. Game object doesn't have to be inherited to define specific object type. By adding specific component to a game object you define what the object is able to do. For example, a car is composed of a motor component and a wheel component where we define 4 wheels. For a motorbike, you also have a motor component and a wheel component where we define 2 wheels.

A common implementation of a component system add all behavior code in the component. In Pulsar, a component contains only data and behavior is programmed in a component system. A component system is bind to one or more type of component and define a behavior specifically for this component.

All game objects are managed by a GameObjectManager class. This class is responsible for notifying component system about added or removed component. If you don't add your game object in a game object manager, their components wouldn't be taken in account.

Here is an example of creating a game object:

GameObjectManager goManager = new GameObjectManager();
ComponentHandlerSystem compSys = new ComponentHandlerSystem (goManager); // this object listens the game object manager
GameObject myGo = new GameObject(1); // ID should be generated by yourself
goManager.Add(myGo);

MyAwsomeComponent myComp = new MyAwsomeComponent();
myGo.Add(myComp);

Now everytime you add a component to a game object, manager will notify component system about this component.

As said previously all logics is contained in a component system. The engine provides a base class for component system called ComponentHandler.

public abstract class ComponentHandler : IDisposable

You have to inherits from this class and implements three methods:

// update the component handler
public abstract void Tick(GameTime time);

// called when a new component is added on a game object
public abstract void Register(Component compo);

// called when a component is removed from a game object
public abstract bool Unregister(Component compo);

The ComponentHandlerSystem manages all ComponentHandler instances. It sends them added/removed component according to what type of components they listen for and it updates them.

A ComponentHandler can listens for specific components via ComponentTypes property. This property is just an array of Type object. The default value is null. A null value is interpreted by the system as "Listen to all components".

An example of component handler implementation:

public class MyAwsomeHandler : ComponentHandler 
{
    public MyAwsomeHandler()
    {
       this.componentTypes = new Type[]{ typeof(MyAwsomeComponent) };
    }

    public override void Tick(GameTime time){ // My awsome logic }

    public override void Register(Component comp) { // Store it in memory, play with it, eat it, do what you want }

    public override bool Unregister(Component comp) { // Same thing as Register method }
}

Now you have to add it in a ComponentHandler system:

GameObjectManager goManager = new GameObjectManager();
ComponentHandlerSystem compSys = new ComponentHandlerSystem (goManager);
MyAwsomeHandler myHnd = new MyAwsomeHandler();

compSys.add(myHnd);

That's all. The ComponentHandlerSystem will take care of it. The only thing you have to do is call update method of GameObjectManager and ComponentHandlerSystem each frame (or less depending on what you want to do).