| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517 |
- /// <summary>
- /// ZWN
- /// 跳和跑代码
- /// 20210917
- /// </summary>
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public enum action_run_state
- {
- None,
- Jump,
- Run
- }
- public class action_run : MonoBehaviour
- {
- public static action_run single;
- public float space_time = 0.5f;//状态改变间隔时间
- public bool Run_Flag = false;//整体是否检测
- public float space_X = 0.5f;//跳跃阈值
- public float spacehand = 0.5f;//跑步阈值
- Dictionary<int, Vector3> zwn_Single_Vec = new Dictionary<int, Vector3>();//关于位置数据的存储
- Vector3 hand_Right, Shoulder_Right, hand_Left, Shoulder_Left;//右手//右肘//右肩//左手//左肘//左肩
- int hand_Count = 6, buff_sz = 3; //存储帧数
- [HideInInspector]
- public action_run_state cur_RunState = action_run_state.None;//当前状态
- [HideInInspector]
- public action_run_state last_RunState = action_run_state.None;//上状态记录
- public float width;//宽
- public List<int> list_Shoulder_Y, list_Shoulder_X;//肩部XY记录
- int jump_num, run_num, none_num;//状态数量统计
- public Text show_Num;//状态显示
- string last_state;//上个状态
- Vector3 default_vec = new Vector3(-1, -1, 0);
- //关于左侧属性
- #region
- bool flag_time_Left = false;
- float state_time_Left = 0;
- public List<float> list_Hand_X_Left, list_Hand_Y_Left;//左右手X Y列表
- #endregion
- //关于右侧属性
- #region
- bool flag_time_Right = false;
- float state_time_Right = 0;
- public List<float> list_Hand_X_Right, list_Hand_Y_Right;//左右手X Y列表
- #endregion
- void Awake()
- {
- space_X = 1f;//跳跃阈值
- spacehand = 1.5f;//跑步阈值
- single = this;
- if (Application.platform == RuntimePlatform.WindowsEditor
- || Application.platform == RuntimePlatform.WindowsPlayer)
- {
- hand_Count = 25;
- }
- if (Application.platform == RuntimePlatform.Android)// 使用Unity切换Platform无法模拟
- {
- hand_Count = 6;
- }
- //Init_Run();
- }
- bool reInit = false;
- /// <summary>
- /// 初始化判断
- /// </summary>
- public void Init_Run()
- {
- time_vec = 0;
- cur_RunState = action_run_state.None;
- flag_time_Left = true;
- state_time_Left = 0;
- flag_time_Right = true;
- state_time_Right = 0;
- if (reInit)
- {
- clear();
- }
- Run_Flag = true;
- if (!reInit)
- {
- reInit = true;
- }
- }
- /// <summary>
- /// 结束判断
- /// </summary>
- public void End_Run()
- {
- if (reInit)
- {
- clear();
- }
- cur_RunState = action_run_state.None;
- flag_time_Left = true;
- state_time_Left = 0;
- flag_time_Right = true;
- state_time_Right = 0;
- Run_Flag = false;
- }
- void Update()
- {
- if (Run_Flag)
- {
- ZWN_Single_Vec();
- List_Run();
- if (show_Num != null)
- {
- show_Num.text = last_state + "跳:" + jump_num +
- "\n跑:" + run_num + "\n空:" + none_num;
- }
- }
- }
- void List_Run()
- {
- if (
- Shoulder_Right.x != -1 && Shoulder_Right.y != -1 &&
- Shoulder_Left.x != -1 && Shoulder_Left.y != -1)
- {
- list_Shoulder_Y.Add((int)((Shoulder_Right.y + Shoulder_Left.y) / 2));
- list_Shoulder_X.Add((int)(Mathf.Abs(Shoulder_Right.x - Shoulder_Left.x) / 2));
- }
- if (list_Shoulder_Y.Count > hand_Count)
- {
- list_Shoulder_Y.Remove(list_Shoulder_Y[0]);
- list_Shoulder_X.Remove(list_Shoulder_X[0]);
- List_Run_Y();
- List_Run_X();
- ratioTmpY = (max_RunY - min_RunY) / width;//肩部Y占比例
- ratioTmpX = ave_List_RunX / width;//肩部X占比例
- /* print("跳"+ cur_DirY +" "+ (cur_DirY > 0) + "\nratioTmpY:" + ratioTmpY
- + "\nratioTmpX:"+ratioTmpX+" "+ ratioTmpX * space_X + (ratioTmpY > ratioTmpX * space_X));*/
- if (cur_DirY > 0 && ratioTmpY > ratioTmpX * space_X)
- {
- State_Jump();
- }
- else
- {
- List_Run_Left();
- }
- }
- }
- float cur_DirY = 0, cur_Dir_StartY = 0, cur_Dir_EndY = 0, max_RunY = -0xffff, min_RunY = 0xffff;
- float ratioTmpY;
- /// <summary>
- /// 求X位移差、位移比例、滑动方向
- /// </summary>
- void List_Run_Y()
- {
- cur_DirY = 0;
- cur_Dir_StartY = 0;
- cur_Dir_EndY = 0;
- max_RunY = -0xffff;
- min_RunY = 0xffff;
- for (int i = 0; i < list_Shoulder_Y.Count; i++)
- {
- if (i == 0)
- {
- max_RunY = list_Shoulder_Y[i];
- min_RunY = list_Shoulder_Y[i];
- }
- if (i < buff_sz)
- {
- cur_Dir_StartY += list_Shoulder_Y[i];
- }
- if (i >= list_Shoulder_Y.Count - buff_sz)
- {
- cur_Dir_EndY += list_Shoulder_Y[i];
- }
- if (list_Shoulder_Y[i] > max_RunY)
- max_RunY = list_Shoulder_Y[i];
- if (list_Shoulder_Y[i] < min_RunY)
- min_RunY = list_Shoulder_Y[i];
- }
- cur_DirY = (cur_Dir_StartY / buff_sz) - (cur_Dir_EndY / buff_sz);
- }
- float cur_DirX = 0, cur_Dir_StartX = 0, cur_Dir_EndX = 0, max_RunX = -0xffff, min_RunX = 0xffff;
- float ave_List_RunX, ratioTmpX;
- /// <summary>
- /// 求X位移差、位移比例、滑动方向
- /// </summary>
- void List_Run_X()
- {
- cur_DirX = 0;
- cur_Dir_StartX = 0;
- cur_Dir_EndX = 0;
- max_RunX = -0xffff;
- min_RunX = 0xffff;
- ave_List_RunX = 0;
- for (int i = 0; i < list_Shoulder_X.Count; i++)
- {
- if (i == 0)
- {
- max_RunX = list_Shoulder_X[i];
- min_RunX = list_Shoulder_X[i];
- }
- if (i < buff_sz)
- {
- cur_Dir_StartX += list_Shoulder_X[i];
- }
- if (i >= list_Shoulder_X.Count - buff_sz)
- {
- cur_Dir_EndX += list_Shoulder_X[i];
- }
- if (list_Shoulder_X[i] > max_RunY)
- max_RunX = list_Shoulder_X[i];
- if (list_Shoulder_X[i] < min_RunY)
- min_RunX = list_Shoulder_X[i];
- ave_List_RunX += list_Shoulder_X[i];
- }
- ave_List_RunX = ave_List_RunX / list_Shoulder_X.Count;
- cur_DirX = (cur_Dir_StartX / buff_sz) - (cur_Dir_EndX / buff_sz);
- }
- public float time_vec = 0;
- /// <summary>
- /// 点信息
- /// </summary>
- void ZWN_Single_Vec()
- {
- for (int i = 0; i < 17; i++)
- {
- if (zwn_Single_Vec.ContainsKey(i))
- {
- zwn_Single_Vec[i] = zwn_common_data.single.zwn_original_pose[i];
- }
- else
- {
- zwn_Single_Vec.Add(i, zwn_common_data.single.zwn_original_pose[i]);
- }
- }
- if (zwn_Single_Vec[9] == default_vec || zwn_Single_Vec[5] == default_vec
- || zwn_Single_Vec[10] == default_vec || zwn_Single_Vec[6] == default_vec)
- {
- time_vec += Time.deltaTime;
- if (time_vec >= 0.2f)
- {
- clear();
- State_None();
- time_vec = 0;
- }
- }
- else
- {
- time_vec = 0;
- }
- if (zwn_Single_Vec[9] != default_vec)
- {
- hand_Right = zwn_Single_Vec[9];
- }
- if (zwn_Single_Vec[5] != default_vec)
- {
- Shoulder_Right = zwn_Single_Vec[5];
- }
- if (zwn_Single_Vec[10] != default_vec)
- {
- hand_Left = zwn_Single_Vec[10];
- }
- if (zwn_Single_Vec[6] != default_vec)
- {
- Shoulder_Left = zwn_Single_Vec[6];
- }
- //if (width != Helper.Width)
- // width = Helper.Width;
- }
- /// <summary>
- /// 右侧状态判断
- /// </summary>
- void List_Run_Right()
- {
- if (hand_Right.x != -1 && hand_Right.y != -1)
- {
- list_Hand_X_Right.Add(hand_Right.x);
- list_Hand_Y_Right.Add(hand_Right.y);
- }
- if (!flag_time_Right)
- {
- state_time_Right += Time.deltaTime;
- if (state_time_Right >= space_time)
- {
- flag_time_Right = true;
- state_time_Right = 0;
- }
- }
- if (list_Hand_X_Right.Count > hand_Count)
- {
- list_Hand_X_Right.Remove(list_Hand_X_Right[0]);
- list_Hand_Y_Right.Remove(list_Hand_Y_Right[0]);
- List_Run_Right_X();
- List_Run_Right_Y();
- ratioTmpX_Right = ave_X_Right / width;//右手整体位移X
- ratioTmpY_Right = ave_Y_Right / width;//右手整体位移Y
- //print("右手跑" + (ratioTmpX_Right > ratioTmpX * spacehand) + " " + (ratioTmpY_Right > ratioTmpX * spacehand));
- if (ratioTmpX_Right > ratioTmpX * spacehand
- || ratioTmpY_Right > ratioTmpX * spacehand
- )
- {
- //print("右");
- State_Run();
- }
- else
- {
- State_None();
- }
- }
- }
- float ave_X_Right;
- float ratioTmpX_Right;
- /// <summary>
- /// 求X位移差、位移比例、滑动方向
- /// </summary>
- void List_Run_Right_X()
- {
- ave_X_Right = 0;
- for (int i = 0; i < list_Hand_X_Right.Count; i++)
- {
- if (i + 1 < list_Hand_X_Right.Count)
- {
- ave_X_Right += Mathf.Abs(list_Hand_X_Right[i + 1] - list_Hand_X_Right[i]);
- }
- }
- }
- float ave_Y_Right;
- float ratioTmpY_Right;
- /// <summary>
- /// 求Y位移差与位移比例
- /// </summary>
- void List_Run_Right_Y()
- {
- ave_Y_Right = 0;
- for (int i = 0; i < list_Hand_Y_Right.Count; i++)
- {
- if (i + 1 < list_Hand_Y_Right.Count)
- {
- ave_Y_Right += Mathf.Abs(list_Hand_Y_Right[i + 1] - list_Hand_Y_Right[i]);
- }
- }
- }
- /// <summary>
- /// 左侧状态判断
- /// </summary>
- void List_Run_Left()
- {
- //print("hand_Left.x" + hand_Left.x + " hand_Left.y" + hand_Left.y);
- if (hand_Left.x != -1 && hand_Left.y != -1)
- {
- list_Hand_X_Left.Add(hand_Left.x);
- list_Hand_Y_Left.Add(hand_Left.y);
- }
- if (!flag_time_Left)
- {
- state_time_Left += Time.deltaTime;
- if (state_time_Left >= space_time)
- {
- flag_time_Left = true;
- state_time_Left = 0;
- }
- }
- if (list_Hand_X_Left.Count > hand_Count)
- {
- list_Hand_X_Left.Remove(list_Hand_X_Left[0]);
- list_Hand_Y_Left.Remove(list_Hand_Y_Left[0]);
- List_Run_Left_X();
- List_Run_Left_Y();
- ratioTmpX_Left = ave_X_Left / width;//右手整体位移X
- ratioTmpY_Left = ave_Y_Left / width;//右手整体位移Y
- //print("左手跑" + (ratioTmpX_Left > ratioTmpX * spacehand) + " " + (ratioTmpY_Left > ratioTmpX * spacehand) +
- //" " + ratioTmpX_Left + " " + ratioTmpX * spacehand + " " + ratioTmpY_Left);
- if (ratioTmpX_Left > ratioTmpX * spacehand
- || ratioTmpY_Left > ratioTmpX * spacehand
- )
- {
- //print("左");
- State_Run();
- }
- else
- {
- //State_None();
- List_Run_Right();
- }
- }
- }
- float ave_X_Left;
- float ratioTmpX_Left;
- /// <summary>
- /// 求X位移差、位移比例、滑动方向
- /// </summary>
- void List_Run_Left_X()
- {
- ave_X_Left = 0;
- for (int i = 0; i < list_Hand_X_Left.Count; i++)
- {
- if (i + 1 < list_Hand_X_Left.Count)
- {
- ave_X_Left += Mathf.Abs(list_Hand_X_Left[i + 1] - list_Hand_X_Left[i]);
- }
- }
- }
- float ave_Y_Left;
- float ratioTmpY_Left;
- /// <summary>
- /// 求Y位移差与位移比例
- /// </summary>
- void List_Run_Left_Y()
- {
- ave_Y_Left = 0;
- for (int i = 0; i < list_Hand_Y_Left.Count; i++)
- {
- if (i + 1 < list_Hand_Y_Left.Count)
- {
- ave_Y_Left += Mathf.Abs(list_Hand_Y_Left[i + 1] - list_Hand_Y_Left[i]);
- }
- }
- }
- /// <summary>
- /// 跳跃状态
- /// </summary>
- void State_Jump()
- {
- if (cur_RunState != action_run_state.Jump)
- {
- jump_num += 1;
- last_State();
- cur_RunState = action_run_state.Jump;
- last_RunState = cur_RunState;
- }
- }
- /// <summary>
- /// 跑步状态
- /// </summary>
- void State_Run()
- {
- /* if (cur_RunState != action_run_state.Run)
- {*/
- run_num += 1;
- cur_RunState = action_run_state.Run;
- last_State();
- last_RunState = cur_RunState;
- state_time_Right = 0;
- /* }*/
- }
- /// <summary>
- /// 空状态
- /// </summary>
- void State_None()
- {
- /* if (cur_RunState != action_run_state.None)
- {*/
- none_num += 1;
- cur_RunState = action_run_state.None;
- last_State();
- last_RunState = cur_RunState;
- //clear();
- /* }*/
- }
- /// <summary>
- /// 上一个状态提示
- /// </summary>
- void last_State()
- {
- last_state = "上一个状态:";
- switch (last_RunState)
- {
- case action_run_state.None:
- last_state += "空\n";
- break;
- case action_run_state.Jump:
- last_state += "跳\n";
- break;
- case action_run_state.Run:
- last_state += "跑\n";
- break;
- }
- switch (cur_RunState)
- {
- case action_run_state.None:
- last_state += "状态空\n";
- break;
- case action_run_state.Jump:
- last_state += "状态跳\n";
- break;
- case action_run_state.Run:
- last_state += "状态跑\n";
- break;
- }
- }
- /// <summary>
- /// 将存储数据清空
- /// </summary>
- void clear()
- {
- if (list_Hand_X_Right.Count > 0)
- {
- list_Hand_Y_Right.Clear();
- list_Hand_X_Right.Clear();
- }
- if (list_Hand_X_Left.Count > 0)
- {
- list_Hand_Y_Left.Clear();
- list_Hand_X_Left.Clear();
- }
- if (list_Shoulder_X.Count > 0)
- {
- list_Shoulder_X.Clear();
- list_Shoulder_Y.Clear();
- }
- }
- }
|