665 lines
25 KiB
C#
665 lines
25 KiB
C#
using Dto;
|
||
using Enum;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Collections.ObjectModel;
|
||
using System.Diagnostics;
|
||
using System.IO;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Windows;
|
||
using System.Windows.Controls;
|
||
using System.Windows.Data;
|
||
using System.Windows.Documents;
|
||
using System.Windows.Input;
|
||
using System.Windows.Media;
|
||
using System.Windows.Media.Animation;
|
||
using System.Windows.Media.Imaging;
|
||
using System.Windows.Navigation;
|
||
using System.Windows.Shapes;
|
||
using System.Windows.Threading;
|
||
using Views.JumpRope;
|
||
using Wpf_AiSportsMicrospace.Common;
|
||
using Wpf_AiSportsMicrospace.Enum;
|
||
using Wpf_AiSportsMicrospace.MyUserControl;
|
||
using Wpf_AiSportsMicrospace.Views;
|
||
using WpfAnimatedGif;
|
||
using Yztob.AiSports.Inferences.Things;
|
||
using Yztob.AiSports.Postures.Sports;
|
||
|
||
namespace Wpf_AiSportsMicrospace.Views.JumpRope
|
||
{
|
||
/// <summary>
|
||
/// MusicJumpRope.xaml 的交互逻辑
|
||
/// </summary>
|
||
public partial class MusicJumpRope : UserControl
|
||
{
|
||
private Main _mainWin => Application.Current.MainWindow as Main;
|
||
private MediaPlayer _mediaPlayer = new MediaPlayer();
|
||
private readonly object _updateLock = new object();
|
||
private readonly Dictionary<int, (string lastNumber, DateTime lastChangeTime, string currentState)> _jumpStatus = new Dictionary<int, (string, DateTime, string)>();
|
||
private MusicJumpRopeContext _musicJumpRopeContext;
|
||
private GameState _currentGameState = GameState.NotStarted;
|
||
|
||
|
||
// 容忍时间(节拍误差)
|
||
public double _beatTolerance = 0.15; // ±150ms
|
||
private int _totalDots = 0;
|
||
// 滚动显示的节拍点集合
|
||
public ObservableCollection<BeatItem> BeatDisplayLeft { get; set; } = new();
|
||
public ObservableCollection<BeatItem> BeatDisplayRight { get; set; } = new();
|
||
|
||
public MusicJumpRope()
|
||
{
|
||
InitializeComponent();
|
||
Loaded += UserControl_Loaded;
|
||
Unloaded += UserControl_Unloaded;
|
||
_musicJumpRopeContext = new MusicJumpRopeContext();
|
||
}
|
||
|
||
private async void UserControl_Loaded(object sender, RoutedEventArgs e)
|
||
{
|
||
DrawCirclesWithText();
|
||
// 播放音乐
|
||
PlayMusic("raisehand.mp3");
|
||
PkBar.LeftProgress = 0.5;
|
||
|
||
popSilder1.MusicBeats = _musicJumpRopeContext.MusicBeats["1"];
|
||
popSilder2.MusicBeats = _musicJumpRopeContext.MusicBeats["1"];
|
||
|
||
//Random rnd = new Random();
|
||
|
||
//// 模拟 20 次随机更新
|
||
//for (int i = 0; i < 200; i++)
|
||
//{
|
||
// // 随机左右进度变化,保证总和=1
|
||
// double left = rnd.NextDouble(); // 0~1
|
||
// PkBar.LeftProgress = left; // RightProgress 自动 1-left
|
||
|
||
// await Task.Delay(1000); // 200ms 间隔,不卡UI线程
|
||
//}
|
||
}
|
||
|
||
private void UserControl_Unloaded(object sender, RoutedEventArgs e)
|
||
{
|
||
_mainWin.HumanFrameUpdated -= OnHumanFrameUpdated;
|
||
}
|
||
private void PlayMusic(string musicFileName)
|
||
{
|
||
// 获取项目根目录
|
||
string projectRoot = System.IO.Path.Combine(AppContext.BaseDirectory, @"..\..\..");
|
||
string musicPath = System.IO.Path.Combine(projectRoot, "Resources", "Music", musicFileName);
|
||
string imgPath = System.IO.Path.Combine(projectRoot, "Resources", "Img", "提示图.png");
|
||
|
||
if (!File.Exists(musicPath))
|
||
{
|
||
Console.WriteLine($"音乐文件不存在: {musicPath}");
|
||
return;
|
||
}
|
||
|
||
_mediaPlayer.Open(new Uri(musicPath, UriKind.Absolute));
|
||
|
||
ShowCenterTip(imgPath, TimeSpan.FromSeconds(3));
|
||
|
||
// 监听播放完成事件
|
||
_mediaPlayer.MediaEnded += MediaPlayer_MediaEnded;
|
||
|
||
_mediaPlayer.Play();
|
||
}
|
||
private void ShowCenterTip(string imagePath, TimeSpan duration)
|
||
{
|
||
var tipImage = new Image
|
||
{
|
||
Source = new BitmapImage(new Uri(imagePath, UriKind.Absolute)),
|
||
HorizontalAlignment = HorizontalAlignment.Center,
|
||
VerticalAlignment = VerticalAlignment.Center,
|
||
Opacity = 0,
|
||
Margin = new Thickness(0, -100, 0, 0),
|
||
};
|
||
|
||
// 增加图片的大小,调整比例
|
||
tipImage.Width = 1920 * 0.9; // 宽度为 Canvas 宽度的 90%
|
||
tipImage.Height = 1080 * 0.6; // 高度为 Canvas 高度的 60%
|
||
|
||
// 将图片添加到 Overlay Canvas
|
||
userBox.Children.Add(tipImage);
|
||
|
||
// 渐变出现动画
|
||
var fadeInAnimation = new DoubleAnimation
|
||
{
|
||
From = 0,
|
||
To = 1,
|
||
Duration = TimeSpan.FromSeconds(1.5)
|
||
};
|
||
tipImage.BeginAnimation(UIElement.OpacityProperty, fadeInAnimation);
|
||
|
||
// 定时移除,并且渐变消失
|
||
Task.Delay(duration).ContinueWith(_ =>
|
||
{
|
||
Dispatcher.Invoke(() =>
|
||
{
|
||
// 渐变消失动画
|
||
var fadeOutAnimation = new DoubleAnimation
|
||
{
|
||
From = 1,
|
||
To = 0,
|
||
Duration = TimeSpan.FromSeconds(1.5)
|
||
};
|
||
tipImage.BeginAnimation(UIElement.OpacityProperty, fadeOutAnimation);
|
||
|
||
// 完成后移除图片
|
||
fadeOutAnimation.Completed += (s, e) =>
|
||
{
|
||
userBox.Children.Remove(tipImage);
|
||
};
|
||
});
|
||
});
|
||
}
|
||
private void MediaPlayer_MediaEnded(object sender, EventArgs e)
|
||
{
|
||
// 音乐播放完成后的逻辑
|
||
Console.WriteLine("音乐播放完成!");
|
||
|
||
// 可在这里绑定抽帧事件
|
||
_mainWin.HumanFrameUpdated += OnHumanFrameUpdated;
|
||
}
|
||
private void OnHumanFrameUpdated(object sender, List<Human> humans)
|
||
{
|
||
try
|
||
{
|
||
if (humans == null || humans.Count == 0) return;
|
||
switch (_currentGameState)
|
||
{
|
||
case GameState.NotStarted: // 未开始
|
||
int rightWaving = DetectRightHandRaise(humans);
|
||
if (rightWaving >= 3)
|
||
{
|
||
switch (rightWaving)
|
||
{
|
||
case (int)WavingAction.FirstHand:
|
||
StartCountdown(3);
|
||
break;
|
||
case (int)WavingAction.Raising:
|
||
UpdateCountdown();
|
||
break;
|
||
case (int)WavingAction.RaiseHand:
|
||
FinishCountdown();
|
||
_currentGameState = GameState.Running; // 倒计时完成 → 游戏开始
|
||
break;
|
||
}
|
||
}
|
||
break;
|
||
|
||
case GameState.Running: // 游戏进行中
|
||
UpdateCircleCounts(humans);
|
||
|
||
// 可选:判断游戏结束条件,将状态切换到 Finished
|
||
// if (CheckGameFinished()) _currentGameState = GameState.Finished;
|
||
break;
|
||
|
||
case GameState.Finished: // 游戏完成
|
||
int leftWaving = DetectLeftHandRaise(humans);
|
||
if (leftWaving == 5)
|
||
{
|
||
_mainWin.HumanFrameUpdated -= OnHumanFrameUpdated;
|
||
_mainWin.WebcamClient.StopExtract();
|
||
|
||
// 举左手逻辑,例如结束动画或退出
|
||
var newPage = new Home();
|
||
_mainWin?.SwitchPageWithMaskAnimation(newPage, true);
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Console.WriteLine("OnFrameExtracted error: " + ex.Message);
|
||
}
|
||
}
|
||
|
||
private int _currentCountdown = 3;
|
||
private DateTime _lastUpdateTime = DateTime.Now;
|
||
private void StartCountdown(int start = 4)
|
||
{
|
||
_currentCountdown = start;
|
||
countdownText.Text = _currentCountdown.ToString();
|
||
countdownGrid.Visibility = Visibility.Visible;
|
||
_lastUpdateTime = DateTime.Now;
|
||
_mainWin.ShowCountDownAnimation();
|
||
Utils.PlayBackgroundMusic("countdown_3.mp3", false);
|
||
}
|
||
|
||
private void UpdateCountdown()
|
||
{
|
||
if ((DateTime.Now - _lastUpdateTime).TotalSeconds >= 1)
|
||
{
|
||
_lastUpdateTime = DateTime.Now;
|
||
_currentCountdown--;
|
||
|
||
if (_currentCountdown > 0)
|
||
{
|
||
countdownText.Text = _currentCountdown.ToString();
|
||
}
|
||
else
|
||
{
|
||
FinishCountdown();
|
||
}
|
||
}
|
||
}
|
||
|
||
private async void FinishCountdown()
|
||
{
|
||
countdownText.Text = "GO!";
|
||
countdownGrid.Visibility = Visibility.Hidden;
|
||
// 启动60秒倒计时(独立任务)
|
||
StartGameCountdown(105);
|
||
ShowMusicAnimation();
|
||
}
|
||
|
||
private async void StartGameCountdown(int seconds)
|
||
{
|
||
//_musicCurrentTime = Utils.GetMusicTotalDuration();
|
||
|
||
countdownGrid.Visibility = Visibility.Visible;
|
||
|
||
// 播放背景音乐(循环)
|
||
Utils.PlayBackgroundMusic("1.MP3", true);
|
||
|
||
popSilder1.StartMarginAnimation();
|
||
popSilder2.StartMarginAnimation();
|
||
|
||
for (int i = seconds; i >= 0; i--)
|
||
{
|
||
countdownText.Text = i.ToString();
|
||
await Task.Delay(1000);
|
||
}
|
||
|
||
// 停止音乐
|
||
Utils.StopBackgroundMusic();
|
||
// 停止实时检测和视频流
|
||
|
||
countdownGrid.Visibility = Visibility.Hidden;
|
||
_mainWin.HumanFrameUpdated -= OnHumanFrameUpdated;
|
||
_mainWin.WebcamClient.StopExtract();
|
||
|
||
// 计算排名
|
||
var rankList = _musicJumpRopeContext.UpdateRankList();
|
||
var scoreList = _musicJumpRopeContext.UpdateScoreRankList();
|
||
|
||
|
||
// 更新游戏状态
|
||
_currentGameState = GameState.Finished;
|
||
|
||
// 恢复视频流
|
||
Application.Current.Dispatcher.Invoke(() =>
|
||
{
|
||
_mainWin.HumanFrameUpdated += OnHumanFrameUpdated;
|
||
_mainWin.WebcamClient.StartExtract();
|
||
|
||
// 更新 UI(排行榜或分数板)
|
||
// ShowRankingBoard(rankList);
|
||
});
|
||
}
|
||
|
||
private DateTime? _raiseStartTime;
|
||
private bool _firstHandTriggered;
|
||
private int _lastCountdownSecond = 3;
|
||
private DateTime? _countdownStartTime;
|
||
public int DetectRightHandRaise(List<Human> humans)
|
||
{
|
||
if (humans == null || humans.Count == 0)
|
||
return (int)WavingAction.None;
|
||
|
||
foreach (var human in humans)
|
||
{
|
||
if (human?.Keypoints == null)
|
||
continue;
|
||
|
||
// --- 筛选右脚踝坐标 ---
|
||
var rightAnkle = human.Keypoints.FirstOrDefault(k => k.Name == "right_ankle");
|
||
if (rightAnkle == null)
|
||
continue;
|
||
|
||
double xNorm = rightAnkle.X / 1920;
|
||
double yNorm = rightAnkle.Y / 1080;
|
||
|
||
// 仅检测中心区域内的人
|
||
if (!(xNorm >= 0.44 && xNorm <= 0.57 && yNorm >= 0.81))
|
||
continue;
|
||
|
||
// --- 获取右臂关键点 ---
|
||
var rightWrist = human.Keypoints.FirstOrDefault(k => k.Name == "right_wrist");
|
||
var rightElbow = human.Keypoints.FirstOrDefault(k => k.Name == "right_elbow");
|
||
|
||
const double raiseThreshold = 60; // 举手阈值
|
||
const double holdDuration = 1; // 持续时间(秒)
|
||
const int countdownSeconds = 3; // 倒计时长度
|
||
|
||
bool handRaised = false;
|
||
|
||
if (rightWrist != null && rightElbow != null)
|
||
{
|
||
double verticalRise = rightElbow.Y - rightWrist.Y;
|
||
handRaised = verticalRise >= raiseThreshold;
|
||
}
|
||
|
||
// ---------- 第一步:持续举手触发倒计时 ----------
|
||
if (!_firstHandTriggered)
|
||
{
|
||
if (handRaised)
|
||
{
|
||
_raiseStartTime ??= DateTime.Now;
|
||
var holdElapsed = (DateTime.Now - _raiseStartTime.Value).TotalSeconds;
|
||
if (holdElapsed >= holdDuration)
|
||
{
|
||
// 举手达到指定时间,启动倒计时
|
||
_firstHandTriggered = true;
|
||
_countdownStartTime = DateTime.Now;
|
||
_lastCountdownSecond = countdownSeconds;
|
||
return (int)WavingAction.FirstHand;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
// 手放下,重置举手开始时间
|
||
_raiseStartTime = DateTime.Now;
|
||
}
|
||
|
||
continue; // 本人未触发,检测下一个人
|
||
}
|
||
|
||
// ---------- 第二步:倒计时逻辑 ----------
|
||
var countdownElapsed = (DateTime.Now - _countdownStartTime.Value).TotalSeconds;
|
||
int currentSecond = countdownSeconds - (int)Math.Floor(countdownElapsed);
|
||
|
||
if (currentSecond > 0 && currentSecond != _lastCountdownSecond)
|
||
{
|
||
_lastCountdownSecond = currentSecond;
|
||
Console.WriteLine($"倒计时:{currentSecond}");
|
||
return (int)WavingAction.Raising; // 倒计时中
|
||
}
|
||
|
||
if (countdownElapsed >= countdownSeconds)
|
||
{
|
||
ResetRaiseState();
|
||
return (int)WavingAction.RaiseHand; // 举手完成
|
||
}
|
||
|
||
return (int)WavingAction.Raising;
|
||
}
|
||
|
||
// 没有任何中心区域内的人举手
|
||
return (int)WavingAction.None;
|
||
}
|
||
public int DetectLeftHandRaise(List<Human> humans)
|
||
{
|
||
if (humans == null || humans.Count == 0)
|
||
return (int)WavingAction.None;
|
||
|
||
|
||
foreach (var human in humans)
|
||
{
|
||
if (human?.Keypoints == null)
|
||
continue;
|
||
|
||
// --- 筛选右脚踝坐标 ---
|
||
var rightAnkle = human.Keypoints.FirstOrDefault(k => k.Name == "right_ankle");
|
||
if (rightAnkle == null)
|
||
continue;
|
||
double xNorm = rightAnkle.X / 1920;
|
||
double yNorm = rightAnkle.Y / 1080;
|
||
// 仅检测中心区域内的人
|
||
if (!(xNorm >= 0.44 && xNorm <= 0.57 && yNorm >= 0.81))
|
||
continue;
|
||
|
||
// 获取左手关键点
|
||
var leftWrist = human.Keypoints.FirstOrDefault(k => k.Name == "left_wrist");
|
||
var leftElbow = human.Keypoints.FirstOrDefault(k => k.Name == "left_elbow");
|
||
|
||
if (leftWrist == null || leftElbow == null)
|
||
continue;
|
||
|
||
const double raiseThreshold = 60; // 举手阈值
|
||
|
||
// 判断左手是否举起
|
||
double verticalRise = leftElbow.Y - leftWrist.Y;
|
||
if (verticalRise >= raiseThreshold)
|
||
{
|
||
return (int)WavingAction.RaiseHand; // 一旦检测到左手举起,立即返回
|
||
}
|
||
}
|
||
|
||
return (int)WavingAction.None; // 没有检测到举手
|
||
}
|
||
|
||
private void ResetRaiseState()
|
||
{
|
||
_raiseStartTime = null;
|
||
_countdownStartTime = null;
|
||
_firstHandTriggered = false;
|
||
_lastCountdownSecond = 3;
|
||
}
|
||
|
||
private void DrawCirclesWithText()
|
||
{
|
||
userBox.Children.Clear();
|
||
_musicJumpRopeContext.Sports.Clear();
|
||
_musicJumpRopeContext.UserList.Clear();
|
||
_musicJumpRopeContext.UserNumberList.Clear();
|
||
_musicJumpRopeContext.UserBeatSyncList.Clear();
|
||
|
||
double imgWidth = userBox.ActualWidth;
|
||
double imgHeight = userBox.ActualHeight;
|
||
double radius = 100;
|
||
|
||
for (int i = 0; i < _musicJumpRopeContext.CirclePositions.Count; i++)
|
||
{
|
||
//var pos = _musicJumpRopeContext.CirclePositions[i == 0 ? 1 : 0];
|
||
|
||
var pos = _musicJumpRopeContext.CirclePositions[i];
|
||
|
||
double x = pos.XNorm * imgWidth;
|
||
double y = pos.YNorm * imgHeight;
|
||
|
||
// 绘制发光圆
|
||
var userItem = AddUserItem(x, y, i);
|
||
// 绑定运动对象
|
||
var sport = SportBase.Create("rope-skipping");
|
||
int indexCopy = i;
|
||
var currentItem = userItem;
|
||
|
||
// 订阅事件
|
||
sport.OnTicked += (count, times) =>
|
||
{
|
||
// 更新UI
|
||
//userItem.NumberText = count.ToString();
|
||
// 更新数字源
|
||
_musicJumpRopeContext.UserNumberList[indexCopy] = count.ToString();
|
||
|
||
if (userItem.ImageState != "2")
|
||
userItem.ImageState = "2";
|
||
//ChangeImgState(indexCopy, "2"); //改变当前图的状态
|
||
|
||
|
||
var currentTime = Utils.GetMusicCurrentTime();
|
||
int currentSecond = (int)Math.Floor(currentTime);
|
||
|
||
// 判断是否命中节拍
|
||
var beats = _musicJumpRopeContext.MusicBeats["1"];
|
||
bool hit = beats.Any(b => Math.Abs(b - currentTime) <= _beatTolerance);
|
||
if (hit)
|
||
{
|
||
_musicJumpRopeContext.UserBeatSyncList[indexCopy] += 1;
|
||
double left = _musicJumpRopeContext.UserBeatSyncList[0];
|
||
double right = _musicJumpRopeContext.UserBeatSyncList[1];
|
||
double total = left + right;
|
||
|
||
|
||
PkBar.LeftProgress = 1 - (total == 0 ? 0.5 : Math.Round(left / total, 2));
|
||
if (indexCopy == 0)
|
||
{
|
||
popSilder2.IsMiss = false;
|
||
//UpdateScore(left, score1);
|
||
score2.Text = (left * 10).ToString();
|
||
}
|
||
else
|
||
{
|
||
popSilder1.IsMiss = false;
|
||
//UpdateScore(right, score2);
|
||
score1.Text = (right * 10).ToString();
|
||
}
|
||
|
||
}
|
||
};
|
||
|
||
sport.Start();
|
||
_musicJumpRopeContext.Sports.Add(sport);
|
||
}
|
||
}
|
||
|
||
private void UpdateScore(double to, TextBlock score)
|
||
{
|
||
DoubleAnimation animation = new DoubleAnimation
|
||
{
|
||
From = double.Parse(score1.Text),
|
||
To = to,
|
||
Duration = TimeSpan.FromSeconds(0.2),
|
||
EasingFunction = new QuadraticEase { EasingMode = EasingMode.EaseOut },
|
||
FillBehavior = FillBehavior.HoldEnd
|
||
};
|
||
score.BeginAnimation(TextBlock.TextProperty, animation);
|
||
}
|
||
|
||
//private void ChangeImgState(int index , string state)
|
||
//{
|
||
// if(index== 0)
|
||
// {
|
||
// left1.Visibility = state == "1" ? Visibility.Visible : Visibility.Hidden;
|
||
// left2.Visibility = state == "2" ? Visibility.Visible : Visibility.Hidden;
|
||
// left3.Visibility = state == "3" ? Visibility.Visible : Visibility.Hidden;
|
||
// }else if (index == 1)
|
||
// {
|
||
// right1.Visibility = state == "1" ? Visibility.Visible : Visibility.Hidden;
|
||
// right2.Visibility = state == "1" ? Visibility.Visible : Visibility.Hidden;
|
||
// right3.Visibility = state == "1" ? Visibility.Visible : Visibility.Hidden;
|
||
// }
|
||
//}
|
||
|
||
public void ShowMusicAnimation()
|
||
{
|
||
ImageBehavior.SetRepeatBehavior(leftm, RepeatBehavior.Forever);
|
||
ImageBehavior.SetRepeatBehavior(rightm, RepeatBehavior.Forever);
|
||
ImageBehavior.GetAnimationController(leftm).Play();
|
||
ImageBehavior.GetAnimationController(rightm).Play();
|
||
}
|
||
|
||
private void UpdateCircleCounts(List<Human> humans)
|
||
{
|
||
double radiusNormX = 0.07;
|
||
double radiusNormY = 0.14;
|
||
|
||
for (int i = 0; i < _musicJumpRopeContext.CirclePositions.Count; i++)
|
||
{
|
||
var circleX = _musicJumpRopeContext.CirclePositions[i].XNorm;
|
||
var circleY = _musicJumpRopeContext.CirclePositions[i].YNorm;
|
||
|
||
Human humanInCircle = null;
|
||
|
||
// 找圈内的人
|
||
foreach (var hu in humans)
|
||
{
|
||
var rightFoot = hu.Keypoints.FirstOrDefault(k => k.Name == "right_ankle");
|
||
var leftFoot = hu.Keypoints.FirstOrDefault(k => k.Name == "left_ankle");
|
||
if (rightFoot == null || leftFoot == null)
|
||
continue;
|
||
|
||
double xRightNorm = rightFoot.X / userBox.ActualWidth;
|
||
double xLeftNorm = leftFoot.X / userBox.ActualWidth;
|
||
double yRightNorm = rightFoot.Y / userBox.ActualHeight;
|
||
double yLeftNorm = leftFoot.Y / userBox.ActualHeight;
|
||
|
||
bool outOfCircle =
|
||
xRightNorm < (circleX - radiusNormX) || xRightNorm > (circleX + radiusNormX) ||
|
||
xLeftNorm < (circleX - radiusNormX) || xLeftNorm > (circleX + radiusNormX) ||
|
||
yRightNorm < (circleY - radiusNormY) || yRightNorm > (circleY + radiusNormY) ||
|
||
yLeftNorm < (circleY - radiusNormY) || yLeftNorm > (circleY + radiusNormY);
|
||
|
||
if (!outOfCircle)
|
||
{
|
||
humanInCircle = hu;
|
||
break; // 每圈只处理一个人
|
||
}
|
||
}
|
||
|
||
// 根据是否有人和跳绳数字判断状态
|
||
bool hasHuman = humanInCircle != null;
|
||
lock (_updateLock)
|
||
{
|
||
_musicJumpRopeContext.UserList[i].ImageState = GetJumpState(i, hasHuman);
|
||
//var state = GetJumpState(i, hasHuman);
|
||
//ChangeImgState(i, state);
|
||
}
|
||
|
||
// 推送计数
|
||
//if (hasHuman)
|
||
_musicJumpRopeContext.Sports[i].Pushing(humanInCircle);
|
||
}
|
||
}
|
||
private string GetJumpState(int circleIndex, bool humanInCircle)
|
||
{
|
||
if (!humanInCircle)
|
||
{
|
||
// 无人 → 出圈
|
||
return "3";
|
||
}
|
||
|
||
string currentNumber = _musicJumpRopeContext.UserNumberList[circleIndex];
|
||
|
||
if (!_jumpStatus.ContainsKey(circleIndex))
|
||
{
|
||
_jumpStatus[circleIndex] = (currentNumber, DateTime.Now, "1"); // 初始状态先为停止
|
||
return "1";
|
||
}
|
||
var (lastNumber, lastChangeTime, lastState) = _jumpStatus[circleIndex];
|
||
|
||
if (currentNumber != lastNumber)
|
||
{
|
||
// 数字变化 → 跳绳中
|
||
_jumpStatus[circleIndex] = (currentNumber, DateTime.Now, "2");
|
||
return "2";
|
||
}
|
||
|
||
// 数字未变化,判断是否超过 2 秒
|
||
double elapsed = (DateTime.Now - lastChangeTime).TotalSeconds;
|
||
if (elapsed >= 0.8)
|
||
{
|
||
// 超过 2 秒未变化 → 停止
|
||
_jumpStatus[circleIndex] = (currentNumber, lastChangeTime, "1");
|
||
return "1";
|
||
}
|
||
// 维持上次状态
|
||
return lastState;
|
||
}
|
||
|
||
private MusicUserItem AddUserItem(double centerX, double centerY, int index)
|
||
{
|
||
var userItem = new MusicUserItem();
|
||
userItem.VerticalAlignment = VerticalAlignment.Top;
|
||
userItem.HorizontalAlignment = index == 1 ? HorizontalAlignment.Left : HorizontalAlignment.Right;
|
||
userItem.ImageState = "1";
|
||
userItem.Margin = index == 1 ? new Thickness(226, 345, 0, 0) : new Thickness(0, 345, 226, 0);
|
||
userBox.Children.Add(userItem);
|
||
_musicJumpRopeContext.UserList.Add(userItem);
|
||
_musicJumpRopeContext.UserNumberList.Add("0");
|
||
_musicJumpRopeContext.UserBeatSyncList.Add(0);
|
||
return userItem; //
|
||
}
|
||
|
||
private void Image_MouseDown(object sender, MouseButtonEventArgs e)
|
||
{
|
||
popSilder1.StartMarginAnimation();
|
||
popSilder2.StartMarginAnimation();
|
||
}
|
||
}
|
||
}
|