diff --git a/DungeonShooting_Godot/project.godot b/DungeonShooting_Godot/project.godot index 8f16079..a14b877 100644 --- a/DungeonShooting_Godot/project.godot +++ b/DungeonShooting_Godot/project.godot @@ -11,7 +11,7 @@ [application] config/name="DungeonShooting" -run/main_scene="res://scene/Main.tscn" +run/main_scene="res://scene/test/TestMask.tscn" config/features=PackedStringArray("4.2", "C#") config/icon="res://icon.png" diff --git a/DungeonShooting_Godot/resource/material/Mask.gdshader b/DungeonShooting_Godot/resource/material/Mask.gdshader new file mode 100644 index 0000000..1c44ff4 --- /dev/null +++ b/DungeonShooting_Godot/resource/material/Mask.gdshader @@ -0,0 +1,9 @@ +shader_type canvas_item; + +uniform sampler2D mask_texture; + +void fragment() { + vec2 size = TEXTURE_PIXEL_SIZE; + COLOR.a = 1.0 - texture(mask_texture, UV).a; +} + diff --git a/DungeonShooting_Godot/scene/test/TestMask.tscn b/DungeonShooting_Godot/scene/test/TestMask.tscn new file mode 100644 index 0000000..2fbff95 --- /dev/null +++ b/DungeonShooting_Godot/scene/test/TestMask.tscn @@ -0,0 +1,10 @@ +[gd_scene load_steps=2 format=3 uid="uid://cnt6y33q15xfq"] + +[ext_resource type="Script" path="res://src/test/TestMask.cs" id="1_0yip6"] + +[node name="TestMask" type="Node2D"] +script = ExtResource("1_0yip6") + +[node name="Sprite2D" type="Sprite2D" parent="."] +modulate = Color(0, 0, 0, 1) +centered = false diff --git a/DungeonShooting_Godot/src/framework/Grid.cs b/DungeonShooting_Godot/src/framework/Grid.cs index ec37b93..b73f989 100644 --- a/DungeonShooting_Godot/src/framework/Grid.cs +++ b/DungeonShooting_Godot/src/framework/Grid.cs @@ -11,7 +11,7 @@ /// 遍历网格数据回调 /// public delegate void EachGridCallback(int x, int y, T data); - + private readonly Dictionary> _map = new Dictionary>(); /// diff --git a/DungeonShooting_Godot/src/framework/common/Utils.cs b/DungeonShooting_Godot/src/framework/common/Utils.cs index d139167..4bda9ac 100644 --- a/DungeonShooting_Godot/src/framework/common/Utils.cs +++ b/DungeonShooting_Godot/src/framework/common/Utils.cs @@ -261,4 +261,12 @@ return point; } + + /// + /// 将 point 位置限制在 anchor 的周围, 最大距离为 distance, 并返回新的位置 + /// + public static Vector2 ConstrainDistance(Vector2 point, Vector2 anchor, float distance) + { + return (point - anchor).Normalized() * distance + anchor; + } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/test/TestMask.cs b/DungeonShooting_Godot/src/test/TestMask.cs new file mode 100644 index 0000000..9679da0 --- /dev/null +++ b/DungeonShooting_Godot/src/test/TestMask.cs @@ -0,0 +1,36 @@ +using Godot; +using System; + +public partial class TestMask : Node2D +{ + private Sprite2D Sprite; + private Image _image; + private ImageTexture _texture; + + private Image _maskImage; + private Grid _grid; + + public override void _Ready() + { + Sprite = GetNode("Sprite2D"); + var size = DisplayServer.WindowGetSize(); + _image = Image.Create(size.X, size.Y, false, Image.Format.Rgba8); + //_image.Fill(Colors.Black); + _texture = ImageTexture.CreateFromImage(_image); + Sprite.Texture = _texture; + _maskImage = ResourceManager.Load(ResourcePath.icon_png).GetImage(); + //Godot.c + //_grid.set + } + + + public override void _Process(double delta) + { + if (Input.IsMouseButtonPressed(MouseButton.Left)) + { + var usedRect = _maskImage.GetUsedRect(); + _image.BlendRect(_maskImage, usedRect, GetLocalMousePosition().AsVector2I() - usedRect.Size / 2); + _texture.Update(_image); + } + } +}