diff --git a/DungeonShooting_Godot/src/framework/common/NodeExtend.cs b/DungeonShooting_Godot/src/framework/common/NodeExtend.cs index ebc149f..deee5c1 100644 --- a/DungeonShooting_Godot/src/framework/common/NodeExtend.cs +++ b/DungeonShooting_Godot/src/framework/common/NodeExtend.cs @@ -73,6 +73,22 @@ { return (control.SizeFlagsHorizontal & Control.SizeFlags.Expand) != 0; } + + /// + /// 设置是否启用节点 + /// + public static void SetActive(this Node node, bool value) + { + if (node is CanvasItem canvasItem) + { + canvasItem.Visible = value; + } + node.SetProcess(value); + node.SetPhysicsProcess(value); + node.SetProcessInput(value); + node.SetPhysicsProcessInternal(value); + node.SetProcessInput(value); + } /// /// 延时指定时间调用一个回调函数 diff --git a/DungeonShooting_Godot/src/framework/map/fog/FogMask.cs b/DungeonShooting_Godot/src/framework/map/fog/FogMask.cs index b80dc10..117e128 100644 --- a/DungeonShooting_Godot/src/framework/map/fog/FogMask.cs +++ b/DungeonShooting_Godot/src/framework/map/fog/FogMask.cs @@ -8,8 +8,18 @@ /// public partial class FogMask : PointLight2D, IDestroy { - public int Width { get; private set; } - public int Height { get; private set; } + /// + /// 迷雾宽度 + /// + public int FogWidth { get; private set; } + /// + /// 迷雾高度 + /// + public int FogHeight { get; private set; } + /// + /// 迷雾透明度值, 这个值在调用 TransitionAlpha() 时改变, 用于透明度判断 + /// + public float TargetAlpha { get; private set; } public bool IsDestroyed { get; private set; } private bool _init = false; @@ -87,9 +97,9 @@ ); //创建光纹理 - Width = (size.X + 2) * GameConfig.TileCellSize; - Height = (size.Y + 2) * GameConfig.TileCellSize; - var img = Image.Create(Width, Height, false, Image.Format.Rgba8); + FogWidth = (size.X + 2) * GameConfig.TileCellSize; + FogHeight = (size.Y + 2) * GameConfig.TileCellSize; + var img = Image.Create(FogWidth, FogHeight, false, Image.Format.Rgba8); img.Fill(Colors.White); //处理边缘过渡 @@ -108,6 +118,7 @@ /// 过渡时间 public void TransitionAlpha(float targetAlpha, float time) { + TargetAlpha = targetAlpha; if (_cid >= 0) { World.Current.StopCoroutine(_cid); diff --git a/DungeonShooting_Godot/src/framework/map/fog/PreviewFogMask.cs b/DungeonShooting_Godot/src/framework/map/fog/PreviewFogMask.cs new file mode 100644 index 0000000..008d4cb --- /dev/null +++ b/DungeonShooting_Godot/src/framework/map/fog/PreviewFogMask.cs @@ -0,0 +1,108 @@ + +using Godot; + +/// +/// 绑定在门上的预览迷雾 +/// +public partial class PreviewFogMask : PointLight2D, IDestroy +{ + public enum PreviewFogType + { + Aisle, + Room + } + + public bool IsDestroyed { get; private set; } + + private static bool _initTexture; + private static Texture2D _previewAisle; + private static Texture2D _previewRoom; + + /// + /// 房间门 + /// + public RoomDoorInfo DoorInfo; + + /// + /// 迷雾类型 + /// + public PreviewFogType FogType { get; private set; } + + private float _previewAisleAngle; + private float _previewRoomAngle; + private Vector2 _previewAislePosition; + private Vector2 _previewRoomPosition; + + private static void InitTexture() + { + if (_initTexture) + { + return; + } + + _initTexture = true; + _previewAisle = ResourceManager.LoadTexture2D(ResourcePath.resource_sprite_map_PreviewTransition_png); + _previewRoom = ResourceManager.LoadTexture2D(ResourcePath.resource_sprite_map_PreviewTransition2_png); + } + + /// + /// 初始化迷雾 + /// + public void Init(RoomDoorInfo doorInfo) + { + InitTexture(); + + DoorInfo = doorInfo; + if (doorInfo.Direction == DoorDirection.E) + { + _previewAislePosition = doorInfo.Door.GlobalPosition + new Vector2(16, 0); + _previewAisleAngle = 90; + } + else if (doorInfo.Direction == DoorDirection.W) + { + _previewRoomPosition = doorInfo.Door.GlobalPosition + new Vector2(16, 0); + _previewRoomAngle = 90; + } + + RefreshPreviewFogType(PreviewFogType.Aisle); + } + + /// + /// 更新预览迷雾类型 + /// + public void SetPreviewFogType(PreviewFogType fogType) + { + if (FogType != fogType) + { + RefreshPreviewFogType(fogType); + } + } + + private void RefreshPreviewFogType(PreviewFogType fogType) + { + FogType = fogType; + if (fogType == PreviewFogType.Aisle) + { + Texture = _previewAisle; + Position = _previewAislePosition; + RotationDegrees = _previewAisleAngle; + } + else + { + Texture = _previewRoom; + Position = _previewRoomPosition; + RotationDegrees = _previewRoomAngle; + } + } + + public void Destroy() + { + if (IsDestroyed) + { + return; + } + + IsDestroyed = true; + QueueFree(); + } +} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/framework/map/room/RoomDoorInfo.cs b/DungeonShooting_Godot/src/framework/map/room/RoomDoorInfo.cs index ebe9183..ec4dcaa 100644 --- a/DungeonShooting_Godot/src/framework/map/room/RoomDoorInfo.cs +++ b/DungeonShooting_Godot/src/framework/map/room/RoomDoorInfo.cs @@ -70,6 +70,11 @@ public AisleFogArea AisleFogArea; /// + /// 门区域预览迷雾 + /// + public PreviewFogMask PreviewFogMask; + + /// /// 当前门连接的过道是否探索过 /// public bool IsExplored { get; private set; } = false; @@ -252,6 +257,7 @@ ConnectDoor.IsExplored = true; if (FogMask != null && Math.Abs(FogMask.Color.A - 1f) > 0.001f) { + RefreshPreviewFog(1); FogMask.TransitionAlpha(1, 0.3f); } } @@ -265,7 +271,48 @@ ConnectDoor.IsExplored = true; if (FogMask != null && Math.Abs(FogMask.Color.A - GameConfig.DarkFogAlpha) > 0.001f) { + RefreshPreviewFog(GameConfig.DarkFogAlpha); FogMask.TransitionAlpha(GameConfig.DarkFogAlpha, 0.3f); } } + + /// + /// 刷新房间中的预览迷雾 + /// + /// 过道迷雾透明度 + public void RefreshPreviewFog(float aisleFogAlpha) + { + var room1Alpha = RoomInfo.FogMask.TargetAlpha; + var room2Alpha = ConnectDoor.RoomInfo.FogMask.TargetAlpha; + + if (aisleFogAlpha < 1 && room1Alpha < 1) //隐藏预览 + { + PreviewFogMask.SetActive(false); + } + else if (aisleFogAlpha >= 1) //预览房间 + { + PreviewFogMask.SetActive(true); + PreviewFogMask.SetPreviewFogType(PreviewFogMask.PreviewFogType.Room); + } + else if (room1Alpha >= 1) //预览过道 + { + PreviewFogMask.SetActive(true); + PreviewFogMask.SetPreviewFogType(PreviewFogMask.PreviewFogType.Aisle); + } + + if (aisleFogAlpha < 1 && room2Alpha < 1) //隐藏预览 + { + ConnectDoor.PreviewFogMask.SetActive(false); + } + else if (aisleFogAlpha >= 1) //预览房间 + { + ConnectDoor.PreviewFogMask.SetActive(true); + ConnectDoor.PreviewFogMask.SetPreviewFogType(PreviewFogMask.PreviewFogType.Room); + } + else if (room2Alpha >= 1) //预览过道 + { + ConnectDoor.PreviewFogMask.SetActive(true); + ConnectDoor.PreviewFogMask.SetPreviewFogType(PreviewFogMask.PreviewFogType.Aisle); + } + } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/framework/map/room/RoomInfo.cs b/DungeonShooting_Godot/src/framework/map/room/RoomInfo.cs index 89cfd54..49da2e7 100644 --- a/DungeonShooting_Godot/src/framework/map/room/RoomInfo.cs +++ b/DungeonShooting_Godot/src/framework/map/room/RoomInfo.cs @@ -340,7 +340,7 @@ { hasEnemy = true; } - + if (!hasEnemy) //没有敌人, 不关门 { IsSeclusion = false; @@ -434,6 +434,7 @@ { if (FogMask != null && Math.Abs(FogMask.Color.A - 1) > 0.001f) { + RefreshPreviewFog(1); FogMask.TransitionAlpha(1, 0.3f); } } @@ -445,7 +446,35 @@ { if (FogMask != null && Math.Abs(FogMask.Color.A - GameConfig.DarkFogAlpha) > 0.001f) { + RefreshPreviewFog(GameConfig.DarkFogAlpha); FogMask.TransitionAlpha(GameConfig.DarkFogAlpha, 0.3f); } } + + /// + /// 刷新房间中的预览迷雾 + /// + /// 当前房间的迷雾透明度 + public void RefreshPreviewFog(float roomFogAlpha) + { + //预览迷雾 + foreach (var roomDoorInfo in Doors) + { + var aisleAlpha = roomDoorInfo.FogMask.TargetAlpha; + if (roomFogAlpha < 1 && aisleAlpha < 1) //隐藏预览 + { + roomDoorInfo.PreviewFogMask.SetActive(false); + } + else if (aisleAlpha >= 1) //预览房间 + { + roomDoorInfo.PreviewFogMask.SetActive(true); + roomDoorInfo.PreviewFogMask.SetPreviewFogType(PreviewFogMask.PreviewFogType.Room); + } + else if (roomFogAlpha >= 1) //预览过道 + { + roomDoorInfo.PreviewFogMask.SetActive(true); + roomDoorInfo.PreviewFogMask.SetPreviewFogType(PreviewFogMask.PreviewFogType.Aisle); + } + } + } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/room/DungeonManager.cs b/DungeonShooting_Godot/src/game/room/DungeonManager.cs index 94360d4..239ade2 100644 --- a/DungeonShooting_Godot/src/game/room/DungeonManager.cs +++ b/DungeonShooting_Godot/src/game/room/DungeonManager.cs @@ -431,7 +431,6 @@ private void CreateRoomFogMask(RoomInfo roomInfo) { var roomFog = new FogMask(); - //roomFog.BlendMode = Light2D.BlendModeEnum.Add; roomFog.Name = "FogMask" + roomFog.IsDestroyed; float alpha; if (roomInfo.RoomType == DungeonRoomType.Inlet) //起始房间没有迷雾 @@ -451,7 +450,7 @@ foreach (var roomDoorInfo in roomInfo.Doors) { //必须是正向门 - if (!roomDoorInfo.IsForward) + if (roomDoorInfo.IsForward) { Rect2I calcRect; Rect2I fogAreaRect; @@ -515,7 +514,6 @@ //过道迷雾遮罩 var aisleFog = new FogMask(); - //aisleFog.BlendMode = Light2D.BlendModeEnum.Add; aisleFog.InitFog(calcRect.Position, calcRect.Size); World.FogMaskRoot.AddChild(aisleFog); roomDoorInfo.FogMask = aisleFog; @@ -524,36 +522,27 @@ //过道迷雾区域 var fogArea = new AisleFogArea(); fogArea.Init(roomDoorInfo, - new Rect2I(fogAreaRect.Position * GameConfig.TileCellSize, - fogAreaRect.Size * GameConfig.TileCellSize)); + new Rect2I( + fogAreaRect.Position * GameConfig.TileCellSize, + fogAreaRect.Size * GameConfig.TileCellSize + ) + ); roomDoorInfo.AisleFogArea = fogArea; roomDoorInfo.ConnectDoor.AisleFogArea = fogArea; World.AffiliationAreaRoot.AddChild(fogArea); } - if (roomDoorInfo.Direction == DoorDirection.E) - { - var previewTexture = - ResourceManager.LoadTexture2D(ResourcePath.resource_sprite_map_PreviewTransition_png); - var previewFog = new PointLight2D(); - previewFog.Texture = previewTexture; - //previewFog.BlendMode = Light2D.BlendModeEnum.Add; - previewFog.GlobalPosition = roomDoorInfo.Door.GlobalPosition + new Vector2(16, 0); - previewFog.RotationDegrees = 90; - World.FogMaskRoot.AddChild(previewFog); - } - else if (roomDoorInfo.Direction == DoorDirection.W) - { - var previewTexture = ResourceManager.LoadTexture2D(ResourcePath.resource_sprite_map_PreviewTransition2_png); - var previewFog = new PointLight2D(); - previewFog.Texture = previewTexture; - //previewFog.BlendMode = Light2D.BlendModeEnum.Add; - previewFog.GlobalPosition = roomDoorInfo.Door.GlobalPosition + new Vector2(16, 0); - previewFog.RotationDegrees = 90; - World.FogMaskRoot.AddChild(previewFog); - } - + //预览迷雾区域 + var previewFog = new PreviewFogMask(); + roomDoorInfo.PreviewFogMask = previewFog; + previewFog.Init(roomDoorInfo); + previewFog.SetPreviewFogType(PreviewFogMask.PreviewFogType.Room); + previewFog.SetActive(false); + World.FogMaskRoot.AddChild(previewFog); } + + //刷新预览迷雾 + roomInfo.RefreshPreviewFog(alpha); } ///