# CorgEng.EntityComponentSystem
## Contents
[toc]
## Dependancies
[CorgEng.Core](https://hackmd.io/72yS03UwTx2C_aOPm9xapA)
## Summary
The EntityComponentSystem module for CorgEng provides an Entity Component System framework for games built with CorgEng.
## Summary
## Entities
Entities have a way to be referenced and a list of components.
```csharp=
class Entity
{
int EntityId { get; }
List<Component> Components { get; }
}
```
## Components
Components are stores of data that are attached to specific entities. Components hold a reference to the entity object that holds them.
Component instances are individual to the entities.
```csharp=
class Component
{
//The parent of the component, the entity holding this component
Entity Parent { get; }
}
```
## Systems
Systems handle the logic that entities and components require. They respond and handle events, manipulating things when they need to be manipulated.
```csharp=
class System
{
//Called when the system is setup
void SystemSetup();
}
```
## Signals / Events
```csharp=
class GenericEvent : Event
{
//Events can contain any data required
public int variable { get; set; } = 0;
}
```
### Event Handling
Internally, event handling is handled with a relay-like effect.
When a component is added to an entity, the component checks if it needs to register any signals for that entity.
If a signal is registered that targets the component type added, then the component will observe the entity's signal callback.
When a signal is sent to an entity, a lambda function on the component is raised, which calls the systems event handler method, injecting the component that was targetted.
```csharp=
internal Dictionary<EventComponentPair, List<InternalSignalHandleDelegate>> EventListeners { get; } = null;
```
Entities with components have a dictionary on them. In the future this should be altered in some way due to the memory overhead of dictionaries.
### Registering for an event
Registering for an event requires 3 things:
- The component
- The event that is being registered
- The event handler delegate function to be called when the event is raised
```csharp=
void Setup()
{
RegisterLocalEvent<GenericComponent, GenericEvent>(HandleGenericEvent);
}
void HandleGenericEvent(Entity entity, GenericComponent component, GenericEvent raisedEvent)
{
//Entity contains the entity that the event was raised on
//Component contains the component that the event was raised on
//Generic event contains the event information that was raised
}
```
### Raising an event
Raising an event involves creating a new event object, and then calling the Raise method with the parameter indicating the entity to target the event towards.
```csharp=
Entity target = ...;
GenericEvent genericEvent = new GenericEvent();
genericEvent.Raise(target);
```
The raise method will get all components that are listening for this event, and then call the associated delegate with the parameters entity(target), the component listening and the event object.
### Registering event implementation
```csharp=
void RegisterLocalEvent<Component, Event>(Func<Entity, Component, Event> eventHandler)
{
//attach the event handler to the EventHandler with the key Event.
}
```