BITFALL/Assets/BITKit/Unity/Scripts/Entity/Player/LocalPlayerComponent.cs

56 lines
1.6 KiB
C#
Raw Normal View History

2023-08-12 01:43:24 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
2023-08-27 02:58:19 +08:00
namespace BITKit.Entities.Player
2023-08-12 01:43:24 +08:00
{
2023-10-30 01:25:53 +08:00
public class LocalPlayerBehavior : EntityBehavior
2023-08-12 01:43:24 +08:00
{
private IEntityPlayerComponent[] playerComponents;
private CancellationTokenSource initializeCancellationTokenSource;
private CancellationTokenSource disposeCancellationTokenSource;
public override async void OnStart()
{
initializeCancellationTokenSource = new CancellationTokenSource();
disposeCancellationTokenSource = new CancellationTokenSource();
playerComponents = GetComponentsInChildren<IEntityPlayerComponent>(true);
2023-08-23 01:59:40 +08:00
2023-08-12 01:43:24 +08:00
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();
}
2023-10-30 01:25:53 +08:00
UnityPlayerServiceService.Register((Entity)UnityEntity);
2023-08-12 01:43:24 +08:00
}
public override async void OnDestroyComponent()
{
2023-08-23 01:59:40 +08:00
initializeCancellationTokenSource?.Cancel();
2023-08-12 01:43:24 +08:00
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();
2023-10-30 01:25:53 +08:00
UnityPlayerServiceService.UnRegister((Entity)UnityEntity);
2023-08-12 01:43:24 +08:00
}
}
}