Skip to content

Commit fc54f1b

Browse files
committed
Have updated tests and batches etc
This should now be ready for the next release.
1 parent da39a7f commit fc54f1b

File tree

27 files changed

+406
-37
lines changed

27 files changed

+406
-37
lines changed

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 2.0.{build}
1+
version: 3.0.{build}
22
branches:
33
only:
44
- master

build/pack.bat

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
set version=1.0.0
1+
set version=3.0.0
22
dotnet pack ../src/EcsRx.MicroRx -c Release -o ../../_dist /p:version=%version%
33
dotnet pack ../src/EcsRx -c Release -o ../../_dist /p:version=%version%
4-
dotnet pack ../src/EcsRx.Systems -c Release -o ../../_dist /p:version=%version%
5-
dotnet pack ../src/EcsRx.Views -c Release -o ../../_dist /p:version=%version%
4+
dotnet pack ../src/EcsRx.Plugins.ReactiveSystems -c Release -o ../../_dist /p:version=%version%
5+
dotnet pack ../src/EcsRx.Plugins.Views -c Release -o ../../_dist /p:version=%version%
6+
dotnet pack ../src/EcsRx.Plugins.Computeds -c Release -o ../../_dist /p:version=%version%
7+
dotnet pack ../src/EcsRx.Plugins.Batching -c Release -o ../../_dist /p:version=%version%
68
dotnet pack ../src/EcsRx.Infrastructure -c Release -o ../../_dist /p:version=%version%
79
dotnet pack ../src/EcsRx.Infrastructure.Ninject -c Release -o ../../_dist /p:version=%version%
810
dotnet pack ../src/EcsRx.ReactiveData -c Release -o ../../_dist /p:version=%version%
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using EcsRx.Components.Lookups;
3+
using EcsRx.Examples.Application;
4+
using EcsRx.Examples.ExampleApps.BatchedGroupExample.Blueprints;
5+
using EcsRx.Examples.ExampleApps.BatchedGroupExample.Components;
6+
using EcsRx.Examples.ExampleApps.BatchedGroupExample.Modules;
7+
using EcsRx.Examples.ExampleApps.HelloWorldExample.Components;
8+
using EcsRx.Extensions;
9+
using EcsRx.Infrastructure.Extensions;
10+
11+
namespace EcsRx.Examples.ExampleApps.BatchedGroupExample
12+
{
13+
public class BatchedGroupExampleApplication : EcsRxConsoleApplication
14+
{
15+
private bool _quit;
16+
private int _entityCount = 2;
17+
18+
protected override void LoadModules()
19+
{
20+
base.LoadModules();
21+
Container.LoadModule<CustomComponentLookupsModule>();
22+
}
23+
24+
protected override void ApplicationStarted()
25+
{
26+
var blueprint = new MoveableBlueprint();
27+
28+
var defaultPool = EntityCollectionManager.GetCollection();
29+
30+
for (var i = 0; i < _entityCount; i++)
31+
{ defaultPool.CreateEntity(blueprint); }
32+
33+
HandleInput();
34+
}
35+
36+
private void HandleInput()
37+
{
38+
while (!_quit)
39+
{
40+
var keyPressed = Console.ReadKey();
41+
if (keyPressed.Key == ConsoleKey.Escape)
42+
{ _quit = true; }
43+
}
44+
}
45+
}
46+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using EcsRx.Blueprints;
3+
using EcsRx.Components.Lookups;
4+
using EcsRx.Entities;
5+
using EcsRx.Extensions;
6+
using EcsRx.Examples.ExampleApps.BatchedGroupExample.Components;
7+
using EcsRx.Examples.ExampleApps.BatchedGroupExample.Lookups;
8+
9+
namespace EcsRx.Examples.ExampleApps.BatchedGroupExample.Blueprints
10+
{
11+
public class MoveableBlueprint : IBlueprint
12+
{
13+
private const float MinimumMovementSpeed = 1;
14+
private const float MaximumMovementSpeed = 5;
15+
16+
private readonly Random _random = new Random();
17+
18+
public void Apply(IEntity entity)
19+
{
20+
entity.AddComponent(new NameComponent {Name = $"BatchedEntity-{entity.Id}"});
21+
entity.AddComponent<PositionComponent>(ComponentLookupTypes.PositionComponentId);
22+
23+
ref var movementSpeedComponent = ref entity.AddComponent<MovementSpeedComponent>(ComponentLookupTypes.MovementSpeedComponentId);
24+
movementSpeedComponent.Speed = (float)_random.NextDouble() * (MaximumMovementSpeed - MinimumMovementSpeed) + MinimumMovementSpeed;
25+
}
26+
}
27+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using EcsRx.Components;
2+
3+
namespace EcsRx.Examples.ExampleApps.BatchedGroupExample.Components
4+
{
5+
public struct MovementSpeedComponent : IComponent
6+
{
7+
public float Speed;
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using EcsRx.Components;
2+
3+
namespace EcsRx.Examples.ExampleApps.BatchedGroupExample.Components
4+
{
5+
public class NameComponent : IComponent
6+
{
7+
public string Name { get; set; }
8+
}
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Numerics;
2+
using EcsRx.Components;
3+
4+
namespace EcsRx.Examples.ExampleApps.BatchedGroupExample.Components
5+
{
6+
public struct PositionComponent : IComponent
7+
{
8+
public Vector3 Position;
9+
}
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace EcsRx.Examples.ExampleApps.BatchedGroupExample.Lookups
2+
{
3+
public static class ComponentLookupTypes
4+
{
5+
public static int NameComponentId = 0;
6+
public static int PositionComponentId = 1;
7+
public static int MovementSpeedComponentId = 2;
8+
}
9+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using EcsRx.Components.Lookups;
4+
using EcsRx.Examples.ExampleApps.BatchedGroupExample.Components;
5+
using EcsRx.Examples.ExampleApps.BatchedGroupExample.Lookups;
6+
using EcsRx.Infrastructure.Dependencies;
7+
using EcsRx.Infrastructure.Extensions;
8+
9+
namespace EcsRx.Examples.ExampleApps.BatchedGroupExample.Modules
10+
{
11+
public class CustomComponentLookupsModule : IDependencyModule
12+
{
13+
public void Setup(IDependencyContainer container)
14+
{
15+
container.Unbind<IComponentTypeLookup>();
16+
var explicitTypeLookups = new Dictionary<Type, int>
17+
{
18+
{typeof(NameComponent), ComponentLookupTypes.NameComponentId},
19+
{typeof(PositionComponent), ComponentLookupTypes.PositionComponentId},
20+
{typeof(MovementSpeedComponent), ComponentLookupTypes.MovementSpeedComponentId}
21+
};
22+
var explicitComponentLookup = new ComponentTypeLookup(explicitTypeLookups);
23+
container.Bind<IComponentTypeLookup>(new BindingConfiguration{ToInstance = explicitComponentLookup});
24+
}
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Numerics;
3+
using System.Reactive.Linq;
4+
using EcsRx.Components.Database;
5+
using EcsRx.Components.Lookups;
6+
using EcsRx.Examples.ExampleApps.BatchedGroupExample.Components;
7+
using EcsRx.Plugins.Batching.Factories;
8+
using EcsRx.Plugins.Batching.Systems;
9+
using EcsRx.Threading;
10+
11+
namespace EcsRx.Examples.ExampleApps.BatchedGroupExample.Systems
12+
{
13+
public class BatchedMovementSystem : BatchedSystem<PositionComponent, MovementSpeedComponent>
14+
{
15+
public BatchedMovementSystem(IComponentDatabase componentDatabase, IComponentTypeLookup componentTypeLookup, IBatchBuilderFactory batchBuilderFactory, IThreadHandler threadHandler) : base(componentDatabase, componentTypeLookup, batchBuilderFactory, threadHandler)
16+
{}
17+
18+
protected override IObservable<bool> ReactWhen()
19+
{ return Observable.Interval(TimeSpan.FromSeconds(0.5f)).Select(x => true); }
20+
21+
protected override void Process(int entityId, ref PositionComponent positionComponent, ref MovementSpeedComponent movementSpeedComponent)
22+
{
23+
positionComponent.Position += Vector3.One * movementSpeedComponent.Speed;
24+
}
25+
}
26+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Reactive.Linq;
3+
using EcsRx.Blueprints;
4+
using EcsRx.Collections;
5+
using EcsRx.Examples.ExampleApps.BatchedGroupExample.Blueprints;
6+
using EcsRx.Groups;
7+
using EcsRx.Groups.Observable;
8+
using EcsRx.Systems;
9+
10+
namespace EcsRx.Examples.ExampleApps.BatchedGroupExample.Systems
11+
{
12+
public class SpawnerSystem : IManualSystem
13+
{
14+
private IDisposable _sub;
15+
private IBlueprint _blueprint = new MoveableBlueprint();
16+
17+
public IGroup Group { get; } = new EmptyGroup();
18+
public IEntityCollection DefaultCollection { get; }
19+
20+
public SpawnerSystem(IEntityCollectionManager collectionManager)
21+
{ DefaultCollection = collectionManager.GetCollection(); }
22+
23+
public void StartSystem(IObservableGroup observableGroup)
24+
{ _sub = Observable.Interval(TimeSpan.FromSeconds(2)).Subscribe(x => Spawn()); }
25+
26+
public void Spawn()
27+
{ DefaultCollection.CreateEntity(_blueprint); }
28+
29+
public void StopSystem(IObservableGroup observableGroup)
30+
{ _sub.Dispose(); }
31+
}
32+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Reactive.Linq;
3+
using EcsRx.Components.Lookups;
4+
using EcsRx.Entities;
5+
using EcsRx.Examples.ExampleApps.BatchedGroupExample.Components;
6+
using EcsRx.Examples.ExampleApps.BatchedGroupExample.Lookups;
7+
using EcsRx.Extensions;
8+
using EcsRx.Groups;
9+
using EcsRx.Groups.Observable;
10+
using EcsRx.Plugins.ReactiveSystems.Systems;
11+
12+
namespace EcsRx.Examples.ExampleApps.BatchedGroupExample.Systems
13+
{
14+
public class LoggingSystem : IReactToGroupExSystem
15+
{
16+
public IGroup Group { get; } = new Group(typeof(NameComponent), typeof(PositionComponent));
17+
18+
public IObservable<IObservableGroup> ReactToGroup(IObservableGroup observableGroup)
19+
{ return Observable.Interval(TimeSpan.FromSeconds(1)).Select(x => observableGroup); }
20+
21+
public void Process(IEntity entity)
22+
{
23+
var nameComponent = entity.GetComponent<NameComponent>();
24+
var positionComponent = entity.GetComponent<PositionComponent>(ComponentLookupTypes.PositionComponentId);
25+
Console.WriteLine($"{nameComponent.Name} - {positionComponent.Position}");
26+
}
27+
28+
public void BeforeProcessing()
29+
{
30+
Console.SetCursorPosition(0,0);
31+
Console.Clear();
32+
}
33+
34+
public void AfterProcessing() {}
35+
}
36+
}

src/EcsRx.Examples/Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using EcsRx.Examples.ExampleApps.BatchedGroupExample;
23
using EcsRx.Examples.ExampleApps.ComputedGroupExample;
34
using EcsRx.Examples.ExampleApps.HealthExample;
45
using EcsRx.Examples.ExampleApps.HelloWorldExample;
@@ -13,9 +14,10 @@ class Program
1314
static void Main(string[] args)
1415
{
1516
// Sample examples
16-
new HelloWorldExampleApplication().StartApplication();
17+
//new HelloWorldExampleApplication().StartApplication();
1718
//new ComputedGroupExampleApplication().StartApplication();
1819
//new HealthExampleApplication().StartApplication();
20+
new BatchedGroupExampleApplication().StartApplication();
1921

2022
// Performance examples
2123
//new SimpleSystemApplication().StartApplication();

src/EcsRx.Plugins.Batching/EcsRx.Plugins.Batching.csproj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.0</TargetFramework>
4+
<Version>0.0.0</Version>
5+
<TargetFrameworks>netstandard2.0;net46</TargetFrameworks>
6+
<Title>EcsRx.Plugins.Batching</Title>
7+
<Authors>Grofit (LP)</Authors>
8+
<PackageLicenseUrl>https://github.com/ecsrx/ecsrx/blob/master/LICENSE</PackageLicenseUrl>
9+
<PackageProjectUrl>https://github.com/ecsrx/ecsrx</PackageProjectUrl>
10+
<Description>EcsRx plugin to allow batching of components in systems</Description>
11+
<PackageTags>ecs rx reactive patterns ioc game-development xna monogame unity</PackageTags>
12+
<LangVersion>latest</LangVersion>
513
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
614
<LangVersion>7.3</LangVersion>
715
</PropertyGroup>

src/EcsRx.Plugins.Batching/Systems/BatchedSystem.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ public abstract unsafe class BatchedSystem<T1, T2> : ManualBatchedSystem
1414
where T2 : unmanaged, IComponent
1515
{
1616
public override IGroup Group { get; } = new Group(typeof(T1), typeof(T2));
17-
private IBatchBuilder<T1, T2> _batchBuilder;
18-
private Batch<T1, T2>[] _batches;
17+
18+
private readonly IBatchBuilder<T1, T2> _batchBuilder;
19+
protected Batch<T1, T2>[] _batches;
1920

2021
protected abstract void Process(int entityId, ref T1 component1, ref T2 component2);
2122

@@ -56,8 +57,8 @@ public abstract unsafe class BatchedSystem<T1, T2, T3> : ManualBatchedSystem
5657
{
5758
public override IGroup Group { get; } = new Group(typeof(T1), typeof(T2), typeof(T3));
5859

59-
private IBatchBuilder<T1, T2, T3> _batchBuilder;
60-
private Batch<T1, T2, T3>[] _batches;
60+
private readonly IBatchBuilder<T1, T2, T3> _batchBuilder;
61+
protected Batch<T1, T2, T3>[] _batches;
6162

6263
protected abstract void Process(int entityId, ref T1 component1, ref T2 component2, ref T3 component3);
6364

@@ -99,8 +100,8 @@ public abstract unsafe class BatchedSystem<T1, T2, T3, T4> : ManualBatchedSystem
99100
{
100101
public override IGroup Group { get; } = new Group(typeof(T1), typeof(T2), typeof(T3), typeof(T4));
101102

102-
private IBatchBuilder<T1, T2, T3, T4> _batchBuilder;
103-
private Batch<T1, T2, T3, T4>[] _batches;
103+
private readonly IBatchBuilder<T1, T2, T3, T4> _batchBuilder;
104+
protected Batch<T1, T2, T3, T4>[] _batches;
104105

105106
protected abstract void Process(int entityId, ref T1 component1, ref T2 component2, ref T3 component3, ref T4 component4);
106107

@@ -145,8 +146,8 @@ public abstract unsafe class BatchedSystem<T1, T2, T3, T4, T5> : ManualBatchedSy
145146
{
146147
public override IGroup Group { get; } = new Group(typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5));
147148

148-
private IBatchBuilder<T1, T2, T3, T4, T5> _batchBuilder;
149-
private Batch<T1, T2, T3, T4, T5>[] _batches;
149+
private readonly IBatchBuilder<T1, T2, T3, T4, T5> _batchBuilder;
150+
protected Batch<T1, T2, T3, T4, T5>[] _batches;
150151

151152
protected abstract void Process(int entityId, ref T1 component1, ref T2 component2, ref T3 component3, ref T4 component4, ref T5 component5);
152153

@@ -192,8 +193,8 @@ public abstract unsafe class BatchedSystem<T1, T2, T3, T4, T5, T6> : ManualBatch
192193
{
193194
public override IGroup Group { get; } = new Group(typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(T6));
194195

195-
private IBatchBuilder<T1, T2, T3, T4, T5, T6> _batchBuilder;
196-
private Batch<T1, T2, T3, T4, T5, T6>[] _batches;
196+
private readonly IBatchBuilder<T1, T2, T3, T4, T5, T6> _batchBuilder;
197+
protected Batch<T1, T2, T3, T4, T5, T6>[] _batches;
197198

198199
protected abstract void Process(int entityId, ref T1 component1, ref T2 component2, ref T3 component3, ref T4 component4, ref T5 component5, ref T6 component6);
199200

src/EcsRx.Plugins.Batching/Systems/ManualBatchedSystem.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ protected ManualBatchedSystem(IComponentDatabase componentDatabase, IComponentTy
3333
}
3434

3535
protected abstract void RebuildBatch();
36+
protected abstract IObservable<bool> ReactWhen();
37+
38+
protected virtual void BeforeProcessing(){}
39+
protected virtual void AfterProcessing(){}
3640
protected abstract void ProcessBatch();
37-
protected abstract IObservable<int> ReactWhen();
3841

3942
public virtual void StartSystem(IObservableGroup observableGroup)
4043
{
@@ -46,11 +49,18 @@ public virtual void StartSystem(IObservableGroup observableGroup)
4649
ObservableGroup.OnEntityRemoved.Subscribe(_ => RebuildBatch()).AddTo(subscriptions);
4750

4851
RebuildBatch();
49-
ReactWhen().Subscribe(_ => ProcessBatch()).AddTo(subscriptions);
52+
ReactWhen().Subscribe(_ => RunBatch()).AddTo(subscriptions);
5053

5154
Subscriptions = subscriptions;
5255
}
5356

57+
private void RunBatch()
58+
{
59+
BeforeProcessing();
60+
ProcessBatch();
61+
AfterProcessing();
62+
}
63+
5464
public virtual void StopSystem(IObservableGroup observableGroup)
5565
{ Subscriptions.Dispose(); }
5666
}

0 commit comments

Comments
 (0)