metalwire.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. class MetalWire extends ItemBase
  2. {
  3. static string SEL_WIRE_ROLLED = "rolled";
  4. static string SEL_WIRE_PREFIX = "Att_";
  5. static string SEL_WIRE_SUFIX = "_plugged";
  6. static string SEL_PLUG_SUFIX = "_plug";
  7. void MetalWire()
  8. {
  9. }
  10. override bool CanPutAsAttachment( EntityAI parent )
  11. {
  12. if (!super.CanPutAsAttachment(parent))
  13. return false;
  14. if ( parent.IsInherited(VehicleBattery) )
  15. {
  16. EntityAI battery_owner = EntityAI.Cast( parent.GetHierarchyParent() );
  17. // Check for Not player as if parent is not player, battery is already attached and should not receive new attachment
  18. if ( battery_owner && !battery_owner.IsInherited(PlayerBase) )
  19. {
  20. return false;
  21. }
  22. }
  23. return true;
  24. }
  25. void UpdateAllSelections()
  26. {
  27. HideAllSelections();
  28. EntityAI energy_source = GetCompEM().GetEnergySource();
  29. if (energy_source)
  30. {
  31. string ES_model = energy_source.GetModelName();
  32. EntityAI powered_device = GetCompEM().GetPluggedDevice();
  33. if (powered_device)
  34. {
  35. // Show metal wire selection
  36. string selection_wire = SEL_WIRE_PREFIX + ES_model + SEL_WIRE_SUFIX;
  37. ShowSelection(selection_wire);
  38. // Show plug selection
  39. string selection_plug = SEL_WIRE_PREFIX + ES_model + SEL_PLUG_SUFIX;
  40. selection_plug.ToLower();
  41. ShowSelection(selection_plug);
  42. // Set plug's texture
  43. int selection_index = GetHiddenSelectionIndex(selection_plug);
  44. string texture_path = powered_device.GetCompEM().GetCordTextureFile();
  45. SetObjectTexture( selection_index, texture_path );
  46. }
  47. else
  48. {
  49. // Show metal wire selection
  50. string selection_wire2 = SEL_WIRE_PREFIX + ES_model;
  51. ShowSelection(selection_wire2);
  52. }
  53. }
  54. else
  55. {
  56. ShowSelection(SEL_WIRE_ROLLED);
  57. }
  58. }
  59. // Event called on item when it is placed in the player(Man) inventory, passes the owner as a parameter
  60. override void OnInventoryEnter( Man player )
  61. {
  62. super.OnInventoryEnter( player );
  63. PlayerBase player_PB;
  64. Class.CastTo( player_PB, player );
  65. if ( player_PB.GetItemInHands() == this && GetCompEM().IsPlugged() )
  66. {
  67. return;
  68. }
  69. //Only unplug if we are "powering" something
  70. ItemBase powered_device = ItemBase.Cast( GetCompEM().GetPluggedDevice() );
  71. if ( powered_device )
  72. {
  73. GetCompEM().UnplugAllDevices();
  74. if ( !player_PB.IsPlacingLocal() )
  75. {
  76. GetCompEM().UnplugThis();
  77. }
  78. }
  79. }
  80. // Called when THIS is attached to battery
  81. override void OnIsPlugged(EntityAI source_device)
  82. {
  83. UpdateAllSelections();
  84. }
  85. // Called when THIS is detached from battery
  86. override void OnIsUnplugged(EntityAI last_energy_source)
  87. {
  88. UpdateAllSelections();
  89. GetCompEM().UnplugAllDevices();
  90. }
  91. // Called when some device is plugged into THIS
  92. override void OnOwnSocketTaken( EntityAI device )
  93. {
  94. UpdateAllSelections();
  95. }
  96. // Called when some device is unplugged from THIS
  97. override void OnOwnSocketReleased( EntityAI device )
  98. {
  99. UpdateAllSelections();
  100. }
  101. override void SetActions()
  102. {
  103. super.SetActions();
  104. AddAction(ActionRestrainTarget);
  105. AddAction(ActionRestrainSelf);
  106. AddAction(ActionAttachToConstruction);
  107. AddAction(ActionAttach);
  108. }
  109. }