54 lines
1.5 KiB
C#
54 lines
1.5 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using UnityEngine;
|
|
|
|
namespace BITKit.Entities
|
|
{
|
|
public class LocalPlayerComponent : EntityComponent
|
|
{
|
|
public override Type BaseType => typeof(LocalPlayerComponent);
|
|
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();
|
|
}
|
|
}
|
|
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();
|
|
}
|
|
}
|
|
}
|