Created
July 2, 2021 15:43
-
-
Save distantcam/7faee47ec6263946e93b62319d43d796 to your computer and use it in GitHub Desktop.
A small EcsRx + StrongInject example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net5.0</TargetFramework> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="EcsRx" Version="5.0.101" /> | |
<PackageReference Include="EcsRx.Plugins.ReactiveSystems" Version="5.0.101" /> | |
<PackageReference Include="StrongInject" Version="1.2.0" /> | |
<PackageReference Include="System.Reactive" Version="5.0.0" /> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using EcsRx.Collections; | |
using EcsRx.Collections.Database; | |
using EcsRx.Collections.Entity; | |
using EcsRx.Components; | |
using EcsRx.Components.Database; | |
using EcsRx.Components.Lookups; | |
using EcsRx.Entities; | |
using EcsRx.Extensions; | |
using EcsRx.Groups; | |
using EcsRx.Groups.Observable; | |
using EcsRx.Plugins.ReactiveSystems.Handlers; | |
using EcsRx.Plugins.ReactiveSystems.Systems; | |
using EcsRx.Systems.Handlers; | |
using StrongInject; | |
using StrongInject.Modules; | |
using System; | |
using System.Reactive.Linq; | |
using SystemsRx.Events; | |
using SystemsRx.Executor; | |
using SystemsRx.Executor.Handlers; | |
using SystemsRx.Executor.Handlers.Conventional; | |
using SystemsRx.MicroRx.Events; | |
using SystemsRx.Pools; | |
using SystemsRx.Scheduling; | |
using SystemsRx.Threading; | |
class Program | |
{ | |
static void Main() => new Container().Run(app => app.StartApplication()); | |
readonly EcsApplication _ecsApplication; | |
bool _quit; | |
public Program(EcsApplication ecsApplication) | |
{ | |
_ecsApplication = ecsApplication; | |
} | |
public void StartApplication() | |
{ | |
_ecsApplication.SystemExecutor.AddSystem(new TalkingGroupSystem()); | |
var defaultPool = _ecsApplication.EntityDatabase.GetCollection(); | |
var entity = defaultPool.CreateEntity(); | |
var canTalkComponent = new CanTalkComponent("Hello world"); | |
entity.AddComponents(canTalkComponent); | |
HandleInput(); | |
} | |
private void HandleInput() | |
{ | |
while (!_quit) | |
{ | |
var keyPressed = Console.ReadKey(); | |
if (keyPressed.Key == ConsoleKey.Escape) | |
{ _quit = true; } | |
} | |
} | |
} | |
record CanTalkComponent(string Message) : IComponent; | |
class TalkingGroupSystem : IReactToGroupSystem | |
{ | |
public IGroup Group => new Group(typeof(CanTalkComponent)); | |
public IObservable<IObservableGroup> ReactToGroup(IObservableGroup observableGroup) => | |
Observable.Interval(TimeSpan.FromSeconds(2)).Select(x => observableGroup); | |
public void Process(IEntity entity) | |
{ | |
var canTalkComponent = entity.GetComponent<CanTalkComponent>(); | |
Console.WriteLine($"Entity says '{canTalkComponent.Message}' @ {DateTime.Now}"); | |
} | |
} | |
record EcsApplication( | |
ISystemExecutor SystemExecutor, | |
IEventSystem EventSystem, | |
IEntityDatabase EntityDatabase, | |
IObservableGroupManager ObservableGroupManager); | |
[Register(typeof(Program))] | |
[Register(typeof(EcsApplication), Scope.SingleInstance)] | |
[RegisterModule(typeof(CollectionsModule))] | |
[RegisterModule(typeof(FrameworkModule))] | |
[RegisterModule(typeof(EcsRxInfrastructureModule))] | |
[Register(typeof(ReactToGroupSystemHandler), Scope.SingleInstance, typeof(IConventionalSystemHandler))] | |
partial class Container : IContainer<Program> { } | |
[Register(typeof(MessageBroker), Scope.SingleInstance, typeof(IMessageBroker))] | |
[Register(typeof(EventSystem), Scope.SingleInstance, typeof(IEventSystem))] | |
[Register(typeof(DefaultThreadHandler), Scope.SingleInstance, typeof(IThreadHandler))] | |
[Register(typeof(ManualSystemHandler), Scope.SingleInstance, typeof(IConventionalSystemHandler))] | |
[Register(typeof(BasicSystemHandler), Scope.SingleInstance, typeof(IConventionalSystemHandler))] | |
[Register(typeof(ReactToEventSystemHandler), Scope.SingleInstance, typeof(IConventionalSystemHandler))] | |
[Register(typeof(SystemExecutor), Scope.SingleInstance, typeof(ISystemExecutor))] | |
[Register(typeof(DefaultUpdateScheduler), Scope.SingleInstance, typeof(IUpdateScheduler), typeof(ITimeTracker))] | |
class FrameworkModule { } | |
[Register(typeof(IdPool), Scope.SingleInstance, typeof(IIdPool))] | |
[Register(typeof(DefaultEntityFactory), Scope.SingleInstance, typeof(IEntityFactory))] | |
[Register(typeof(DefaultEntityCollectionFactory), Scope.SingleInstance, typeof(IEntityCollectionFactory))] | |
[Register(typeof(EntityDatabase), Scope.SingleInstance, typeof(IEntityDatabase))] | |
[Register(typeof(DefaultObservableObservableGroupFactory), Scope.SingleInstance, typeof(IObservableGroupFactory))] | |
[Register(typeof(ObservableGroupManager), Scope.SingleInstance, typeof(IObservableGroupManager))] | |
[Register(typeof(BasicEntitySystemHandler), Scope.SingleInstance, typeof(IConventionalSystemHandler))] | |
[Register(typeof(DefaultComponentTypeAssigner), Scope.SingleInstance, typeof(IComponentTypeAssigner))] | |
[Register(typeof(ComponentDatabase), Scope.SingleInstance, typeof(IComponentDatabase))] | |
class EcsRxInfrastructureModule | |
{ | |
[Factory(Scope.SingleInstance)] | |
public static IComponentTypeLookup CreateComponentTypeLookup(IComponentTypeAssigner componentTypeAssigner) | |
{ | |
var allComponents = componentTypeAssigner.GenerateComponentLookups(); | |
return new ComponentTypeLookup(allComponents); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment