diff --git a/DungeonShooting_Godot/project.godot b/DungeonShooting_Godot/project.godot index 60c6fa3..67e6968 100644 --- a/DungeonShooting_Godot/project.godot +++ b/DungeonShooting_Godot/project.godot @@ -20,14 +20,12 @@ [display] -window/size/width=480 -window/size/height=270 -window/size/test_width=1920 -window/size/test_height=1080 +window/size/width=1920 +window/size/height=1080 window/dpi/allow_hidpi=true window/vsync/vsync_via_compositor=true +window/stretch/mode="2d" window/stretch/aspect="keep_width" -window/stretch/shrink=4.0 [editor_plugins] diff --git a/DungeonShooting_Godot/scene/Main.tscn b/DungeonShooting_Godot/scene/Main.tscn index 92a282a..2a3a84f 100644 --- a/DungeonShooting_Godot/scene/Main.tscn +++ b/DungeonShooting_Godot/scene/Main.tscn @@ -1,20 +1,44 @@ -[gd_scene load_steps=2 format=2] +[gd_scene load_steps=4 format=2] [ext_resource path="res://scene/Room.tscn" type="PackedScene" id=1] +[sub_resource type="Shader" id=1] +code = "shader_type canvas_item; + +uniform vec2 offset = vec2(0.0 , 0.0); + +void vertex() { + + VERTEX += offset; + +} + +" + +[sub_resource type="ShaderMaterial" id=2] +shader = SubResource( 1 ) +shader_param/offset = Vector2( 0, 0 ) + [node name="Main" type="Node2D"] [node name="CanvasLayer" type="CanvasLayer" parent="."] +offset = Vector2( -4, -4 ) +transform = Transform2D( 1, 0, 0, 1, -4, -4 ) [node name="ViewportContainer" type="ViewportContainer" parent="CanvasLayer"] +material = SubResource( 2 ) anchor_right = 1.0 anchor_bottom = 1.0 +margin_right = -1438.0 +margin_bottom = -808.0 +rect_scale = Vector2( 4, 4 ) stretch = true [node name="Viewport" type="Viewport" parent="CanvasLayer/ViewportContainer"] -size = Vector2( 480, 270 ) +size = Vector2( 482, 272 ) own_world = true transparent_bg = true +handle_input_locally = false debanding = true render_target_update_mode = 3 diff --git a/DungeonShooting_Godot/src/game/GameConfig.cs b/DungeonShooting_Godot/src/game/GameConfig.cs index 91ebe06..998f5c4 100644 --- a/DungeonShooting_Godot/src/game/GameConfig.cs +++ b/DungeonShooting_Godot/src/game/GameConfig.cs @@ -1,4 +1,6 @@ +using Godot; + public static class GameConfig { /// @@ -9,4 +11,12 @@ /// 重力加速度 /// public static readonly float G = 250f; + /// + /// 像素缩放 + /// + public static readonly int WindowScale = 4; + /// + /// 游戏视图大小 + /// + public static readonly Vector2 ViewportSize = new Vector2(480, 270); } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/camera/MainCamera.cs b/DungeonShooting_Godot/src/game/camera/MainCamera.cs index c68b933..71cf8ac 100644 --- a/DungeonShooting_Godot/src/game/camera/MainCamera.cs +++ b/DungeonShooting_Godot/src/game/camera/MainCamera.cs @@ -19,34 +19,57 @@ public bool Enable { get; set; } = true; private long _index = 0; - private Vector2 _prossesDistance = Vector2.Zero; - private Vector2 _prossesDirectiona = Vector2.Zero; + 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; - } - public override void _PhysicsProcess(float delta) - { - _Shake(delta); + _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(); + // 使用lerp,相机的位置移动到鼠标的位置 + var camPos = player.GlobalPosition; + //var camPos = player.GlobalPosition.LinearInterpolate(mousePos, 0); + // 用另一种方式使运动平稳 + _camPos = _camPos.LinearInterpolate(camPos, 5 * delta) + _shakeOffset; + // 计算新相机的“亚像素”位置 + var camSubpixelPos = _camPos.Round() - _camPos; + // 更新主要视图端口的shader参数 + (viewportContainer.Material as ShaderMaterial)?.SetShaderParam("offset", camSubpixelPos); + // 把相机调到新位置,然后旋转。 + GlobalPosition = _camPos.Round(); + } + + /// /// 设置帧抖动, 结束后自动清零, 需要每一帧调用 /// /// 抖动的力度 public void ProcessShake(Vector2 value) { - if (value.Length() > _prossesDistance.Length()) + if (value.Length() > _processDistance.Length()) { - _prossesDistance = value; + _processDistance = value; } } public void ProcessDirectionalShake(Vector2 value) { - _prossesDirectiona += value; + _processDirection += value; } /// @@ -71,18 +94,17 @@ { if (Enable) { - var _distance = _CalculateDistance(); - Offset += new Vector2( - (float)GD.RandRange(-_distance.x, _distance.x) - Offset.x, - (float)GD.RandRange(-_distance.y, _distance.y) - Offset.y + 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 ); - Offset += _prossesDirectiona; - _prossesDistance = Vector2.Zero; - _prossesDirectiona = Vector2.Zero; + _processDistance = Vector2.Zero; + _processDirection = Vector2.Zero; } else { - Offset = Offset.LinearInterpolate(Vector2.Zero, RecoveryCoefficient * delta); + _shakeOffset = _shakeOffset.LinearInterpolate(Vector2.Zero, RecoveryCoefficient * delta); } } @@ -101,7 +123,7 @@ temp = value; } } - return _prossesDistance.Length() > length ? _prossesDistance : temp; + return _processDistance.Length() > length ? _processDistance : temp; } } diff --git a/DungeonShooting_Godot/src/game/item/weapon/bullet/OrdinaryBullets.cs b/DungeonShooting_Godot/src/game/item/weapon/bullet/OrdinaryBullets.cs index 4458e5f..3546cf0 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; - GetTree().CurrentScene.AddChild(hit); + RoomManager.Current.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 ced6bee..a5445aa 100644 --- a/DungeonShooting_Godot/src/game/item/weapon/gun/Gun.cs +++ b/DungeonShooting_Godot/src/game/item/weapon/gun/Gun.cs @@ -23,6 +23,8 @@ ScatteringRangeBackSpeed = 40; //连发 ContinuousShoot = true; + AmmoCapacity = 120; + MaxAmmoCapacity = 120 * 70; //扳机检测间隔 TriggerInterval = 0f; //连发数量 diff --git a/DungeonShooting_Godot/src/game/manager/GameManager.cs b/DungeonShooting_Godot/src/game/manager/GameManager.cs index 87b3571..6c47b98 100644 --- a/DungeonShooting_Godot/src/game/manager/GameManager.cs +++ b/DungeonShooting_Godot/src/game/manager/GameManager.cs @@ -14,4 +14,9 @@ //扫描并注册当前程序集下的武器 WeaponManager.RegisterWeaponFromAssembly(GetType().Assembly); } + + public override void _Process(float delta) + { + InputManager.Update(delta); + } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/manager/InputManager.cs b/DungeonShooting_Godot/src/game/manager/InputManager.cs index e176a71..ad3f978 100644 --- a/DungeonShooting_Godot/src/game/manager/InputManager.cs +++ b/DungeonShooting_Godot/src/game/manager/InputManager.cs @@ -3,11 +3,22 @@ public static class InputManager { - // public static Vector2 MousePosition => GameManager.Instance.GetGlobalMousePosition() / 4; - // - // public static Vector2 GetLocalMousePosition(Node2D target) - // { - // return target.GetLocalMousePosition() / 4; - // } - + /// + /// 获取鼠标坐标 + /// + public static Vector2 GetMousePosition() + { + var viewport = RoomManager.Current.Viewport; + return viewport.GetMousePosition() / GameConfig.WindowScale + - (GameConfig.ViewportSize / 2) + MainCamera.Main.GlobalPosition; + } + + /// + /// 更新输入管理器 + /// + 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 e817f1c..112277c 100644 --- a/DungeonShooting_Godot/src/game/role/Player.cs +++ b/DungeonShooting_Godot/src/game/role/Player.cs @@ -64,11 +64,11 @@ base._Ready(); //让相机跟随玩家 - var remoteTransform = new RemoteTransform2D(); - AddChild(remoteTransform); - MainCamera.Main.GlobalPosition = GlobalPosition; - MainCamera.Main.ResetSmoothing(); - remoteTransform.RemotePath = remoteTransform.GetPathTo(MainCamera.Main); + // var remoteTransform = new RemoteTransform2D(); + // AddChild(remoteTransform); + // MainCamera.Main.GlobalPosition = GlobalPosition; + // MainCamera.Main.ResetSmoothing(); + // remoteTransform.RemotePath = remoteTransform.GetPathTo(MainCamera.Main); Holster.SlotList[2].Enable = true; Holster.SlotList[3].Enable = true; @@ -84,7 +84,7 @@ { base._Process(delta); - Vector2 mousePos = GetGlobalMousePosition(); + Vector2 mousePos = InputManager.GetMousePosition(); //枪口跟随鼠标 MountPoint.LookAt(mousePos); //脸的朝向 diff --git a/DungeonShooting_Godot/src/game/room/RoomManager.cs b/DungeonShooting_Godot/src/game/room/RoomManager.cs index cb27b0c..138d9d0 100644 --- a/DungeonShooting_Godot/src/game/room/RoomManager.cs +++ b/DungeonShooting_Godot/src/game/room/RoomManager.cs @@ -18,11 +18,13 @@ public Player Player { get; 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; + Input.MouseMode = Input.MouseModeEnum.Hidden; UI = GetNode("UI"); @@ -31,6 +33,12 @@ AddChild(Cursor); SortRoot = GetNode("ItemRoot"); + Viewport = GetParentOrNull(); + if (Viewport != null) + { + ViewportContainer = Viewport.GetParentOrNull(); + } + ObjectRoot = GetNode("ObjectRoot"); //初始化地图 @@ -50,19 +58,16 @@ { //播放bgm SoundManager.PlayeMusic("intro.ogg", this, -17f); - var gun1 = WeaponManager.GetGun("1001"); - gun1.PutDown(new Vector2(80, 80)); - var gun2 = WeaponManager.GetGun("1002"); - gun2.PutDown(new Vector2(80, 120)); - var gun3 = WeaponManager.GetGun("1003"); - gun3.PutDown(new Vector2(120, 80)); - - var gun4 = WeaponManager.GetGun("1003"); - gun4.PutDown(new Vector2(180, 80)); - var gun5 = WeaponManager.GetGun("1003"); - gun5.PutDown(new Vector2(180, 180)); - var gun6 = WeaponManager.GetGun("1002"); - gun6.PutDown(new Vector2(180, 120)); + WeaponManager.GetGun("1001").PutDown(new Vector2(100, 80)); + WeaponManager.GetGun("1001").PutDown(new Vector2(80, 100)); + WeaponManager.GetGun("1001").PutDown(new Vector2(80, 80)); + WeaponManager.GetGun("1002").PutDown(new Vector2(80, 120)); + WeaponManager.GetGun("1003").PutDown(new Vector2(120, 80)); + + WeaponManager.GetGun("1003").PutDown(new Vector2(180, 80)); + WeaponManager.GetGun("1003").PutDown(new Vector2(180, 180)); + WeaponManager.GetGun("1002").PutDown(new Vector2(180, 120)); + } public override void _Process(float delta) diff --git a/DungeonShooting_Godot/src/game/ui/Cursor.cs b/DungeonShooting_Godot/src/game/ui/Cursor.cs index a2d6dd4..4076349 100644 --- a/DungeonShooting_Godot/src/game/ui/Cursor.cs +++ b/DungeonShooting_Godot/src/game/ui/Cursor.cs @@ -5,7 +5,6 @@ /// public class Cursor : Node2D { - private Sprite lt; private Sprite lb; private Sprite rt; @@ -33,6 +32,11 @@ SetCursorPos(); } + public Vector2 GetPos() + { + return Vector2.Zero; + } + /// /// 设置光标半径范围 /// @@ -55,6 +59,6 @@ private void SetCursorPos() { - Position = GetGlobalMousePosition(); + Position = InputManager.GetMousePosition(); } } \ No newline at end of file