56 lines
1.6 KiB
C#
56 lines
1.6 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using UnityEngine;
|
|
|
|
namespace BITKit.Entities.Player
|
|
{
|
|
public class LocalPlayerBehavior : EntityBehavior
|
|
{
|
|
private IEntityPlayerComponent[] playerComponents;
|
|
private CancellationTokenSource initializeCancellationTokenSource;
|
|
private CancellationTokenSource disposeCancellationTokenSource;
|
|
public override async void OnStart()
|
|
{
|
|
initializeCancellationTokenSource = new CancellationTokenSource();
|
|
disposeCancellationTokenSource = new CancellationTokenSource();
|
|
playerComponents = GetComponentsInChildren<IEntityPlayerComponent>(true);
|
|
|
|
foreach (var x in playerComponents)
|
|
{
|
|
x.OnPlayerInitialize();
|
|
}
|
|
foreach (var x in playerComponents)
|
|
{
|
|
initializeCancellationTokenSource.Token.ThrowIfCancellationRequested();
|
|
await x.OnPlayerInitializedAsync(initializeCancellationTokenSource.Token);
|
|
}
|
|
foreach (var x in playerComponents)
|
|
{
|
|
x.OnPlayerInitialized();
|
|
}
|
|
UnityPlayerServiceService.Register((Entity)UnityEntity);
|
|
}
|
|
public override async void OnDestroyComponent()
|
|
{
|
|
initializeCancellationTokenSource?.Cancel();
|
|
foreach (var x in playerComponents)
|
|
{
|
|
x.OnPlayerDispose();
|
|
}
|
|
foreach (var x in playerComponents)
|
|
{
|
|
disposeCancellationTokenSource.Token.ThrowIfCancellationRequested();
|
|
await x.OnPlayerDisposeAsync(disposeCancellationTokenSource.Token);
|
|
}
|
|
foreach (var x in playerComponents)
|
|
{
|
|
x.OnPlayerDisposed();
|
|
}
|
|
disposeCancellationTokenSource.Dispose();
|
|
UnityPlayerServiceService.UnRegister((Entity)UnityEntity);
|
|
}
|
|
}
|
|
}
|