BITFALL/Assets/Artists/Scripts/Equip/MeleeController.cs

170 lines
4.0 KiB
C#
Raw Normal View History

2023-10-20 19:31:12 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using BITFALL.Items.Melee;
2023-10-24 23:37:59 +08:00
using BITFALL.Combat;
using BITFALL.Player.Movement;
2023-10-20 19:31:12 +08:00
using BITKit;
using BITKit.Entities;
using BITKit.Entities.Melee;
using BITKit.StateMachine;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Interactions;
namespace BITFALL.Entities.Equipment.Melee
{
public interface IPlayerMeleeControllerState : IState
{
}
public abstract class PlayerMeleeControllerState : IPlayerMeleeControllerState
{
2024-02-21 01:40:53 +08:00
[SerializeField] protected MeleeController my;
2023-10-20 19:31:12 +08:00
public virtual bool Enabled { get; set; }
public virtual void Initialize()
{
2024-02-21 01:40:53 +08:00
my.Entity.Inject(this);
2023-10-20 19:31:12 +08:00
}
public virtual void OnStateEntry(IState old)
{
}
public virtual void OnStateUpdate(float deltaTime)
{
}
public virtual void OnStateExit(IState old, IState newState)
{
}
}
2024-02-21 01:40:53 +08:00
public sealed class MeleeController : StateWeaponController<IPlayerMeleeControllerState>
2023-10-20 19:31:12 +08:00
{
[Header(Constant.Header.Input)]
[SerializeField] internal InputActionReference attackAction;
[SerializeField] internal InputActionReference blockAction;
2023-10-20 19:31:12 +08:00
[Header(Constant.Header.Gameobjects)]
[SerializeField] private Transform velocityReference;
2024-02-21 01:40:53 +08:00
[Header(Constant.Header.Services)]
[SerializeReference,SubclassSelector] private IMeleeService _meleeService;
2024-04-06 16:33:57 +08:00
public ScriptableMelee melee => scriptableItem as ScriptableMelee;
2024-02-21 01:40:53 +08:00
2023-10-20 19:31:12 +08:00
private Vector3 _velocity;
private Vector3 _lastPosition;
[Inject]
2024-02-21 01:40:53 +08:00
internal IEntityMovement _movement;
2023-10-29 15:27:13 +08:00
[Inject(true)]
2024-02-21 01:40:53 +08:00
internal IPlayerMovement _playerMovement;
[Inject]
internal IHealth _health;
2023-10-20 19:31:12 +08:00
public override void OnAwake()
{
base.OnAwake();
2023-12-30 17:37:48 +08:00
2023-10-29 15:27:13 +08:00
if (_playerMovement is not null)
_health.OnDamageFactory += OnDamageFactory;
2023-10-24 23:37:59 +08:00
}
2024-02-21 01:40:53 +08:00
2023-10-24 23:37:59 +08:00
private int OnDamageFactory(DamageMessage arg1, int arg2)
{
if (Enabled is false || arg1.Initiator is null) return arg2;
if (CurrentState is not Blocking blocking) return arg2;
var combat = arg1.Initiator.Get<IMeleeCombat>();
if (combat is null) return arg2;
if (blocking.AllowBlockStun && _playerMovement.Stamina >= melee.BlockStaminaCost)
{
arg2 =0;
combat.HitStun();
_playerMovement.Stamina -= melee.BlockStaminaCost*0.5f;
}
else
{
2024-02-21 01:40:53 +08:00
// if (_playerMovement.Stamina > melee.BlockStaminaCost)
// {
// animator.Play(BITConstant.Player.BlockStun);
// arg2 /= 3;
// }
// else
// {
// animator.Play(BITConstant.Player.BlockBreak);
// arg2 /= 2;
// }
2023-10-24 23:37:59 +08:00
_playerMovement.Stamina -= melee.BlockStaminaCost;
}
return arg2;
}
2023-10-20 19:31:12 +08:00
public override void Entry()
{
base.Entry();
2023-10-24 23:37:59 +08:00
2023-10-20 19:31:12 +08:00
TransitionState<Draw>();
}
2023-11-15 23:54:54 +08:00
public override void Exited()
{
base.Exited();
_movement.ExecuteCommand<PlayerPauseRunCommand>(new(this,false));
2024-02-21 01:40:53 +08:00
TransitionState<Empty>();
2023-11-15 23:54:54 +08:00
}
2023-10-20 19:31:12 +08:00
public override void OnUpdate(float deltaTime)
{
base.OnUpdate(deltaTime);
var position = velocityReference.position;
_velocity = (position - _lastPosition).normalized;
_lastPosition = position;
UpdateState(deltaTime);
2023-11-15 23:54:54 +08:00
_movement.ExecuteCommand<PlayerPauseRunCommand>(new(this,CurrentState is not Idle));
2023-10-20 19:31:12 +08:00
}
2024-02-21 01:40:53 +08:00
public override void AnimationEvent(string eventName)
{
base.AnimationEvent(eventName);
switch (eventName)
2023-10-20 19:31:12 +08:00
{
2024-02-21 01:40:53 +08:00
case BITConstant.Player.Attack:
case BITConstant.Player.Melee:
_meleeService.Melee(new MeleeCommand()
{
Damage =melee.MeleeDamage,
Force = melee.MeleeForce,
Forward = transform.forward,
Limit = 1,
PlayerId = Entity.Id,
Position = transform.position,
Range = melee.MeleeRange,
});
2023-10-20 19:31:12 +08:00
break;
2024-02-21 01:40:53 +08:00
case BITConstant.Player.HeavyAttack:
_playerMovement.Stamina -= melee.HeavyAttackStaminaCost;
_meleeService.Melee(new MeleeCommand()
{
Damage =melee.HeavyMeleeDamage,
Force = melee.HeavyMeleeForce,
Forward = transform.forward,
Limit = 1,
PlayerId = Entity.Id,
Position = transform.position,
Range = melee.HeavyMeleeRange,
});
2023-10-20 19:31:12 +08:00
break;
}
}
}
}