BITFALL/Assets/Artists/Scripts/Entities/Melee/EntityMelee.cs

119 lines
3.5 KiB
C#

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