2023-09-01 14:33:54 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading;
|
|
|
|
using BITKit;
|
|
|
|
using BITKit.Entities;
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace BITFALL.Player.Survival
|
|
|
|
{
|
2023-10-20 19:31:12 +08:00
|
|
|
[Serializable]
|
|
|
|
public struct SurvivalDamage : IDamageType
|
|
|
|
{
|
|
|
|
public IPlayerSurvivalElement element;
|
|
|
|
public string Message;
|
|
|
|
}
|
2023-10-02 23:24:56 +08:00
|
|
|
[CustomType(typeof(IPlayerSurvivalService))]
|
|
|
|
public class PlayerSurvivalService : EntityComponent, IPlayerSurvivalService
|
2023-09-01 14:33:54 +08:00
|
|
|
{
|
2023-10-02 23:24:56 +08:00
|
|
|
public IPlayerSurvivalElement[] Elements { get; set; } = Array.Empty<IPlayerSurvivalElement>();
|
|
|
|
[SerializeReference, SubclassSelector] private IPlayerSurvivalElement[] initialElements = Array.Empty<IPlayerSurvivalElement>();
|
2023-10-20 19:31:12 +08:00
|
|
|
private readonly IntervalUpdate _interval = new(1);
|
|
|
|
[SerializeReference,SubclassSelector,Inject] private IDamageService _damageService;
|
|
|
|
[Inject]
|
|
|
|
private IHealth _health;
|
2023-09-01 14:33:54 +08:00
|
|
|
public override void OnAwake()
|
|
|
|
{
|
2023-10-02 23:24:56 +08:00
|
|
|
Elements = initialElements;
|
2023-10-24 23:37:59 +08:00
|
|
|
_health.OnSetAlive += OnSetAlive;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnSetAlive(bool obj)
|
|
|
|
{
|
|
|
|
if (obj)
|
|
|
|
{
|
|
|
|
foreach (var VARIABLE in Elements)
|
|
|
|
{
|
|
|
|
VARIABLE.Value = 100;
|
|
|
|
}
|
|
|
|
}
|
2023-09-01 14:33:54 +08:00
|
|
|
}
|
2023-10-02 23:24:56 +08:00
|
|
|
|
|
|
|
public override void OnUpdate(float deltaTime)
|
2023-09-01 14:33:54 +08:00
|
|
|
{
|
2023-10-02 23:24:56 +08:00
|
|
|
if (_interval.AllowUpdate is false) return;
|
2023-10-24 23:37:59 +08:00
|
|
|
if (_health.IsAlive is false) return;
|
2023-10-02 23:24:56 +08:00
|
|
|
foreach (var x in Elements)
|
2023-09-01 14:33:54 +08:00
|
|
|
{
|
2023-10-02 23:24:56 +08:00
|
|
|
x.Value -= 1;
|
2023-10-20 19:31:12 +08:00
|
|
|
if (x.Value <= 0)
|
|
|
|
{
|
|
|
|
_damageService.Execute(new DamageMessage()
|
|
|
|
{
|
2023-10-24 23:37:59 +08:00
|
|
|
DamageType = new SurvivalDamage()
|
2023-10-20 19:31:12 +08:00
|
|
|
{
|
|
|
|
element = x,
|
|
|
|
},
|
2023-10-24 23:37:59 +08:00
|
|
|
Target = entity,
|
|
|
|
Damage = 1,
|
2023-10-20 19:31:12 +08:00
|
|
|
});
|
|
|
|
}
|
2023-09-01 14:33:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|