using System.Text; using Milimoe.FunGame.Core.Api.Utility; using Milimoe.FunGame.Core.Interface.Base; using Milimoe.FunGame.Core.Interface.Entity; using Milimoe.FunGame.Core.Library.Constant; namespace Milimoe.FunGame.Core.Entity { /// /// 和 一样,需要继承构造 /// public class Item : BaseEntity, IItem { /// /// 唯一标识符 /// public override Guid Guid { get; set; } = Guid.NewGuid(); /// /// 物品的描述 /// public virtual string Description { get; set; } = ""; /// /// 物品的通用描述 /// public virtual string GeneralDescription { get; set; } = ""; /// /// 物品的背景故事 /// public virtual string BackgroundStory { get; set; } = ""; /// /// 物品类型 /// public virtual ItemType ItemType { get; set; } = ItemType.Others; /// /// 是否是装备 /// public bool IsEquipment { get { return ItemType switch { ItemType.MagicCardPack => true, ItemType.Weapon => true, ItemType.Armor => true, ItemType.Shoes => true, ItemType.Accessory => true, _ => false }; } } /// /// 是否允许装备 /// [ 注意:这个不是用来判断是不是装备类型的,判断装备类型时,请判断他们的 ] /// public bool Equipable { get; set; } = true; /// /// 是否允许取消装备 /// [ 注意:这个不是用来判断是不是装备类型的,判断装备类型时,使用 ] /// public bool Unequipable { get; set; } = true; /// /// 当前装备的槽位 /// public virtual EquipSlotType EquipSlotType { get; set; } = EquipSlotType.None; /// /// 武器类型(如果是武器) /// public virtual WeaponType WeaponType { get; set; } = WeaponType.None; /// /// 品质类型 /// public virtual QualityType QualityType { get; set; } = QualityType.White; /// /// 稀有度类型 /// public virtual RarityType RarityType { get; set; } = RarityType.OneStar; /// /// 物品评级 /// public virtual ItemRankType RankType { get; set; } = ItemRankType.D; /// /// 快捷键 /// public char Key { get; set; } = '/'; /// /// 是否是主动物品 /// public bool IsActive => Skills.Active != null; /// /// 是否可用(涉及冷却和禁用等) /// public bool Enable { get; set; } = true; /// /// 是否是局内使用的物品(局内是指对角色生效的物品) /// public bool IsInGameItem { get; set; } = true; /// /// 是否允许购买 /// public bool IsPurchasable { get; set; } = true; /// /// 物品的价格 /// public double Price { get; set; } = 0; /// /// 是否允许出售 /// public bool IsSellable { get; set; } = true; /// /// 下次可出售的时间 /// public DateTime NextSellableTime { get; set; } = DateTime.MinValue; /// /// 是否允许交易 /// public bool IsTradable { get; set; } = true; /// /// 下次可交易的时间 /// public DateTime NextTradableTime { get; set; } = DateTime.MinValue; /// /// 剩余使用次数(消耗品才会用到) /// public int RemainUseTimes { get; set; } = 0; /// /// 物品所属的角色(只有装备物品,才需要设置) /// public Character? Character { get => _character; set { _character = value; if (Skills.Active != null) Skills.Active.Character = _character; foreach (Skill skill in Skills.Passives) { skill.Character = _character; foreach (Effect e in skill.Effects) { e.Source = _character; } } foreach (Skill skill in Skills.Magics) { skill.Character = _character; foreach (Effect e in skill.Effects) { e.Source = _character; } } } } /// /// 所属的玩家 /// public User? User { get; set; } = null; /// /// 物品拥有的技能 /// public SkillGroup Skills { get; set; } = new(); /// /// 其他内容 /// public Dictionary Others { get; set; } = []; /// /// 当装备物品时 /// public void OnItemEquip(Character character, EquipSlotType type) { Character = character; EquipSlotType = type; foreach (Skill skill in Skills.Passives) { if (!skill.IsActive && skill.Level > 0) { foreach (Effect e in skill.AddInactiveEffectToCharacter()) { e.GamingQueue = skill.GamingQueue; if (Character != null && !Character.Effects.Contains(e)) { Character.Effects.Add(e); e.OnEffectGained(Character); } } } } foreach (Skill skill in Skills.Magics) { if (Character != null && skill.IsMagic && skill.Level > 0) { Character.Skills.Add(skill); } } if (Character != null) OnItemEquipped(Character, this, type); } /// /// 当取消装备物品时 /// public void OnItemUnEquip(EquipSlotType type) { if (Character != null) { foreach (Skill skill in Skills.Passives) { List effects = Character.Effects.Where(e => e.Skill == skill && e.Level > 0).ToList(); foreach (Effect e in effects) { Character.Effects.Remove(e); e.OnEffectLost(Character); } } foreach (Skill skill in Skills.Magics) { Character.Skills.Remove(skill); } switch (type) { case EquipSlotType.MagicCardPack: Character.EquipSlot.MagicCardPack = null; break; case EquipSlotType.Weapon: Character.EquipSlot.Weapon = null; break; case EquipSlotType.Armor: Character.EquipSlot.Armor = null; break; case EquipSlotType.Shoes: Character.EquipSlot.Shoes = null; break; case EquipSlotType.Accessory1: Character.EquipSlot.Accessory1 = null; break; case EquipSlotType.Accessory2: Character.EquipSlot.Accessory2 = null; break; } OnItemUnEquipped(Character, this, type); } Character = null; EquipSlotType = EquipSlotType.None; } /// /// 设置游戏内的行动顺序表实例 /// /// public void SetGamingQueue(IGamingQueue queue) { if (Skills.Active != null) Skills.Active.GamingQueue = queue; foreach (Skill skill in Skills.Passives) { skill.GamingQueue = queue; } foreach (Skill skill in Skills.Magics) { skill.GamingQueue = queue; } } /// /// 局内使用物品触发 对某个角色使用 /// public void UseItem(IGamingQueue queue, Character character, List enemys, List teammates) { bool cancel = false; bool used = false; if (Skills.Active != null) { Skill skill = Skills.Active; List targets = queue.SelectTargets(character, skill, enemys, teammates, out cancel); if (!cancel) { skill.OnSkillCasted(queue, character, targets); used = true; } } OnItemUsed(character, this, cancel, used); } /// /// 局外(库存)使用物品触发 /// public void UseItem() { if (User != null) OnItemUsed(User, this); } /// /// 当物品被角色使用时 /// /// /// /// /// protected virtual void OnItemUsed(Character character, Item item, bool cancel, bool used) { } /// /// 当物品被玩家使用时 /// /// /// protected virtual void OnItemUsed(User user, Item item) { } /// /// 当物品被装备时 /// /// /// /// protected virtual void OnItemEquipped(Character character, Item item, EquipSlotType type) { } /// /// 当物品被取消装备时 /// /// /// /// protected virtual void OnItemUnEquipped(Character character, Item item, EquipSlotType type) { } protected Item(ItemType type, bool isInGame = true) { ItemType = type; IsInGameItem = isInGame; } internal Item() { } /// /// Id.Name /// /// public string GetIdName() { return Id + "." + Name; } /// /// 显示物品的详细信息 /// /// public override string ToString() { return ToString(false); } /// /// 显示物品的详细信息 /// /// 是否显示通用描述,而不是描述 /// 是否在商店中显示 /// public string ToString(bool isShowGeneralDescription, bool isShowInStore = false) { StringBuilder builder = new(); builder.AppendLine($"【{Name}】"); string itemquality = ItemSet.GetQualityTypeName(QualityType); string itemtype = ItemSet.GetItemTypeName(ItemType) + (ItemType == ItemType.Weapon && WeaponType != WeaponType.None ? "-" + ItemSet.GetWeaponTypeName(WeaponType) : ""); if (itemtype != "") itemtype = $" {itemtype}"; builder.AppendLine($"{itemquality + itemtype}"); if (isShowInStore && Price > 0) { builder.AppendLine($"售价:{Price} {General.GameplayEquilibriumConstant.InGameCurrency}"); } if (RemainUseTimes > 0) { builder.AppendLine($"{(isShowInStore ? "" : "剩余")}可用次数:{RemainUseTimes}"); } if (isShowInStore) { if (IsSellable) { builder.AppendLine($"购买此物品后可立即出售"); } if (IsTradable) { DateTime date = DateTimeUtility.GetTradableTime(); builder.AppendLine($"购买此物品后将在 {date.ToString(General.GeneralDateTimeFormatChinese)} 后可交易"); } } else { List sellandtrade = []; if (IsSellable) { sellandtrade.Add("可出售"); } if (!IsSellable && NextSellableTime != DateTime.MinValue) { builder.AppendLine($"此物品将在 {NextSellableTime.ToString(General.GeneralDateTimeFormatChinese)} 后可出售"); } else if (!IsSellable) { sellandtrade.Add("不可出售"); } if (IsTradable) { sellandtrade.Add("可交易"); } if (!IsTradable && NextTradableTime != DateTime.MinValue) { builder.AppendLine($"此物品将在 {NextTradableTime.ToString(General.GeneralDateTimeFormatChinese)} 后可交易"); } else if (!IsTradable) { sellandtrade.Add("不可交易"); } if (sellandtrade.Count > 0) builder.AppendLine(string.Join(" ", sellandtrade).Trim()); } if (isShowGeneralDescription && GeneralDescription != "") { builder.AppendLine("物品描述:" + GeneralDescription); } else if (Description != "") { builder.AppendLine("物品描述:" + Description); } if (ItemType == ItemType.MagicCardPack && Skills.Magics.Count > 0) { builder.AppendLine("== 魔法卡 ==\r\n" + string.Join("\r\n", Skills.Magics.Select(m => m.ToString().Trim()))); } builder.AppendLine("== 物品技能 =="); if (Skills.Active != null) builder.AppendLine($"{Skills.Active.ToString().Trim()}"); foreach (Skill skill in Skills.Passives) { builder.AppendLine($"{skill.ToString().Trim()}"); } if (BackgroundStory != "") { builder.AppendLine($"\"{BackgroundStory}\""); } return builder.ToString(); } public string ToStringInventory(bool showAll) { StringBuilder builder = new(); if (showAll) { builder.Append($"{ToString()}"); if (IsEquipment && Character != null) builder.AppendLine($"装备于:{Character.ToStringWithLevelWithOutUser()}"); builder.AppendLine(); } else { List sellandtrade = []; if (!IsSellable && NextSellableTime != DateTime.MinValue) { builder.AppendLine($"此物品将在 {NextSellableTime.ToString(General.GeneralDateTimeFormatChinese)} 后可出售"); } else if (!IsSellable) { sellandtrade.Add("不可出售"); } if (!IsTradable && NextTradableTime != DateTime.MinValue) { builder.AppendLine($"此物品将在 {NextTradableTime.ToString(General.GeneralDateTimeFormatChinese)} 后可交易"); } else if (!IsTradable) { sellandtrade.Add("不可交易"); } if (sellandtrade.Count > 0) builder.AppendLine(string.Join(" ", sellandtrade).Trim()); if (Description != "") builder.AppendLine($"{Description}"); if (IsEquipment && Character != null) builder.AppendLine($"装备于:{Character.ToStringWithLevelWithOutUser()}"); } return builder.ToString(); } /// /// 判断两个物品是否相同 检查Id.Name /// /// /// public override bool Equals(IBaseEntity? other) { return other is Item c && c.Id + "." + c.Name == Id + "." + Name; } /// /// 设置一些属性给从工厂构造出来的 对象 /// /// public void SetPropertyToItemModuleNew(Item newbyFactory) { newbyFactory.WeaponType = WeaponType; newbyFactory.EquipSlotType = EquipSlotType; newbyFactory.Equipable = Equipable; newbyFactory.IsPurchasable = IsPurchasable; newbyFactory.Price = Price; newbyFactory.IsSellable = IsSellable; newbyFactory.NextSellableTime = NextSellableTime; newbyFactory.IsTradable = IsTradable; newbyFactory.NextTradableTime = NextTradableTime; } /// /// 复制一个物品 /// /// public Item Copy(bool copyLevel = false, bool copyGuid = false) { Item item = Factory.OpenFactory.GetInstance(Id, Name, []); SetPropertyToItemModuleNew(item); item.Id = Id; item.Name = Name; if (copyGuid) item.Guid = Guid; item.Description = Description; item.GeneralDescription = GeneralDescription; item.BackgroundStory = BackgroundStory; item.ItemType = ItemType; item.Equipable = Equipable; item.Unequipable = Unequipable; item.WeaponType = WeaponType; item.QualityType = QualityType; item.RarityType = RarityType; item.RankType = RankType; item.Key = Key; item.Enable = Enable; item.IsInGameItem = IsInGameItem; item.IsPurchasable = IsPurchasable; item.Price = Price; item.IsSellable = IsSellable; item.NextSellableTime = NextSellableTime; item.IsTradable = IsTradable; item.NextTradableTime = NextTradableTime; item.RemainUseTimes = RemainUseTimes; if (item is OpenItem) { item.Skills.Active = Skills.Active?.Copy(); if (item.Skills.Active != null) { item.Skills.Active.Level = copyLevel ? (Skills.Active?.Level ?? 1) : 1; item.Skills.Active.Guid = item.Guid; } foreach (Skill skill in Skills.Passives) { Skill newskill = skill.Copy(); newskill.Item = item; newskill.Level = copyLevel ? skill.Level : 1; newskill.Guid = item.Guid; item.Skills.Passives.Add(newskill); } foreach (Skill skill in Skills.Magics) { Skill newskill = skill.Copy(); newskill.Item = item; newskill.Level = copyLevel ? skill.Level : 1; newskill.Guid = item.Guid; item.Skills.Magics.Add(newskill); } } return item; } /// /// 设置所有技能的等级 /// /// public void SetLevel(int level) { if (Skills.Active != null) { Skills.Active.Level = level; } foreach (Skill skill in Skills.Passives) { skill.Level = level; } } /// /// 设置所有魔法的等级 /// /// public void SetMagicsLevel(int level) { if (Skills.Active != null) { Skills.Active.Level = level; } foreach (Skill skill in Skills.Magics) { skill.Level = level; } } /// /// 所属的角色 /// private Character? _character = null; } }