WEB开发网
开发学院软件开发C语言 C#开发WPF/Silverlight动画及游戏系列教程(Game C... 阅读

C#开发WPF/Silverlight动画及游戏系列教程(Game Course):(十六) 牵引式地图移动模式②

 2009-06-24 07:08:21 来源:WEB开发网   
核心提示: 首先定义两个函数SpiritGameX、SpiritGameY用于将主角在地图坐标系中的坐标换算成游戏寻路坐标系中的坐标: int GridSize = 20; //单位格子大小 int SpiritCenterX = 4; //主角脚底离主角图片左边的距离(寻路坐标系中) int Sp

首先定义两个函数SpiritGameX、SpiritGameY用于将主角在地图坐标系中的坐标换算成游戏寻路坐标系中的坐标:

        int GridSize = 20; //单位格子大小
        int SpiritCenterX = 4; //主角脚底离主角图片左边的距离(寻路坐标系中)
        int SpiritCenterY = 5; //主角脚底离主角顶部的距离(寻路坐标系中)
        //将主角的坐标换算成游戏坐标系中的坐标(缩小操作)
        int GameSystemX {
            get { return ((int)(Spirit.X) / GridSize) + SpiritCenterX; }
        }
        int GameSystemY {
            get { return ((int)(Spirit.Y) / GridSize) + SpiritCenterY; }
        }

接下来就是对A*寻路移动方法进行改造了:

        private void AStarMoveTo(Point p) {
            //进行坐标系缩小
            int start_x = GameSystemX;
            int start_y = GameSystemY;
            Start = new System.Drawing.Point(start_x, start_y); //设置起点坐标
            int end_x = (int)p.X / GridSize;
            int end_y = (int)p.Y / GridSize;
            End = new System.Drawing.Point(end_x, end_y); //设置终点坐标

            if (path == null) {
                //MessageBox.Show("路径不存在!");
            } else {
                ......
                //创建X轴方向逐帧动画
                DoubleAnimationUsingKeyFrames keyFramesAnimationX = new DoubleAnimationUsingKeyFrames();
                //总共花费时间 = path.Count * cost
                keyFramesAnimationX.Duration = new Duration(TimeSpan.FromMilliseconds(path.Count * cost));
                Storyboard.SetTarget(keyFramesAnimationX, Spirit);
                Storyboard.SetTargetProperty(keyFramesAnimationX, new PropertyPath("X"));
                //创建Y轴方向逐帧动画
                DoubleAnimationUsingKeyFrames keyFramesAnimationY = new DoubleAnimationUsingKeyFrames();
                keyFramesAnimationY.Duration = new Duration(TimeSpan.FromMilliseconds(path.Count * cost));
                Storyboard.SetTarget(keyFramesAnimationY, Spirit);
                Storyboard.SetTargetProperty(keyFramesAnimationY, new PropertyPath("Y"));
                for (int i = 0; i < framePosition.Count(); i++) {
                    //加入X轴方向的匀速关键帧
                    LinearDoubleKeyFrame keyFrame = new LinearDoubleKeyFrame();
                    //平滑衔接动画(将寻路坐标系中的坐标放大回地图坐标系中的坐标)
                    keyFrame.Value = i == 0 ? Spirit.X : (framePosition[i].X - SpiritCenterX) * GridSize;
                    keyFrame.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(cost * i));
                    keyFramesAnimationX.KeyFrames.Add(keyFrame);
                    //加入X轴方向的匀速关键帧
                    keyFrame = new LinearDoubleKeyFrame();
                    keyFrame.Value = i == 0 ? Spirit.Y : (framePosition[i].Y - SpiritCenterY) * GridSize;
                    keyFrame.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(cost * i));
                    keyFramesAnimationY.KeyFrames.Add(keyFrame);
                }
                ......
            }
        }

上一页  1 2 3 4  下一页

Tags:开发 WPF Silverlight

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接