diff --git a/DungeonShooting_Godot/prefab/map/RoomDoor.tscn b/DungeonShooting_Godot/prefab/map/RoomDoor.tscn index cbe5556..787a669 100644 --- a/DungeonShooting_Godot/prefab/map/RoomDoor.tscn +++ b/DungeonShooting_Godot/prefab/map/RoomDoor.tscn @@ -28,7 +28,7 @@ }] [sub_resource type="RectangleShape2D" id="RectangleShape2D_xgcls"] -size = Vector2(26, 26) +size = Vector2(32, 32) [node name="RoomDoor" type="Node"] script = ExtResource("1_8es7a") @@ -41,7 +41,7 @@ [node name="AnimatedSprite" type="AnimatedSprite2D" parent="."] material = SubResource("ShaderMaterial_f8vun") -scale = Vector2(0.40625, 0.40625) +scale = Vector2(0.5, 0.5) sprite_frames = SubResource("SpriteFrames_ugstb") [node name="Collision" type="CollisionShape2D" parent="."] diff --git a/DungeonShooting_Godot/src/framework/activity/ActivityObject.cs b/DungeonShooting_Godot/src/framework/activity/ActivityObject.cs index 1e123f2..dbbdacb 100644 --- a/DungeonShooting_Godot/src/framework/activity/ActivityObject.cs +++ b/DungeonShooting_Godot/src/framework/activity/ActivityObject.cs @@ -147,7 +147,9 @@ MotionMode = MotionModeEnum.Floating; MoveController = AddComponent(); - //tempNode.CallDeferred(Node.MethodName.QueueFree); + + //临时处理, 4.0 有bug, 不能销毁模板实例, 不然关闭游戏会报错!!! + //_templateInstance.CallDeferred(Node.MethodName.QueueFree); } /// @@ -819,16 +821,22 @@ } IsDestroyed = true; - - OnDestroy(); QueueFree(); + OnDestroy(); + var arr = _components.ToArray(); for (int i = 0; i < arr.Length; i++) { arr[i].Value?.Destroy(); } + + if (Affiliation != null) + { + Affiliation.RemoveItem(this); + } + //临时处理, 4.0 有bug, 不能销毁模板实例, 不然关闭游戏会报错!!! - _templateInstance.QueueFree(); + //_templateInstance.QueueFree(); } /// diff --git a/DungeonShooting_Godot/src/framework/map/AffiliationArea.cs b/DungeonShooting_Godot/src/framework/map/AffiliationArea.cs index cd0e1d7..ddcc845 100644 --- a/DungeonShooting_Godot/src/framework/map/AffiliationArea.cs +++ b/DungeonShooting_Godot/src/framework/map/AffiliationArea.cs @@ -1,4 +1,6 @@  +using System; +using System.Collections.Generic; using Godot; /// @@ -6,12 +8,22 @@ /// public partial class AffiliationArea : Area2D { + /// + /// 当前实例所属房间 + /// + public RoomInfo RoomInfo; + + /// + /// 当前归属区域包含的所有物体对象 + /// + private readonly HashSet _includeItems = new HashSet(); + private bool _init = false; /// /// 根据矩形区域初始化归属区域 /// - public void Init(Rect2 rect2) + public void Init(RoomInfo roomInfo, Rect2 rect2) { if (_init) { @@ -19,6 +31,8 @@ } _init = true; + + RoomInfo = roomInfo; var collisionShape = new CollisionShape2D(); collisionShape.GlobalPosition = rect2.Position + rect2.Size / 2; var shape = new RectangleShape2D(); @@ -28,22 +42,22 @@ _Init(); } - /// - /// 更具多边形初始化归属区域 - /// - public void Init(Vector2[] polygon) - { - if (_init) - { - return; - } - - _init = true; - var collisionPolygon = new CollisionPolygon2D(); - collisionPolygon.Polygon = polygon; - AddChild(collisionPolygon); - _Init(); - } + // /// + // /// 更具多边形初始化归属区域 + // /// + // public void Init(Vector2[] polygon) + // { + // if (_init) + // { + // return; + // } + // + // _init = true; + // var collisionPolygon = new CollisionPolygon2D(); + // collisionPolygon.Polygon = polygon; + // AddChild(collisionPolygon); + // _Init(); + // } private void _Init() { @@ -55,12 +69,63 @@ BodyEntered += OnBodyEntered; } + /// + /// 将物体添加到当前所属区域中 + /// + public void InsertItem(ActivityObject activityObject) + { + if (activityObject.Affiliation == this) + { + return; + } + + if (activityObject.Affiliation != null) + { + activityObject.Affiliation.RemoveItem(activityObject); + } + activityObject.Affiliation = this; + _includeItems.Add(activityObject); + } + + /// + /// 将物体从当前所属区域移除 + /// + public void RemoveItem(ActivityObject activityObject) + { + activityObject.Affiliation = null; + _includeItems.Remove(activityObject); + } + + /// + /// 获取该区域中物体的总数 + /// + public int GetIncludeItemsCount() + { + return _includeItems.Count; + } + + /// + /// 统计符合条件的数量 + /// + /// 操作函数, 返回是否满足要求 + public int FindIncludeItemsCount(Func handler) + { + var count = 0; + foreach (var activityObject in _includeItems) + { + if (handler(activityObject)) + { + count++; + } + } + return count; + } + private void OnBodyEntered(Node2D body) { if (body is ActivityObject activityObject) { - activityObject.Affiliation = this; - GD.Print("有物体进入: " + body.Name + ", " + Name); + InsertItem(activityObject); } } } \ 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 45d1986..998efc5 100644 --- a/DungeonShooting_Godot/src/game/event/EventEnum.cs +++ b/DungeonShooting_Godot/src/game/event/EventEnum.cs @@ -4,5 +4,8 @@ /// public enum EventEnum { - Test, + /// + /// 敌人死亡, 参数为死亡的敌人的实例 + /// + OnEnemyDie, } diff --git a/DungeonShooting_Godot/src/game/role/enemy/Enemy.cs b/DungeonShooting_Godot/src/game/role/enemy/Enemy.cs index 1389f75..3c66f88 100644 --- a/DungeonShooting_Godot/src/game/role/enemy/Enemy.cs +++ b/DungeonShooting_Godot/src/game/role/enemy/Enemy.cs @@ -139,6 +139,8 @@ { weapons[i].ThrowWeapon(this); } + //派发敌人死亡信号 + EventManager.EmitEvent(EventEnum.OnEnemyDie, this); Destroy(); } diff --git a/DungeonShooting_Godot/src/game/room/RoomManager.cs b/DungeonShooting_Godot/src/game/room/RoomManager.cs index 0d068e7..31a93fc 100644 --- a/DungeonShooting_Godot/src/game/room/RoomManager.cs +++ b/DungeonShooting_Godot/src/game/room/RoomManager.cs @@ -94,6 +94,22 @@ 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.CollisionLayer == PhysicsLayer.Enemy && activityObject != inst + ); + GD.Print("有敌人死亡! 当前房间还剩: " + count + "个敌人"); + if (count == 0) + { + foreach (var doorInfo in Player.Affiliation.RoomInfo.Doors) + { + doorInfo.Door.OpenDoor(); + } + } + }); } /// @@ -218,7 +234,7 @@ } door.Position = (doorInfo.OriginPosition + offset) * GenerateDungeon.TileCellSize; door.Init(doorInfo); - door.OpenDoor(); + //door.OpenDoor(); door.PutDown(RoomLayerEnum.NormalLayer, false); } } @@ -228,24 +244,24 @@ { var affiliation = new AffiliationArea(); affiliation.Name = "AffiliationArea" + (_affiliationIndex++); - affiliation.Init(new Rect2(roomInfo.GetWorldPosition(), roomInfo.Size * GenerateDungeon.TileCellSize)); + affiliation.Init(roomInfo, new Rect2(roomInfo.GetWorldPosition(), roomInfo.Size * GenerateDungeon.TileCellSize)); roomInfo.Affiliation = affiliation; TileRoot.AddChild(affiliation); } - //创建过道归属区域 - 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 CreateAisleAffiliation(NavigationPolygonData[] aisleData) + // { + // foreach (var aisle in aisleData) + // { + // var affiliation = new AffiliationArea(); + // affiliation.Name = "AffiliationArea" + (_affiliationIndex++); + // affiliation.Init(aisle.ConvertPointsToVector2Array()); + // + // TileRoot.AddChild(affiliation); + // } + // } //绘制房间区域, debug 用 private void DrawRoomInfo(RoomInfo room)