Files
Cielonos/Assets/Scripts/MainGame/Characters/Base/Subcontrollers/Reaction/BlockSubmodule.cs

192 lines
7.3 KiB
C#
Raw Normal View History

2025-11-25 08:19:33 -05:00
using System;
using System.Collections.Generic;
using System.Linq;
2026-03-20 12:07:44 -04:00
using Cielonos.MainGame.Characters.Inventory;
2025-11-25 08:19:33 -05:00
using Sirenix.OdinInspector;
2026-02-13 09:22:11 -05:00
using SLSUtilities.General;
2025-11-25 08:19:33 -05:00
using UnityEngine;
namespace Cielonos.MainGame.Characters
{
public class BlockSubmodule : SubmoduleBase<ReactionSubcontroller>
{
public List<BlockSource> blockSources;
public bool canBlock;
[ShowInInspector]
public bool isBlocking => blockSources.Count > 0;
[ShowInInspector]
public bool isPerfectBlocking => blockSources.Any(source => source.hasPerfectBlock && source.isDuringPerfectBlock);
public BlockSubmodule(ReactionSubcontroller owner) : base(owner)
{
blockSources = new List<BlockSource>();
canBlock = true;
}
public void ApplyBlock(BlockSource source, bool refreshPerfect = false)
{
if (canBlock)
{
BlockSource existingSource = blockSources.Find(x => x.blockName == source.blockName);
if (existingSource != null)
{
if (source.blockTime > existingSource.blockTime)
{
existingSource.blockTime = source.blockTime;
}
if (refreshPerfect && existingSource.hasPerfectBlock)
{
existingSource.isDuringPerfectBlock = true;
existingSource.perfectTime = source.perfectTime;
}
return;
}
blockSources.AddByPriority(source);
}
}
public void ApplyBlock(CharacterBase sourceCharacter, ItemBase sourceItem, string sourceName,
int priority = 0, float blockTime = Mathf.Infinity, float perfectTime = 0.2f, bool refreshPerfect = false,
2026-03-20 12:07:44 -04:00
BreakthroughType normalBreakType = BreakthroughType.Disruption, string normalEffectName = "NormalBlock",
BreakthroughType perfectBreakType = BreakthroughType.Disruption, string perfectEffectName = "PerfectBlock")
2025-11-25 08:19:33 -05:00
{
BlockSource newSource = new BlockSource(sourceCharacter, sourceItem, sourceName, priority,
2026-03-20 12:07:44 -04:00
blockTime, perfectTime, normalBreakType, normalEffectName, perfectBreakType, perfectEffectName);
2025-11-25 08:19:33 -05:00
ApplyBlock(newSource, refreshPerfect);
}
public void RemoveBlock(string sourceName)
{
blockSources.RemoveAll(source => source.blockName == sourceName);
}
public BlockSource GetCurrentBlockSource()
{
return blockSources.Count > 0 ? blockSources[0] : null;
}
2026-02-13 09:22:11 -05:00
public bool HaveBlockSource(string sourceName)
{
return blockSources.Any(source => source.blockName == sourceName);
}
2025-11-25 08:19:33 -05:00
public void Update()
{
if (isBlocking)
{
blockSources.ForEach(source =>
{
source.blockTime -= owner.owner.selfTimeSm.DeltaTime;
if (source.hasPerfectBlock)
{
source.perfectTime -= owner.owner.selfTimeSm.DeltaTime;
if (source.perfectTime <= 0)
{
source.isDuringPerfectBlock = false;
}
}
});
blockSources.RemoveAll(source => source.blockTime <= 0);
}
}
}
public class BlockSource : IPrioritized
{
public int Priority { get; private set; }
public CharacterBase sourceCharacter;
public ItemBase sourceItem;
public string blockName;
public bool hasPerfectBlock;
public bool isDuringPerfectBlock;
public float perfectTime;
public float blockTime;
2026-03-20 12:07:44 -04:00
public BreakthroughType perfectBlockBreakType;
2025-11-25 08:19:33 -05:00
public string perfectEffectName;
2025-12-08 05:27:53 -05:00
public Action<AttackAreaBase> onPerfectBlock;
2026-03-20 12:07:44 -04:00
public BreakthroughType normalBlockBreakType;
2025-11-25 08:19:33 -05:00
public string normalEffectName;
2025-12-08 05:27:53 -05:00
public Action<AttackAreaBase> onNormalBlock;
2025-11-25 08:19:33 -05:00
public BlockSource(CharacterBase sourceCharacter, ItemBase sourceItem, string blockName,
2026-03-20 12:07:44 -04:00
int priority, float blockTime, BreakthroughType normalBlockBreakType, string blockEffectName)
2025-11-25 08:19:33 -05:00
{
this.sourceCharacter = sourceCharacter;
this.sourceItem = sourceItem;
this.blockName = blockName;
2026-03-20 12:07:44 -04:00
2025-11-25 08:19:33 -05:00
this.Priority = priority;
this.hasPerfectBlock = false;
this.isDuringPerfectBlock = false;
2026-03-20 12:07:44 -04:00
this.blockTime = blockTime;
this.normalEffectName = blockEffectName;
this.normalBlockBreakType = normalBlockBreakType;
2025-11-25 08:19:33 -05:00
}
2026-03-20 12:07:44 -04:00
public BlockSource(CharacterBase sourceCharacter, ItemBase sourceItem, string blockName, int priority,
float blockTime, float perfectTime,
BreakthroughType normalBlockBreakType, string normalEffectName,
BreakthroughType perfectBlockBreakType, string perfectEffectName)
2025-11-25 08:19:33 -05:00
{
this.sourceCharacter = sourceCharacter;
this.sourceItem = sourceItem;
this.blockName = blockName;
this.Priority = priority;
this.hasPerfectBlock = true;
this.isDuringPerfectBlock = true;
2026-03-20 12:07:44 -04:00
this.blockTime = blockTime;
2025-11-25 08:19:33 -05:00
this.perfectTime = perfectTime;
2026-03-20 12:07:44 -04:00
this.normalBlockBreakType = normalBlockBreakType;
2025-11-25 08:19:33 -05:00
this.perfectEffectName = perfectEffectName;
2026-03-20 12:07:44 -04:00
this.perfectBlockBreakType = perfectBlockBreakType;
2025-11-25 08:19:33 -05:00
this.normalEffectName = normalEffectName;
}
2025-12-08 05:27:53 -05:00
public void PerfectBlock(AttackAreaBase attackArea, Vector3 blockEffectPosition)
2025-11-25 08:19:33 -05:00
{
2026-03-20 12:07:44 -04:00
GameObject pObj;
2025-11-25 08:19:33 -05:00
if (sourceItem != null)
{
sourceItem.audioContainer.PlaySoundFX("PerfectBlock");
2026-04-29 06:16:07 -04:00
sourceItem.feedbackSc.PlayFeedback("PerfectBlock");
2026-03-20 12:07:44 -04:00
pObj = sourceItem.blockData.InstantiateBlockEffect(perfectEffectName, sourceCharacter, blockEffectPosition, Quaternion.identity);
}
else
{
pObj = sourceCharacter.blockData.InstantiateBlockEffect(perfectEffectName, sourceCharacter, blockEffectPosition, Quaternion.identity);
2025-11-25 08:19:33 -05:00
}
2026-03-20 12:07:44 -04:00
pObj.GetComponent<ParticleSystem>().Simulate(0.1f, true, true);
pObj.GetComponent<ParticleSystem>().Play();//TODO: 增加起点时间参数
2025-12-08 05:27:53 -05:00
onPerfectBlock?.Invoke(attackArea);
2025-11-25 08:19:33 -05:00
}
2025-12-08 05:27:53 -05:00
public void NormalBlock(AttackAreaBase attackArea, Vector3 blockEffectPosition)
2025-11-25 08:19:33 -05:00
{
if (sourceItem != null)
{
sourceItem.audioContainer.PlaySoundFX("NormalBlock");
2026-04-29 06:16:07 -04:00
sourceItem.feedbackSc.PlayFeedback("NormalBlock");
2025-12-22 18:36:29 -05:00
sourceItem.blockData.InstantiateBlockEffect(normalEffectName, sourceCharacter, blockEffectPosition, Quaternion.identity);
2025-11-25 08:19:33 -05:00
}
2026-03-20 12:07:44 -04:00
else
{
sourceCharacter.blockData.InstantiateBlockEffect(normalEffectName, sourceCharacter, blockEffectPosition, Quaternion.identity);
}
2025-11-25 08:19:33 -05:00
2025-12-08 05:27:53 -05:00
onNormalBlock?.Invoke(attackArea);
2025-11-25 08:19:33 -05:00
}
}
}