From 839139619126a08c216641d4218ef1d3a9a79eb4 Mon Sep 17 00:00:00 2001 From: Cold-Mint Date: Sun, 16 Jun 2024 22:44:50 +0800 Subject: [PATCH] =?UTF-8?q?Level=20graphs=20migrated=20to=20yaml.=20?= =?UTF-8?q?=E5=85=B3=E5=8D=A1=E5=9B=BE=E8=BF=81=E7=A7=BB=E5=88=B0yaml?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data/itemRegs/weapons.yaml | 2 +- data/levelGraphs/test.json | 88 ------------------- data/levelGraphs/test.yaml | 57 ++++++++++++ project.godot | 1 + scenes/LevelGraphEditor.tscn | 8 +- scenes/game.tscn | 2 +- scenes/mainMenu.tscn | 1 + scripts/character/CharacterTemplate.cs | 2 +- scripts/character/Player.cs | 1 - scripts/item/ItemTypeRegister.cs | 71 ++++++++------- scripts/item/weapon/WeaponTemplate.cs | 2 - .../loader/uiLoader/LevelGraphEditorLoader.cs | 8 +- scripts/loader/uiLoader/MainMenuLoader.cs | 2 - scripts/loader/uiLoader/PacksackUi.cs | 3 +- scripts/loot/LootDatum.cs | 4 +- scripts/loot/LootEntry.cs | 3 +- scripts/loot/LootList.cs | 2 - scripts/loot/LootListManager.cs | 4 - scripts/map/MapGenerator.cs | 5 +- .../map/interfaces/IRoomInjectionProcessor.cs | 4 +- .../map/layoutStrategy/TestLayoutStrategy.cs | 4 +- .../RoomInjectionProcessorTemplate.cs | 13 +-- scripts/projectile/ProjectileTemplate.cs | 6 +- scripts/serialization/JsonSerialization.cs | 11 ++- scripts/serialization/YamlSerialization.cs | 58 ++++++++++++ scripts/utils/NodeUtils.cs | 4 +- 26 files changed, 195 insertions(+), 171 deletions(-) delete mode 100644 data/levelGraphs/test.json create mode 100644 data/levelGraphs/test.yaml create mode 100644 scripts/serialization/YamlSerialization.cs diff --git a/data/itemRegs/weapons.yaml b/data/itemRegs/weapons.yaml index 3700f97..a72d0f1 100644 --- a/data/itemRegs/weapons.yaml +++ b/data/itemRegs/weapons.yaml @@ -13,4 +13,4 @@ value: 1000 - name: UniqueName type: string - value: 劣化的死灵法杖 + value: item_staff_of_the_undead_desc diff --git a/data/levelGraphs/test.json b/data/levelGraphs/test.json deleted file mode 100644 index 0b7ee14..0000000 --- a/data/levelGraphs/test.json +++ /dev/null @@ -1,88 +0,0 @@ -{ - "ConnectionDataList": [ - { - "FromId": "c0255eb6-2c75-44f7-9058-0921fe8fb0d8", - "ToId": "4ae948ea-82b7-4b2d-bec2-19ed8a9d4c03", - "FromPort": 0, - "ToPort": 0 - }, - { - "FromId": "4ae948ea-82b7-4b2d-bec2-19ed8a9d4c03", - "ToId": "ba32e05c-0c80-4a79-b5ce-5b8150400e05", - "FromPort": 0, - "ToPort": 0 - }, - { - "FromId": "4ae948ea-82b7-4b2d-bec2-19ed8a9d4c03", - "ToId": "8b3d645a-96c0-407e-871d-6f4a0b69557b", - "FromPort": 0, - "ToPort": 0 - }, - { - "FromId": "8b3d645a-96c0-407e-871d-6f4a0b69557b", - "ToId": "7e3ae680-3d7e-4ae9-b82f-5bfbe1c2f613", - "FromPort": 0, - "ToPort": 0 - }, - { - "FromId": "ba32e05c-0c80-4a79-b5ce-5b8150400e05", - "ToId": "7e3ae680-3d7e-4ae9-b82f-5bfbe1c2f613", - "FromPort": 0, - "ToPort": 0 - } - ], - "RoomNodeDataList": [ - { - "Id": "c0255eb6-2c75-44f7-9058-0921fe8fb0d8", - "Title": "起点房间", - "Description": "测试的起点房间。", - "RoomTemplateSet": [ - "res://prefab/roomTemplates/dungeon/initialRoom.tscn" - ], - "Tags": [ - "StartingRoom" - ], - "RoomInjectionProcessorData": null - }, - { - "Id": "ba32e05c-0c80-4a79-b5ce-5b8150400e05", - "Title": "房间3", - "Description": "", - "RoomTemplateSet": [ - "res://prefab/roomTemplates/dungeon/" - ], - "Tags": null, - "RoomInjectionProcessorData": "[\n {\n \"Id\": \"TimeInterval\",\n \"Config\": \"{\\\"DateSpecifiesLevel\\\":0,\\\"StartTime\\\":\\\"2024/1/1 20:34:23\\\",\\\"EndTime\\\":\\\"2025/1/1 20:34:23\\\"}\"\n }\n]" - }, - { - "Id": "4ae948ea-82b7-4b2d-bec2-19ed8a9d4c03", - "Title": "大厅", - "Description": "比普通房间要大一些的房间,有多个门,用于连接到其他房间。", - "RoomTemplateSet": [ - "res://prefab/roomTemplates/dungeon/utilityRoom.tscn" - ], - "Tags": null, - "RoomInjectionProcessorData": null - }, - { - "Id": "8b3d645a-96c0-407e-871d-6f4a0b69557b", - "Title": "房间4", - "Description": "", - "RoomTemplateSet": [ - "res://prefab/roomTemplates/dungeon/" - ], - "Tags": null, - "RoomInjectionProcessorData": "" - }, - { - "Id": "7e3ae680-3d7e-4ae9-b82f-5bfbe1c2f613", - "Title": "房间5", - "Description": "", - "RoomTemplateSet": [ - "res://prefab/roomTemplates/dungeon/" - ], - "Tags": null, - "RoomInjectionProcessorData": "" - } - ] -} diff --git a/data/levelGraphs/test.yaml b/data/levelGraphs/test.yaml new file mode 100644 index 0000000..b3b1f4f --- /dev/null +++ b/data/levelGraphs/test.yaml @@ -0,0 +1,57 @@ +connection_data_list: + - from_id: c0255eb6-2c75-44f7-9058-0921fe8fb0d8 + to_id: 4ae948ea-82b7-4b2d-bec2-19ed8a9d4c03 + from_port: 0 + to_port: 0 + - from_id: 4ae948ea-82b7-4b2d-bec2-19ed8a9d4c03 + to_id: ba32e05c-0c80-4a79-b5ce-5b8150400e05 + from_port: 0 + to_port: 0 + - from_id: 4ae948ea-82b7-4b2d-bec2-19ed8a9d4c03 + to_id: 8b3d645a-96c0-407e-871d-6f4a0b69557b + from_port: 0 + to_port: 0 + - from_id: 8b3d645a-96c0-407e-871d-6f4a0b69557b + to_id: 7e3ae680-3d7e-4ae9-b82f-5bfbe1c2f613 + from_port: 0 + to_port: 0 + - from_id: ba32e05c-0c80-4a79-b5ce-5b8150400e05 + to_id: 7e3ae680-3d7e-4ae9-b82f-5bfbe1c2f613 + from_port: 0 + to_port: 0 +room_node_data_list: + - id: c0255eb6-2c75-44f7-9058-0921fe8fb0d8 + title: 起点房间 + description: 测试的起点房间。 + room_template_set: + - 'res://prefab/roomTemplates/dungeon/initialRoom.tscn' + tags: + - StartingRoom + room_injection_processor_data: null + - id: ba32e05c-0c80-4a79-b5ce-5b8150400e05 + title: 房间3 + description: '' + room_template_set: + - 'res://prefab/roomTemplates/dungeon/' + tags: null + - id: 4ae948ea-82b7-4b2d-bec2-19ed8a9d4c03 + title: 大厅 + description: 比普通房间要大一些的房间,有多个门,用于连接到其他房间。 + room_template_set: + - 'res://prefab/roomTemplates/dungeon/utilityRoom.tscn' + tags: null + room_injection_processor_data: null + - id: 8b3d645a-96c0-407e-871d-6f4a0b69557b + title: 房间4 + description: '' + room_template_set: + - 'res://prefab/roomTemplates/dungeon/' + tags: null + room_injection_processor_data: '' + - id: 7e3ae680-3d7e-4ae9-b82f-5bfbe1c2f613 + title: 房间5 + description: '' + room_template_set: + - 'res://prefab/roomTemplates/dungeon/' + tags: null + room_injection_processor_data: '' diff --git a/project.godot b/project.godot index fa06aa6..3b5cfb9 100644 --- a/project.godot +++ b/project.godot @@ -147,6 +147,7 @@ hotbar_previous={ [internationalization] locale/translations=PackedStringArray("res://locals/DeathInfo.en.translation", "res://locals/DeathInfo.ja.translation", "res://locals/DeathInfo.zh.translation", "res://locals/InputMapping.en.translation", "res://locals/InputMapping.ja.translation", "res://locals/InputMapping.zh.translation", "res://locals/Log.en.translation", "res://locals/Log.ja.translation", "res://locals/Log.zh.translation", "res://locals/Slogan.en.translation", "res://locals/Slogan.ja.translation", "res://locals/Slogan.zh.translation", "res://locals/UI.en.translation", "res://locals/UI.ja.translation", "res://locals/UI.zh.translation", "res://locals/Item.en.translation", "res://locals/Item.ja.translation", "res://locals/Item.zh.translation", "res://locals/Action.en.translation", "res://locals/Action.ja.translation", "res://locals/Action.zh.translation", "res://locals/Misc.en.translation", "res://locals/Misc.ja.translation", "res://locals/Misc.zh.translation") +locale/test="ja" [layer_names] diff --git a/scenes/LevelGraphEditor.tscn b/scenes/LevelGraphEditor.tscn index 9e9f07d..9bcbdb4 100644 --- a/scenes/LevelGraphEditor.tscn +++ b/scenes/LevelGraphEditor.tscn @@ -238,7 +238,7 @@ offset_top = 15.0 offset_right = 21.5 offset_bottom = 40.0 grow_horizontal = 2 -text = "save" +text = "ui_save" [node name="FileNameLineEdit" type="LineEdit" parent="SaveOrLoadPanel"] layout_mode = 1 @@ -263,7 +263,7 @@ offset_top = -38.0 offset_right = 80.0 offset_bottom = -13.0 grow_vertical = 2 -text = "filename" +text = "ui_filename" [node name="HBoxContainer" type="HBoxContainer" parent="SaveOrLoadPanel"] layout_mode = 1 @@ -281,8 +281,8 @@ grow_vertical = 0 [node name="CancelButton" type="Button" parent="SaveOrLoadPanel/HBoxContainer"] layout_mode = 2 -text = "cancel" +text = "ui_cancel" [node name="ActionButton" type="Button" parent="SaveOrLoadPanel/HBoxContainer"] layout_mode = 2 -text = "save" +text = "ui_save" diff --git a/scenes/game.tscn b/scenes/game.tscn index 739ca48..d68aafb 100644 --- a/scenes/game.tscn +++ b/scenes/game.tscn @@ -54,7 +54,6 @@ texture = ExtResource("2_n1yht") [node name="OperationTip" type="RichTextLabel" parent="CanvasLayer/Control/VBoxContainer"] layout_mode = 2 bbcode_enabled = true -text = "OperationTip" fit_content = true [node name="FPSLabel" type="Label" parent="CanvasLayer/Control"] @@ -85,6 +84,7 @@ offset_bottom = 25.0 grow_horizontal = 2 [node name="BackpackUIContainer" type="Control" parent="CanvasLayer"] +visible = false layout_mode = 3 anchors_preset = 15 anchor_right = 1.0 diff --git a/scenes/mainMenu.tscn b/scenes/mainMenu.tscn index 6fb0a0a..55186b5 100644 --- a/scenes/mainMenu.tscn +++ b/scenes/mainMenu.tscn @@ -127,6 +127,7 @@ underline = 1 [node name="HBoxContainer" type="HBoxContainer" parent="VBoxContainer2"] layout_mode = 2 +size_flags_horizontal = 8 [node name="GodotLabel" type="Label" parent="VBoxContainer2/HBoxContainer"] layout_mode = 2 diff --git a/scripts/character/CharacterTemplate.cs b/scripts/character/CharacterTemplate.cs index 0c7c82f..eb61728 100644 --- a/scripts/character/CharacterTemplate.cs +++ b/scripts/character/CharacterTemplate.cs @@ -205,7 +205,7 @@ public partial class CharacterTemplate : CharacterBody2D CharacterName = GetMeta("Name", Name).AsString(); CampId = GetMeta("CampId", Config.CampId.Default).AsString(); MaxHp = GetMeta("MaxHp", Config.DefaultMaxHp).AsInt32(); - var lootListId = GetMeta("LootListId", string.Empty).AsString(); + // var lootListId = GetMeta("LootListId", string.Empty).AsString(); if (MaxHp <= 0) { diff --git a/scripts/character/Player.cs b/scripts/character/Player.cs index f60298f..e5f57bb 100644 --- a/scripts/character/Player.cs +++ b/scripts/character/Player.cs @@ -4,7 +4,6 @@ using System.Threading.Tasks; using ColdMint.scripts.damage; using ColdMint.scripts.deathInfo; -using ColdMint.scripts.debug; using ColdMint.scripts.item; using ColdMint.scripts.map.events; using ColdMint.scripts.utils; diff --git a/scripts/item/ItemTypeRegister.cs b/scripts/item/ItemTypeRegister.cs index dcad2c2..85bb773 100644 --- a/scripts/item/ItemTypeRegister.cs +++ b/scripts/item/ItemTypeRegister.cs @@ -1,15 +1,11 @@ using System; using System.Collections.Generic; using System.Linq; - using ColdMint.scripts.debug; +using ColdMint.scripts.serialization; using ColdMint.scripts.utils; - using Godot; -using YamlDotNet.Serialization; -using YamlDotNet.Serialization.NamingConventions; - namespace ColdMint.scripts.item; /// @@ -21,7 +17,9 @@ public static class ItemTypeRegister /// Register items here /// 在这里注册物品 /// - public static void StaticRegister() { } + public static void StaticRegister() + { + } /// /// Register items from yaml file @@ -30,43 +28,51 @@ public static class ItemTypeRegister public static void RegisterFromFile() { LogCat.Log("start_item_register_from_file"); - - // initialize yaml deserializer - var deserializer = new DeserializerBuilder() - .WithNamingConvention(UnderscoredNamingConvention.Instance) // convent snake_case - .Build(); - - // initialize file dir - string itemRegsDirPath = "res://data/itemRegs/"; + //初始化文件目录 + //initialize file dir + var itemRegsDirPath = "res://data/itemRegs/"; var itemRegsDir = DirAccess.Open(itemRegsDirPath); if (DirAccess.GetOpenError() is not Error.Ok) { LogCat.LogError("error_when_open_item_regs_dir"); } - // find files + //找到文件 + //find files var files = itemRegsDir.GetFiles(); + if (files == null) + { + LogCat.LogWithFormat("found_files", 0); + return; + } LogCat.LogWithFormat("found_files", files.Length); - - // parse files to item type infos + //将文件解析为项目类型信息 + //parse files to item type infos IEnumerable typeInfos = - files.SelectMany(file => ParseFile(deserializer, $"{itemRegsDirPath}/{file}")).ToList(); + files.SelectMany(file => ParseFile( $"{itemRegsDirPath}/{file}")).ToList(); LogCat.LogWithFormat("found_item_types", typeInfos.Count()); - // traverse type infos and register them. + //遍历类型信息并注册它们。 + //traverse type infos and register them. foreach (var typeInfo in typeInfos) { RegisterTypeInfo(typeInfo); } } - private static IList ParseFile(IDeserializer deserializer, string filePath) + /// + /// ParseFile + /// 解析文件 + /// + /// + /// + private static IList ParseFile(string filePath) { var yamlFile = FileAccess.Open(filePath, FileAccess.ModeFlags.Read); + //阅读和反序列化 //Read & deserialize var yamlString = yamlFile.GetAsText(); - var typeInfos = deserializer.Deserialize>(yamlString); - + var typeInfos = YamlSerialization.Deserialize>(yamlString); yamlFile.Close(); return typeInfos; } @@ -102,28 +108,33 @@ public static class ItemTypeRegister //construct item type, register var itemType = new ItemType(typeInfo.Id, - newItemFunc, - icon, typeInfo.MaxStackValue); + newItemFunc, + icon, typeInfo.MaxStackValue); var succeed = ItemTypeManager.Register(itemType); LogCat.LogWithFormat("register_item", itemType.Id, succeed); } //Use for yaml deserialization private record struct ItemTypeInfo( - string Id, string ScenePath, string IconPath, int MaxStackValue, - IList? CustomArgs) { } + string Id, + string ScenePath, + string IconPath, + int MaxStackValue, + IList? CustomArgs) + { + } private readonly record struct CustomArg(string Name, CustomArgType Type, string Value) { public Variant ParseValue() => Type switch { - CustomArgType.String => Value, - CustomArgType.Int => int.Parse(Value), - CustomArgType.Float => double.Parse(Value), + CustomArgType.String => Value, + CustomArgType.Int => int.Parse(Value), + CustomArgType.Float => double.Parse(Value), CustomArgType.Vector2 => ParseVector2FromString(Value), CustomArgType.Texture => ResourceLoader.Load("res://sprites/" + Value), - _ => throw new ArgumentOutOfRangeException($"Unknown Arg Type {Type}") + _ => throw new ArgumentOutOfRangeException($"Unknown Arg Type {Type}") }; private Vector2 ParseVector2FromString(string s) diff --git a/scripts/item/weapon/WeaponTemplate.cs b/scripts/item/weapon/WeaponTemplate.cs index d323f0e..e3a01d3 100644 --- a/scripts/item/weapon/WeaponTemplate.cs +++ b/scripts/item/weapon/WeaponTemplate.cs @@ -2,8 +2,6 @@ using System; using ColdMint.scripts.character; using ColdMint.scripts.pickable; -using ColdMint.scripts.damage; - using Godot; namespace ColdMint.scripts.item.weapon; diff --git a/scripts/loader/uiLoader/LevelGraphEditorLoader.cs b/scripts/loader/uiLoader/LevelGraphEditorLoader.cs index 57de62d..e7e4c1f 100644 --- a/scripts/loader/uiLoader/LevelGraphEditorLoader.cs +++ b/scripts/loader/uiLoader/LevelGraphEditorLoader.cs @@ -583,7 +583,7 @@ public partial class LevelGraphEditorLoader : UiLoaderTemplate } var filePath = Path.Join(Config.GetLevelGraphExportDirectory(), FileNameToActualName(fileName)); - await File.WriteAllTextAsync(filePath, JsonSerialization.Serialize(levelGraphEditorSaveData)); + await File.WriteAllTextAsync(filePath, YamlSerialization.Serialize(levelGraphEditorSaveData)); } /// @@ -598,13 +598,13 @@ public partial class LevelGraphEditorLoader : UiLoaderTemplate private string FileNameToActualName(string fileName) { string actualName; - if (fileName.EndsWith(".json")) + if (fileName.EndsWith(".yaml")) { actualName = fileName; } else { - actualName = fileName + ".json"; + actualName = fileName + ".yaml"; } return actualName; @@ -627,7 +627,7 @@ public partial class LevelGraphEditorLoader : UiLoaderTemplate } var levelGraphEditorSaveData = - await JsonSerialization.ReadJsonFileToObj(filePath); + await YamlSerialization.ReadYamlFileToObj(filePath); if (levelGraphEditorSaveData == null) { //Deserialization failed. diff --git a/scripts/loader/uiLoader/MainMenuLoader.cs b/scripts/loader/uiLoader/MainMenuLoader.cs index 925a1a9..14d263b 100644 --- a/scripts/loader/uiLoader/MainMenuLoader.cs +++ b/scripts/loader/uiLoader/MainMenuLoader.cs @@ -6,7 +6,6 @@ using ColdMint.scripts.camp; using ColdMint.scripts.contribute; using ColdMint.scripts.deathInfo; using ColdMint.scripts.debug; -using ColdMint.scripts.inventory; using ColdMint.scripts.item; using ColdMint.scripts.loot; using ColdMint.scripts.map; @@ -132,7 +131,6 @@ public partial class MainMenuLoader : UiLoaderTemplate { _startGameButton.Pressed += () => { - LogCat.Log("start_game"); if (_gameScene == null) { return; diff --git a/scripts/loader/uiLoader/PacksackUi.cs b/scripts/loader/uiLoader/PacksackUi.cs index 0b94223..37fd574 100644 --- a/scripts/loader/uiLoader/PacksackUi.cs +++ b/scripts/loader/uiLoader/PacksackUi.cs @@ -1,5 +1,4 @@ -using ColdMint.scripts.debug; -using ColdMint.scripts.inventory; +using ColdMint.scripts.inventory; using ColdMint.scripts.utils; using Godot; diff --git a/scripts/loot/LootDatum.cs b/scripts/loot/LootDatum.cs index 4053484..457b90d 100644 --- a/scripts/loot/LootDatum.cs +++ b/scripts/loot/LootDatum.cs @@ -1,6 +1,4 @@ -using Godot; - -namespace ColdMint.scripts.loot; +namespace ColdMint.scripts.loot; public readonly record struct LootDatum(string ItemId, int Quantity) { diff --git a/scripts/loot/LootEntry.cs b/scripts/loot/LootEntry.cs index c4d9ea4..d73e08e 100644 --- a/scripts/loot/LootEntry.cs +++ b/scripts/loot/LootEntry.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using ColdMint.scripts.utils; diff --git a/scripts/loot/LootList.cs b/scripts/loot/LootList.cs index ffba1f3..a04ab74 100644 --- a/scripts/loot/LootList.cs +++ b/scripts/loot/LootList.cs @@ -4,8 +4,6 @@ using System.Collections.Generic; using ColdMint.scripts.debug; using ColdMint.scripts.utils; -using Godot; - namespace ColdMint.scripts.loot; /// diff --git a/scripts/loot/LootListManager.cs b/scripts/loot/LootListManager.cs index 09e7709..8b96cf3 100644 --- a/scripts/loot/LootListManager.cs +++ b/scripts/loot/LootListManager.cs @@ -1,9 +1,5 @@ using System.Collections.Generic; -using ColdMint.scripts.utils; - -using Godot; - namespace ColdMint.scripts.loot; /// diff --git a/scripts/map/MapGenerator.cs b/scripts/map/MapGenerator.cs index f77c834..d321080 100644 --- a/scripts/map/MapGenerator.cs +++ b/scripts/map/MapGenerator.cs @@ -178,7 +178,6 @@ public static class MapGenerator var roomDictionary = new Dictionary(); var randomNumberGenerator = new RandomNumberGenerator(); randomNumberGenerator.Seed = _seed; - LogCat.LogWithFormat("seed_info", _seed); var startRoomNodeData = await _layoutParsingStrategy.GetStartRoomNodeData(); if (startRoomNodeData == null || string.IsNullOrEmpty(startRoomNodeData.Id)) { @@ -223,8 +222,8 @@ public static class MapGenerator if (_roomInjectionProcessorsDictionary != null && !string.IsNullOrEmpty(roomInjectionProcessorData)) { var roomInjectionProcessorDataArray = - JsonSerialization.Deserialize(roomInjectionProcessorData); - if (roomInjectionProcessorDataArray != null && roomInjectionProcessorDataArray.Length > 0) + YamlSerialization.Deserialize(roomInjectionProcessorData); + if (roomInjectionProcessorDataArray.Length > 0) { foreach (var injectionProcessorData in roomInjectionProcessorDataArray) { diff --git a/scripts/map/interfaces/IRoomInjectionProcessor.cs b/scripts/map/interfaces/IRoomInjectionProcessor.cs index 08d7fcf..10a7ac3 100644 --- a/scripts/map/interfaces/IRoomInjectionProcessor.cs +++ b/scripts/map/interfaces/IRoomInjectionProcessor.cs @@ -24,10 +24,10 @@ public interface IRoomInjectionProcessor ///Random probability generator based on world seed ///根据世界种子确定的随机概率生成器 /// - /// + /// ///Inject data into the processor ///注入处理器的数据 /// /// - public Task CanBePlaced(RandomNumberGenerator randomNumberGenerator, string? jsonConfigData); + public Task CanBePlaced(RandomNumberGenerator randomNumberGenerator, string? yamlConfigData); } \ No newline at end of file diff --git a/scripts/map/layoutStrategy/TestLayoutStrategy.cs b/scripts/map/layoutStrategy/TestLayoutStrategy.cs index 8326104..d60dbad 100644 --- a/scripts/map/layoutStrategy/TestLayoutStrategy.cs +++ b/scripts/map/layoutStrategy/TestLayoutStrategy.cs @@ -11,7 +11,7 @@ namespace ColdMint.scripts.map.layoutStrategy; /// public class TestLayoutStrategy : ILayoutStrategy { - private const string Path = "res://data/levelGraphs/test.json"; + private const string Path = "res://data/levelGraphs/test.yaml"; public Task GetLayout() { @@ -27,6 +27,6 @@ public class TestLayoutStrategy : ILayoutStrategy return Task.FromResult(null); } - return Task.FromResult(JsonSerialization.Deserialize(json)); + return Task.FromResult(YamlSerialization.Deserialize(json)); } } \ No newline at end of file diff --git a/scripts/map/roomInjectionProcessor/RoomInjectionProcessorTemplate.cs b/scripts/map/roomInjectionProcessor/RoomInjectionProcessorTemplate.cs index c94ca30..243a5a2 100644 --- a/scripts/map/roomInjectionProcessor/RoomInjectionProcessorTemplate.cs +++ b/scripts/map/roomInjectionProcessor/RoomInjectionProcessorTemplate.cs @@ -14,20 +14,15 @@ public abstract class RoomInjectionProcessorTemplate : IRoomInjectionPr { public abstract string GetId(); - public Task CanBePlaced(RandomNumberGenerator randomNumberGenerator, string? jsonConfigData) + public Task CanBePlaced(RandomNumberGenerator randomNumberGenerator, string? yamlConfigData) { - if (jsonConfigData == null) + if (yamlConfigData == null) { return Task.FromResult(false); } - var configData = JsonSerialization.Deserialize(jsonConfigData); - if (configData == null) - { - return Task.FromResult(false); - } - - return OnCreateConfigData(randomNumberGenerator, configData); + var configData = YamlSerialization.Deserialize(yamlConfigData); + return configData == null ? Task.FromResult(false) : OnCreateConfigData(randomNumberGenerator, configData); } /// diff --git a/scripts/projectile/ProjectileTemplate.cs b/scripts/projectile/ProjectileTemplate.cs index a98d1bf..684eda4 100644 --- a/scripts/projectile/ProjectileTemplate.cs +++ b/scripts/projectile/ProjectileTemplate.cs @@ -5,7 +5,7 @@ using ColdMint.scripts.character; using ColdMint.scripts.damage; using ColdMint.scripts.item; using ColdMint.scripts.item.weapon; - +using ColdMint.scripts.pickable; using Godot; namespace ColdMint.scripts.projectile; @@ -175,7 +175,7 @@ public partial class ProjectileTemplate : CharacterBody2D characterTemplate.AddForce(force); } } - else if (target is WeaponTemplate weaponTemplate) + else if (target is PickAbleTemplate pickAbleTemplate) { if (KnockbackForce != Vector2.Zero) { @@ -192,7 +192,7 @@ public partial class ProjectileTemplate : CharacterBody2D force.X = forceX * Config.CellSize; force.Y = KnockbackForce.Y * Config.CellSize; - weaponTemplate.ApplyImpulse(force); + pickAbleTemplate.ApplyImpulse(force); } } } diff --git a/scripts/serialization/JsonSerialization.cs b/scripts/serialization/JsonSerialization.cs index a626571..121fe72 100644 --- a/scripts/serialization/JsonSerialization.cs +++ b/scripts/serialization/JsonSerialization.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; using System.Text.Encodings.Web; using System.Text.Json; using System.Threading.Tasks; @@ -9,6 +10,12 @@ namespace ColdMint.scripts.serialization; /// JsonSerialization /// Json序列化工具 /// +/// +///This serializer is no longer recommended and Yaml is recommended instead of Json. +///此序列化器已不再推荐使用,建议用Yaml代替Json。 +/// +/// +[Obsolete("The Json serializer is out of date, we recommend yaml serialization.\nJson序列化器已过时了,我们推荐使用Yaml。", true)] public static class JsonSerialization { private static readonly JsonSerializerOptions Options = new() @@ -47,7 +54,7 @@ public static class JsonSerialization { return JsonSerializer.Serialize(obj, Options); } - + /// /// Deserialize Json to an object /// 将Json反序列化为对象 diff --git a/scripts/serialization/YamlSerialization.cs b/scripts/serialization/YamlSerialization.cs new file mode 100644 index 0000000..ff05b83 --- /dev/null +++ b/scripts/serialization/YamlSerialization.cs @@ -0,0 +1,58 @@ +using System.IO; +using System.Threading.Tasks; +using YamlDotNet.Serialization; +using YamlDotNet.Serialization.NamingConventions; + +namespace ColdMint.scripts.serialization; + +public static class YamlSerialization +{ + /// + /// YamlDeserializer + /// Yaml反序列化器 + /// + private static readonly IDeserializer YamlDeserializer = new DeserializerBuilder() + .WithNamingConvention(UnderscoredNamingConvention.Instance) // convent snake_case + .Build(); + + private static readonly ISerializer YamlSerializer = new SerializerBuilder() + .WithNamingConvention(UnderscoredNamingConvention.Instance) // convent snake_case + .Build(); + + /// + /// Read a Json file to type T + /// 读取一个Json文件到T类型 + /// + /// + /// + /// + public static async Task ReadYamlFileToObj(string path) + { + await using var openStream = File.OpenRead(path); + var yaml = await new StreamReader(openStream).ReadToEndAsync(); + return YamlDeserializer.Deserialize(yaml); + } + + /// + /// Serialize the object to Json + /// 将对象序列化为Yaml + /// + /// + /// + public static string Serialize(object obj) + { + return YamlSerializer.Serialize(obj); + } + + /// + /// Deserialize Yaml to the object + /// 反序列化Yaml到对象 + /// + /// + /// + /// + public static T Deserialize(string yaml) + { + return YamlDeserializer.Deserialize(yaml); + } +} \ No newline at end of file diff --git a/scripts/utils/NodeUtils.cs b/scripts/utils/NodeUtils.cs index 3d183e2..0cd7b16 100644 --- a/scripts/utils/NodeUtils.cs +++ b/scripts/utils/NodeUtils.cs @@ -1,9 +1,7 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Threading.Tasks; using ColdMint.scripts.debug; -using ColdMint.scripts.inventory; using ColdMint.scripts.item; using ColdMint.scripts.item.weapon;