| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- using System.Text.Json;
- using System.Text.Json.Serialization;
- namespace XafPet.Desktop.PetEngine;
- [JsonConverter(typeof(JsonStringEnumConverter))]
- public enum PetType { Animal, Human, Custom }
- /// <summary>宠物形象配置(pet-config.json)</summary>
- public sealed class PetConfig
- {
- [JsonPropertyName("version")]
- public string Version { get; set; } = "1.0";
- [JsonPropertyName("type")]
- public PetType Type { get; set; } = PetType.Custom;
- [JsonPropertyName("species")]
- public string Species { get; set; } = "other";
- [JsonPropertyName("gender")]
- public string Gender { get; set; } = "neutral";
- [JsonPropertyName("image_base_url")]
- public string ImageBaseUrl { get; set; } = "";
- [JsonPropertyName("default_image")]
- public string DefaultImage { get; set; } = "standing.png";
- [JsonPropertyName("actions")]
- public Dictionary<string, string> Actions { get; set; } = new();
- [JsonPropertyName("sounds")]
- public Dictionary<string, string> Sounds { get; set; } = new();
- [JsonPropertyName("parameters")]
- public PetParameters Parameters { get; set; } = new();
- [JsonIgnore]
- public int CustomStateCount => Actions.Keys.Count(k => k.StartsWith("custom_state_"));
- /// <summary>解析 JSON 字符串;缺省 actions 时补默认映射(key=value)</summary>
- public static PetConfig Parse(string json)
- {
- var config = JsonSerializer.Deserialize<PetConfig>(json)
- ?? throw new JsonException("pet-config.json 反序列化结果为 null");
- if (config.Actions.Count == 0)
- {
- config.Actions = new Dictionary<string, string>
- {
- ["idle"] = "idle",
- ["happy"] = "happy",
- ["busy"] = "busy",
- ["sleeping"] = "sleeping"
- };
- }
- return config;
- }
- public string GetActionForState(string stateName)
- => Actions.TryGetValue(stateName, out var v) ? v : stateName;
- }
- public sealed class PetParameters
- {
- [JsonPropertyName("move_distance")]
- public int MoveDistance { get; set; } = 15;
- [JsonPropertyName("animation_speed")]
- public double AnimationSpeed { get; set; } = 1.0;
- [JsonPropertyName("interaction_timeout")]
- public int InteractionTimeout { get; set; } = 300; // 秒
- }
|