diff --git a/DungeonShooting_Godot/src/game/camera/GameCamera.cs b/DungeonShooting_Godot/src/game/camera/GameCamera.cs
index 2dc34fe..993097e 100644
--- a/DungeonShooting_Godot/src/game/camera/GameCamera.cs
+++ b/DungeonShooting_Godot/src/game/camera/GameCamera.cs
@@ -1,3 +1,4 @@
+using System;
using System.Collections.Generic;
using Godot;
@@ -12,10 +13,15 @@
public static GameCamera Main { get; private set; }
///
+ /// 相机坐标更新完成事件
+ ///
+ public event Action OnPositionUpdateEvent;
+
+ ///
/// 恢复系数
///
[Export]
- public float RecoveryCoefficient = 100f;
+ public float RecoveryCoefficient = 25f;
///
/// 抖动开关
@@ -31,7 +37,8 @@
//public Vector2 SubPixelPosition { get; private set; }
private long _index = 0;
- private Vector2 _processDistance = Vector2.Zero;
+
+ private Vector2 _processDistanceSquared = Vector2.Zero;
private Vector2 _processDirection = Vector2.Zero;
//抖动数据
private readonly Dictionary _shakeMap = new Dictionary();
@@ -39,9 +46,13 @@
private Vector2 _camPos;
private Vector2 _shakeOffset = Vector2.Zero;
- public override void _Ready()
+ public GameCamera()
{
Main = this;
+ }
+
+ public override void _Ready()
+ {
_camPos = GlobalPosition;
}
@@ -72,8 +83,18 @@
{
targetPos = targetPosition.Lerp(mousePosition, 0.3f);
}
- _camPos = _camPos.Lerp(targetPos, Mathf.Min(6 * newDelta, 1)) + _shakeOffset;
- GlobalPosition = _camPos.Round();
+ _camPos = (_camPos.Lerp(targetPos, Mathf.Min(6 * newDelta, 1))).Round();
+ GlobalPosition = _camPos;
+
+ Offset = _shakeOffset.Round();
+
+ //_temp = _camPos - targetPosition;
+
+ //调用相机更新事件
+ if (OnPositionUpdateEvent != null)
+ {
+ OnPositionUpdateEvent(newDelta);
+ }
}
}
@@ -98,32 +119,35 @@
/// 设置帧抖动, 结束后自动清零, 需要每一帧调用
///
/// 抖动的力度
- public void ProcessShake(Vector2 value)
+ public void Shake(Vector2 value)
{
- if (value.Length() > _processDistance.Length())
+ if (value.LengthSquared() > _processDistanceSquared.LengthSquared())
{
- _processDistance = value;
+ _processDistanceSquared = value;
}
}
-
- public void ProcessDirectionalShake(Vector2 value)
+
+ ///
+ /// 添加一个单方向上的抖动, 该帧结束后自动清零
+ ///
+ public void DirectionalShake(Vector2 value)
{
_processDirection += value;
}
-
+
///
/// 创建一个抖动, 并设置抖动时间
///
- /// 抖动力度
- /// 抖动生效时间
public async void CreateShake(Vector2 value, float time)
{
if (time > 0)
{
- long tempIndex = _index++;
- SceneTreeTimer sceneTreeTimer = GetTree().CreateTimer(time);
+ value.X = Mathf.Abs(value.X);
+ value.Y = Mathf.Abs(value.Y);
+ var tempIndex = _index++;
+ var sceneTreeTimer = GetTree().CreateTimer(time);
_shakeMap[tempIndex] = value;
- await ToSignal(sceneTreeTimer, "timeout");
+ await ToSignal(sceneTreeTimer, Timer.SignalName.Timeout);
_shakeMap.Remove(tempIndex);
}
}
@@ -133,13 +157,14 @@
{
if (EnableShake)
{
- var distance = _CalculateDistance();
+ var distance = _CalculateDistanceSquared();
+ distance = new Vector2(Mathf.Sqrt(distance.X), Mathf.Sqrt(distance.Y));
_shakeOffset += _processDirection + new Vector2(
- Utils.RandomRangeFloat(-distance.X, distance.X) - _shakeOffset.X,
- Utils.RandomRangeFloat(-distance.Y, distance.Y) - _shakeOffset.Y
+ (float)GD.RandRange(-distance.X, distance.X) - Offset.X,
+ (float)GD.RandRange(-distance.Y, distance.Y) - Offset.Y
);
- _processDistance = Vector2.Zero;
- _processDirection = Vector2.Zero;
+ _processDistanceSquared = Vector2.Zero;
+ _processDirection = _processDirection.Lerp(Vector2.Zero, RecoveryCoefficient * delta);
}
else
{
@@ -148,21 +173,21 @@
}
//计算相机需要抖动的值
- private Vector2 _CalculateDistance()
+ private Vector2 _CalculateDistanceSquared()
{
- Vector2 temp = Vector2.Zero;
+ var temp = Vector2.Zero;
float length = 0;
- foreach (var item in _shakeMap)
+
+ foreach (var keyValuePair in _shakeMap)
{
- var value = item.Value;
- float tempLenght = value.Length();
+ var tempLenght = keyValuePair.Value.LengthSquared();
if (tempLenght > length)
{
length = tempLenght;
- temp = value;
+ temp = keyValuePair.Value;
}
}
- return _processDistance.Length() > length ? _processDistance : temp;
+
+ return _processDistanceSquared.LengthSquared() > length ? _processDistanceSquared : temp;
}
-
}
diff --git a/DungeonShooting_Godot/src/game/item/weapon/gun/Gun.cs b/DungeonShooting_Godot/src/game/item/weapon/gun/Gun.cs
index 89060d4..acad95f 100644
--- a/DungeonShooting_Godot/src/game/item/weapon/gun/Gun.cs
+++ b/DungeonShooting_Godot/src/game/item/weapon/gun/Gun.cs
@@ -110,7 +110,7 @@
if (Master == GameApplication.Instance.RoomManager.Player)
{
//创建抖动
- GameCamera.Main.ProcessDirectionalShake(Vector2.Right.Rotated(GlobalRotation) * 2f);
+ GameCamera.Main.DirectionalShake(Vector2.Right.Rotated(GlobalRotation) * 2f);
}
//创建开火特效
diff --git a/DungeonShooting_Godot/src/game/item/weapon/gun/Shotgun.cs b/DungeonShooting_Godot/src/game/item/weapon/gun/Shotgun.cs
index 7fb1847..74183ad 100644
--- a/DungeonShooting_Godot/src/game/item/weapon/gun/Shotgun.cs
+++ b/DungeonShooting_Godot/src/game/item/weapon/gun/Shotgun.cs
@@ -75,7 +75,7 @@
if (Master == GameApplication.Instance.RoomManager.Player)
{
//创建抖动
- GameCamera.Main.ProcessDirectionalShake(Vector2.Right.Rotated(GlobalRotation) * 2f);
+ GameCamera.Main.DirectionalShake(Vector2.Right.Rotated(GlobalRotation) * 2f);
}
//创建开火特效
diff --git a/DungeonShooting_Godot/src/game/role/Player.cs b/DungeonShooting_Godot/src/game/role/Player.cs
index ac1d6dc..21d2d68 100644
--- a/DungeonShooting_Godot/src/game/role/Player.cs
+++ b/DungeonShooting_Godot/src/game/role/Player.cs
@@ -25,7 +25,9 @@
public override void OnInit()
{
base.OnInit();
-
+
+ GameCamera.Main.OnPositionUpdateEvent += OnCameraPositionUpdate;
+
AttackLayer = PhysicsLayer.Wall | PhysicsLayer.Props | PhysicsLayer.Enemy;
Camp = CampEnum.Camp1;
@@ -58,9 +60,9 @@
{
base.Process(delta);
//脸的朝向
- var gPos = GlobalPosition;
if (LookTarget == null)
{
+ var gPos = GlobalPosition;
Vector2 mousePos = InputManager.GetViewportMousePosition();
if (mousePos.X > gPos.X && Face == FaceDirection.Left)
{
@@ -100,16 +102,6 @@
}
//刷新显示的弹药剩余量
RefreshGunAmmunition();
-
- var reloadBar = GameApplication.Instance.Ui.ReloadBar;
- if (Holster.ActiveWeapon != null && Holster.ActiveWeapon.Reloading)
- {
- reloadBar.ShowBar(gPos, 1 - Holster.ActiveWeapon.ReloadProgress);
- }
- else
- {
- reloadBar.HideBar();
- }
}
protected override void PhysicsProcess(float delta)
@@ -120,6 +112,21 @@
PlayAnim();
}
+ private void OnCameraPositionUpdate(float delta)
+ {
+ var reloadBar = GameApplication.Instance.Ui.ReloadBar;
+ if (Holster.ActiveWeapon != null && Holster.ActiveWeapon.Reloading)
+ {
+ reloadBar.ShowBar(GlobalPosition, 1 - Holster.ActiveWeapon.ReloadProgress);
+ }
+ else
+ {
+ reloadBar.HideBar();
+ }
+ // GameApplication.Instance.Ui.Control.MapBar.Instance.GlobalPosition =
+ // GameApplication.Instance.ViewToGlobalPosition(Player.Current.GlobalPosition);
+ }
+
public override void ExchangeNext()
{
base.ExchangeNext();