Health/Assets/Scripts/YogaManager.cs

108 lines
3.7 KiB
C#

using NUnit.Framework.Internal;
using OpenCVForUnity.CoreModule;
using OpenCVForUnity.DnnModule;
using OpenCVForUnity.UnityUtils;
using OpenCVForUnity.UnityUtils.Helper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using Yoga;
public class YogaManager : MonoSingleton<YogaManager>
{
private List<Point> _points = new List<Point>();
public List<Point> Points { get => _points; private set => _points = value; }
private int _currentActionCount; //当前动作计数
public int CurrentActionCount { get => _currentActionCount; }
private int _currentSuccessActionCount; //当前成功动作计数
public int CurrentSuccessActionCount { get => _currentSuccessActionCount; }
public int MaxActionCount => Action.MaxActionCount;
private int _actionIndex; //用户选择界面选择的动作索引
public int ActionIndex { get => _actionIndex; internal set => _actionIndex = value; }
private Dictionary<AvatarAction, PoseBase> _actions = new Dictionary<AvatarAction, PoseBase>();
public YogaDataData Action { get => _action; set => _action = value; }
private YogaDataData _action = null;
public void InitData()
{
_actions[AvatarAction.HeadTurnLeft] = new HeadTurnLeft();
_actions[AvatarAction.HeadTurnRight] = new HeadTurnRight();
_actions[AvatarAction.HeadTurnUp] = new HeadTurnUp();
_actions[AvatarAction.HeadTurnDown] = new HeadTurnDown();
_actions[AvatarAction.HandsUp] = new HandsUp();
_actions[AvatarAction.HandsHold] = new HandsHold();
_currentActionCount = 0;
_currentSuccessActionCount = 0;
//根据用户选择的动作索引,获取相应的资源
Action = YogaDataLoader.LoadData(ActionIndex);
EventManager.Instance.Dispatch("UpdateProgress", CurrentSuccessActionCount, CurrentActionCount, MaxActionCount);
}
private void OnEnable()
{
EventManager.Instance.AddEventListener("ActionSuccess", OnActionSuccess);
EventManager.Instance.AddEventListener("ActionFailed", OnActionFailed);
}
private void OnDisable()
{
EventManager.Instance.RemoveEventListener("ActionSuccess", OnActionSuccess);
EventManager.Instance.RemoveEventListener("ActionFailed", OnActionFailed);
}
private void OnActionSuccess()
{
_currentSuccessActionCount++;
_currentActionCount++;
UpdateData();
}
private void OnActionFailed()
{
_currentActionCount++;
UpdateData();
}
private void UpdateData()
{
//如果达到最大数,则停止检测,跳转到下一个动作/下一个动作引导/奖励界面
if (CurrentActionCount >= MaxActionCount)
{
//跳转到下一个动作/下一个动作引导/奖励界面
UIManager.Instance.CloseCurrent(); //关闭当前界面并停止检测
//args[0] = successCount args[1] = totalCount
UIManager.Instance.ShowPanel<ClearingSettlementUI>(false, CurrentSuccessActionCount, MaxActionCount);
}
//args[0] = successCount args[1] = excutedCount args[2] = totalCount
EventManager.Instance.Dispatch("UpdateProgress", CurrentSuccessActionCount, CurrentActionCount, MaxActionCount);
}
public bool IsCorrectAction(List<Point> personPoints, AvatarAction actionType)
{
if (!_actions.ContainsKey(actionType))
{
Debug.LogError("ActionType is not exist");
return false;
}
var result = _actions[actionType].CheckPose(personPoints);
Debug.LogWarning("ActionType: " + actionType + " result: " + result);
return result;
}
}