Bullet bullet = new Bullet(new BulletDatas() { Code = pathAnimationPainter[n].AnimationCode, Loop = true, Type = BulletTypes.Animation }) { Z = 100 };
bullet.Center = bullet.Offset;
tabCanvas2.Children.Add(bullet);
Storyboard storyboard = new Storyboard();
PointAnimationUsingKeyFrames pointAnimationUsingKeyFrames = new PointAnimationUsingKeyFrames();
Storyboard.SetTarget(pointAnimationUsingKeyFrames, bullet);
Storyboard.SetTargetProperty(pointAnimationUsingKeyFrames, new PropertyPath("Position"));
DoubleAnimationUsingKeyFrames doubleAnimationUsingKeyFrames = new DoubleAnimationUsingKeyFrames();
Storyboard.SetTarget(doubleAnimationUsingKeyFrames, bullet);
Storyboard.SetTargetProperty(doubleAnimationUsingKeyFrames, new PropertyPath("Angle"));
double speed = pathAnimationPainter[n].Rate;
double a = 0.002; //加速系数
double timeSpanTemp = 0;
double durationTemp = 0;
double angleTemp = 0;
int circle = 0;
if (pathAnimationPainter[n].Easing == 2) { //加速逆行即减速
for (int i = 0; i < pathAnimationPainter[n].Path.Count; i++)
{ speed += a * i; }
}
for (int i = 0; i < pathAnimationPainter[n].Path.Count; i++) {
TimeSpan timeSpan = new TimeSpan();
Point lastPath = (i == 0 pathAnimationPainter[n].Path[0] : pathAnimationPainter[n].Path[i - 1]);
Point nowPath = pathAnimationPainter[n].Path[i];
Point nextPath = (i == pathAnimationPainter[n].Path.Count - 1 pathAnimationPainter[n].Path[pathAnimationPainter[n].Path.Count - 1] : pathAnimationPainter[n].Path[i + 1]);
switch (pathAnimationPainter[n].Easing) {
case 0:
timeSpanTemp = GlobalMethod.GetDistance(lastPath, nowPath) / speed;
durationTemp += timeSpanTemp;
timeSpan = TimeSpan.FromMilliseconds(durationTemp);
break;
case 1:
speed += a * i;
timeSpanTemp = GlobalMethod.GetDistance(lastPath, nowPath) / speed;
durationTemp += timeSpanTemp;
timeSpan = TimeSpan.FromMilliseconds(durationTemp);
break;
case 2:
speed -= a * (pathAnimationPainter[n].Path.Count - 1 - i);
timeSpanTemp = GlobalMethod.GetDistance(lastPath, nowPath) / speed;
durationTemp += timeSpanTemp;
timeSpan = TimeSpan.FromMilliseconds(durationTemp);
break;
}
pointAnimationUsingKeyFrames.KeyFrames.Add(
new LinearPointKeyFrame() {
KeyTime = KeyTime.FromTimeSpan(timeSpan),
Value = new Point() { X = nowPath.X * pathAnimationPainter[n].Proportion, Y = nowPath.Y * pathAnimationPainter[n].Proportion }
}
);
double angle = GlobalMethod.GetAngle(nowPath.Y - lastPath.Y, nowPath.X - lastPath.X) + 360 * circle;
//check大于0为顺时针个方向
double check = (nowPath.X - lastPath.X) * (nextPath.Y - nowPath.Y) - (nowPath.Y - lastPath.Y) * (nextPath.X - nowPath.X);
if (check > 0) {
if (angleTemp > 360 * circle && angle < 360 * circle) { angle += 360; } else if (angleTemp > 360 * circle + 180 && angle < angleTemp) { angle += 360; circle++; }
} else {
if (angleTemp < 360 * circle && angle > 360 * circle) { angle -= 360; } else if (angleTemp < 360 * circle - 180 && angle > angleTemp) { angle -= 360; circle--; }
}
angleTemp = angle; //用于旋转时的角度衔接
doubleAnimationUsingKeyFrames.KeyFrames.Add(
new LinearDoubleKeyFrame() {
KeyTime = KeyTime.FromTimeSpan(timeSpan),
Value = angle
}
);
}
pointAnimationUsingKeyFrames.Duration = new Duration(TimeSpan.FromMilliseconds(durationTemp));
storyboard.Children.Add(pointAnimationUsingKeyFrames);
if (pathAnimationPainter[n].Direction == 0) { storyboard.Children.Add(doubleAnimationUsingKeyFrames); }
EventHandler handler = null;
storyboard.Completed += handler = delegate {
storyboard.Completed -= handler;
tabCanvas2.Children.Remove(bullet);
bullet.Move_Completed(bullet, null);
};
storyboard.Begin();