protected override void HeartTick(object sender, EventArgs e) { string key = string.Format("{0}-{1}-{2}", (int)State, (int)Direction, frame.Current); if (IsResReady) { #region 方案1(资源布局合理,内存占用较少,但性能低) //BodyStream = Application.GetResourceStream(resList[Code], new Uri(string.Format("{0}.png", key), UriKind.Relative)).Stream; //BodyPosition = frameOffset[key]; #endregion #region 方案2(牺牲内存换高性能,如配合上独立存储效果将非常棒) string value = string.Format("{0}/{1}", Code, key); if (!bodyList.ContainsKey(value)) { BitmapImage bi = new BitmapImage(); bi.SetSource(Application.GetResourceStream(resList[Code], new Uri(string.Format("{0}.png", key), UriKind.Relative)).Stream); Image img = new Image() { Source = bi }; WriteableBitmap wb = new WriteableBitmap(bi.PixelWidth + frameOffset[key].X, bi.PixelHeight + frameOffset[key].Y); wb.Render(img, new TranslateTransform() { X = frameOffset[key].X, Y = frameOffset[key].Y }); wb.Invalidate(); bodyList.Add(value, wb); } BodySource = bodyList[value]; BodyPosition = new Point2D(0, 0); #endregion #region 方案3(资源暴露,内存少,下载对象多易导致卡UI以及山所等现象,不过性能较好) //BodySource = Global.GetWebImage(string.Format("Sprite/{0}/{1}.png", Code, key)); //BodyPosition = frameOffset[key]; #endregion } else { if (State == States.Casting) { key = string.Format("2-{0}-{1}", (int)Direction, frame.Current); }; //测试用(模型没魔法图) BodySource = Global.GetProjectImage(string.Format("Model/Sprite/{0}/{1}.png", ModelCode, key)); BodyPosition = new Point2D((int)((RealWidth - 150) / 2), (int)(LifeCenterY - 7)); } switch (State) { case States.Attack: if (frame.Current == frames.AttackEffect) { if (DoAttack != null) { DoAttack(this, e); } } break; case States.Casting: if (frame.Current == frames.CastingEffect) { if (DoCasting != null) { //解析具体等级魔法参数 XElement args = Global.ResInfos["AnimationInfo"].DescendantsAndSelf("Animations").Elements().Single(X => X.Attribute("Code").Value == magicArgs.Code.ToString()).Element("Levels").Elements().Single(X => X.Attribute("ID").Value == magicArgs.Level.ToString()); magicArgs.DamageMin = (int)(args.Attribute("DamageMin")); magicArgs.DamageMax = (int)(args.Attribute("DamageMax")); magicArgs.Radius = (int)(args.Attribute("Radius")); magicArgs.MagicType = (MagicTypes)(int)(args.Attribute("MagicType")); magicArgs.SpecialEffect = (SpecialEffects)(int)(args.Attribute("SpecialEffect")); magicArgs.AdditionalEffect = (AdditionalEffects)(int)(args.Attribute("AdditionalEffect")); magicArgs.Number = (int)(args.Attribute("Number")); DoCasting(this, new DoCastingEventArgs() { MagicArgs = magicArgs }); } Stand(); } break; } frame.Current++; if (frame.Current > frame.Total) { frame.Current = 0; switch (State) { case States.Injure: Stand(); break; } } //进行攻击侦测 if (FightDetect != null) { FightDetect(this, e); } }
/// <summary> /// 配置文件下载完毕 /// </summary> void infoDownloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { WebClient infoDownloader = sender as WebClient; infoDownloader.OpenReadCompleted -= infoDownloader_OpenReadCompleted; info = XElement.Load(e.Result); //通过LINQ2XML解析配置文件 XElement xScene = info.DescendantsAndSelf("Scene").Single(); //加载地图参数 FullName = xScene.Attribute("FullName").Value; Media = xScene.Attribute("Media").Value; BodyWidth = (double)xScene.Attribute("MapWidth"); BodyHeight = (double)xScene.Attribute("MapHeight"); //解析地形 Offset = new Point2D((int)xScene.Attribute("OffsetX"), (int)xScene.Attribute("OffsetY")); RenderTransform = new TranslateTransform() { X = Offset.X, Y = Offset.Y }; BodyTransform = new TranslateTransform() { X = -Offset.X, Y = -Offset.Y }; TerrainGridSize = (int)xScene.Attribute("TerrainGridSize"); TerrainGradient = (double)xScene.Attribute("TerrainGradient"); TerrainMatrix = new byte[(int)xScene.Attribute("TerrainMatrixDimension"), (int)xScene.Attribute("TerrainMatrixDimension")]; teleportMatrix = new byte[(int)xScene.Attribute("TerrainMatrixDimension"), (int)xScene.Attribute("TerrainMatrixDimension")]; string[] terrain = xScene.Attribute("Terrain").Value.Split(','); for (int y = 0; y < TerrainMatrix.GetUpperBound(1); y++) { for (int x = 0; x < TerrainMatrix.GetUpperBound(0); x++) { //设置默认值,可以通过的均在矩阵中用1表示 TerrainMatrix[x, y] = 1; //设置传送默认值 teleportMatrix[x, y] = 255; } } for (int i = 0; i < terrain.Count(); i++) { if (terrain[i] != "") { string[] position = terrain[i].Split('_'); TerrainMatrix[Convert.ToByte(position[0]), Convert.ToByte(position[1])] = Convert.ToByte(position[2]); } } DynamicMatrix = TerrainMatrix.Clone() as byte[,]; //解析传送点 IEnumerable<XElement> iTeleport = xScene.Element("Teleports").Elements(); for (int i = 0; i < iTeleport.Count(); i++) { XElement xTeleport = iTeleport.ElementAt(i); Teleport teleport = new Teleport() { Code = (int)xTeleport.Attribute("Code"), ToScene = (int)xTeleport.Attribute("ToScene"), ToCoordinate = new Point((double)xTeleport.Attribute("ToX"), (double)xTeleport.Attribute("ToY")), ToDirection = (Directions)(int)xTeleport.Attribute("ToDirection"), }; teleports.Add(teleport); string[] teleportTerrain = xTeleport.Attribute("Terrain").Value.Split(','); for (int j = 0; j < teleportTerrain.Count(); j++) { if (teleportTerrain[j] != "") { string[] position = teleportTerrain[j].Split('_'); teleportMatrix[Convert.ToByte(position[0]), Convert.ToByte(position[1])] = (byte)teleport.Code; } } } HeartStart(); //下载缩略图 WebClient miniMapDownloader = new WebClient(); miniMapDownloader.OpenReadCompleted += new OpenReadCompletedEventHandler(miniMapDownloader_OpenReadCompleted); miniMapDownloader.OpenReadAsync(new Uri(Global.WebPath(string.Format("Scene/{0}/MiniMap.jpg", e.UserState)), UriKind.Relative), string.Format("{0},{1}", e.UserState, index)); }