Skip to content

Commit e8fabd0

Browse files
committed
Have updated readme to give simple examples of systems and components
1 parent 64f5cfc commit e8fabd0

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

README.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,59 @@ It is advised to look at the examples, which show the [bare bones required setup
3434

3535
If you are using unity it is recommended you just ignore everything here and use the instructions on the [ecsrx.unity repository](ttps://github.com/grofit/ecsrx.unity).
3636

37+
### Simple components
38+
39+
```csharp
40+
public class HealthComponent : IComponent
41+
{
42+
public int CurrentHealth { get; set; }
43+
public int MaxHealth { get; set; }
44+
}
45+
```
46+
47+
You implement the `IComponent` interface which marks the class as a component, and you can optionally implement `IDisposable` if you want to dispose stuff like so:
48+
49+
```csharp
50+
public class HealthComponent : IComponent, IDisposable
51+
{
52+
public ReactiveProperty<int> CurrentHealth { get; set; }
53+
public int MaxHealth { get; set; }
54+
55+
public HealthComponent()
56+
{ CurrentHealth = new ReactiveProperty<int>(); }
57+
58+
public void Dispose()
59+
{ CurrentHealth.Dispose; }
60+
}
61+
```
62+
63+
Any component which is marked with `IDisposable` will be auto disposed of by entities.
64+
65+
### Simple systems
66+
67+
```csharp
68+
public class CheckForDeathSystem : IReactToEntitySystem
69+
{
70+
public IGroup TargetGroup => new Group(typeof(HealthComponent)); // Get any entities with health component
71+
72+
public IObservable<IEntity> ReactToEntity(IEntity entity) // Explain when you want to execute
73+
{
74+
var healthComponent = entity.GetComponent<HealthComponent>();
75+
return healthComponent.CurrentHealth.Where(x => x <= 0).Select(x => entity);
76+
}
77+
78+
public void Execute(IEntity entity) // Logic run whenever the above reaction occurs
79+
{
80+
entity.RemoveComponent<HealthComponent>();
81+
entity.AddComponent<IsDeadComponent>();
82+
}
83+
}
84+
```
85+
86+
Systems are conventional, so there are many built in types like `IReactToEntitySystem`, `IReactToGroupSystem`, `IManualSystem` and many others, but you can read about them in the [docs/systems](docs/systems.md), you can add your own conventional systems by extending `ISystem` and systems are handled for you by the `ISystemExecutor`.
87+
88+
Check the examples for more use cases, and the unity flavour of ecsrx which has more examples and demo projects, and drop into the gitter channel to ask any questions.
89+
3790
## Running Examples
3891

3992
If you want to run the examples then just clone it and open examples project in the `src` folder, then run the examples, I will try to add to as the library matures.
@@ -42,7 +95,7 @@ There are also a suite of tests which are being expanded as the project grows, i
4295

4396
## Docs
4497

45-
See the docs folder for more information. (This will grow)
98+
See the [docs folder](docs) for more information. (This will grow)
4699

47100
[build-status-image]: https://travis-ci.org/grofit/ecsrx.svg
48101
[build-status-url]: https://travis-ci.org/grofit/ecsrx

0 commit comments

Comments
 (0)