Health/Assets/Scripts/YogaManager.cs

124 lines
4.2 KiB
C#
Raw Normal View History

2023-11-07 13:55:35 +00:00
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;
2023-11-12 15:09:33 +00:00
using System.Runtime.CompilerServices;
2023-11-07 13:55:35 +00:00
using UnityEngine;
2023-11-10 07:17:23 +00:00
using UnityEngine.UI;
using Yoga;
2023-11-07 13:55:35 +00:00
public class YogaManager : MonoSingleton<YogaManager>
{
private List<Point> _points = new List<Point>();
2023-11-12 15:09:33 +00:00
public List<Point> Points { get => _points; set => _points = value; }
2023-11-07 13:55:35 +00:00
2023-11-07 16:51:42 +00:00
private int _currentActionCount; //当前动作计数
2023-11-10 07:17:23 +00:00
public int CurrentActionCount { get => _currentActionCount; }
2023-11-07 16:51:42 +00:00
private int _currentSuccessActionCount; //当前成功动作计数
2023-11-07 17:51:34 +00:00
public int CurrentSuccessActionCount { get => _currentSuccessActionCount; }
2023-11-10 07:17:23 +00:00
public int MaxActionCount => Action.MaxActionCount;
2023-11-07 17:51:34 +00:00
2023-11-12 15:09:33 +00:00
private int _actionIndex = -1; //用户选择界面选择的动作索引
2023-11-07 16:51:42 +00:00
public int ActionIndex { get => _actionIndex; internal set => _actionIndex = value; }
private Dictionary<AvatarAction, PoseBase> _actions = new Dictionary<AvatarAction, PoseBase>();
2023-11-07 13:55:35 +00:00
2023-11-10 07:17:23 +00:00
public YogaDataData Action { get => _action; set => _action = value; }
private YogaDataData _action = null;
2023-11-07 15:28:19 +00:00
public void InitData()
2023-11-07 13:55:35 +00:00
{
2023-11-07 16:51:42 +00:00
_actions[AvatarAction.HeadTurnLeft] = new HeadTurnLeft();
_actions[AvatarAction.HeadTurnRight] = new HeadTurnRight();
2023-11-10 07:17:23 +00:00
_actions[AvatarAction.HeadTurnUp] = new HeadTurnUp();
_actions[AvatarAction.HeadTurnDown] = new HeadTurnDown();
_actions[AvatarAction.HandsUp] = new HandsUp();
_actions[AvatarAction.HandsHold] = new HandsHold();
2023-11-07 13:55:35 +00:00
2023-11-07 15:28:19 +00:00
_currentActionCount = 0;
_currentSuccessActionCount = 0;
2023-11-07 17:51:34 +00:00
2023-11-10 07:17:23 +00:00
//根据用户选择的动作索引,获取相应的资源
Action = YogaDataLoader.LoadData(ActionIndex);
2023-11-12 15:09:33 +00:00
EventManager.Instance.Dispatch(YogaEventType.UpdateProgress, CurrentSuccessActionCount, CurrentActionCount, MaxActionCount);
2023-11-07 13:55:35 +00:00
}
2023-11-07 17:51:34 +00:00
private void OnEnable()
{
2023-11-12 15:09:33 +00:00
EventManager.Instance.AddEventListener(YogaEventType.ActionSuccess, OnActionSuccess);
EventManager.Instance.AddEventListener(YogaEventType.ActionFailed, OnActionFailed);
2023-11-07 17:51:34 +00:00
}
private void OnDisable()
{
2023-11-12 15:09:33 +00:00
EventManager.Instance.RemoveEventListener(YogaEventType.ActionSuccess, OnActionSuccess);
EventManager.Instance.RemoveEventListener(YogaEventType.ActionFailed, OnActionFailed);
2023-11-07 17:51:34 +00:00
}
private void OnActionSuccess()
{
_currentSuccessActionCount++;
_currentActionCount++;
2023-11-10 07:17:23 +00:00
UpdateData();
2023-11-07 17:51:34 +00:00
}
private void OnActionFailed()
{
_currentActionCount++;
2023-11-10 07:17:23 +00:00
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
2023-11-12 15:09:33 +00:00
EventManager.Instance.Dispatch(YogaEventType.UpdateProgress, CurrentSuccessActionCount, CurrentActionCount, MaxActionCount);
2023-11-07 17:51:34 +00:00
}
2023-11-07 16:51:42 +00:00
public bool IsCorrectAction(List<Point> personPoints, AvatarAction actionType)
2023-11-07 13:55:35 +00:00
{
2023-11-07 16:51:42 +00:00
if (!_actions.ContainsKey(actionType))
2023-11-07 13:55:35 +00:00
{
2023-11-07 16:51:42 +00:00
Debug.LogError("ActionType is not exist");
return false;
2023-11-07 13:55:35 +00:00
}
2023-11-07 16:51:42 +00:00
var result = _actions[actionType].CheckPose(personPoints);
Debug.LogWarning("ActionType: " + actionType + " result: " + result);
return result;
2023-11-07 13:55:35 +00:00
}
2023-11-12 15:09:33 +00:00
public bool ActionCheckPoints(List<Point> points)
{
if (points == null || points.Count == 0)
{
Debug.LogWarning("ActionCheckPoints points is null");
return false;
}
foreach(var p in Action.MustPoints)
{
if (!p.IsValid(Points)) //有一个点不符合 返回false
return false;
}
return true;
}
2023-11-07 13:55:35 +00:00
}