using Invector; using System.Collections.Generic; using UnityEngine; namespace Beyond { [System.Serializable] public partial class bItem : ScriptableObject { #region SerializedProperties in customEditor [HideInInspector] public int id; [HideInInspector] public string secondaryName = "secondary Name"; [HideInInspector] public string description = "Item Description"; [HideInInspector] public string secondaryDescription = "Secondary Description"; [HideInInspector] public bItemType type; [HideInInspector] public Sprite icon, secondaryIcon, teriaryIcon; [HideInInspector] public string iconPath, secondaryIconPath, teriaryIconPath; [HideInInspector] public bool stackable = true; [HideInInspector] public int maxStack; [HideInInspector] public int amount; [HideInInspector] public GameObject originalObject; [HideInInspector] public GameObject dropObject; [HideInInspector] public List attributes = new List(); [HideInInspector] public bool isInEquipArea; public Color checkColor = Color.cyan; [HideInInspector] public bool isEquiped; #endregion SerializedProperties in customEditor #region Properties in defaultInspector public bool destroyAfterUse = true; public bool canBeUsed = true; public bool canBeDroped = true; public bool canBeDestroyed = true; [Header("Animation Settings")] [vHelpBox("Triggers a animation when Equipping a Weapon or enabling item.\nYou can also trigger an animation if the ItemType is a Consumable")] public string EnableAnim = "LowBack"; [vHelpBox("Triggers a animation when Unequipping a Weapon or disable item")] public string DisableAnim = "LowBack"; [vHelpBox("Delay to enable the Weapon/Item object when Equipping\n If ItemType is a Consumable use this to delay the item usage.")] public float enableDelayTime = 0.5f; [vHelpBox("Delay to hide the Weapon/Item object when Unequipping")] public float disableDelayTime = 0.5f; [vHelpBox("If the item is equippable use this to set a custom handler to instantiate the SpawnObject")] public string customHandler; [vHelpBox("If the item is equippable and need to use two hand\nThis option makes it impossible to equip two items")] public bool twoHandWeapon; [HideInInspector] public OnHandleItemEvent onDestroy; #endregion Properties in defaultInspector public void OnDestroy() { onDestroy.Invoke(this); } /// /// Convert Sprite icon to texture /// public Texture2D iconTexture { get { if (!icon) return null; try { if (icon.rect.width != icon.texture.width || icon.rect.height != icon.texture.height) { Texture2D newText = new Texture2D((int)icon.textureRect.width, (int)icon.textureRect.height); newText.name = icon.name; Color[] newColors = icon.texture.GetPixels((int)icon.textureRect.x, (int)icon.textureRect.y, (int)icon.textureRect.width, (int)icon.textureRect.height); newText.SetPixels(newColors); newText.Apply(); return newText; } else return icon.texture; } catch { Debug.LogWarning("Icon texture of the " + name + " is not Readable", icon.texture); return icon.texture; } } } /// /// Get the Item Attribute via /// /// /// public bItemAttribute GetItemAttribute(bItemAttributes attribute) { if (attributes != null) return attributes.Find(_attribute => _attribute.name == attribute); return null; } /// /// Get the Item Attribute via string /// /// /// public bItemAttribute GetItemAttribute(string name) { if (attributes != null) return attributes.Find(attribute => attribute.name.ToString().Equals(name)); return null; } /// /// Get Selected Item Attributes via by ignoring the ones you don't want /// /// /// public string GetItemAttributesText(List ignore = null) { System.Text.StringBuilder text = new System.Text.StringBuilder(); for (int i = 0; i < attributes.Count; i++) { if (ignore == null || !ignore.Contains(attributes[i].name)) text.AppendLine(GetItemAttributeText(i)); } return text.ToString(); } /// /// Get Item Attribute Text /// /// /// protected string GetItemAttributeText(int i) { if (attributes.Count > 0 && i < attributes.Count) { if (attributes.Count > 0 && i < attributes.Count) { return attributes[i].GetDisplayText(); } } return string.Empty; } /// /// Get Item Attribut Text with a custom Format to display /// /// /// /// protected string GetItemAttributeText(int i, string customFormat) { if (attributes.Count > 0 && i < attributes.Count) { return attributes[i].GetDisplayText(customFormat); } return string.Empty; } /// /// Get Default Item type text /// /// public string ItemTypeText() { return ItemTypeText(type.DisplayFormat()); } /// /// Get Custom Item type text /// /// Custom format for text /// public string ItemTypeText(string format) { var _text = format; var value = type.ToString().InsertSpaceBeforeUpperCase().RemoveUnderline(); if (string.IsNullOrEmpty(_text)) return value; else if (_text.Contains("(NAME)")) _text.Replace("(NAME)", value); return _text; } /// /// Get Item Full Description text including item Name, Type, Description and Attributes /// /// Custom format /// Attributes to ignore /// public string GetFullItemDescription(string format = null, List ignoreAttributes = null) { string text = ""; if (string.IsNullOrEmpty(format)) { text += (name); text += "\n" + (ItemTypeText()); text += "\n" + (description); text += "\n" + (GetItemAttributesText()); } else { text = format; if (text.Contains("(NAME)")) text = text.Replace("(NAME)", name); if (text.Contains("(TYPE)")) text = text.Replace("(TYPE)", ItemTypeText()); if (text.Contains("(DESC)")) text = text.Replace("(DESC)", description); if (text.Contains("(ATTR)")) text = text.Replace("(ATTR)", GetItemAttributesText(ignoreAttributes)); } return text; } } }