inventoryactionhandler.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //! Client only - manage set up crafting on client
  2. class InventoryActionHandler
  3. {
  4. ActionBase m_action;
  5. ActionTarget m_target;
  6. ItemBase m_mainItem;
  7. bool m_useItemInHands;
  8. PlayerBase m_player;
  9. bool m_isActive;
  10. vector m_actionStartPos;
  11. const float MIN_DISTANCE_TO_INTERRUPT = 1.0;
  12. const int IAH_SINGLE_USE = 1;
  13. const int IAH_CONTINUOUS = 2;
  14. void InventoryActionHandler(PlayerBase player)
  15. {
  16. m_player = player;
  17. m_isActive = false;
  18. m_action = null;
  19. m_target = null;
  20. m_mainItem = null;
  21. m_useItemInHands = false;
  22. }
  23. void SetAction(ActionBase action, ItemBase target_item, ItemBase main_item )
  24. {
  25. Object target_parent = null;
  26. if(target_item)
  27. {
  28. target_parent = target_item.GetHierarchyParent();
  29. }
  30. ActionTarget at = new ActionTarget(target_item, target_parent, -1, vector.Zero, -1);
  31. SetAction(action, at, main_item );
  32. }
  33. void SetAction(ActionBase action, ActionTarget target, ItemBase main_item )
  34. {
  35. ActionManagerClient mngr;
  36. Class.CastTo(mngr, m_player.GetActionManager());
  37. m_action = action;
  38. m_target = target;
  39. m_mainItem = main_item;
  40. ItemBase itemInHand = m_player.GetItemInHands();
  41. m_useItemInHands = main_item == itemInHand;
  42. m_actionStartPos = m_player.GetPosition();
  43. m_isActive = true;
  44. //mngr.InjectAction( action, target, main_item );
  45. mngr.ForceTarget(m_target.GetObject());
  46. GetGame().GetMission().HideInventory();
  47. }
  48. bool IsActiveAction()
  49. {
  50. return m_isActive;
  51. }
  52. void OnUpdate()
  53. {
  54. if( !m_isActive ) return;
  55. if( m_player.IsRaised() || m_player.GetCommand_Melee() )
  56. {
  57. DeactiveAction();
  58. return;
  59. }
  60. if( GetGame().IsInventoryOpen() )
  61. {
  62. DeactiveAction();
  63. return;
  64. }
  65. if (m_useItemInHands)
  66. {
  67. ItemBase handItem = m_player.GetItemInHands();
  68. if( handItem != m_mainItem )
  69. {
  70. DeactiveAction();
  71. return;
  72. }
  73. }
  74. if( Math.AbsFloat( vector.Distance(m_actionStartPos, m_player.GetPosition())) > MIN_DISTANCE_TO_INTERRUPT )
  75. {
  76. DeactiveAction();
  77. return;
  78. }
  79. }
  80. void DeactiveAction()
  81. {
  82. if( !m_isActive ) return;
  83. m_isActive = false;
  84. ActionManagerClient mngr;
  85. Class.CastTo(mngr, m_player.GetActionManager());
  86. mngr.EjectAction(m_action);
  87. mngr.ClearForceTarget();
  88. m_player.GetCraftingManager().ResetInventoryCraft();
  89. m_action = null;
  90. m_target = null;
  91. m_mainItem = null;
  92. m_useItemInHands = false;
  93. }
  94. }