This commit is contained in:
CortexCore
2025-03-09 13:38:32 +08:00
parent 787ae12bf8
commit 88f1ff1b04
14 changed files with 181 additions and 35 deletions

View File

@@ -3,10 +3,19 @@ using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using BITKit;
using BITKit.Entities;
using BITKit.WorldNode;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Net.Project.B.Health
{
[Serializable]
public class KnockedComponent:IKnockedComponent,IWorldNode{}
public interface IKnockedComponent
{
}
/// <summary>
/// 击倒服务
/// </summary>
@@ -32,19 +41,25 @@ namespace Net.Project.B.Health
public class KnockedService : IKnockedService,IDisposable
{
private readonly IEntitiesService _entitiesService;
private readonly IHealthService _healthService;
private readonly ILogger<KnockedService> _logger;
public KnockedService(IHealthService healthService, ILogger<KnockedService> logger)
public KnockedService(IHealthService healthService, ILogger<KnockedService> logger, IEntitiesService entitiesService)
{
_healthService = healthService;
_logger = logger;
_entitiesService = entitiesService;
_healthService.OnHealthPlus += OnHealthPlus;
_healthService.OnHealthChanged += OnHealthChanged;
}
private int OnHealthPlus(int id, int oldHp, int plus, object sendor)
{
if (_entitiesService.TryGetEntity(id, out var entity) is false) return plus;
if (entity.ServiceProvider.GetService<IKnockedComponent>() is null) return plus;
var isKnocked = _knocked.Contains(id);
if (oldHp < 0) return plus;
if ((oldHp + plus) > -1) return plus;
@@ -57,7 +72,7 @@ namespace Net.Project.B.Health
var nextHealth = currentHealth + plus;
_knockedHealth[id] = nextHealth;
_onKnockedHealthChanged?.Invoke(id, currentHealth, nextHealth);
_logger.LogInformation($"Entity {id} 的击倒生命值减少了,剩余{_knockedHealth[id]}");
// _logger.LogInformation($"Entity {id} 的击倒生命值减少了,剩余{_knockedHealth[id]}");
return 0;
}
_knockedHealth.Set(id,-1);
@@ -66,7 +81,7 @@ namespace Net.Project.B.Health
_knockedHealth.TryAdd(id, 100);
_knocked.Add(id);
_onKnocked?.Invoke(id, true);
_logger.LogInformation($"Entity {id} 被击倒了");
// _logger.LogInformation($"Entity {id} 被击倒了");
return 0;
}
@@ -79,7 +94,7 @@ namespace Net.Project.B.Health
_knockedHealth.TryRemove(arg1,out _);
_onKnocked?.Invoke(arg1, false);
_logger.LogInformation($"Entity {arg1} 自起了");
// _logger.LogInformation($"Entity {arg1} 自起了");
}
private static readonly HashSet<int> _knocked = new();