diff --git a/DungeonShooting_Godot/prefab/role/Enemy.tscn b/DungeonShooting_Godot/prefab/role/Enemy.tscn index 0315428..3fad24b 100644 --- a/DungeonShooting_Godot/prefab/role/Enemy.tscn +++ b/DungeonShooting_Godot/prefab/role/Enemy.tscn @@ -26,6 +26,7 @@ [node name="AnimatedSprite" parent="." index="2"] material = SubResource( 2 ) +frame = 0 [node name="ViewRay" type="RayCast2D" parent="." index="6"] position = Vector2( 0, -8 ) @@ -36,3 +37,5 @@ [node name="CollisionShape2D" type="CollisionShape2D" parent="MarginArea" index="0"] position = Vector2( 0, -8 ) shape = SubResource( 3 ) + +[node name="NavigationAgent2D" type="NavigationAgent2D" parent="." index="9"] diff --git a/DungeonShooting_Godot/scene/Room.tscn b/DungeonShooting_Godot/scene/Room.tscn index a9137ae..815aea1 100644 --- a/DungeonShooting_Godot/scene/Room.tscn +++ b/DungeonShooting_Godot/scene/Room.tscn @@ -1,16 +1,24 @@ -[gd_scene load_steps=5 format=2] +[gd_scene load_steps=6 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://src/game/camera/GameCamera.cs" type="Script" id=5] +[sub_resource type="NavigationPolygon" id=2] +vertices = PoolVector2Array( 387, 266, 587, 263, 586, 345, 531, 345, 531, 412, 588, 411, 587, 588, 342, 588, 525, 412, 343, 520, 344, 508, 283, 508, 284, 520, 284, 588, 53, 589, 53, 411, 108, 412, 115, 413, 55, 71, 283, 69, 282, 201, 244, 202, 236, 202, 381, 203, 342, 203, 341, 67, 588, 68, 587, 201, 387, 203, 380, 266, 524, 345, 114, 348, 244, 266, 109, 348, 53, 347, 53, 267, 236, 266, 56, 202, 345, 412, 280, 412 ) +polygons = [ PoolIntArray( 0, 1, 2, 3 ), PoolIntArray( 4, 5, 6, 7 ), PoolIntArray( 8, 4, 7, 9, 10 ), PoolIntArray( 11, 12, 13, 14, 15, 16, 17 ), PoolIntArray( 18, 19, 20, 21, 22 ), PoolIntArray( 23, 24, 25, 26, 27 ), PoolIntArray( 23, 27, 28 ), PoolIntArray( 23, 28, 0, 29 ), PoolIntArray( 3, 4, 8, 30 ), PoolIntArray( 0, 3, 30 ), PoolIntArray( 0, 30, 31, 29 ), PoolIntArray( 32, 29, 31 ), PoolIntArray( 31, 33, 34, 35, 36, 32 ), PoolIntArray( 22, 37, 18 ), PoolIntArray( 22, 21, 32, 36 ), PoolIntArray( 10, 38, 8 ), PoolIntArray( 11, 10, 9, 12 ), PoolIntArray( 33, 31, 17, 16 ), PoolIntArray( 11, 17, 39 ) ] +outlines = [ PoolVector2Array( 55, 71, 56, 202, 236, 202, 236, 266, 53, 267, 53, 347, 109, 348, 108, 412, 53, 411, 53, 589, 284, 588, 284, 520, 343, 520, 342, 588, 587, 588, 588, 411, 531, 412, 531, 345, 586, 345, 587, 263, 387, 266, 387, 203, 587, 201, 588, 68, 341, 67, 342, 203, 381, 203, 380, 266, 244, 266, 244, 202, 282, 201, 283, 69 ), PoolVector2Array( 114, 348, 115, 413, 280, 412, 283, 508, 344, 508, 345, 412, 525, 412, 524, 345 ) ] + [sub_resource type="Environment" id=1] background_mode = 4 glow_enabled = true -[node name="Room" type="Node2D"] +[node name="Room" type="Navigation2D"] script = ExtResource( 3 ) +[node name="NavigationPolygonInstance" type="NavigationPolygonInstance" parent="."] +navpoly = SubResource( 2 ) + [node name="WorldEnvironment" type="WorldEnvironment" parent="."] environment = SubResource( 1 ) diff --git a/DungeonShooting_Godot/src/game/role/PathSign.cs b/DungeonShooting_Godot/src/game/role/PathSign.cs index 078c3e4..a3487ed 100644 --- a/DungeonShooting_Godot/src/game/role/PathSign.cs +++ b/DungeonShooting_Godot/src/game/role/PathSign.cs @@ -177,19 +177,6 @@ RayCast.ForceRaycastUpdate(); var flag = RayCast.IsColliding(); - // if (flag) - // { - // var collider = RayCast.GetCollider(); - // if (collider is TileMap tileMap) - // { - // var tilesIds = tileMap.TileSet.GetTilesIds(); - // var collPos = RayCast.GetCollisionPoint(); - // // tileMap.TileSet.TileGetShape() - // // var colliderShape = RayCast.GetColliderShape(); - // //GD.Print(fullName + ", " + colliderShape); - // } - // - // } RayCast.Enabled = false; return flag; } diff --git a/DungeonShooting_Godot/src/game/role/enemy/Enemy.cs b/DungeonShooting_Godot/src/game/role/enemy/Enemy.cs index 5ed7de4..1da5264 100644 --- a/DungeonShooting_Godot/src/game/role/enemy/Enemy.cs +++ b/DungeonShooting_Godot/src/game/role/enemy/Enemy.cs @@ -54,6 +54,7 @@ //------------------------------------------------------- private Area2D _marginArea; + private NavigationAgent2D _navigationAgent2D; public Enemy() : base(ResourcePath.prefab_role_Enemy_tscn) { @@ -68,6 +69,8 @@ //视野射线 ViewRay = GetNode("ViewRay"); _marginArea = GetNode("MarginArea"); + + _navigationAgent2D = GetNode("NavigationAgent2D"); PathSign = new PathSign(this, PathSignLength, GameApplication.Instance.Room.Player); @@ -84,6 +87,7 @@ StateController.ChangeState(AIStateEnum.AINormal); _marginArea.Connect("body_shape_entered", this, nameof(OnObjectEnter)); + _navigationAgent2D.SetTargetLocation(GameApplication.Instance.Room.Player.GlobalPosition); } public void OnObjectEnter(RID id, Node node, int shapeIndex, int localShapeIndex) @@ -110,44 +114,18 @@ public override void _PhysicsProcess(float delta) { base._PhysicsProcess(delta); + + if (_navigationAgent2D.IsNavigationFinished()) + { + return; + } - // var player = GameApplication.Instance.Room.Player; - // //玩家中心点坐标 - // var playerPos = player.MountPoint.GlobalPosition; - // - // //玩家是否在前方 - // var isForward = IsPositionInForward(playerPos); - // - // if (isForward) //脸朝向玩家 - // { - // // if (GlobalPosition.DistanceSquaredTo(playerPos) <= ViewRange * ViewRange) //没有超出视野半径 - // // { - // // //射线检测墙体 - // // ViewRay.Enabled = true; - // // var localPos = ViewRay.ToLocal(playerPos); - // // ViewRay.CastTo = localPos; - // // ViewRay.ForceRaycastUpdate(); - // // - // // if (ViewRay.IsColliding()) //在视野范围内, 但是碰到墙壁 - // // { - // // LookTarget = null; - // // StateController.ChangeState(StateEnum.Idle); - // // } - // // else //视野无阻 - // // { - // // LookTarget = player; - // // StateController.ChangeState(StateEnum.Run); - // // } - // // - // // ViewRay.Enabled = false; - // // } - // // else //超出视野半径 - // // { - // // LookTarget = null; - // // StateController.ChangeState(StateEnum.Idle); - // // } - // // //_prevPlayerPos = playerPos; - // } + _navigationAgent2D.SetTargetLocation(GameApplication.Instance.Room.Player.GlobalPosition); + var nextPos = _navigationAgent2D.GetNextLocation(); + LookTargetPosition(nextPos); + AnimatedSprite.Animation = AnimatorNames.Run; + Velocity = (nextPos - GlobalPosition).Normalized() * MoveSpeed; + CalcMove(delta); } public override void _Draw() diff --git a/DungeonShooting_Godot/src/game/role/enemy/state/AITailAfterState.cs b/DungeonShooting_Godot/src/game/role/enemy/state/AITailAfterState.cs index 170cb24..1e16c6a 100644 --- a/DungeonShooting_Godot/src/game/role/enemy/state/AITailAfterState.cs +++ b/DungeonShooting_Godot/src/game/role/enemy/state/AITailAfterState.cs @@ -14,6 +14,7 @@ public void PhysicsProcess(float delta) { + return; var master = Master; var targetSign = master.PathSign; var enemyPos = master.GlobalPosition; diff --git a/DungeonShooting_Godot/src/game/room/RoomManager.cs b/DungeonShooting_Godot/src/game/room/RoomManager.cs index 35e709c..d91a9b5 100644 --- a/DungeonShooting_Godot/src/game/room/RoomManager.cs +++ b/DungeonShooting_Godot/src/game/room/RoomManager.cs @@ -3,13 +3,12 @@ /// /// 房间管理器 /// -public class RoomManager : Node2D +public class RoomManager : Navigation2D { public Role Player { get; private set; } private Node2D ObjectRoot; private YSort SortRoot; - private Enemy _enemy; public override void _EnterTree()