Support to detect when the player has entered or exited the room.

支持检测玩家进入或退出房间了。
This commit is contained in:
Cold-Mint 2024-06-02 21:48:38 +08:00
parent db402ab7b2
commit b474ac0ed3
Signed by: Cold-Mint
GPG Key ID: C5A9BF8A98E0CE99
16 changed files with 256 additions and 17 deletions

View File

@ -19,4 +19,6 @@ room_placement_information,房间{0}已被成功放置在{1}。,Room {0} has bee
room_injection_processor_does_not_exist,找不到房间注入处理器{0}请检测是否在MapGenerator内注册。,"Room injection processor {0} not found, check to see if it is registered in MapGenerator.",部屋注入プロセッサ{0}が見つかりません、MapGenerator内に登録されているかどうか検出してください。
time_range_debug,当前时间:{0}起始时间:{1},结束时间{2},是否在范围内{3},"Current time: {0} Start time: {1}, end time {2}, whether in range {3}",現在時間:{0}開始時間:{1}終了時間{2}範囲内かどうか{3}です
player_spawn_debug,玩家{0}生成在{1}。,"Player {0} spawned at {1}.",プレイヤー{0}が{1}に生成されました。
player_packed_scene_not_exist,玩家预制场景不存在。,Player packed scene does not exist.,プレイヤーのパックされたシーンが存在しません。
player_packed_scene_not_exist,玩家预制场景不存在。,Player packed scene does not exist.,プレイヤーのパックされたシーンが存在しません。
exit_the_room_debug,节点{0}退出房间{1}。,"Node {0} exits room {1}.",ノード{0}が部屋{1}を退出します。
enter_the_room_debug,节点{0}进入房间{1}。,"Node {0} enters room {1}.",ノード{0}が部屋{1}に入ります。
1 id zh en jp
19 room_injection_processor_does_not_exist 找不到房间注入处理器{0},请检测是否在MapGenerator内注册。 Room injection processor {0} not found, check to see if it is registered in MapGenerator. 部屋注入プロセッサ{0}が見つかりません、MapGenerator内に登録されているかどうか検出してください。
20 time_range_debug 当前时间:{0}起始时间:{1},结束时间{2},是否在范围内{3} Current time: {0} Start time: {1}, end time {2}, whether in range {3} 現在時間:{0}開始時間:{1}終了時間{2}範囲内かどうか{3}です
21 player_spawn_debug 玩家{0}生成在{1}。 Player {0} spawned at {1}. プレイヤー{0}が{1}に生成されました。
22 player_packed_scene_not_exist 玩家预制场景不存在。 Player packed scene does not exist. プレイヤーのパックされたシーンが存在しません。
23 exit_the_room_debug 节点{0}退出房间{1}。 Node {0} exits room {1}. ノード{0}が部屋{1}を退出します。
24 enter_the_room_debug 节点{0}进入房间{1}。 Node {0} enters room {1}. ノード{0}が部屋{1}に入ります。

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -3,7 +3,7 @@
[ext_resource type="TileSet" uid="uid://c4wpp12rr44hi" path="res://tileSets/dungeon.tres" id="1_a15hy"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_kiih8"]
size = Vector2(508.75, 194)
size = Vector2(443, 118)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_o85u0"]
size = Vector2(20, 48)
@ -31,7 +31,7 @@ layer_2/tile_data = PackedInt32Array(0, 1, 3, 65536, 131073, 1, 131072, 131073,
collision_mask = 0
[node name="CollisionShape2D" type="CollisionShape2D" parent="RoomArea"]
position = Vector2(256.625, 97)
position = Vector2(256.5, 95)
shape = SubResource("RectangleShape2D_kiih8")
[node name="RoomSlotList" type="Node2D" parent="."]

View File

@ -3,7 +3,7 @@
[ext_resource type="TileSet" uid="uid://c4wpp12rr44hi" path="res://tileSets/dungeon.tres" id="1_rn2om"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_kiih8"]
size = Vector2(508.75, 191)
size = Vector2(441, 122)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_o85u0"]
size = Vector2(20, 48)
@ -31,7 +31,7 @@ layer_2/tile_data = PackedInt32Array(0, 1, 3, 65536, 131073, 1, 131072, 131073,
collision_mask = 0
[node name="CollisionShape2D" type="CollisionShape2D" parent="RoomArea"]
position = Vector2(256.625, 98.5)
position = Vector2(255.5, 97)
shape = SubResource("RectangleShape2D_kiih8")
[node name="RoomSlotList" type="Node2D" parent="."]

View File

@ -4,7 +4,7 @@
[ext_resource type="Script" path="res://scripts/map/PlayerSpawn.cs" id="2_6p8mv"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_kiih8"]
size = Vector2(508.75, 254)
size = Vector2(450, 191)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_jxmys"]
size = Vector2(18, 57.75)
@ -25,7 +25,7 @@ layer_2/tile_data = PackedInt32Array(0, 1, 3, 65536, 131073, 1, 131072, 131073,
[node name="RoomArea" type="Area2D" parent="."]
[node name="CollisionShape2D" type="CollisionShape2D" parent="RoomArea"]
position = Vector2(255.625, 128)
position = Vector2(253, 130.5)
shape = SubResource("RectangleShape2D_kiih8")
[node name="RoomSlotList" type="Node2D" parent="."]

View File

@ -3,7 +3,7 @@
[ext_resource type="TileSet" uid="uid://c4wpp12rr44hi" path="res://tileSets/dungeon.tres" id="1_rn2om"]
[sub_resource type="RectangleShape2D" id="RectangleShape2D_kiih8"]
size = Vector2(681, 453)
size = Vector2(669, 442)
[sub_resource type="RectangleShape2D" id="RectangleShape2D_o85u0"]
size = Vector2(20, 48)
@ -31,7 +31,7 @@ layer_2/tile_data = PackedInt32Array(0, 1, 3, 65536, 131073, 1, 131072, 131073,
collision_mask = 0
[node name="CollisionShape2D" type="CollisionShape2D" parent="RoomArea"]
position = Vector2(371.5, 254.5)
position = Vector2(367.5, 256)
shape = SubResource("RectangleShape2D_kiih8")
[node name="RoomSlotList" type="Node2D" parent="."]

View File

@ -358,6 +358,22 @@ public static class Config
public const int Mob = 7;
}
/// <summary>
/// <para>The event of entering the room</para>
/// <para>进入房间的事件</para>
/// </summary>
public class EnterRoomEventId
{
}
/// <summary>
/// <para>Exit the room event</para>
/// <para>退出房间的事件</para>
/// </summary>
public class ExitRoomEventId
{
}
public class RoomDataTag
{
/// <summary>

View File

@ -18,6 +18,10 @@ public class RoomNodeData
/// </summary>
public string? RoomInjectionProcessorData { get; set; }
public string? EnterRoomEventHandlerId { get; set; }
public string? ExitRoomEventHandlerId { get; set; }
/// <summary>
/// <para>Whether a tag is held</para>
/// <para>是否持有某个标签</para>

View File

@ -0,0 +1,25 @@
using Godot;
namespace ColdMint.scripts.map.room;
/// <summary>
/// <para>Enter the event handler in the room</para>
/// <para>进入房间的事件处理器</para>
/// </summary>
public interface IEnterRoomEventHandler
{
/// <summary>
/// <para>Get ID</para>
/// <para>获取ID</para>
/// </summary>
/// <returns></returns>
string GetId();
/// <summary>
/// <para>When entering the room</para>
/// <para>当进入房间时</para>
/// </summary>
/// <param name="node"></param>
/// <param name="room"></param>
void OnEnterRoom(Node node,Room room);
}

View File

@ -0,0 +1,25 @@
using Godot;
namespace ColdMint.scripts.map.room;
/// <summary>
/// <para>Exit the room's processor</para>
/// <para>退出房间的处理器</para>
/// </summary>
public interface IExitRoomEventHandler
{
/// <summary>
/// <para>Get ID</para>
/// <para>获取ID</para>
/// </summary>
/// <returns></returns>
string GetId();
/// <summary>
/// <para>When exiting the room</para>
/// <para>当退出房间时</para>
/// </summary>
/// <param name="node"></param>
/// <param name="room"></param>
void OnExitRoom(Node node,Room room);
}

View File

@ -23,6 +23,52 @@ public class Room
private Area2D? _area2D;
private CollisionShape2D? _collisionShape2D;
public string? EnterRoomEventHandlerId { get; set; }
public string? ExitRoomEventHandlerId { get; set; }
/// <summary>
/// <para>When a node enters the room</para>
/// <para>当有节点进入房间时</para>
/// </summary>
/// <param name="node"></param>
private void OnEnterRoom(Node node)
{
if (_rootNode != null)
{
LogCat.LogWithFormat("enter_the_room_debug", node.Name, _rootNode.Name);
}
if (string.IsNullOrEmpty(EnterRoomEventHandlerId))
{
return;
}
var enterRoomEventHandler = RoomEventManager.GetEnterRoomEventHandler(EnterRoomEventHandlerId);
enterRoomEventHandler?.OnEnterRoom(node, this);
}
/// <summary>
/// <para>When a node exits the room</para>
/// <para>当有节点退出房间时</para>
/// </summary>
/// <param name="node"></param>
private void OnExitRoom(Node node)
{
if (_rootNode != null)
{
LogCat.LogWithFormat("exit_the_room_debug", node.Name, _rootNode.Name);
}
if (string.IsNullOrEmpty(ExitRoomEventHandlerId))
{
return;
}
var exitRoomEventHandler = RoomEventManager.GetExitRoomEventHandler(ExitRoomEventHandlerId);
exitRoomEventHandler?.OnExitRoom(node, this);
}
/// <summary>
/// <para>The collision shape of the room</para>
/// <para>房间的碰撞形状</para>
@ -32,6 +78,7 @@ public class Room
get => _collisionShape2D;
set => _collisionShape2D = value;
}
public Area2D? Area2D
{
get => _area2D;
@ -68,7 +115,13 @@ public class Room
_rootNode = node2D;
_tileMap = node2D.GetNode<TileMap>("TileMap");
_area2D = node2D.GetNode<Area2D>("RoomArea");
_area2D.Monitorable = true;
_area2D.Monitoring = true;
_area2D.SetCollisionLayerValue(Config.LayerNumber.RoomArea, true);
//Sets the collision layer that can be detected in the current room area.
//设置当前房间区域可检测到的碰撞层。
_area2D.SetCollisionMaskValue(Config.LayerNumber.Player, true);
_area2D.BodyExited += OnExitRoom;
_area2D.BodyEntered += OnEnterRoom;
_collisionShape2D = _area2D.GetChild<CollisionShape2D>(0);
_roomSlots = GetRoomSlots(_tileMap, _area2D,
node2D.GetNode<Node2D>("RoomSlotList"));
@ -113,7 +166,7 @@ public class Room
//Prevent other areas from detecting the room slot
//禁止其他区域检测到房间槽
area2D.Monitorable = false;
var collisionShape2D = area2D.GetChild<CollisionShape2D>(0);
var rect2 = collisionShape2D.Shape.GetRect();
//Round the size of the impactor to the tile size For example, the impactor size 44 is converted to the tile size 44/32=1.375 rounded to 1

View File

@ -0,0 +1,98 @@
using System.Collections.Generic;
namespace ColdMint.scripts.map.room;
/// <summary>
/// <para>Room event Manager</para>
/// <para>房间事件管理器</para>
/// </summary>
public static class RoomEventManager
{
/// <summary>
/// <para>The event of entering the room</para>
/// <para>进入房间的事件</para>
/// </summary>
private static Dictionary<string, IEnterRoomEventHandler>? _enterRoomEventHandlers;
/// <summary>
/// <para>Exit the room event</para>
/// <para>退出房间的事件</para>
/// </summary>
private static Dictionary<string, IExitRoomEventHandler>? _exitRoomEventHandlers;
/// <summary>
/// <para>Register the event handler that enters the room</para>
/// <para>注册进入房间的事件处理器</para>
/// </summary>
/// <param name="enterRoomEventHandler"></param>
/// <returns></returns>
public static bool RegisterEnterRoomEventHandler(IEnterRoomEventHandler enterRoomEventHandler)
{
var id = enterRoomEventHandler.GetId();
if (_enterRoomEventHandlers != null) return _enterRoomEventHandlers.TryAdd(id, enterRoomEventHandler);
_enterRoomEventHandlers = new Dictionary<string, IEnterRoomEventHandler> { { id, enterRoomEventHandler } };
return true;
}
/// <summary>
/// <para>Gets the event that entered the room</para>
/// <para>获取进入房间的事件</para>
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static IEnterRoomEventHandler? GetEnterRoomEventHandler(string id)
{
return _enterRoomEventHandlers != null && _enterRoomEventHandlers.TryGetValue(id, out var enterRoomEventHandler)
? enterRoomEventHandler
: null;
}
/// <summary>
/// <para>Unregister the room entry event</para>
/// <para>取消注册进入房间事件</para>
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static bool UnRegisterEnterRoomEventHandler(string id)
{
return _enterRoomEventHandlers != null && _enterRoomEventHandlers.Remove(id);
}
/// <summary>
/// <para>Unregister for exit room events</para>
/// <para>取消注册退出房间事件</para>
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static bool UnRegisterExitRoomEventHandler(string id)
{
return _exitRoomEventHandlers != null && _exitRoomEventHandlers.Remove(id);
}
/// <summary>
/// <para>Gets the event to exit the room</para>
/// <para>获取退出房间的事件</para>
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static IExitRoomEventHandler? GetExitRoomEventHandler(string id)
{
return _exitRoomEventHandlers != null && _exitRoomEventHandlers.TryGetValue(id, out var exitRoomEventHandler)
? exitRoomEventHandler
: null;
}
/// <summary>
/// <para>Sign up to exit the room's event handler</para>
/// <para>注册退出房间的事件处理器</para>
/// </summary>
/// <param name="exitRoomEventHandler"></param>
/// <returns></returns>
public static bool RegisterExitRoomEventHandler(IExitRoomEventHandler exitRoomEventHandler)
{
var id = exitRoomEventHandler.GetId();
if (_exitRoomEventHandlers != null) return _exitRoomEventHandlers.TryAdd(id, exitRoomEventHandler);
_exitRoomEventHandlers = new Dictionary<string, IExitRoomEventHandler> { { id, exitRoomEventHandler } };
return true;
}
}

View File

@ -26,7 +26,7 @@ public static class RoomFactory
var resList = new List<string>();
foreach (var roomTemplate in roomTemplateSet)
{
var roomTemplatePath = ResUtils.GetRunTimeResPath(roomTemplate);
var roomTemplatePath = ResUtils.GetRunTimeResPath(roomTemplate);
//Detects whether it is a folder
//检测是否为文件夹
if (DirAccess.DirExistsAbsolute(roomTemplatePath))
@ -62,9 +62,21 @@ public static class RoomFactory
/// <para>CreateRoom</para>
/// <para>创建房间模板</para>
/// </summary>
/// <param name="resPath"></param>
/// <param name="resPath">
///<para>resources path</para>
///<para>资源路径</para>
/// </param>
/// <param name="enterRoomEventHandlerId">
///<para>The ID of the event handler when entering the room</para>
///<para>进入房间时的事件处理器ID</para>
/// </param>
/// <param name="exitRoomEventHandlerId">
///<para>Event handler ID when exiting the room</para>
///<para>退出房间时的事件处理器ID</para>
/// </param>
/// <returns></returns>
public static Room? CreateRoom(string resPath)
public static Room? CreateRoom(string resPath, string? enterRoomEventHandlerId = null,
string? exitRoomEventHandlerId = null)
{
//If the file does not exist, null is returned
//如果文件不存在则返回null
@ -76,7 +88,9 @@ public static class RoomFactory
var room = new Room
{
RoomScene = GD.Load<PackedScene>(ResUtils.GetEditorResPath(resPath))
RoomScene = GD.Load<PackedScene>(ResUtils.GetEditorResPath(resPath)),
EnterRoomEventHandlerId = enterRoomEventHandlerId,
ExitRoomEventHandlerId = exitRoomEventHandlerId
};
return room;
}

View File

@ -145,7 +145,8 @@ public class PatchworkRoomPlacementStrategy : IRoomPlacementStrategy
var useableRoomPlacementData = new List<RoomPlacementData>();
foreach (var roomRes in roomResArray)
{
var newRoom = RoomFactory.CreateRoom(roomRes);
var newRoom = RoomFactory.CreateRoom(roomRes, newRoomNodeData.EnterRoomEventHandlerId,
newRoomNodeData.ExitRoomEventHandlerId);
if (newRoom == null)
{
continue;
@ -216,7 +217,8 @@ public class PatchworkRoomPlacementStrategy : IRoomPlacementStrategy
var index = randomNumberGenerator.Randi() % roomResArray.Length;
var roomPlacementData = new RoomPlacementData
{
Room = RoomFactory.CreateRoom(roomResArray[index]),
Room = RoomFactory.CreateRoom(roomResArray[index], startRoomNodeData.EnterRoomEventHandlerId,
startRoomNodeData.ExitRoomEventHandlerId),
Position = Vector2.Zero
};
return Task.FromResult<RoomPlacementData?>(roomPlacementData);