Application

EcsRxApplication 클래스를 상속하는 Application 클래스의 작성이 필요합니다.

Application.cs

public class Application : EcsRxApplication
{
    protected override void GameStarting()
    {
        RegisterAllBoundSystems();
    }

    protected override void GameStarted()
    {
        ...    
}

애플리케이션 시작시 GameStarting 함수를 호출하고 GameStarted 함수를 함수를 호출합니다.

EcxRxApplication.RegisterAllBoundSystems

EcsRxApplication.cs

protected virtual void RegisterAllBoundSystems()
{
    var allSystems = Container.ResolveAll<ISystem>();

    var orderedSystems = allSystems
        .OrderByDescending(x => x is ViewResolverSystem)
        .ThenByDescending(x => x is ISetupSystem);
    orderedSystems.ForEachRun(SystemExecutor.AddSystem);
}

RegisterAllBoundSystems 에서는 Container에 포함된 모든 System들을 찾아서 ViewResolverSystem과 SetupSystem의 순으로 정렬합니다. 이렇게 정렬하는 이유는 System 클래스의 인스턴스를 생성하고 실행할 때 이들 클래스들이 먼저 실행해야 하는 클래스들이기 때문입니다.

SetupSystem은 Entity의 초기화를 위한 시스템이므로 당연히 다른 System 클래스보다 먼저 실행되어야 합니다.

정렬을 끝낸 다음에 SystemExecutor.AddSystem 함수를 호출, 실제 System 클래스들을 추가하게 됩니다.

SystemExecutor.AddSystem

AddSystem 함수에서는 System 클래스가 상속하는 인터페이스 클래스의 타입에 따라 해당 System 클래스의 작동을 설정하게 됩니다.

System 클래스의 작동은 Rx를 사용해서 스트림 소스를 생성하고 구독하는 처리입니다.

SystemExecutor.cs

public void AddSystem(ISystem system)
{
    _systems.Add(system);
    var subscriptionList = new List<SubscriptionToken>();

    if (system is ISetupSystem)
    {
        var subscriptions = SetupSystemHandler.Setup(system as ISetupSystem);
        subscriptionList.AddRange(subscriptions);
    }

    if (system is IReactToGroupSystem)
    {
        var subscription = ReactToGroupSystemHandler.Setup(system as IReactToGroupSystem);
        subscriptionList.Add(subscription);
    }

    if (system is IReactToEntitySystem)
    {
        var subscriptions = ReactToEntitySystemHandler.Setup(system as IReactToEntitySystem);
        subscriptionList.AddRange(subscriptions);
    }

    if (system.IsReactiveDataSystem())
    {
        var subscriptions = ReactToDataSystemHandler.SetupWithoutType(system);
        subscriptionList.AddRange(subscriptions);
    }

    if (system is IManualSystem)
    { ManualSystemHandler.Start(system as IManualSystem); }

    _systemSubscriptions.Add(system, subscriptionList);
}

EcsRx에서는 모두 다섯가지 형태의 System이 존재합니다.

  • ISetupSystem
  • IReactToGroupSystem
  • IReactToEntitySystem
  • IReactiveDataSystem
  • IManualSystem

이들의 구체적인 내용은 System 챕터에서 자세하게 다룹니다.

results matching ""

    No results matching ""