diff --git a/DungeonShooting_Godot/prefab/role/Player.tscn b/DungeonShooting_Godot/prefab/role/Player.tscn index 7625613..bf3afb0 100644 --- a/DungeonShooting_Godot/prefab/role/Player.tscn +++ b/DungeonShooting_Godot/prefab/role/Player.tscn @@ -14,3 +14,6 @@ [node name="ShadowSprite" parent="." index="0"] material = SubResource( 1 ) + +[node name="AnimatedSprite" parent="." index="2"] +frame = 1 diff --git a/DungeonShooting_Godot/prefab/role/Role.tscn b/DungeonShooting_Godot/prefab/role/Role.tscn index 45897d9..df672e5 100644 --- a/DungeonShooting_Godot/prefab/role/Role.tscn +++ b/DungeonShooting_Godot/prefab/role/Role.tscn @@ -91,6 +91,7 @@ position = Vector2( 0, -12 ) frames = SubResource( 6 ) animation = "idle" +frame = 2 playing = true [node name="Collision" type="CollisionShape2D" parent="."] diff --git a/DungeonShooting_Godot/prefab/ui/Cursor.tscn b/DungeonShooting_Godot/prefab/ui/Cursor.tscn index b3d50b8..7d1a4a4 100644 --- a/DungeonShooting_Godot/prefab/ui/Cursor.tscn +++ b/DungeonShooting_Godot/prefab/ui/Cursor.tscn @@ -3,31 +3,33 @@ [ext_resource path="res://resource/sprite/ui/Cursor.png" type="Texture" id=1] [ext_resource path="res://src/game/ui/Cursor.cs" type="Script" id=2] - - [node name="Cursor" type="Node2D"] z_index = 10 script = ExtResource( 2 ) [node name="LT" type="Sprite" 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="Sprite" 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="Sprite" 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="Sprite" parent="."] +scale = Vector2( 4, 4 ) texture = ExtResource( 1 ) offset = Vector2( 2, 2 ) region_enabled = true diff --git a/DungeonShooting_Godot/prefab/ui/RoomUI.tscn b/DungeonShooting_Godot/prefab/ui/RoomUI.tscn index 9091777..98b2ab6 100644 --- a/DungeonShooting_Godot/prefab/ui/RoomUI.tscn +++ b/DungeonShooting_Godot/prefab/ui/RoomUI.tscn @@ -25,8 +25,9 @@ margin_bottom = -20.0 [node name="HealthBar" type="TextureRect" parent="Control"] -margin_right = 55.0 +margin_right = 67.0 margin_bottom = 14.0 +rect_scale = Vector2( 4, 4 ) texture = ExtResource( 4 ) [node name="HpSlot" type="NinePatchRect" parent="Control/HealthBar"] @@ -70,39 +71,43 @@ [node name="MapBar" type="TextureRect" parent="Control"] anchor_left = 1.0 anchor_right = 1.0 -margin_left = -44.0 +margin_left = -176.0 +margin_right = -132.0 margin_bottom = 44.0 +rect_scale = Vector2( 4, 4 ) texture = ExtResource( 2 ) [node name="GunBar" type="Control" parent="Control"] anchor_top = 1.0 anchor_bottom = 1.0 -margin_top = -43.0 -margin_right = 40.0 +margin_top = -264.0 +margin_right = 264.0 +margin_bottom = 0.00146484 [node name="GunSprite" type="TextureRect" parent="Control/GunBar"] anchor_left = 0.5 anchor_top = 0.5 anchor_right = 0.5 anchor_bottom = 0.5 -margin_left = -33.0 -margin_top = -33.0 -margin_right = 33.0 -margin_bottom = 33.0 +margin_left = -132.0 +margin_top = -132.0 +margin_right = -66.0 +margin_bottom = -66.0 +rect_scale = Vector2( 4, 4 ) texture = ExtResource( 5 ) [node name="BulletText" type="Label" parent="Control/GunBar"] anchor_left = 0.5 anchor_right = 0.5 -margin_left = -28.0 -margin_right = 28.0 -margin_bottom = 17.0 +margin_left = -89.0 +margin_top = 39.0 +margin_right = 89.0 +margin_bottom = 74.0 text = "30/90" align = 1 [node name="GlobalNode" type="Node" parent="."] [node name="InteractiveTipBar" parent="GlobalNode" instance=ExtResource( 10 )] -visible = true [node name="ReloadBar" parent="GlobalNode" instance=ExtResource( 3 )] diff --git a/DungeonShooting_Godot/prefab/ui/bar/InteractiveTipBar.tscn b/DungeonShooting_Godot/prefab/ui/bar/InteractiveTipBar.tscn index 98512bd..37e2357 100644 --- a/DungeonShooting_Godot/prefab/ui/bar/InteractiveTipBar.tscn +++ b/DungeonShooting_Godot/prefab/ui/bar/InteractiveTipBar.tscn @@ -4,31 +4,30 @@ [ext_resource path="res://resource/sprite/ui/keyboard/e.png" type="Texture" id=2] [ext_resource path="res://resource/sprite/ui/icon/icon_bullet.png" type="Texture" id=3] [ext_resource path="res://resource/sprite/ui/font_bg.png" type="Texture" id=4] -[ext_resource path="res://resource/font/cn_font_4.tres" type="DynamicFont" id=5] - +[ext_resource path="res://resource/font/cn_font_12.tres" type="DynamicFont" id=5] [sub_resource type="Gradient" id=1] colors = PoolColorArray( 0.4, 0.498039, 1, 1, 0.4, 0.498039, 1, 0.313726 ) [node name="InteractiveTipBar" type="Node2D"] -visible = false z_index = 10 script = ExtResource( 1 ) [node name="Bg" type="Sprite" parent="."] -position = Vector2( 14, -17 ) -scale = Vector2( 28, 7 ) +position = Vector2( 39, -68 ) +scale = Vector2( 78, 22 ) texture = ExtResource( 4 ) [node name="Icon" type="Sprite" parent="."] -position = Vector2( 0, -32 ) +position = Vector2( 1, -120 ) +scale = Vector2( 4, 4 ) texture = ExtResource( 3 ) [node name="Message" type="Label" parent="."] -margin_left = -18.0 -margin_top = -27.0 -margin_right = 18.0 -margin_bottom = -15.0 +margin_left = -25.0 +margin_top = -90.0 +margin_right = 25.0 +margin_bottom = -68.0 custom_colors/font_color = Color( 0, 0, 0, 1 ) custom_fonts/font = ExtResource( 5 ) text = "111111" @@ -38,9 +37,11 @@ [node name="InteractiveIcon" type="Sprite" parent="."] visible = false position = Vector2( 0, -30 ) +scale = Vector2( 4, 4 ) texture = ExtResource( 2 ) [node name="Line2D" type="Line2D" parent="."] +scale = Vector2( 4, 4 ) points = PoolVector2Array( 0, -17, 0, 0 ) width = 1.0 gradient = SubResource( 1 ) diff --git a/DungeonShooting_Godot/prefab/ui/bar/ReloadBar.tscn b/DungeonShooting_Godot/prefab/ui/bar/ReloadBar.tscn index 41c5c96..c7b4164 100644 --- a/DungeonShooting_Godot/prefab/ui/bar/ReloadBar.tscn +++ b/DungeonShooting_Godot/prefab/ui/bar/ReloadBar.tscn @@ -4,8 +4,8 @@ [ext_resource path="res://resource/sprite/ui/reloadBarBlock.png" type="Texture" id=2] [ext_resource path="res://resource/sprite/ui/reloadBar.png" type="Texture" id=3] - [node name="ReloadBar" type="Node2D"] +scale = Vector2( 4, 4 ) script = ExtResource( 1 ) [node name="Slot" type="Sprite" parent="."] diff --git a/DungeonShooting_Godot/project.godot b/DungeonShooting_Godot/project.godot index cc6119c..acd52e2 100644 --- a/DungeonShooting_Godot/project.godot +++ b/DungeonShooting_Godot/project.godot @@ -38,7 +38,7 @@ [gui] theme/custom="res://resource/theme/mainTheme.tres" -theme/custom_font="res://resource/font/cn_font_6.tres" +theme/custom_font="res://resource/font/cn_font_18.tres" [importer_defaults] diff --git a/DungeonShooting_Godot/resource/font/cn_font_12.tres b/DungeonShooting_Godot/resource/font/cn_font_12.tres new file mode 100644 index 0000000..dbd8053 --- /dev/null +++ b/DungeonShooting_Godot/resource/font/cn_font_12.tres @@ -0,0 +1,9 @@ +[gd_resource type="DynamicFont" load_steps=2 format=2] + +[ext_resource path="res://SourceHanSerifCN-SemiBold.otf" type="DynamicFontData" id=1] + +[resource] +size = 12 +extra_spacing_top = -2 +extra_spacing_bottom = -2 +font_data = ExtResource( 1 ) diff --git a/DungeonShooting_Godot/resource/font/cn_font_18.tres b/DungeonShooting_Godot/resource/font/cn_font_18.tres new file mode 100644 index 0000000..032a49a --- /dev/null +++ b/DungeonShooting_Godot/resource/font/cn_font_18.tres @@ -0,0 +1,7 @@ +[gd_resource type="DynamicFont" load_steps=2 format=2] + +[ext_resource path="res://SourceHanSerifCN-SemiBold.otf" type="DynamicFontData" id=1] + +[resource] +size = 18 +font_data = ExtResource( 1 ) diff --git a/DungeonShooting_Godot/resource/font/cn_font_4.tres b/DungeonShooting_Godot/resource/font/cn_font_4.tres deleted file mode 100644 index fe24004..0000000 --- a/DungeonShooting_Godot/resource/font/cn_font_4.tres +++ /dev/null @@ -1,9 +0,0 @@ -[gd_resource type="DynamicFont" load_steps=2 format=2] - -[ext_resource path="res://SourceHanSerifCN-SemiBold.otf" type="DynamicFontData" id=1] - -[resource] -size = 4 -extra_spacing_top = -2 -extra_spacing_bottom = -2 -font_data = ExtResource( 1 ) diff --git a/DungeonShooting_Godot/resource/font/cn_font_6.tres b/DungeonShooting_Godot/resource/font/cn_font_6.tres deleted file mode 100644 index be51965..0000000 --- a/DungeonShooting_Godot/resource/font/cn_font_6.tres +++ /dev/null @@ -1,7 +0,0 @@ -[gd_resource type="DynamicFont" load_steps=2 format=2] - -[ext_resource path="res://SourceHanSerifCN-SemiBold.otf" type="DynamicFontData" id=1] - -[resource] -size = 6 -font_data = ExtResource( 1 ) diff --git a/DungeonShooting_Godot/scene/Main.tscn b/DungeonShooting_Godot/scene/Main.tscn index 2a3a84f..aff7b6d 100644 --- a/DungeonShooting_Godot/scene/Main.tscn +++ b/DungeonShooting_Godot/scene/Main.tscn @@ -1,6 +1,9 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=7 format=2] [ext_resource path="res://scene/Room.tscn" type="PackedScene" id=1] +[ext_resource path="res://prefab/ui/RoomUI.tscn" type="PackedScene" id=2] +[ext_resource path="res://src/game/GameApplication.cs" type="Script" id=3] +[ext_resource path="res://prefab/ui/Cursor.tscn" type="PackedScene" id=4] [sub_resource type="Shader" id=1] code = "shader_type canvas_item; @@ -20,12 +23,20 @@ shader_param/offset = Vector2( 0, 0 ) [node name="Main" type="Node2D"] +script = ExtResource( 3 ) +CursorPack = ExtResource( 4 ) +RoomPath = NodePath("ViewCanvas/ViewportContainer/Viewport/Room") +ViewportPath = NodePath("ViewCanvas/ViewportContainer/Viewport") +ViewportContainerPath = NodePath("ViewCanvas/ViewportContainer") +UiPath = NodePath("UiCanvas/RoomUI") +GlobalNodeRootPath = NodePath("GlobalNodeRoot") -[node name="CanvasLayer" type="CanvasLayer" parent="."] +[node name="ViewCanvas" type="CanvasLayer" parent="."] +layer = -1 offset = Vector2( -4, -4 ) transform = Transform2D( 1, 0, 0, 1, -4, -4 ) -[node name="ViewportContainer" type="ViewportContainer" parent="CanvasLayer"] +[node name="ViewportContainer" type="ViewportContainer" parent="ViewCanvas"] material = SubResource( 2 ) anchor_right = 1.0 anchor_bottom = 1.0 @@ -34,7 +45,7 @@ rect_scale = Vector2( 4, 4 ) stretch = true -[node name="Viewport" type="Viewport" parent="CanvasLayer/ViewportContainer"] +[node name="Viewport" type="Viewport" parent="ViewCanvas/ViewportContainer"] size = Vector2( 482, 272 ) own_world = true transparent_bg = true @@ -42,4 +53,10 @@ debanding = true render_target_update_mode = 3 -[node name="Room" parent="CanvasLayer/ViewportContainer/Viewport" instance=ExtResource( 1 )] +[node name="Room" parent="ViewCanvas/ViewportContainer/Viewport" instance=ExtResource( 1 )] + +[node name="GlobalNodeRoot" type="Node2D" parent="."] + +[node name="UiCanvas" type="CanvasLayer" parent="."] + +[node name="RoomUI" parent="UiCanvas" instance=ExtResource( 2 )] diff --git a/DungeonShooting_Godot/scene/Room.tscn b/DungeonShooting_Godot/scene/Room.tscn index 0262f17..9f664bf 100644 --- a/DungeonShooting_Godot/scene/Room.tscn +++ b/DungeonShooting_Godot/scene/Room.tscn @@ -1,18 +1,11 @@ -[gd_scene load_steps=6 format=2] +[gd_scene load_steps=4 format=2] [ext_resource path="res://resource/map/dungeon_test.tmx" type="PackedScene" id=2] [ext_resource path="res://src/game/room/RoomManager.cs" type="Script" id=3] -[ext_resource path="res://prefab/ui/Cursor.tscn" type="PackedScene" id=4] -[ext_resource path="res://src/game/camera/MainCamera.cs" type="Script" id=5] -[ext_resource path="res://prefab/ui/RoomUI.tscn" type="PackedScene" id=6] +[ext_resource path="res://src/game/camera/GameCamera.cs" type="Script" id=5] [node name="Room" type="Node2D"] script = ExtResource( 3 ) -MouseCursor = ExtResource( 4 ) - -[node name="UI" type="CanvasLayer" parent="."] - -[node name="RoomUI" parent="UI" instance=ExtResource( 6 )] [node name="MapRoot" type="Node2D" parent="."] z_index = -10 @@ -29,4 +22,4 @@ [node name="ObjectRoot" type="Node2D" parent="."] -[node name="ItemRoot" type="YSort" parent="."] +[node name="SortRoot" type="YSort" parent="."] diff --git a/DungeonShooting_Godot/src/framework/ActivityObject.cs b/DungeonShooting_Godot/src/framework/ActivityObject.cs index 1334e78..e288578 100644 --- a/DungeonShooting_Godot/src/framework/ActivityObject.cs +++ b/DungeonShooting_Godot/src/framework/ActivityObject.cs @@ -219,15 +219,17 @@ public void PutDown() { var parent = GetParent(); - if (parent != RoomManager.Current.ObjectRoot) + var root = GameApplication.Instance.Room.ObjectRoot; + if (parent != root) { if (parent != null) { parent.RemoveChild(this); } - RoomManager.Current.ObjectRoot.AddChild(this); + root.AddChild(this); } + //注意需要延时调用 CallDeferred(nameof(ShowShadowSprite)); } @@ -520,14 +522,15 @@ private void Throw() { var parent = GetParent(); + var room = GameApplication.Instance.Room; if (parent == null) { - RoomManager.Current.SortRoot.AddChild(this); + room.SortRoot.AddChild(this); } - else if (parent == RoomManager.Current.ObjectRoot) + else if (parent == room.ObjectRoot) { parent.RemoveChild(this); - RoomManager.Current.SortRoot.AddChild(this); + room.SortRoot.AddChild(this); } GlobalPosition = _throwData.StartPosition + new Vector2(0, -_throwData.Y); @@ -604,7 +607,7 @@ private void ThrowOver() { GetParent().RemoveChild(this); - RoomManager.Current.ObjectRoot.AddChild(this); + GameApplication.Instance.Room.ObjectRoot.AddChild(this); RestoreCollision(); OnThrowOver(); diff --git a/DungeonShooting_Godot/src/game/GameApplication.cs b/DungeonShooting_Godot/src/game/GameApplication.cs new file mode 100644 index 0000000..c6e5b35 --- /dev/null +++ b/DungeonShooting_Godot/src/game/GameApplication.cs @@ -0,0 +1,75 @@ + +using Godot; + +public class GameApplication : Node2D +{ + public static GameApplication Instance { get; private set; } + + [Export] public PackedScene CursorPack; + + [Export] public NodePath RoomPath; + + [Export] public NodePath ViewportPath; + + [Export] public NodePath ViewportContainerPath; + + [Export] public NodePath UiPath; + + [Export] public NodePath GlobalNodeRootPath; + + /// + /// 鼠标指针 + /// + public Cursor Cursor { get; private set; } + + /// + /// 游戏房间 + /// + public RoomManager Room { get; private set; } + + /// + /// 游戏渲染视口 + /// + public Viewport Viewport { get; private set; } + + /// + /// ViewportContainer 组件 + /// + public ViewportContainer ViewportContainer { get; private set; } + + /// + /// 游戏ui对象 + /// + public RoomUI Ui { get; private set; } + + public Node2D GlobalNodeRoot { get; private set; } + + public GameApplication() + { + Instance = this; + } + + public override void _EnterTree() + { + GlobalNodeRoot = GetNode(GlobalNodeRootPath); + // 初始化鼠标 + Cursor = CursorPack.Instance(); + + Room = GetNode(RoomPath); + Viewport = GetNode(ViewportPath); + ViewportContainer = GetNode(ViewportContainerPath); + Ui = GetNode(UiPath); + + Ui.AddChild(Cursor); + } + + public Vector2 GlobalToViewPosition(Vector2 globalPos) + { + return globalPos / GameConfig.WindowScale - (GameConfig.ViewportSize / 2) + GameCamera.Main.GlobalPosition; + } + + public Vector2 ViewToGlobalPosition(Vector2 viewPos) + { + return (viewPos - GameCamera.Main.GlobalPosition + (GameConfig.ViewportSize / 2)) * GameConfig.WindowScale; + } +} \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/camera/GameCamera.cs b/DungeonShooting_Godot/src/game/camera/GameCamera.cs new file mode 100644 index 0000000..70ca0e0 --- /dev/null +++ b/DungeonShooting_Godot/src/game/camera/GameCamera.cs @@ -0,0 +1,128 @@ +using System.Collections.Generic; +using Godot; + +/// +/// 游戏相机 +/// +public class GameCamera : Camera2D +{ + /// + /// 当前场景的相机对象 + /// + public static GameCamera Main { get; private set; } + + /// + /// 恢复系数 + /// + [Export] + public float RecoveryCoefficient = 100f; + /// + /// 抖动开关 + /// + public bool Enable { get; set; } = true; + + private long _index = 0; + private Vector2 _processDistance = Vector2.Zero; + private Vector2 _processDirection = Vector2.Zero; + private readonly Dictionary _shakeMap = new Dictionary(); + + private Vector2 _camPos; + private Vector2 _shakeOffset = Vector2.Zero; + + public override void _Ready() + { + Main = this; + _camPos = GlobalPosition; + } + + //public override void _PhysicsProcess(float delta); + + public override void _Process(float delta) + { + _Shake(delta); + + var player = GameApplication.Instance.Room.Player; + var viewportContainer = GameApplication.Instance.ViewportContainer; + //var mousePos = InputManager.GetMousePosition(); + var camPos = player.GlobalPosition; + //var camPos = player.GlobalPosition.LinearInterpolate(mousePos, 0); + //_camPos = camPos + _shakeOffset; + _camPos = _camPos.LinearInterpolate(camPos, Mathf.Min(5 * delta, 1)) + _shakeOffset; + var camSubpixelPos = _camPos.Round() - _camPos; + (viewportContainer.Material as ShaderMaterial)?.SetShaderParam("offset", camSubpixelPos); + //GlobalPosition = _camPos.Round(); + GlobalPosition = _camPos.Round(); + } + + /// + /// 设置帧抖动, 结束后自动清零, 需要每一帧调用 + /// + /// 抖动的力度 + public void ProcessShake(Vector2 value) + { + if (value.Length() > _processDistance.Length()) + { + _processDistance = value; + } + } + + public void ProcessDirectionalShake(Vector2 value) + { + _processDirection += value; + } + + /// + /// 创建一个抖动, 并设置抖动时间 + /// + /// 抖动力度 + /// 抖动生效时间 + public async void CreateShake(Vector2 value, float time) + { + if (time > 0) + { + long tempIndex = _index++; + SceneTreeTimer sceneTreeTimer = GetTree().CreateTimer(time); + _shakeMap[tempIndex] = value; + await ToSignal(sceneTreeTimer, "timeout"); + _shakeMap.Remove(tempIndex); + } + } + + //抖动调用 + private void _Shake(float delta) + { + if (Enable) + { + var distance = _CalculateDistance(); + _shakeOffset += _processDirection + new Vector2( + (float)GD.RandRange(-distance.x, distance.x) - _shakeOffset.x, + (float)GD.RandRange(-distance.y, distance.y) - _shakeOffset.y + ); + _processDistance = Vector2.Zero; + _processDirection = Vector2.Zero; + } + else + { + _shakeOffset = _shakeOffset.LinearInterpolate(Vector2.Zero, RecoveryCoefficient * delta); + } + } + + //计算相机需要抖动的值 + private Vector2 _CalculateDistance() + { + Vector2 temp = Vector2.Zero; + float length = 0; + foreach (var item in _shakeMap) + { + var value = item.Value; + float tempLenght = value.Length(); + if (tempLenght > length) + { + length = tempLenght; + temp = value; + } + } + return _processDistance.Length() > length ? _processDistance : temp; + } + +} diff --git a/DungeonShooting_Godot/src/game/camera/MainCamera.cs b/DungeonShooting_Godot/src/game/camera/MainCamera.cs deleted file mode 100644 index 9336fa0..0000000 --- a/DungeonShooting_Godot/src/game/camera/MainCamera.cs +++ /dev/null @@ -1,125 +0,0 @@ -using System.Collections.Generic; -using Godot; - -public class MainCamera : Camera2D -{ - /// - /// 当前场景的相机对象 - /// - public static MainCamera Main { get; private set; } - - /// - /// 恢复系数 - /// - [Export] - public float RecoveryCoefficient = 100f; - /// - /// 抖动开关 - /// - public bool Enable { get; set; } = true; - - private long _index = 0; - private Vector2 _processDistance = Vector2.Zero; - private Vector2 _processDirection = Vector2.Zero; - private readonly Dictionary _shakeMap = new Dictionary(); - - private Vector2 _camPos; - private Vector2 _shakeOffset = Vector2.Zero; - - public override void _Ready() - { - Main = this; - _camPos = GlobalPosition; - } - - //public override void _PhysicsProcess(float delta); - - public override void _Process(float delta) - { - _Shake(delta); - - var player = RoomManager.Current.Player; - var viewportContainer = RoomManager.Current.ViewportContainer; - //var mousePos = InputManager.GetMousePosition(); - var camPos = player.GlobalPosition; - //var camPos = player.GlobalPosition.LinearInterpolate(mousePos, 0); - //_camPos = camPos + _shakeOffset; - _camPos = _camPos.LinearInterpolate(camPos, Mathf.Min(5 * delta, 1)) + _shakeOffset; - var camSubpixelPos = _camPos.Round() - _camPos; - (viewportContainer.Material as ShaderMaterial)?.SetShaderParam("offset", camSubpixelPos); - //GlobalPosition = _camPos.Round(); - GlobalPosition = _camPos.Round(); - } - - /// - /// 设置帧抖动, 结束后自动清零, 需要每一帧调用 - /// - /// 抖动的力度 - public void ProcessShake(Vector2 value) - { - if (value.Length() > _processDistance.Length()) - { - _processDistance = value; - } - } - - public void ProcessDirectionalShake(Vector2 value) - { - _processDirection += value; - } - - /// - /// 创建一个抖动, 并设置抖动时间 - /// - /// 抖动力度 - /// 抖动生效时间 - public async void CreateShake(Vector2 value, float time) - { - if (time > 0) - { - long tempIndex = _index++; - SceneTreeTimer sceneTreeTimer = GetTree().CreateTimer(time); - _shakeMap[tempIndex] = value; - await ToSignal(sceneTreeTimer, "timeout"); - _shakeMap.Remove(tempIndex); - } - } - - //抖动调用 - private void _Shake(float delta) - { - if (Enable) - { - var distance = _CalculateDistance(); - _shakeOffset += _processDirection + new Vector2( - (float)GD.RandRange(-distance.x, distance.x) - _shakeOffset.x, - (float)GD.RandRange(-distance.y, distance.y) - _shakeOffset.y - ); - _processDistance = Vector2.Zero; - _processDirection = Vector2.Zero; - } - else - { - _shakeOffset = _shakeOffset.LinearInterpolate(Vector2.Zero, RecoveryCoefficient * delta); - } - } - - //计算相机需要抖动的值 - private Vector2 _CalculateDistance() - { - Vector2 temp = Vector2.Zero; - float length = 0; - foreach (var item in _shakeMap) - { - var value = item.Value; - float tempLenght = value.Length(); - if (tempLenght > length) - { - length = tempLenght; - temp = value; - } - } - return _processDistance.Length() > length ? _processDistance : temp; - } - -} diff --git a/DungeonShooting_Godot/src/game/item/weapon/Weapon.cs b/DungeonShooting_Godot/src/game/item/weapon/Weapon.cs index 3ee09af..25fd6fd 100644 --- a/DungeonShooting_Godot/src/game/item/weapon/Weapon.cs +++ b/DungeonShooting_Godot/src/game/item/weapon/Weapon.cs @@ -774,7 +774,7 @@ bullet.GlobalRotation = globalRotation; if (parent == null) { - RoomManager.Current.SortRoot.AddChild(bullet); + GameApplication.Instance.Room.SortRoot.AddChild(bullet); } else { diff --git a/DungeonShooting_Godot/src/game/item/weapon/bullet/OrdinaryBullets.cs b/DungeonShooting_Godot/src/game/item/weapon/bullet/OrdinaryBullets.cs index 3546cf0..092fe89 100644 --- a/DungeonShooting_Godot/src/game/item/weapon/bullet/OrdinaryBullets.cs +++ b/DungeonShooting_Godot/src/game/item/weapon/bullet/OrdinaryBullets.cs @@ -78,7 +78,7 @@ Node2D hit = Hit.Instance(); hit.RotationDegrees = MathUtils.RandRangeInt(0, 360); hit.GlobalPosition = pos; - RoomManager.Current.SortRoot.AddChild(hit); + GameApplication.Instance.Room.SortRoot.AddChild(hit); QueueFree(); } else //没有碰到, 继续移动 diff --git a/DungeonShooting_Godot/src/game/item/weapon/gun/Gun.cs b/DungeonShooting_Godot/src/game/item/weapon/gun/Gun.cs index a5445aa..79bcce7 100644 --- a/DungeonShooting_Godot/src/game/item/weapon/gun/Gun.cs +++ b/DungeonShooting_Godot/src/game/item/weapon/gun/Gun.cs @@ -106,7 +106,7 @@ var shell = new ShellCase(); shell.Throw(new Vector2(10, 5), startPos, startHeight, direction, xf, yf, rotate, true); //创建抖动 - MainCamera.Main.ProcessDirectionalShake(Vector2.Right.Rotated(GlobalRotation) * 1.5f); + GameCamera.Main.ProcessDirectionalShake(Vector2.Right.Rotated(GlobalRotation) * 1.5f); } protected override void OnShoot() diff --git a/DungeonShooting_Godot/src/game/item/weapon/gun/Shotgun.cs b/DungeonShooting_Godot/src/game/item/weapon/gun/Shotgun.cs index 273ce39..e3190ef 100644 --- a/DungeonShooting_Godot/src/game/item/weapon/gun/Shotgun.cs +++ b/DungeonShooting_Godot/src/game/item/weapon/gun/Shotgun.cs @@ -71,7 +71,7 @@ var shell = new ShellCase(); shell.Throw(new Vector2(5, 10), startPos, startHeight, direction, xf, yf, rotate, true); //创建抖动 - MainCamera.Main.ProcessDirectionalShake(Vector2.Right.Rotated(GlobalRotation) * 1.5f); + GameCamera.Main.ProcessDirectionalShake(Vector2.Right.Rotated(GlobalRotation) * 1.5f); } protected override void OnShoot() diff --git a/DungeonShooting_Godot/src/game/manager/InputManager.cs b/DungeonShooting_Godot/src/game/manager/InputManager.cs index ad3f978..9dc3121 100644 --- a/DungeonShooting_Godot/src/game/manager/InputManager.cs +++ b/DungeonShooting_Godot/src/game/manager/InputManager.cs @@ -8,9 +8,8 @@ /// public static Vector2 GetMousePosition() { - var viewport = RoomManager.Current.Viewport; - return viewport.GetMousePosition() / GameConfig.WindowScale - - (GameConfig.ViewportSize / 2) + MainCamera.Main.GlobalPosition; + var application = GameApplication.Instance; + return application.GlobalToViewPosition(application.GetGlobalMousePosition()); } /// @@ -18,7 +17,7 @@ /// public static void Update(float delta) { - + } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/role/Player.cs b/DungeonShooting_Godot/src/game/role/Player.cs index 2909229..c2dd69f 100644 --- a/DungeonShooting_Godot/src/game/role/Player.cs +++ b/DungeonShooting_Godot/src/game/role/Player.cs @@ -131,11 +131,11 @@ if (Holster.ActiveWeapon != null && Holster.ActiveWeapon.Reloading) { - RoomUI.Current.ReloadBar.ShowBar(gPos, 1 - Holster.ActiveWeapon.ReloadProgress); + GameApplication.Instance.Ui.ReloadBar.ShowBar(gPos, 1 - Holster.ActiveWeapon.ReloadProgress); } else { - RoomUI.Current.ReloadBar.HideBar(); + GameApplication.Instance.Ui.ReloadBar.HideBar(); } } @@ -151,12 +151,12 @@ protected override void OnChangeHp(int hp) { - RoomUI.Current.SetHp(hp); + GameApplication.Instance.Ui.SetHp(hp); } protected override void OnChangeMaxHp(int maxHp) { - RoomUI.Current.SetMaxHp(maxHp); + GameApplication.Instance.Ui.SetMaxHp(maxHp); } protected override void ChangeInteractiveItem(CheckInteractiveResult result) @@ -164,26 +164,26 @@ if (result == null) { //隐藏互动提示 - RoomUI.Current.InteractiveTipBar.HideBar(); + GameApplication.Instance.Ui.InteractiveTipBar.HideBar(); } else { if (InteractiveItem is Weapon gun) { //显示互动提示 - RoomUI.Current.InteractiveTipBar.ShowBar(result.Target.GlobalPosition, result.ShowIcon, result.Message); + GameApplication.Instance.Ui.InteractiveTipBar.ShowBar(result.Target.GlobalPosition, result.ShowIcon, result.Message); } } } protected void OnChangeShield(int shield) { - RoomUI.Current.SetShield(shield); + GameApplication.Instance.Ui.SetShield(shield); } protected void OnChangeMaxShield(int maxShield) { - RoomUI.Current.SetMaxShield(maxShield); + GameApplication.Instance.Ui.SetMaxShield(maxShield); } /// @@ -194,11 +194,11 @@ var gun = Holster.ActiveWeapon; if (gun != null) { - RoomUI.Current.SetGunTexture(gun.GetDefaultTexture()); + GameApplication.Instance.Ui.SetGunTexture(gun.GetDefaultTexture()); } else { - RoomUI.Current.SetGunTexture(null); + GameApplication.Instance.Ui.SetGunTexture(null); } } @@ -210,7 +210,7 @@ var gun = Holster.ActiveWeapon; if (gun != null) { - RoomUI.Current.SetAmmunition(gun.CurrAmmo, gun.ResidueAmmo); + GameApplication.Instance.Ui.SetAmmunition(gun.CurrAmmo, gun.ResidueAmmo); } } diff --git a/DungeonShooting_Godot/src/game/room/RoomManager.cs b/DungeonShooting_Godot/src/game/room/RoomManager.cs index 64fef5a..6d432fa 100644 --- a/DungeonShooting_Godot/src/game/room/RoomManager.cs +++ b/DungeonShooting_Godot/src/game/room/RoomManager.cs @@ -1,4 +1,3 @@ -using System; using Godot; /// @@ -6,39 +5,15 @@ /// public class RoomManager : Node2D { - /// - /// 鼠标指针 - /// - [Export] public PackedScene MouseCursor; - - public static RoomManager Current { get; private set; } - - public CanvasLayer UI; - public Cursor Cursor { get; private set; } public Player Player { get; private set; } public Node2D ObjectRoot { get; private set; } public YSort SortRoot { get; private set; } - public Viewport Viewport { get; private set; } - public ViewportContainer ViewportContainer { get; private set; } public override void _EnterTree() { - Current = this; Input.MouseMode = Input.MouseModeEnum.Hidden; - UI = GetNode("UI"); - - // 初始化鼠标 - Cursor = MouseCursor.Instance(); - AddChild(Cursor); - - SortRoot = GetNode("ItemRoot"); - Viewport = GetParentOrNull(); - if (Viewport != null) - { - ViewportContainer = Viewport.GetParentOrNull(); - } - + SortRoot = GetNode("SortRoot"); ObjectRoot = GetNode("ObjectRoot"); //初始化地图 diff --git a/DungeonShooting_Godot/src/game/ui/Cursor.cs b/DungeonShooting_Godot/src/game/ui/Cursor.cs index 4076349..12b6a6a 100644 --- a/DungeonShooting_Godot/src/game/ui/Cursor.cs +++ b/DungeonShooting_Godot/src/game/ui/Cursor.cs @@ -20,7 +20,7 @@ public override void _Process(float delta) { - var targetGun = RoomManager.Current?.Player?.Holster.ActiveWeapon; + var targetGun = GameApplication.Instance.Room.Player?.Holster.ActiveWeapon; if (targetGun != null) { SetScope(targetGun.CurrScatteringRange, targetGun); @@ -44,7 +44,8 @@ { if (targetGun != null) { - var len = GlobalPosition.DistanceTo(targetGun.GlobalPosition); + 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); @@ -59,6 +60,6 @@ private void SetCursorPos() { - Position = InputManager.GetMousePosition(); + GlobalPosition = GetGlobalMousePosition(); } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/ui/InteractiveTipBar.cs b/DungeonShooting_Godot/src/game/ui/InteractiveTipBar.cs index 09f1f1b..b582128 100644 --- a/DungeonShooting_Godot/src/game/ui/InteractiveTipBar.cs +++ b/DungeonShooting_Godot/src/game/ui/InteractiveTipBar.cs @@ -35,7 +35,7 @@ /// 显示文本 public void ShowBar(Vector2 pos, string icon, string message) { - GlobalPosition = pos; + GlobalPosition = GameApplication.Instance.ViewToGlobalPosition(pos); Message.Text = message; if (currImage != icon) { diff --git a/DungeonShooting_Godot/src/game/ui/ReloadBar.cs b/DungeonShooting_Godot/src/game/ui/ReloadBar.cs index f496360..520a332 100644 --- a/DungeonShooting_Godot/src/game/ui/ReloadBar.cs +++ b/DungeonShooting_Godot/src/game/ui/ReloadBar.cs @@ -35,7 +35,7 @@ public void ShowBar(Vector2 position, float progress) { Visible = true; - GlobalPosition = position; + GlobalPosition = GameApplication.Instance.ViewToGlobalPosition(position); progress = Mathf.Clamp(progress, 0, 1); block.Position = new Vector2(startX + (width - 3) * progress, 0); } diff --git a/DungeonShooting_Godot/src/game/ui/RoomUI.cs b/DungeonShooting_Godot/src/game/ui/RoomUI.cs index 23482dd..b2427a9 100644 --- a/DungeonShooting_Godot/src/game/ui/RoomUI.cs +++ b/DungeonShooting_Godot/src/game/ui/RoomUI.cs @@ -1,4 +1,3 @@ -using System; using Godot; /// @@ -6,8 +5,7 @@ /// public class RoomUI : Control { - - public static RoomUI Current { get; private set; } + //public static RoomUI Current { get; private set; } /// /// 当前血量 @@ -43,11 +41,6 @@ public override void _EnterTree() { - Current = this; - } - - public override void _Ready() - { hpSlot = GetNode("Control/HealthBar/HpSlot"); shieldSlot = GetNode("Control/HealthBar/ShieldSlot"); hpBar = GetNode("Control/HealthBar/HpSlot/HpBar"); @@ -61,10 +54,13 @@ ReloadBar = GetNode("GlobalNode/ReloadBar"); ReloadBar.Visible = false; + } + public override void _Ready() + { //将 GlobalNode 节点下的 ui 节点放入全局坐标中 var tempNode = GetNode("GlobalNode"); - var root = RoomManager.Current; + var root = GameApplication.Instance.GlobalNodeRoot; var count = tempNode.GetChildCount(); for (int i = count - 1; i >= 0; i--) {