66 lines
1.5 KiB
C#
66 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
|
|
{
|
|
[Serializable]
|
|
public struct SurvivalDamage : IDamageType
|
|
{
|
|
public IPlayerSurvivalElement element;
|
|
public string Message;
|
|
}
|
|
[CustomType(typeof(IPlayerSurvivalService))]
|
|
public class PlayerSurvivalService : EntityBehavior, IPlayerSurvivalService
|
|
{
|
|
public IPlayerSurvivalElement[] Elements { get; set; } = Array.Empty<IPlayerSurvivalElement>();
|
|
[SerializeReference, SubclassSelector] private IPlayerSurvivalElement[] initialElements = Array.Empty<IPlayerSurvivalElement>();
|
|
private readonly IntervalUpdate _interval = new(1);
|
|
[SerializeReference,SubclassSelector,Inject] private IDamageService _damageService;
|
|
[Inject]
|
|
private IHealth _health;
|
|
public override void OnAwake()
|
|
{
|
|
Elements = initialElements;
|
|
_health.OnSetAlive += OnSetAlive;
|
|
}
|
|
|
|
private void OnSetAlive(bool obj)
|
|
{
|
|
if (obj)
|
|
{
|
|
foreach (var VARIABLE in Elements)
|
|
{
|
|
VARIABLE.Value = 100;
|
|
}
|
|
}
|
|
}
|
|
|
|
public override void OnUpdate(float deltaTime)
|
|
{
|
|
if (_interval.AllowUpdate is false) return;
|
|
if (_health.IsAlive is false) return;
|
|
foreach (var x in Elements)
|
|
{
|
|
x.Value -= 1;
|
|
if (x.Value <= 0)
|
|
{
|
|
_damageService.Execute(new DamageMessage()
|
|
{
|
|
DamageType = new SurvivalDamage()
|
|
{
|
|
element = x,
|
|
},
|
|
Target = UnityEntity,
|
|
Damage = 1,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|