Files
Hasebe/Assets/Code/OfficeGame/BattleMode/Actions/Attacks/BattleActionPlayerAttackMMA.cs
T

153 lines
6.3 KiB
C#

using System.Linq;
using Code.Game.Entities;
using Code.Infrastructure.Utils;
using Code.OfficeGame.BattleMode.Actions.Deciders;
using Code.OfficeGame.BattleMode.HitBoxes;
using Code.OfficeGame.BattleMode.Timeline;
using UnityEngine;
namespace Code.OfficeGame.BattleMode.Actions.Attacks
{
public class BattleActionPlayerAttackMMA : BattleAction
{
private readonly AttackActionConfig _attackData;
private static readonly int EntryStateHash = Animator.StringToHash("WalkBlend");
public BattleActionPlayerAttackMMA(MultipleConfig<BattleActionParameters> attackData)
{
_attackData = (AttackActionConfig)attackData.Configs.SingleOrDefault(x => x.ActionType == ActionType);
}
public override BattleActionType ActionType => BattleActionType.PlayerAttackMMA;
public override DeciderType DeciderType => DeciderType.Rotation;
public override BattleActionData MountAndGetFrames(EntityProperties entity, BattleActionDecisionResult result)
{
return new AttackBattleActionData
{
TotalFrames = _attackData.TotalFrames,
TransitionFrames = _attackData.TransitionFrames,
StartRotation = entity.KinematicCharacterMotor.TransientRotation
};
}
public override void Tick(
float tickDelta,
EntityProperties entity,
BattleActionDecisionResult battleActionDecisionResult,
BattleActionData battleActionData)
{
if (battleActionData is not AttackBattleActionData actionData
|| battleActionDecisionResult is not RotationBattleActionDeciderResult deciderResult)
{
battleActionData.Finished = true;
return;
}
if (actionData.CurrentFrame == 0)
{
actionData.TransitionFrames = _attackData.TransitionFrames;
actionData.TotalFrames = _attackData.TotalFrames;
actionData.StartRotation = entity.KinematicCharacterMotor.TransientRotation;
actionData.HitEntities.Clear();
entity.SyncRootMotionToMotor();
var entityAnimator = entity.animator;
entityAnimator.applyRootMotion = true;
entityAnimator.CrossFadeInFixedTime(_attackData.StateHash, actionData.TransitionFrames * BattleTimeline.TickDelta);
}
else if (actionData.CurrentFrame <= actionData.TransitionFrames && actionData.TransitionFrames > 0)
{
float t = (float)actionData.CurrentFrame / actionData.TransitionFrames;
Quaternion rot = Quaternion.Slerp(actionData.StartRotation, deciderResult.Rotation, t);
entity.KinematicCharacterMotor.SetRotation(rot);
}
actionData.CurrentFrame++;
}
public override void PostPhysicsTick(
EntityProperties entity,
BattleActionDecisionResult battleActionDecisionResult,
BattleActionData battleActionData)
{
if (battleActionData is not AttackBattleActionData actionData) return;
if (actionData.CurrentFrame >= _attackData.HitboxStartFrame
&& actionData.CurrentFrame <= _attackData.HitboxEndFrame)
{
entity.EntityHitBoxes.CheckHitsForCurrentFrame(
SimContext,
(attackerCol, enemyCol, hitPart) => OnEnemyHit(actionData, attackerCol, enemyCol, hitPart),
_attackData.AttackingBodyPart);
entity.EntityHitBoxes.CheckHitsForCurrentFrame(
SimContext,
(attackerCol, enemyCol, hitPart) => OnEnemyHit(actionData, attackerCol, enemyCol, hitPart),
HitBoxPartType.RAnkle);
}
if (actionData.CurrentFrame >= actionData.TotalFrames)
{
var entityAnimator = entity.animator;
entityAnimator.applyRootMotion = false;
entityAnimator.Play(EntryStateHash, 0, 0f);
entityAnimator.Update(0f);
battleActionData.Finished = true;
}
}
public override void Finish(EntityProperties entity, BattleActionData actionData, BattleActionDecisionResult result)
{
entity.animator.applyRootMotion = false;
}
private void OnEnemyHit(
AttackBattleActionData attackData,
Collider attacker,
Collider enemyCollider,
HitBoxPartType hitPart)
{
var defenderProps = enemyCollider.GetComponentInParent<EntityProperties>();
var attackerProps = attacker.GetComponentInParent<EntityProperties>();
if (defenderProps == null || attackerProps == null) return;
var defenderId = defenderProps.GetEntityId();
var attackerId = attackerProps.GetEntityId();
// Защита от многокадрового урона по одной цели за один удар
if (!attackData.HitEntities.Add(defenderId)) return;
Vector3 knockbackDir = attackerProps.KinematicCharacterMotor.Transform
.TransformDirection(_attackData.LocalKnockbackDirection);
knockbackDir.y = 0f;
if (knockbackDir.sqrMagnitude > 0.0001f) knockbackDir.Normalize();
var forcedActionData = new StuggerActionData
{
TotalFrames = _attackData.StuggerFrames,
CurrentFrame = 0,
KnockbackDirection = knockbackDir,
KnockbackSpeed = _attackData.KnockbackSpeed,
KnockbackDistance = _attackData.KnockbackDistance
};
var forcedDecision = new AsIsBattleActionDeciderResult
{
ActionType = BattleActionType.Stugger,
ResultCode = BattleActionDecisionResultCode.Decided
};
var attackedData = new EntityAttackedData
{
ForcedActionData = forcedActionData,
ForcedDecisionResult = forcedDecision,
Damage = _attackData.Damage
};
SimContext.SubmitAttack(attackerId, defenderId, hitPart, enemyCollider.transform.position, attackedData);
}
}
}