Skip to content

Defining Entity Components

Christian Oeing edited this page May 2, 2017 · 2 revisions

In the Slash Framework, entity components are defined by implementing the IEntityComponent interface:

[InspectorComponent]
public class HealthComponent : IEntityComponent
{
    /// <summary>
    ///   Attribute: How much damage the entity can take.
    /// </summary>
    public const string AttributeHealth = "HealthComponent.Health";

    /// <summary>
    ///   Attribute default: How much damage the entity can take.
    /// </summary>
    public const int DefaultHealth = 25;

    /// <summary>
    ///   How much damage the entity can take.
    /// </summary>
    [InspectorProperty(
        Name = AttributeHealth,
        Description = "How much damage the entity can take",
        Default = DefaultHealth)]
    public int Health { get; set; }

    /// <summary>
    ///   Initializes this component with the data stored in the specified
    ///   attribute table.
    /// </summary>
    /// <param name="attributeTable">Component data.</param>
    public void InitComponent(IAttributeTable attributeTable)
    {
        this.Health = attributeTable.GetIntOrDefault(AttributeHealth, DefaultHealth);
    }
}

Here, the AttributeHealth constant is used as key for storing the health value in dictionaries and files. The Health property is the actual health value of the entity which is accessed and modified by game systems during the game. The InitComponent method is used for initializing the entity when it is created, using data read from an XML file, for example.