BITFALL/Assets/Artists/Scripts/Player/Medical/PlayerMedicalService.cs

63 lines
1.7 KiB
C#

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using BITKit;
using BITKit.Entities;
using UnityEngine;
namespace BITFALL.Player.Medical
{
[CustomType(typeof(IPlayerMedical))]
public class PlayerMedicalService : EntityBehavior,IPlayerMedical
{
public IPlayerMedicalWound GetWound<T>() where T : IPlayerMedicalWoundType, new()
{
return _wounds[new T()];
}
public bool TryGetWound<T>(out IPlayerMedicalWound wound) where T : IPlayerMedicalWoundType, new()
{
return _wounds.TryGetValue(new T(), out wound);
}
public void AddOrUpdateWound<T>(IPlayerMedicalWound medical) where T : IPlayerMedicalWoundType, new()
{
if(_wounds.TryAdd(new T(), medical))
{
OnWoundAdded?.Invoke(new T(), medical);
}
else
{
_wounds[new T()] = medical;
OnWoundChanged?.Invoke(new T(), medical);
}
}
public void RemoveWound<T>() where T : IPlayerMedicalWoundType, new()
{
RemoveWound(new T());
}
public void RemoveWound(IPlayerMedicalWoundType type)
{
if(_wounds.TryRemove(type, out _))
{
OnWoundRemoved?.Invoke(type, null);
}
}
public IDictionary<IPlayerMedicalWoundType, IPlayerMedicalWound> GetAllWounds()
{
return new Dictionary<IPlayerMedicalWoundType, IPlayerMedicalWound>(_wounds);
}
public event Action<IPlayerMedicalWoundType, IPlayerMedicalWound> OnWoundChanged;
public event Action<IPlayerMedicalWoundType, IPlayerMedicalWound> OnWoundAdded;
public event Action<IPlayerMedicalWoundType, IPlayerMedicalWound> OnWoundRemoved;
private readonly ConcurrentDictionary<IPlayerMedicalWoundType, IPlayerMedicalWound> _wounds = new();
}
}