VPet/VPet-Simulator.Core/Graph/FoodAnimation.cs

181 lines
6.0 KiB
C#
Raw Normal View History

2023-05-31 17:47:23 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using static System.Windows.Forms.AxHost;
using System.Windows.Media;
using static VPet_Simulator.Core.Picture;
namespace VPet_Simulator.Core
{
public class FoodAnimation : IGraph
{
/// <summary>
/// 前层名字
/// </summary>
public string Front_Lay;
/// <summary>
/// 后层名字
/// </summary>
public string Back_Lay;
/// <summary>
/// 所有动画帧
/// </summary>
public List<Animation> Animations;
/// <summary>
/// 当前动画播放状态
/// </summary>
public bool PlayState { get; set; } = false;
/// <summary>
/// 当前动画是否执行ENDACTION
/// </summary>
private bool DoEndAction = true;
/// <summary>
/// 是否循环播放
/// </summary>
public bool IsLoop { get; set; }
/// <summary>
/// 是否循环播放
/// </summary>
public bool IsContinue { get; set; } = false;
public GameSave.ModeType ModeType { get; private set; }
public GraphCore.GraphType GraphType { get; private set; }
/// <summary>
/// 是否准备完成
/// </summary>
public bool IsReady { get; private set; } = false;
/// <summary>
/// 动画停止时运行的方法
/// </summary>
private Action StopAction;
int nowid;
/// <summary>
/// 图片资源
/// </summary>
public string Path;
private GraphCore GraphCore;
/// <summary>
/// 单帧动画
/// </summary>
public class Animation
{
private FoodAnimation parent;
public Thickness MarginWI;
public double Rotate;
/// <summary>
/// 帧时间
/// </summary>
public int Time;
/// <summary>
/// 创建单帧动画
/// </summary>
/// <param name="parent">FoodAnimation</param>
/// <param name="time">持续时间</param>
/// <param name="wx"></param>
public Animation(FoodAnimation parent, int time, Thickness wx, double rotate)
{
this.parent = parent;
Time = time;
MarginWI = wx;
Rotate = rotate;
}
/// <summary>
/// 运行该图层
/// </summary>
public void Run(FrameworkElement This, Action EndAction = null)
{
//先显示该图层
This.Dispatcher.Invoke(() =>
{
This.Margin = MarginWI;
This.LayoutTransform = new RotateTransform(Rotate);
});
//然后等待帧时间毫秒
Thread.Sleep(Time);
//判断是否要下一步
if (parent.PlayState)
{
if (++parent.nowid >= parent.Animations.Count)
if (parent.IsLoop)
parent.nowid = 0;
else if (parent.IsContinue)
{
parent.IsContinue = false;
parent.nowid = 0;
}
else
{
//parent.endwilldo = () => parent.Dispatcher.Invoke(Hidden);
//parent.Dispatcher.Invoke(Hidden);
parent.PlayState = false;
if (parent.DoEndAction)
EndAction?.Invoke();//运行结束动画时事件
parent.StopAction?.Invoke();
parent.StopAction = null;
////延时隐藏
//Task.Run(() =>
//{
// Thread.Sleep(25);
// parent.Dispatcher.Invoke(Hidden);
//});
return;
}
//要下一步,现在就隐藏图层
//隐藏该图层
//parent.Dispatcher.Invoke(Hidden);
parent.Animations[parent.nowid].Run(This, EndAction);
return;
}
else
{
parent.IsContinue = false;
//parent.Dispatcher.Invoke(Hidden);
if (parent.DoEndAction)
EndAction?.Invoke();//运行结束动画时事件
parent.StopAction?.Invoke();
parent.StopAction = null;
//Task.Run(() =>
//{
// Thread.Sleep(25);
// parent.Dispatcher.Invoke(Hidden);
//});
}
}
}
public static FoodAnimatGrid FoodGrid = new FoodAnimatGrid();
public class FoodAnimatGrid : Grid
{
public FoodAnimatGrid()
{
Width = 500;
Height = 500;
Front = new Image();
Back = new Image();
Food = new Image();
Food.RenderTransformOrigin = new Point(0.5, 0.5);
this.Children.Add(Front);
this.Children.Add(Back);
}
public Image Front;
public Image Food;
public Image Back;
}
public void Run(Border parant, Action EndAction = null)
{
throw new NotImplementedException();
}
public void Stop(bool StopEndAction = false)
{
throw new NotImplementedException();
}
}
}