diff --git a/DungeonShooting_Godot/src/framework/map/RoomInfo.cs b/DungeonShooting_Godot/src/framework/map/RoomInfo.cs index 0f7abad..600f1d1 100644 --- a/DungeonShooting_Godot/src/framework/map/RoomInfo.cs +++ b/DungeonShooting_Godot/src/framework/map/RoomInfo.cs @@ -77,7 +77,7 @@ /// /// 静态精灵绘制画布 /// - public RoomStaticSpriteCanvas StaticSpriteCanvas; + public RoomStaticImageCanvas StaticImageCanvas; /// /// 是否处于闭关状态, 也就是房间门没有主动打开 @@ -180,9 +180,9 @@ } ActivityMarks.Clear(); - if (StaticSpriteCanvas != null) + if (StaticImageCanvas != null) { - StaticSpriteCanvas.Destroy(); + StaticImageCanvas.Destroy(); } } diff --git a/DungeonShooting_Godot/src/framework/map/RoomStaticImageCanvas.cs b/DungeonShooting_Godot/src/framework/map/RoomStaticImageCanvas.cs new file mode 100644 index 0000000..f89009b --- /dev/null +++ b/DungeonShooting_Godot/src/framework/map/RoomStaticImageCanvas.cs @@ -0,0 +1,42 @@ + +using Godot; + +public class RoomStaticImageCanvas : IDestroy +{ + public bool IsDestroyed { get; private set; } + /// + /// 画布节点实例 + /// + public ImageCanvas CanvasSprite { get; } + /// + /// 房间坐标相对于画布坐标偏移量, 单位: 像素 + /// + public Vector2I RoomOffset { get; set; } + + public RoomStaticImageCanvas(Node root, Vector2I position, int width, int height) + { + CanvasSprite = new ImageCanvas(width, height); + //CanvasSprite.Clear(new Color(1, 1, 1, 0.2f)); + CanvasSprite.GlobalPosition = position; + root.AddChild(CanvasSprite); + } + + /// + /// 将世界坐标转为画布下的坐标 + /// + public Vector2I ToImageCanvasPosition(Vector2 pos) + { + pos = pos - CanvasSprite.GlobalPosition; + return new Vector2I((int)pos.X, (int)pos.Y); + } + + public void Destroy() + { + if (IsDestroyed) + { + return; + } + + IsDestroyed = true; + } +} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/framework/map/RoomStaticSpriteCanvas.cs b/DungeonShooting_Godot/src/framework/map/RoomStaticSpriteCanvas.cs deleted file mode 100644 index e4e8ecb..0000000 --- a/DungeonShooting_Godot/src/framework/map/RoomStaticSpriteCanvas.cs +++ /dev/null @@ -1,33 +0,0 @@ - -using Godot; - -public class RoomStaticSpriteCanvas : IDestroy -{ - public bool IsDestroyed { get; private set; } - /// - /// 画布节点实例 - /// - public ImageCanvas CanvasSprite { get; } - /// - /// 房间坐标相对于画布坐标偏移量, 单位: 像素 - /// - public Vector2I RoomOffset { get; set; } - - public RoomStaticSpriteCanvas(Node root, Vector2I position, int width, int height) - { - CanvasSprite = new ImageCanvas(width, height); - //CanvasSprite.Clear(new Color(1, 1, 1, 0.2f)); - CanvasSprite.GlobalPosition = position; - root.AddChild(CanvasSprite); - } - - public void Destroy() - { - if (IsDestroyed) - { - return; - } - - IsDestroyed = true; - } -} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/framework/map/image/ImageCanvas.cs b/DungeonShooting_Godot/src/framework/map/image/ImageCanvas.cs index dcd939c..68e1f15 100644 --- a/DungeonShooting_Godot/src/framework/map/image/ImageCanvas.cs +++ b/DungeonShooting_Godot/src/framework/map/image/ImageCanvas.cs @@ -50,6 +50,7 @@ /// 添加到预渲染队列中 /// /// 需要渲染的纹理 + /// 渲染材质, 不需要则传null /// 离画布左上角x坐标 /// 离画布左上角y坐标 /// 旋转角度, 角度制 @@ -57,12 +58,13 @@ /// 旋转中心点y /// 是否翻转y轴 /// 绘制完成的回调函数 - public void DrawImageInCanvas(Texture2D texture, int x, int y, float angle, int centerX, int centerY, bool flipY, Action onDrawingComplete = null) + public void DrawImageInCanvas(Texture2D texture, Material material, int x, int y, float angle, int centerX, int centerY, bool flipY, Action onDrawingComplete = null) { var item = new ImageRenderData(); item.OnDrawingComplete = onDrawingComplete; item.ImageCanvas = this; item.SrcImage = texture.GetImage(); + item.Material = material; var width = item.SrcImage.GetWidth(); var height = item.SrcImage.GetHeight(); if (width > 128) @@ -129,7 +131,30 @@ _queueItems.Enqueue(item); } - + + public void DrawActivityObjectInCanvas(ActivityObject activityObject, Action onDrawingComplete = null) + { + if (activityObject.AffiliationArea == null) + { + return; + } + var staticImageCanvas = activityObject.AffiliationArea.RoomInfo.StaticImageCanvas; + var texture = activityObject.GetCurrentTexture(); + if (texture != null) + { + var pos = staticImageCanvas.ToImageCanvasPosition(activityObject.GlobalPosition); + var spriteOffset = activityObject.AnimatedSprite.Offset; + var centerX = (int)-spriteOffset.X; + var centerY = (int)-spriteOffset.Y; + DrawImageInCanvas( + texture, activityObject.AnimatedSprite.Material, + pos.X, pos.Y, + activityObject.AnimatedSprite.GlobalRotationDegrees, + centerX, centerY, false + ); + } + } + public void Destroy() { if (IsDestroyed) diff --git a/DungeonShooting_Godot/src/framework/map/image/ImageCanvas_Static.cs b/DungeonShooting_Godot/src/framework/map/image/ImageCanvas_Static.cs index bf021fa..65c3611 100644 --- a/DungeonShooting_Godot/src/framework/map/image/ImageCanvas_Static.cs +++ b/DungeonShooting_Godot/src/framework/map/image/ImageCanvas_Static.cs @@ -322,6 +322,8 @@ item.SrcImage.FlipY(); } } + + renderSprite.Sprite.Material = item.Material; renderSprite.Sprite.Offset = new Vector2(-item.CenterX, -item.CenterY); //设置旋转 diff --git a/DungeonShooting_Godot/src/framework/map/image/ImageRenderData.cs b/DungeonShooting_Godot/src/framework/map/image/ImageRenderData.cs index 6b05360..9d675a8 100644 --- a/DungeonShooting_Godot/src/framework/map/image/ImageRenderData.cs +++ b/DungeonShooting_Godot/src/framework/map/image/ImageRenderData.cs @@ -14,6 +14,10 @@ /// public Image SrcImage; /// + /// 渲染材质 + /// + public Material Material; + /// /// x坐标 /// public int X; diff --git a/DungeonShooting_Godot/src/game/room/DungeonManager.cs b/DungeonShooting_Godot/src/game/room/DungeonManager.cs index 4c9cd0d..7d1cadd 100644 --- a/DungeonShooting_Godot/src/game/room/DungeonManager.cs +++ b/DungeonShooting_Godot/src/game/room/DungeonManager.cs @@ -397,13 +397,13 @@ minY -= GameConfig.TileCellSize; maxX += GameConfig.TileCellSize; maxY += GameConfig.TileCellSize; - var staticSpriteCanvas = new RoomStaticSpriteCanvas( + var staticSpriteCanvas = new RoomStaticImageCanvas( _world.StaticSpriteRoot, new Vector2I(minX, minY), maxX - minX, maxY - minY ); staticSpriteCanvas.RoomOffset = new Vector2I(worldPos.X - minX, worldPos.Y - minY); - roomInfo.StaticSpriteCanvas = staticSpriteCanvas; + roomInfo.StaticImageCanvas = staticSpriteCanvas; } /// diff --git a/DungeonShooting_Godot/src/test/TestOptimizeSprite.cs b/DungeonShooting_Godot/src/test/TestOptimizeSprite.cs index fcb299b..bc8bfa3 100644 --- a/DungeonShooting_Godot/src/test/TestOptimizeSprite.cs +++ b/DungeonShooting_Godot/src/test/TestOptimizeSprite.cs @@ -25,7 +25,7 @@ var centerX = Utils.RandomRangeInt(0, texture.GetWidth()); var centerY = Utils.RandomRangeInt(0, texture.GetHeight()); var angle = Utils.RandomRangeInt(0, 360); - imageCanvas.DrawImageInCanvas(texture, x, y, + imageCanvas.DrawImageInCanvas(texture, null, x, y, angle, centerX, centerY, Utils.RandomBoolean(), () => {