2023-10-20 19:31:12 +08:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Xml;
|
2023-10-24 23:37:59 +08:00
|
|
|
using BITFALL.Combat;
|
|
|
|
using BITKit.Animations;
|
2023-10-20 19:31:12 +08:00
|
|
|
using BITKit.Entities.Melee;
|
2024-04-19 00:40:34 +08:00
|
|
|
using BITKit.Sensors;
|
2023-10-20 19:31:12 +08:00
|
|
|
using Unity.Mathematics;
|
|
|
|
using UnityEngine;
|
2024-02-21 01:40:53 +08:00
|
|
|
|
2023-10-20 19:31:12 +08:00
|
|
|
namespace BITKit.Entities
|
|
|
|
{
|
2023-10-24 23:37:59 +08:00
|
|
|
[CustomType(typeof(IMeleeCombat))]
|
2024-04-19 00:40:34 +08:00
|
|
|
public class EntityMelee : EntityBehavior, IMeleeCombat,IMeleeService
|
2023-10-20 19:31:12 +08:00
|
|
|
{
|
2024-04-19 00:40:34 +08:00
|
|
|
[Header(Constant.Header.Settings)]
|
|
|
|
[SerializeField] private bool debug;
|
2024-02-21 01:40:53 +08:00
|
|
|
[SerializeField] private int damage = 50;
|
|
|
|
[SerializeField] private Tag ignoreTags;
|
|
|
|
[SerializeField] private bool allowAnimationEvent;
|
|
|
|
[SerializeField] private bool allowAIAnimationEvent;
|
2024-04-19 00:40:34 +08:00
|
|
|
|
|
|
|
[Header(Constant.Header.Components)]
|
|
|
|
[SerializeField] private RangeSensor rangeSensor;
|
|
|
|
|
2024-02-21 01:40:53 +08:00
|
|
|
[SerializeReference, SubclassSelector, Inject(true)]
|
|
|
|
private IMeleeService meleeService;
|
|
|
|
|
2024-03-29 00:58:24 +08:00
|
|
|
[Inject(true)] private IHealth _health;
|
|
|
|
|
2023-10-24 23:37:59 +08:00
|
|
|
[Inject(true)] private IEntityOverride entityOverride;
|
2024-02-21 01:40:53 +08:00
|
|
|
private readonly IntervalUpdate disableMelee = new(0.64f);
|
|
|
|
|
2024-04-19 00:40:34 +08:00
|
|
|
private readonly DoubleBuffer<MeleeConfig> _configBuffer = new();
|
2023-10-20 19:31:12 +08:00
|
|
|
public override void OnStart()
|
|
|
|
{
|
2023-11-15 23:54:54 +08:00
|
|
|
if (entityOverride is not null)
|
|
|
|
{
|
|
|
|
entityOverride.OnOverride += OnOverride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnOverride(bool obj)
|
|
|
|
{
|
2024-02-21 01:40:53 +08:00
|
|
|
// disableMelee.Reset();
|
2023-10-20 19:31:12 +08:00
|
|
|
}
|
2023-10-24 23:37:59 +08:00
|
|
|
|
2024-02-21 01:40:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
public void AnimationEvent(string actionName)
|
2023-10-24 23:37:59 +08:00
|
|
|
{
|
2024-02-21 01:40:53 +08:00
|
|
|
if (debug)
|
2023-10-24 23:37:59 +08:00
|
|
|
{
|
2024-02-21 01:40:53 +08:00
|
|
|
BIT4Log.Log($"{name} AnimationEvent {actionName}");
|
2023-10-24 23:37:59 +08:00
|
|
|
}
|
2024-02-21 01:40:53 +08:00
|
|
|
|
|
|
|
if (allowAnimationEvent) AnimationEventInternal(actionName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void AIAnimationEvent(string actionName)
|
|
|
|
{
|
|
|
|
if (debug)
|
2023-10-24 23:37:59 +08:00
|
|
|
{
|
2024-02-21 01:40:53 +08:00
|
|
|
BIT4Log.Log($"{name} AIAnimationEvent {actionName}");
|
2023-10-24 23:37:59 +08:00
|
|
|
}
|
2024-02-21 01:40:53 +08:00
|
|
|
|
|
|
|
if (allowAIAnimationEvent) AnimationEventInternal(actionName);
|
2023-10-24 23:37:59 +08:00
|
|
|
}
|
2024-02-21 01:40:53 +08:00
|
|
|
|
|
|
|
private void AnimationEventInternal(string actionName)
|
2023-10-20 19:31:12 +08:00
|
|
|
{
|
2024-03-29 00:58:24 +08:00
|
|
|
if (_health is { IsAlive: false }) return;
|
2023-10-20 19:31:12 +08:00
|
|
|
switch (actionName)
|
|
|
|
{
|
|
|
|
case "Melee":
|
2023-12-26 20:07:19 +08:00
|
|
|
Melee(damage);
|
2023-10-20 19:31:12 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-02-21 01:40:53 +08:00
|
|
|
|
2023-10-20 19:31:12 +08:00
|
|
|
private void Melee(int _damage)
|
|
|
|
{
|
2024-02-21 01:40:53 +08:00
|
|
|
if (disableMelee.AllowUpdateWithoutReset is false)
|
|
|
|
{
|
|
|
|
if (debug)
|
|
|
|
BIT4Log.Log("Melee is disabled");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-31 18:07:15 +08:00
|
|
|
var forward = Transform.forward;
|
2024-04-19 00:40:34 +08:00
|
|
|
|
|
|
|
var isConfig = _configBuffer.TryGetRelease(out var config);
|
|
|
|
|
2023-10-20 19:31:12 +08:00
|
|
|
meleeService.Melee(new MeleeCommand()
|
|
|
|
{
|
|
|
|
PlayerId = Entity.Id,
|
2024-04-19 00:40:34 +08:00
|
|
|
Position = isConfig ? config.Position :Transform.position + Vector3.up,
|
2023-10-31 18:07:15 +08:00
|
|
|
Force = forward * 128,
|
2024-04-19 00:40:34 +08:00
|
|
|
Range = 2.4f,
|
2023-10-20 19:31:12 +08:00
|
|
|
Damage = _damage,
|
2024-04-19 00:40:34 +08:00
|
|
|
Forward = isConfig ? (Quaternion)config.Rotation * Vector3.forward : forward,
|
2024-02-21 01:40:53 +08:00
|
|
|
IgnoreTags = ignoreTags.GetTags(),
|
2023-10-20 19:31:12 +08:00
|
|
|
});
|
2024-02-21 01:40:53 +08:00
|
|
|
if (debug)
|
|
|
|
BIT4Log.Log("Melee Command Queued");
|
2023-10-24 23:37:59 +08:00
|
|
|
}
|
|
|
|
public void Execute() => Melee(damage);
|
2024-02-21 01:40:53 +08:00
|
|
|
|
2023-10-24 23:37:59 +08:00
|
|
|
public void HitStun()
|
|
|
|
{
|
2024-02-21 01:40:53 +08:00
|
|
|
UnityEntity.Invoke(Constant.Animation.Play, nameof(HitStun));
|
2023-10-20 19:31:12 +08:00
|
|
|
}
|
2024-04-19 00:40:34 +08:00
|
|
|
public void SetConfig(MeleeConfig config)
|
|
|
|
{
|
|
|
|
_configBuffer.Release(config);
|
|
|
|
}
|
|
|
|
public void Melee(MeleeCommand command)=>meleeService.Melee(command);
|
2023-10-20 19:31:12 +08:00
|
|
|
}
|
|
|
|
}
|