diff --git a/DungeonShooting_Godot/src/framework/map/AffiliationArea.cs b/DungeonShooting_Godot/src/framework/map/AffiliationArea.cs index ddcc845..fe9547c 100644 --- a/DungeonShooting_Godot/src/framework/map/AffiliationArea.cs +++ b/DungeonShooting_Godot/src/framework/map/AffiliationArea.cs @@ -19,6 +19,8 @@ private readonly HashSet _includeItems = new HashSet(); private bool _init = false; + //玩家是否是第一次进入 + private bool _isFirstEnterFlag = true; /// /// 根据矩形区域初始化归属区域 @@ -42,23 +44,6 @@ _Init(); } - // /// - // /// 更具多边形初始化归属区域 - // /// - // public void Init(Vector2[] polygon) - // { - // if (_init) - // { - // return; - // } - // - // _init = true; - // var collisionPolygon = new CollisionPolygon2D(); - // collisionPolygon.Polygon = polygon; - // AddChild(collisionPolygon); - // _Init(); - // } - private void _Init() { Monitoring = true; @@ -85,6 +70,12 @@ } activityObject.Affiliation = this; _includeItems.Add(activityObject); + + //如果是玩家 + if (activityObject == Player.Current) + { + OnPlayerEnterRoom(); + } } /// @@ -128,4 +119,15 @@ InsertItem(activityObject); } } + + //玩家进入房间 + private void OnPlayerEnterRoom() + { + if (_isFirstEnterFlag) + { + EventManager.EmitEvent(EventEnum.OnPlayerFirstEnterRoom, RoomInfo); + _isFirstEnterFlag = false; + } + EventManager.EmitEvent(EventEnum.OnPlayerEnterRoom, RoomInfo); + } } \ No newline at end of file diff --git a/DungeonShooting_Godot/src/game/event/EventEnum.cs b/DungeonShooting_Godot/src/game/event/EventEnum.cs index 998efc5..02591f3 100644 --- a/DungeonShooting_Godot/src/game/event/EventEnum.cs +++ b/DungeonShooting_Godot/src/game/event/EventEnum.cs @@ -8,4 +8,12 @@ /// 敌人死亡, 参数为死亡的敌人的实例 /// OnEnemyDie, + /// + /// 玩家进入一个房间,参数为房间对象 + /// + OnPlayerEnterRoom, + /// + /// 玩家第一次进入某个房间,参数为房间对象 + /// + OnPlayerFirstEnterRoom, } diff --git a/DungeonShooting_Godot/src/game/room/RoomDoor.cs b/DungeonShooting_Godot/src/game/room/RoomDoor.cs index 597f1be..e77eede 100644 --- a/DungeonShooting_Godot/src/game/room/RoomDoor.cs +++ b/DungeonShooting_Godot/src/game/room/RoomDoor.cs @@ -7,7 +7,6 @@ public void Init(RoomDoorInfo doorInfo) { _door = doorInfo; - CloseDoor(); } public void OpenDoor() diff --git a/DungeonShooting_Godot/src/game/room/RoomManager.cs b/DungeonShooting_Godot/src/game/room/RoomManager.cs index 2f288af..bed68f7 100644 --- a/DungeonShooting_Godot/src/game/room/RoomManager.cs +++ b/DungeonShooting_Godot/src/game/room/RoomManager.cs @@ -41,10 +41,14 @@ public override void _Ready() { TileRoot.YSortEnabled = false; - //FloorTileMap.NavigationVisibilityMode = TileMap.VisibilityMode.ForceShow; _font = ResourceManager.Load(ResourcePath.resource_font_cn_font_36_tres); + //绑定事件 + EventManager.AddEventListener(EventEnum.OnEnemyDie, OnEnemyDie); + EventManager.AddEventListener(EventEnum.OnPlayerFirstEnterRoom, OnPlayerFirstEnterRoom); + EventManager.AddEventListener(EventEnum.OnPlayerEnterRoom, OnPlayerEnterRoom); + var nowTicks = DateTime.Now.Ticks; //生成地牢房间 _generateDungeon = new GenerateDungeon(); @@ -60,12 +64,9 @@ //挂载过道导航区域 _dungeonTile.MountNavigationPolygon(this); //过道导航区域数据 - var aisleData = _dungeonTile.GetPolygonData(); - _roomStaticNavigationList.AddRange(aisleData); + _roomStaticNavigationList.AddRange(_dungeonTile.GetPolygonData()); //门导航区域数据 _roomStaticNavigationList.AddRange(_dungeonTile.GetConnectDoorPolygonData()); - //创建过道的归属区域 - //CreateAisleAffiliation(aisleData); //初始化所有房间 _generateDungeon.EachRoom(InitRoom); @@ -94,23 +95,6 @@ var cursor = GameApplication.Instance.Cursor; cursor.SetGuiMode(false); cursor.SetMountRole(Player); - - EventManager.AddEventListener(EventEnum.OnEnemyDie, o => - { - var inst = (ActivityObject)o; - var count = Player.Affiliation.FindIncludeItemsCount( - activityObject => activityObject != inst && activityObject.CollisionWithLayer(PhysicsLayer.Enemy) - ); - GD.Print("有敌人死亡! 当前房间还剩: " + count + "个敌人"); - if (count == 0) - { - foreach (var doorInfo in Player.Affiliation.RoomInfo.Doors) - { - doorInfo.Door.OpenDoor(); - doorInfo.ConnectDoor.Door.OpenDoor(); - } - } - }); } /// @@ -146,9 +130,6 @@ { if (_dungeonTile != null) { - // DrawLine(new Vector2(0, -5000), new Vector2(0, 5000), Colors.Green); - // DrawLine(new Vector2(-5000, 0), new Vector2(5000, 0), Colors.Green); - //绘制ai寻路区域 Utils.DrawNavigationPolygon(this, _roomStaticNavigationList.ToArray()); } @@ -169,10 +150,10 @@ CreateRoomAisleAffiliation(roomInfo); //创建敌人 - foreach (var roomInfoActivityMark in roomInfo.ActivityMarks) - { - roomInfoActivityMark.BeReady(roomInfo); - } + // foreach (var roomInfoActivityMark in roomInfo.ActivityMarks) + // { + // roomInfoActivityMark.BeReady(roomInfo); + // } } //挂载房间导航区域 @@ -235,7 +216,6 @@ } door.Position = (doorInfo.OriginPosition + offset) * GenerateDungeon.TileCellSize; door.Init(doorInfo); - //door.OpenDoor(); door.PutDown(RoomLayerEnum.NormalLayer, false); } } @@ -250,20 +230,49 @@ roomInfo.Affiliation = affiliation; TileRoot.AddChild(affiliation); } + + /// + /// 玩家第一次进入某个房间回调 + /// + private void OnPlayerFirstEnterRoom(object o) + { + var room = (RoomInfo)o; + foreach (var mark in room.ActivityMarks) + { + mark.BeReady(room); + } + } + + /// + /// 玩家进入某个房间回调 + /// + private void OnPlayerEnterRoom(object o) + { + var room = (RoomInfo)o; + foreach (var doorInfo in room.Doors) + { + doorInfo.Door.CloseDoor(); + } + } - // //创建过道归属区域 - // private void CreateAisleAffiliation(NavigationPolygonData[] aisleData) - // { - // foreach (var aisle in aisleData) - // { - // var affiliation = new AffiliationArea(); - // affiliation.Name = "AffiliationArea" + (_affiliationIndex++); - // affiliation.Init(aisle.ConvertPointsToVector2Array()); - // - // TileRoot.AddChild(affiliation); - // } - // } - + /// + /// 敌人死亡回调 + /// + private void OnEnemyDie(object o) + { + var inst = (ActivityObject)o; + var count = Player.Affiliation.FindIncludeItemsCount( + activityObject => activityObject != inst && activityObject.CollisionWithLayer(PhysicsLayer.Enemy) + ); + if (count == 0) + { + foreach (var doorInfo in Player.Affiliation.RoomInfo.Doors) + { + doorInfo.Door.OpenDoor(); + } + } + } + //绘制房间区域, debug 用 private void DrawRoomInfo(RoomInfo room) {