PetConfig.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System.Text.Json;
  2. using System.Text.Json.Serialization;
  3. namespace XafPet.Desktop.PetEngine;
  4. public enum PetType { Animal, Human, Custom }
  5. /// <summary>宠物形象配置(pet-config.json)</summary>
  6. public sealed class PetConfig
  7. {
  8. [JsonPropertyName("version")]
  9. public string Version { get; set; } = "1.0";
  10. [JsonPropertyName("type")]
  11. public PetType Type { get; set; } = PetType.Custom;
  12. [JsonPropertyName("species")]
  13. public string Species { get; set; } = "other";
  14. [JsonPropertyName("gender")]
  15. public string Gender { get; set; } = "neutral";
  16. [JsonPropertyName("image_base_url")]
  17. public string ImageBaseUrl { get; set; } = "";
  18. [JsonPropertyName("default_image")]
  19. public string DefaultImage { get; set; } = "standing.png";
  20. [JsonPropertyName("actions")]
  21. public Dictionary<string, string> Actions { get; set; } = new();
  22. [JsonPropertyName("sounds")]
  23. public Dictionary<string, string> Sounds { get; set; } = new();
  24. [JsonPropertyName("parameters")]
  25. public PetParameters Parameters { get; set; } = new();
  26. [JsonIgnore]
  27. public int CustomStateCount => Actions.Keys.Count(k => k.StartsWith("custom_state_"));
  28. /// <summary>解析 JSON 字符串;缺省 actions 时补默认映射(key=value)</summary>
  29. public static PetConfig Parse(string json)
  30. {
  31. var config = JsonSerializer.Deserialize<PetConfig>(json)
  32. ?? throw new JsonException("pet-config.json 反序列化结果为 null");
  33. if (config.Actions.Count == 0)
  34. {
  35. config.Actions = new Dictionary<string, string>
  36. {
  37. ["idle"] = "idle",
  38. ["happy"] = "happy",
  39. ["busy"] = "busy",
  40. ["sleeping"] = "sleeping"
  41. };
  42. }
  43. return config;
  44. }
  45. public string GetActionForState(string stateName)
  46. => Actions.TryGetValue(stateName, out var v) ? v : stateName;
  47. }
  48. public sealed class PetParameters
  49. {
  50. [JsonPropertyName("move_distance")]
  51. public int MoveDistance { get; set; } = 15;
  52. [JsonPropertyName("animation_speed")]
  53. public double AnimationSpeed { get; set; } = 1.0;
  54. [JsonPropertyName("interaction_timeout")]
  55. public int InteractionTimeout { get; set; } = 300; // 秒
  56. }