using System.Collections; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using KairoEngine.Core; using KairoEngine.Inventory; namespace KairoEngine.CharacterSystem { public enum ItemDropRule { DoNothing, DropItem, RewardPlayer } public class DropItemsOnDeath : MonoBehaviour { public ItemContainer itemContainer; public DamageController damageController; [FoldoutGroup("Rules")] public ItemDropRule firearmsRule = ItemDropRule.DoNothing; [FoldoutGroup("Rules")] public ItemDropRule handWeaponsRule = ItemDropRule.DoNothing; [FoldoutGroup("Rules")] public ItemDropRule moneyRule = ItemDropRule.RewardPlayer; [FoldoutGroup("Rules")] public ItemDropRule ammoRule = ItemDropRule.DropItem; [FoldoutGroup("Rules")] public ItemDropRule usableItemsRule = ItemDropRule.DropItem; [FoldoutGroup("Rules")] public ItemDropRule miscItemsRule = ItemDropRule.DropItem; [FoldoutGroup("Rules"), Range(0, 100)] public int dropItemProbability = 100; [FoldoutGroup("Rules"), Range(0, 100)] public int rewardPlayerProbability = 100; private CharacterController playerCharacter; void OnEnable() { DamageEvents.OnDeath += DropItems; } void OnDisable() { DamageEvents.OnDeath -= DropItems; } private void DropItems(DamageController damageController, CharacterController killedBy) { if(this.damageController == damageController) { //if(killedBy == GameController.instance.heroCharacter) playerCharacter = killedBy; DropItem(); } } private void DropItem() { if(itemContainer.ItemList.Count > 0) { ItemRef itemRef = itemContainer.ItemList[0]; ItemDropRule rule; int diceRoll = Random.Range(0, 101); switch (itemRef.item.category) { case ItemType.firearm: rule = firearmsRule; break; case ItemType.handWeapon: rule = handWeaponsRule; break; case ItemType.ammo: rule = ammoRule; break; case ItemType.instantEffect: rule = usableItemsRule; break; default: if(itemRef.item.title == "Old Dollars") rule = moneyRule; else rule = miscItemsRule; break; } switch (rule) { case ItemDropRule.DoNothing: //Debug.Log($"{rule.ToString()} for {itemRef.item.title} ({itemRef.item.category.ToString()})"); itemContainer.ItemList.Remove(itemRef); break; case ItemDropRule.DropItem: //Debug.Log($"{rule.ToString()} for {itemRef.item.title} (probability: {diceRoll}/{dropItemProbability}, {itemRef.item.category.ToString()})"); if(diceRoll > dropItemProbability) { itemContainer.ItemList.Remove(itemRef); break; } ICommand dropItemCommand = new DropItemCommand(itemContainer, itemRef); CommandInvoker.AddCommand(dropItemCommand); break; case ItemDropRule.RewardPlayer: if(playerCharacter == null) break; //Debug.Log($"{rule.ToString()} for {itemRef.item.title} (probability: {diceRoll}/{rewardPlayerProbability}, {itemRef.item.category.ToString()})"); if(diceRoll > rewardPlayerProbability) { itemContainer.ItemList.Remove(itemRef); break; } playerCharacter.itemContainer.AddItem(itemRef); itemContainer.ItemList.Remove(itemRef); break; default: break; } StartCoroutine(Timer.Start(0.1f, false, () => { DropItem(); })); } } } }