DropItemsOnDeath.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using KairoEngine.Core;
  6. using KairoEngine.Inventory;
  7. namespace KairoEngine.CharacterSystem
  8. {
  9. public enum ItemDropRule
  10. {
  11. DoNothing,
  12. DropItem,
  13. RewardPlayer
  14. }
  15. public class DropItemsOnDeath : MonoBehaviour
  16. {
  17. public ItemContainer itemContainer;
  18. public DamageController damageController;
  19. [FoldoutGroup("Rules")] public ItemDropRule firearmsRule = ItemDropRule.DoNothing;
  20. [FoldoutGroup("Rules")] public ItemDropRule handWeaponsRule = ItemDropRule.DoNothing;
  21. [FoldoutGroup("Rules")] public ItemDropRule moneyRule = ItemDropRule.RewardPlayer;
  22. [FoldoutGroup("Rules")] public ItemDropRule ammoRule = ItemDropRule.DropItem;
  23. [FoldoutGroup("Rules")] public ItemDropRule usableItemsRule = ItemDropRule.DropItem;
  24. [FoldoutGroup("Rules")] public ItemDropRule miscItemsRule = ItemDropRule.DropItem;
  25. [FoldoutGroup("Rules"), Range(0, 100)] public int dropItemProbability = 100;
  26. [FoldoutGroup("Rules"), Range(0, 100)] public int rewardPlayerProbability = 100;
  27. private CharacterController playerCharacter;
  28. void OnEnable()
  29. {
  30. DamageEvents.OnDeath += DropItems;
  31. }
  32. void OnDisable()
  33. {
  34. DamageEvents.OnDeath -= DropItems;
  35. }
  36. private void DropItems(DamageController damageController, CharacterController killedBy)
  37. {
  38. if(this.damageController == damageController)
  39. {
  40. //if(killedBy == GameController.instance.heroCharacter) playerCharacter = killedBy;
  41. DropItem();
  42. }
  43. }
  44. private void DropItem()
  45. {
  46. if(itemContainer.ItemList.Count > 0)
  47. {
  48. ItemRef itemRef = itemContainer.ItemList[0];
  49. ItemDropRule rule;
  50. int diceRoll = Random.Range(0, 101);
  51. switch (itemRef.item.category)
  52. {
  53. case ItemType.firearm:
  54. rule = firearmsRule;
  55. break;
  56. case ItemType.handWeapon:
  57. rule = handWeaponsRule;
  58. break;
  59. case ItemType.ammo:
  60. rule = ammoRule;
  61. break;
  62. case ItemType.instantEffect:
  63. rule = usableItemsRule;
  64. break;
  65. default:
  66. if(itemRef.item.title == "Old Dollars") rule = moneyRule;
  67. else rule = miscItemsRule;
  68. break;
  69. }
  70. switch (rule)
  71. {
  72. case ItemDropRule.DoNothing:
  73. //Debug.Log($"{rule.ToString()} for {itemRef.item.title} ({itemRef.item.category.ToString()})");
  74. itemContainer.ItemList.Remove(itemRef);
  75. break;
  76. case ItemDropRule.DropItem:
  77. //Debug.Log($"{rule.ToString()} for {itemRef.item.title} (probability: {diceRoll}/{dropItemProbability}, {itemRef.item.category.ToString()})");
  78. if(diceRoll > dropItemProbability)
  79. {
  80. itemContainer.ItemList.Remove(itemRef);
  81. break;
  82. }
  83. ICommand dropItemCommand = new DropItemCommand(itemContainer, itemRef);
  84. CommandInvoker.AddCommand(dropItemCommand);
  85. break;
  86. case ItemDropRule.RewardPlayer:
  87. if(playerCharacter == null) break;
  88. //Debug.Log($"{rule.ToString()} for {itemRef.item.title} (probability: {diceRoll}/{rewardPlayerProbability}, {itemRef.item.category.ToString()})");
  89. if(diceRoll > rewardPlayerProbability)
  90. {
  91. itemContainer.ItemList.Remove(itemRef);
  92. break;
  93. }
  94. playerCharacter.itemContainer.AddItem(itemRef);
  95. itemContainer.ItemList.Remove(itemRef);
  96. break;
  97. default:
  98. break;
  99. }
  100. StartCoroutine(Timer.Start(0.1f, false, () =>
  101. {
  102. DropItem();
  103. }));
  104. }
  105. }
  106. }
  107. }