openitem.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. class OpenItem
  2. {
  3. //! WIll open the 'item_target' by spawning a new entity and transferring item variables to the new one
  4. static void OpenAndSwitch(ItemBase item_tool, ItemBase item_target, PlayerBase player, float specialty_weight = 0)
  5. {
  6. array<int> spill_range = new array<int>;
  7. if( item_tool.ConfigIsExisting("OpenItemSpillRange") )
  8. {
  9. item_tool.ConfigGetIntArray("OpenItemSpillRange", spill_range );
  10. }
  11. else
  12. {
  13. Debug.LogError("OpenItemSpillRange config parameter missing, default values used ! ");
  14. Error("OpenItemSpillRange config parameter missing, default values used !");
  15. spill_range.Insert(0);
  16. spill_range.Insert(100);
  17. }
  18. float spill_modificator = Math.RandomIntInclusive( spill_range.Get(0),spill_range.Get(1) ) / 100;
  19. OpenItem.SwitchItems(item_target, player, spill_modificator, specialty_weight);
  20. }
  21. //! Will switch the 'item' for a new game entity, the new entity's classname will be formed by adding the 'suffix' to the classname of the old 'item'
  22. static void SwitchItems (EntityAI old_item, PlayerBase player, float spill_modificator, float specialty_weight)
  23. {
  24. string old_name = old_item.GetType();
  25. string new_name = old_name + "_Opened";
  26. OpenAndSwitchLambda l = new OpenAndSwitchLambda(old_item, new_name, player, spill_modificator, specialty_weight);
  27. l.SetTransferParams(true, true, true, true);
  28. MiscGameplayFunctions.TurnItemIntoItemEx(player, l);
  29. }
  30. };
  31. class OpenAndSwitchLambda : TurnItemIntoItemLambda
  32. {
  33. float m_SpillModifier;
  34. float m_SpecialtyWeight;
  35. void OpenAndSwitchLambda (EntityAI old_item, string new_item_type, PlayerBase player, float spill_modificator, float specialty_weight) { m_SpillModifier = spill_modificator; m_SpecialtyWeight = specialty_weight; }
  36. override void CopyOldPropertiesToNew (notnull EntityAI old_item, EntityAI new_item)
  37. {
  38. super.CopyOldPropertiesToNew(old_item, new_item);
  39. }
  40. override void OnSuccess (EntityAI new_item)
  41. {
  42. super.OnSuccess(new_item);
  43. ItemBase ib = ItemBase.Cast(new_item);
  44. if ( new_item )
  45. {
  46. float quantity_old = ib.GetQuantity();
  47. float spill_amount = quantity_old * m_SpillModifier;
  48. float quantity_new = quantity_old - spill_amount;
  49. Debug.Log("quantity before spill: "+quantity_old.ToString());
  50. Debug.Log("spill_amount: "+spill_amount.ToString());
  51. Debug.Log("quantity_new: "+quantity_new.ToString());
  52. ib.SetQuantity(quantity_new);
  53. }
  54. }
  55. };