55 lines
1.5 KiB
C#
55 lines
1.5 KiB
C#
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Threading;
|
||
|
using BITKit;
|
||
|
using BITKit.Entities;
|
||
|
using UnityEngine;
|
||
|
|
||
|
namespace BITFALL.Player.Survival
|
||
|
{
|
||
|
[CustomType(typeof(IPlayerSurvival))]
|
||
|
public class PlayerSurvival : EntityComponent, IPlayerSurvival
|
||
|
{
|
||
|
[SerializeReference, SubclassSelector] private IPlayerSurvivalState[] survivalStates;
|
||
|
private readonly IntervalUpdate interval = new(1);
|
||
|
private bool initialized;
|
||
|
private CancellationToken _cancellationToken;
|
||
|
public override void OnAwake()
|
||
|
{
|
||
|
_cancellationToken = entity.Get<CancellationToken>();
|
||
|
foreach (var x in survivalStates)
|
||
|
{
|
||
|
x.OnStateInitialize();
|
||
|
}
|
||
|
}
|
||
|
public override async void OnStart()
|
||
|
{
|
||
|
foreach (var x in survivalStates)
|
||
|
{
|
||
|
await x.OnStateInitializeAsync(_cancellationToken);
|
||
|
}
|
||
|
foreach (var x in survivalStates)
|
||
|
{
|
||
|
x.OnStateInitialized();
|
||
|
}
|
||
|
initialized = true;
|
||
|
}
|
||
|
public override void OnFixedUpdate(float deltaTime)
|
||
|
{
|
||
|
if (interval.AllowUpdate is false || initialized is false) return;
|
||
|
foreach (var x in survivalStates)
|
||
|
{
|
||
|
x.ProcessState();
|
||
|
if(x.TryGetNewEvent(out var newEvent))
|
||
|
OnSurvivalEventOpened?.Invoke(newEvent);
|
||
|
if(x.TryGetClosedEvent(out var closedEvent))
|
||
|
OnSurvivalEventClosed?.Invoke(closedEvent);
|
||
|
}
|
||
|
}
|
||
|
public event Action<IPlayerSurvivalState> OnSurvivalStateChanged;
|
||
|
public event Action<IPlayerSurvivalEvent> OnSurvivalEventOpened;
|
||
|
public event Action<IPlayerSurvivalEvent> OnSurvivalEventClosed;
|
||
|
}
|
||
|
}
|