diff --git a/DungeonShooting_Godot/prefab/Cursor.tscn b/DungeonShooting_Godot/prefab/Cursor.tscn new file mode 100644 index 0000000..c228d62 --- /dev/null +++ b/DungeonShooting_Godot/prefab/Cursor.tscn @@ -0,0 +1,43 @@ +[gd_scene load_steps=4 format=3] + +[ext_resource type="Texture2D" uid="uid://ct5v768lsf6nc" path="res://resource/sprite/ui/Cursor.png" id="1"] +[ext_resource type="Script" path="res://src/game/Cursor.cs" id="2"] +[ext_resource type="Texture2D" uid="uid://cjiiu86a42mnj" path="res://resource/sprite/ui/CursorCenter.png" id="2_2j135"] + + +[node name="Cursor" type="Node2D"] +z_index = 10 +script = ExtResource("2") + +[node name="Center" type="Sprite2D" parent="."] +visible = false +scale = Vector2(4, 4) +texture = ExtResource("2_2j135") + +[node name="LT" type="Sprite2D" parent="."] +scale = Vector2(4, 4) +texture = ExtResource("1") +offset = Vector2(-2, -2) +region_enabled = true +region_rect = Rect2(0, 0, 4, 4) + +[node name="LB" type="Sprite2D" parent="."] +scale = Vector2(4, 4) +texture = ExtResource("1") +offset = Vector2(-2, 2) +region_enabled = true +region_rect = Rect2(0, 4, 4, 4) + +[node name="RT" type="Sprite2D" parent="."] +scale = Vector2(4, 4) +texture = ExtResource("1") +offset = Vector2(2, -2) +region_enabled = true +region_rect = Rect2(4, 0, 4, 4) + +[node name="RB" type="Sprite2D" parent="."] +scale = Vector2(4, 4) +texture = ExtResource("1") +offset = Vector2(2, 2) +region_enabled = true +region_rect = Rect2(4, 4, 4, 4) diff --git a/DungeonShooting_Godot/prefab/ui/Cursor.tscn b/DungeonShooting_Godot/prefab/ui/Cursor.tscn deleted file mode 100644 index 93edd6b..0000000 --- a/DungeonShooting_Godot/prefab/ui/Cursor.tscn +++ /dev/null @@ -1,42 +0,0 @@ -[gd_scene load_steps=4 format=3 uid="uid://brx7nc4mdgjat"] - -[ext_resource type="Texture2D" uid="uid://ct5v768lsf6nc" path="res://resource/sprite/ui/Cursor.png" id="1"] -[ext_resource type="Script" path="res://src/game/ui/Cursor.cs" id="2"] -[ext_resource type="Texture2D" uid="uid://cjiiu86a42mnj" path="res://resource/sprite/ui/CursorCenter.png" id="2_2j135"] - -[node name="Cursor" type="Node2D"] -z_index = 10 -script = ExtResource("2") - -[node name="Center" type="Sprite2D" parent="."] -visible = false -scale = Vector2(4, 4) -texture = ExtResource("2_2j135") - -[node name="LT" type="Sprite2D" parent="."] -scale = Vector2(4, 4) -texture = ExtResource("1") -offset = Vector2(-2, -2) -region_enabled = true -region_rect = Rect2(0, 0, 4, 4) - -[node name="LB" type="Sprite2D" parent="."] -scale = Vector2(4, 4) -texture = ExtResource("1") -offset = Vector2(-2, 2) -region_enabled = true -region_rect = Rect2(0, 4, 4, 4) - -[node name="RT" type="Sprite2D" parent="."] -scale = Vector2(4, 4) -texture = ExtResource("1") -offset = Vector2(2, -2) -region_enabled = true -region_rect = Rect2(4, 0, 4, 4) - -[node name="RB" type="Sprite2D" parent="."] -scale = Vector2(4, 4) -texture = ExtResource("1") -offset = Vector2(2, 2) -region_enabled = true -region_rect = Rect2(4, 4, 4, 4) diff --git a/DungeonShooting_Godot/src/game/Cursor.cs b/DungeonShooting_Godot/src/game/Cursor.cs new file mode 100644 index 0000000..639c7cd --- /dev/null +++ b/DungeonShooting_Godot/src/game/Cursor.cs @@ -0,0 +1,115 @@ +using Godot; + +/// +/// 鼠标指针 +/// +public partial class Cursor : Node2D +{ + /// + /// 是否是GUI模式 + /// + private bool _isGuiMode = true; + + /// + /// 非GUI模式下鼠标指针所挂载的角色 + /// + private Role _mountRole; + + private Sprite2D center; + private Sprite2D lt; + private Sprite2D lb; + private Sprite2D rt; + private Sprite2D rb; + + public override void _Ready() + { + center = GetNode("Center"); + lt = GetNode("LT"); + lb = GetNode("LB"); + rt = GetNode("RT"); + rb = GetNode("RB"); + } + + public override void _Process(double delta) + { + if (_isGuiMode) + { + SetScope(0, null); + } + else + { + var targetGun = _mountRole?.Holster.ActiveWeapon; + if (targetGun != null) + { + SetScope(targetGun.CurrScatteringRange, targetGun); + } + else + { + SetScope(0, null); + } + } + + SetCursorPos(); + } + + /// + /// 设置是否是Gui模式 + /// + public void SetGuiMode(bool flag) + { + _isGuiMode = flag; + } + + /// + /// 获取是否是Gui模式 + /// + public bool GetGuiMode() + { + return _isGuiMode; + } + + /// + /// 设置非GUI模式下鼠标指针所挂载的角色 + /// + public void SetMountRole(Role role) + { + _mountRole = role; + } + + /// + /// 获取非GUI模式下鼠标指针所挂载的角色 + /// + public Role GetMountRole() + { + return _mountRole; + } + + /// + /// 设置光标半径范围 + /// + private void SetScope(float scope, Weapon targetGun) + { + if (targetGun != null) + { + var tunPos = GameApplication.Instance.ViewToGlobalPosition(targetGun.GlobalPosition); + var len = GlobalPosition.DistanceTo(tunPos); + if (targetGun.Attribute != null) + { + len = Mathf.Max(0, len - targetGun.Attribute.FirePosition.X); + } + scope = len / GameConfig.ScatteringDistance * scope; + } + scope = Mathf.Clamp(scope, 0, 192); + center.Visible = scope > 64; + + lt.Position = new Vector2(-scope, -scope); + lb.Position = new Vector2(-scope, scope); + rt.Position = new Vector2(scope, -scope); + rb.Position = new Vector2(scope, scope); + } + + private void SetCursorPos() + { + GlobalPosition = GetGlobalMousePosition(); + } +} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/GameApplication.cs b/DungeonShooting_Godot/src/game/GameApplication.cs index d6b0007..0a0dd2c 100644 --- a/DungeonShooting_Godot/src/game/GameApplication.cs +++ b/DungeonShooting_Godot/src/game/GameApplication.cs @@ -72,7 +72,7 @@ // 初始化鼠标 Input.MouseMode = Input.MouseModeEnum.Hidden; - Cursor = ResourceManager.Load(ResourcePath.prefab_ui_Cursor_tscn).Instantiate(); + Cursor = ResourceManager.Load(ResourcePath.prefab_Cursor_tscn).Instantiate(); AddChild(Cursor); //打开ui diff --git a/DungeonShooting_Godot/src/game/manager/ResourcePath.cs b/DungeonShooting_Godot/src/game/manager/ResourcePath.cs index 37f7d76..ba1e64c 100644 --- a/DungeonShooting_Godot/src/game/manager/ResourcePath.cs +++ b/DungeonShooting_Godot/src/game/manager/ResourcePath.cs @@ -3,6 +3,7 @@ /// public class ResourcePath { + public const string prefab_Cursor_tscn = "res://prefab/Cursor.tscn"; public const string prefab_FanCollisionShape_tscn = "res://prefab/FanCollisionShape.tscn"; public const string prefab_effect_Blood_tscn = "res://prefab/effect/Blood.tscn"; public const string prefab_effect_BulletDisappear_tscn = "res://prefab/effect/BulletDisappear.tscn"; @@ -15,7 +16,6 @@ public const string prefab_role_Role_tscn = "res://prefab/role/Role.tscn"; public const string prefab_test_MoveComponent_tscn = "res://prefab/test/MoveComponent.tscn"; public const string prefab_test_TestActivity_tscn = "res://prefab/test/TestActivity.tscn"; - public const string prefab_ui_Cursor_tscn = "res://prefab/ui/Cursor.tscn"; public const string prefab_ui_EditorTools_tscn = "res://prefab/ui/EditorTools.tscn"; public const string prefab_ui_RoomUI_tscn = "res://prefab/ui/RoomUI.tscn"; public const string prefab_weapon_Knife_tscn = "res://prefab/weapon/Knife.tscn"; diff --git a/DungeonShooting_Godot/src/game/ui/Cursor.cs b/DungeonShooting_Godot/src/game/ui/Cursor.cs deleted file mode 100644 index 639c7cd..0000000 --- a/DungeonShooting_Godot/src/game/ui/Cursor.cs +++ /dev/null @@ -1,115 +0,0 @@ -using Godot; - -/// -/// 鼠标指针 -/// -public partial class Cursor : Node2D -{ - /// - /// 是否是GUI模式 - /// - private bool _isGuiMode = true; - - /// - /// 非GUI模式下鼠标指针所挂载的角色 - /// - private Role _mountRole; - - private Sprite2D center; - private Sprite2D lt; - private Sprite2D lb; - private Sprite2D rt; - private Sprite2D rb; - - public override void _Ready() - { - center = GetNode("Center"); - lt = GetNode("LT"); - lb = GetNode("LB"); - rt = GetNode("RT"); - rb = GetNode("RB"); - } - - public override void _Process(double delta) - { - if (_isGuiMode) - { - SetScope(0, null); - } - else - { - var targetGun = _mountRole?.Holster.ActiveWeapon; - if (targetGun != null) - { - SetScope(targetGun.CurrScatteringRange, targetGun); - } - else - { - SetScope(0, null); - } - } - - SetCursorPos(); - } - - /// - /// 设置是否是Gui模式 - /// - public void SetGuiMode(bool flag) - { - _isGuiMode = flag; - } - - /// - /// 获取是否是Gui模式 - /// - public bool GetGuiMode() - { - return _isGuiMode; - } - - /// - /// 设置非GUI模式下鼠标指针所挂载的角色 - /// - public void SetMountRole(Role role) - { - _mountRole = role; - } - - /// - /// 获取非GUI模式下鼠标指针所挂载的角色 - /// - public Role GetMountRole() - { - return _mountRole; - } - - /// - /// 设置光标半径范围 - /// - private void SetScope(float scope, Weapon targetGun) - { - if (targetGun != null) - { - var tunPos = GameApplication.Instance.ViewToGlobalPosition(targetGun.GlobalPosition); - var len = GlobalPosition.DistanceTo(tunPos); - if (targetGun.Attribute != null) - { - len = Mathf.Max(0, len - targetGun.Attribute.FirePosition.X); - } - scope = len / GameConfig.ScatteringDistance * scope; - } - scope = Mathf.Clamp(scope, 0, 192); - center.Visible = scope > 64; - - lt.Position = new Vector2(-scope, -scope); - lb.Position = new Vector2(-scope, scope); - rt.Position = new Vector2(scope, -scope); - rb.Position = new Vector2(scope, scope); - } - - private void SetCursorPos() - { - GlobalPosition = GetGlobalMousePosition(); - } -} \ No newline at end of file