public void SetField2DBackground(Field2D field, Dictionary <object, object> pathMap, String fileName) { SetField2D(field); LPixmap background = null; if (fileName != null) { LPixmap tmp = new LPixmap(fileName); background = SetTileBackground(tmp, true); if (tmp != null) { tmp.Dispose(); tmp = null; } } else { background = new LPixmap(GetWidth(), GetHeight(), false); } for (int i = 0; i < field.GetWidth(); i++) { for (int j = 0; j < field.GetHeight(); j++) { int index = field.GetType(j, i); Object o = CollectionUtils.Get(pathMap, index); if (o != null) { if (o is LPixmap) { background.DrawPixmap(((LPixmap)o), field.TilesToWidthPixels(i), field.TilesToHeightPixels(j)); } else if (o is Actor) { AddObject(((Actor)o), field.TilesToWidthPixels(i), field.TilesToHeightPixels(j)); } } } } SetBackground(background.Texture); if (background != null) { background.Dispose(); background = null; } }
public override void Update(long elapsedTime) { if (layerMap == null || original == null || pActorPath == null) { return; } lock (pActorPath) { if (synchroLayerField) { if (original != null) { Field2D field = original.GetField2D(); if (field != null && layerMap != field) { this.layerMap = field; } } } if (endX == startX && endY == startY) { if (pActorPath.Count > 1) { Vector2f moveStart = pActorPath[0]; Vector2f moveEnd = pActorPath[1]; startX = layerMap.TilesToWidthPixels(moveStart.X()); startY = layerMap.TilesToHeightPixels(moveStart.Y()); endX = moveEnd.X() * layerMap.GetTileWidth(); endY = moveEnd.Y() * layerMap.GetTileHeight(); moveX = moveEnd.X() - moveStart.X(); moveY = moveEnd.Y() - moveStart.Y(); if (moveX > -2 && moveY > -2 && moveX < 2 && moveY < 2) { direction = Loon.Action.Map.Field2D.GetDirection(moveX, moveY, direction); } } CollectionUtils.RemoveAt(pActorPath, 0); } switch (direction) { case Config.TUP: startY -= speed; if (startY < endY) { startY = endY; } break; case Config.TDOWN: startY += speed; if (startY > endY) { startY = endY; } break; case Config.TLEFT: startX -= speed; if (startX < endX) { startX = endX; } break; case Config.TRIGHT: startX += speed; if (startX > endX) { startX = endX; } break; case Config.UP: startX += speed; startY -= speed; if (startX > endX) { startX = endX; } if (startY < endY) { startY = endY; } break; case Config.DOWN: startX -= speed; startY += speed; if (startX < endX) { startX = endX; } if (startY > endY) { startY = endY; } break; case Config.LEFT: startX -= speed; startY -= speed; if (startX < endX) { startX = endX; } if (startY < endY) { startY = endY; } break; case Config.RIGHT: startX += speed; startY += speed; if (startX > endX) { startX = endX; } if (startY > endY) { startY = endY; } break; } lock (original) { original.SetLocation(startX + offsetX, startY + offsetY); } } }
public virtual void SetField2DBackground(Field2D field, Dictionary <object, object> pathMap, string fileName) { SetField2D(field); LImage background = null; if (fileName != null) { LImage tmp = LImage.CreateImage(fileName); background = SetTileBackground(tmp, true); if (tmp != null) { tmp.Dispose(); tmp = null; } } else { background = LImage.CreateImage(GetWidth(), GetHeight(), false); } int srcWidth = GetWidth(); int srcHeight = GetHeight(); //在C#环境下LGraphics采取像素渲染,得不到硬件加速,此处直接将像素操作方法粘过来了(虽然也快不了几毫秒……) int[] dstColors = background.GetIntPixels(); int[] srcColors = null; for (int i = 0; i < field.GetWidth(); i++) { for (int j = 0; j < field.GetHeight(); j++) { int index = field.GetType(j, i); object o = CollectionUtils.Get(pathMap, index); if (o != null) { if (o is LImage) { LImage img = (LImage)o; srcColors = img.GetIntPixels(); int w = img.Width; int h = img.Height; int y = field.TilesToHeightPixels(j); int x = field.TilesToWidthPixels(i); if (x < 0) { w += x; x = 0; } if (y < 0) { h += y; y = 0; } if (x + w > srcWidth) { w = srcWidth - x; } if (y + h > srcHeight) { h = srcHeight - y; } if (img.hasAlpha) { int findIndex = y * srcWidth + x; int drawIndex = 0; int moveFind = srcWidth - w; for (int col = 0; col < h; col++) { for (int row = 0; row < w;) { if (srcColors[drawIndex] != 0) { dstColors[findIndex] = srcColors[drawIndex]; } row++; findIndex++; drawIndex++; } findIndex += moveFind; } } else { for (int size = 0; size < h; size++) { System.Array.Copy(srcColors, size * w, dstColors, (y + size) * srcWidth + x, w); } } } else if (o is Actor) { AddObject(((Actor)o), field.TilesToWidthPixels(i), field.TilesToHeightPixels(j)); } } } } background.SetIntPixels(dstColors); background.SetFormat(Loon.Core.Graphics.Opengl.LTexture.Format.SPEED); SetBackground(background.GetTexture()); srcColors = null; dstColors = null; if (background != null) { background.Dispose(); background = null; } }