Files

131 lines
4.3 KiB
C#

using System.Collections.Generic;
using AibisDream.Framework;
using UnityEngine;
using Yarn.Unity;
namespace AibisDream.FixSystem
{
[RequireComponent(typeof(BodyModuleSystem))]
public class WhackMoleSystem : MonoBehaviour
{
private BodyModuleSystem _bodyModuleSystem;
[SerializeField] private PipelineSystem pipelineSystem;
[SerializeField] private VerificationCodeSystem verificationCodeSystem;
private WhackMoleHandler _whackMoleHandler;
private WhackMoleData _data;
private Dictionary<string, MoleModule> _moleModules;
private void Awake()
{
_bodyModuleSystem = GetComponent<BodyModuleSystem>();
FixSystemCenter.SystemDic.Register(this);
}
#region 打地鼠游戏流程控制
public void StartWhack(WhackMoleData levelData)
{
_data = levelData;
// 打地鼠部分
WhackMoleHandler handler = new WhackMoleHandler(levelData);
handler.OnBatchEnd += OnBatchEnd;
handler.OnGameEnd += OnGameEnd;
handler.Start();
// 流水线部分
if (levelData.HasPipeline())
{
pipelineSystem.Init(levelData.checkRules, levelData.checkDatas);
pipelineSystem.OnGameEnd += OnGameEnd;
}
// 赋值
_whackMoleHandler = handler;
}
private void OnBatchEnd()
{
if (_data.isAddVerifyCode)
{
verificationCodeSystem.StartVerification();
}
}
private void OnGameEnd()
{
// 打地鼠结果
_whackMoleHandler.EndWhackGame();
var whackRes = _whackMoleHandler.res;
Debug.Log($"结果:共计{whackRes.totalMoleNum}个,成功击中{whackRes.hitMoleNum}个");
YarnVariableStorage.Instance.SetValue("$whackRes.totalMoleNum", whackRes.totalMoleNum);
YarnVariableStorage.Instance.SetValue("$whackRes.hitMoleNum", whackRes.hitMoleNum);
// 流水线结果
if (_data.HasPipeline())
{
var pipelineRes = pipelineSystem.res;
Debug.Log(
$"流水线结果:共计{pipelineRes.totalProductNum}个,成功{pipelineRes.successNum}个,失败{pipelineRes.failNum}个");
YarnVariableStorage.Instance.SetValue("$whackRes.totalProductNum", pipelineRes.totalProductNum);
YarnVariableStorage.Instance.SetValue("$whackRes.successNum", pipelineRes.successNum);
YarnVariableStorage.Instance.SetValue("$whackRes.failNum", pipelineRes.failNum);
}
// 结束
DialogController.Instance?.StartDialogNode("打地鼠结束");
}
#endregion
#region 效果相关, 控制BodyModuleSystem实现
public void StartAllFlicker()
{
foreach (var module in _moleModules.Values)
{
module.StartFlicker();
}
}
public void StopAllFlicker()
{
foreach (var module in _moleModules.Values)
{
module.StopFlicker();
}
}
public Dictionary<string, MoleModule> MoleModules
{
get
{
if (_moleModules == null)
{
_moleModules = new Dictionary<string, MoleModule>();
foreach (var module in _bodyModuleSystem.BodyModules)
{
if (module is MoleModule moleModule)
{
_moleModules.Add(moleModule.data.moduleName, moleModule);
}
}
}
return _moleModules;
}
}
#endregion
}
public static class WholeMoleCommand
{
private static WhackMoleSystem WhackMoleSystem => FixSystemCenter.SystemDic.Get<WhackMoleSystem>();
[YarnCommand("start_whack_game")]
public static void StartWhack(string levelName)
{
var levelData = ResourceKit.LoadAssetSync<WhackMoleData>($"WhackMoleData/{levelName}.asset");
WhackMoleSystem.StartWhack(levelData);
}
}
}