diff --git a/DungeonShooting_Godot/src/framework/Component.cs b/DungeonShooting_Godot/src/framework/Component.cs index 2f2264a..c07223d 100644 --- a/DungeonShooting_Godot/src/framework/Component.cs +++ b/DungeonShooting_Godot/src/framework/Component.cs @@ -206,7 +206,7 @@ } /// - /// 如果开启 debug, 则每帧调用该函数, 可用于绘制文字线段等 + /// 如果开启 debug, 则每帧调用该函数, 可用于绘制文字线段等, 需要调用 ActivityObject 身上的绘制函数 /// public virtual void DebugDraw() { diff --git a/DungeonShooting_Godot/src/framework/ExternalForce.cs b/DungeonShooting_Godot/src/framework/ExternalForce.cs index a3c3225..cfd4ce9 100644 --- a/DungeonShooting_Godot/src/framework/ExternalForce.cs +++ b/DungeonShooting_Godot/src/framework/ExternalForce.cs @@ -17,6 +17,16 @@ public bool Enable { get; set; } = true; /// + /// 阻力大小, 也就是速度每秒衰减的量 + /// + public float Resistance { get; set; } = 5; + + /// + /// 当速度到达 0 后是否自动销毁 + /// + public bool AutoDestroy { get; set; } = true; + + /// /// 当前力的速率 /// public Vector2 Velocity { get; set; } = Vector2.Zero; diff --git a/DungeonShooting_Godot/src/framework/MoveController.cs b/DungeonShooting_Godot/src/framework/MoveController.cs index ccedee0..4c23d43 100644 --- a/DungeonShooting_Godot/src/framework/MoveController.cs +++ b/DungeonShooting_Godot/src/framework/MoveController.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; +using System.Linq; using Godot; /// @@ -7,6 +8,8 @@ /// public class MoveController : Component { + private static long _index = 0; + /// /// 玩家受到的外力的集合 /// @@ -41,6 +44,19 @@ } /// + /// 快速窗口一个外力, 该外力为匿名外力, 当速率变为 0 时自动销毁 + /// + /// 外力速率 + /// 阻力大小 + public ExternalForce AddForce(Vector2 velocity, float resistance) + { + var force = AddForce("_anonymity_" + _index++); + force.Velocity = velocity; + force.Resistance = resistance; + return force; + } + + /// /// 根据名称添加一个外力, 并返回创建的外力的对象, 如果存在这个名称的外力, 移除之前的外力 /// public ExternalForce AddForce(string name) @@ -163,12 +179,23 @@ var scaleX = finallyEf.x == 0f ? 0 : Mathf.Abs((_velocity.x - _basisVelocity.x) / finallyEf.x); //y轴外力 var scaleY = finallyEf.y == 0f ? 0 : Mathf.Abs((_velocity.y - _basisVelocity.y) / finallyEf.y); - foreach (var force in _forceList) + for (var i = 0; i < _forceList.Count; i++) { + var force = _forceList[i]; if (force.Enable) { var velocity = force.Velocity; force.Velocity = new Vector2(velocity.x * scaleX, velocity.y * scaleY); + if (force.Resistance != 0) + { + force.Velocity = force.Velocity.MoveToward(Vector2.Zero, force.Resistance * delta); + } + + if (force.AutoDestroy && force.Velocity == Vector2.Zero) + { + _forceList.RemoveAt(i--); + GD.Print("移除外力: " + force.Name); + } } } } @@ -178,4 +205,14 @@ _velocity = finallyEf; } } + + public override void DebugDraw() + { + var globalRotation = GlobalRotation; + ActivityObject.DrawLine(Vector2.Zero, BasisVelocity.Rotated(-globalRotation), Colors.Yellow); + foreach (var force in _forceList) + { + ActivityObject.DrawLine(Vector2.Zero, force.Velocity.Rotated(-globalRotation), Colors.YellowGreen); + } + } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/role/enemy/state/AiNormalState.cs b/DungeonShooting_Godot/src/game/role/enemy/state/AiNormalState.cs index 1e569b9..fe9312a 100644 --- a/DungeonShooting_Godot/src/game/role/enemy/state/AiNormalState.cs +++ b/DungeonShooting_Godot/src/game/role/enemy/state/AiNormalState.cs @@ -23,6 +23,7 @@ //移动停顿计时器 private float _pauseTimer; + private bool _moveFlag; public AiNormalState() : base(AiStateEnum.AiNormal) { @@ -35,6 +36,7 @@ _againstWall = false; _againstWallNormalAngle = 0; _pauseTimer = 0; + _moveFlag = false; } public override void PhysicsProcess(float delta) @@ -79,8 +81,18 @@ { _pauseTimer = Utils.RandRange(0.3f, 2f); _isMoveOver = true; + _moveFlag = false; Master.BasisVelocity = Vector2.Zero; } + else if (!_moveFlag) + { + _moveFlag = true; + //计算移动 + var nextPos = Master.NavigationAgent2D.GetNextLocation(); + Master.AnimatedSprite.Animation = AnimatorNames.Run; + Master.BasisVelocity = (nextPos - Master.GlobalPosition - Master.NavigationPoint.Position).Normalized() * + Master.MoveSpeed; + } else { var lastSlideCollision = Master.GetLastSlideCollision(); @@ -88,6 +100,7 @@ { _pauseTimer = Utils.RandRange(0.1f, 0.5f); _isMoveOver = true; + _moveFlag = false; Master.BasisVelocity = Vector2.Zero; } else diff --git a/DungeonShooting_Godot/src/game/role/enemy/state/AiSurroundState.cs b/DungeonShooting_Godot/src/game/role/enemy/state/AiSurroundState.cs index 49644d8..2643331 100644 --- a/DungeonShooting_Godot/src/game/role/enemy/state/AiSurroundState.cs +++ b/DungeonShooting_Godot/src/game/role/enemy/state/AiSurroundState.cs @@ -16,7 +16,8 @@ //移动停顿计时器 private float _pauseTimer; - + private bool _moveFlag; + //下一个移动点 private Vector2 _nextPosition; @@ -29,6 +30,7 @@ IsInView = true; _isMoveOver = true; _pauseTimer = 0; + _moveFlag = false; } public override void PhysicsProcess(float delta) @@ -81,8 +83,18 @@ { _pauseTimer = Utils.RandRange(0f, 0.5f); _isMoveOver = true; + _moveFlag = false; Master.BasisVelocity = Vector2.Zero; } + else if (!_moveFlag) + { + _moveFlag = true; + //计算移动 + var nextPos = Master.NavigationAgent2D.GetNextLocation(); + Master.AnimatedSprite.Animation = AnimatorNames.Run; + Master.BasisVelocity = (nextPos - Master.GlobalPosition - Master.NavigationPoint.Position).Normalized() * + Master.MoveSpeed; + } else { var lastSlideCollision = Master.GetLastSlideCollision(); @@ -90,6 +102,7 @@ { _pauseTimer = Utils.RandRange(0f, 0.3f); _isMoveOver = true; + _moveFlag = false; Master.BasisVelocity = Vector2.Zero; } else diff --git a/DungeonShooting_Godot/src/game/room/RoomManager.cs b/DungeonShooting_Godot/src/game/room/RoomManager.cs index aab39af..f38fd4e 100644 --- a/DungeonShooting_Godot/src/game/room/RoomManager.cs +++ b/DungeonShooting_Godot/src/game/room/RoomManager.cs @@ -67,6 +67,8 @@ Player.Name = "Player"; Player.PutDown(); + Player.GetComponent().AddForce(new Vector2(10, 10), 5); + // var testActivity = new TestActivity(); // testActivity.Position = new Vector2(10, 10); // testActivity.PutDown(); @@ -105,14 +107,14 @@ //enemy1.PickUpWeapon(WeaponManager.GetGun("1003")); enemy1.PickUpWeapon(WeaponManager.GetGun("1001")); - for (int i = 0; i < 10; i++) - { - var enemyTemp = new Enemy(); - enemyTemp.Name = "EnemyTemp" + i; - enemyTemp.PutDown(new Vector2(150 + (i + 1) * 20, 300)); - enemyTemp.PickUpWeapon(WeaponManager.GetGun("1003")); - enemyTemp.PickUpWeapon(WeaponManager.GetGun("1001")); - } + // for (int i = 0; i < 10; i++) + // { + // var enemyTemp = new Enemy(); + // enemyTemp.Name = "EnemyTemp" + i; + // enemyTemp.PutDown(new Vector2(150 + (i + 1) * 20, 300)); + // enemyTemp.PickUpWeapon(WeaponManager.GetGun("1003")); + // enemyTemp.PickUpWeapon(WeaponManager.GetGun("1001")); + // } var enemy2 = new Enemy(); enemy2.Name = "Enemy2";