123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- 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();
- }));
- }
-
- }
- }
- }
|