7 bool m_bIsControlledInit =
false;
8 bool m_bIsDefaultInit =
false;
9 bool m_bIsControlledEnt =
false;
31 SCR_InventoryStorageManagerComponent invManager = SCR_InventoryStorageManagerComponent.Cast(character.FindComponent(SCR_InventoryStorageManagerComponent));
37 invManager.m_OnItemRemovedInvoker.Insert(
m_GadgetManager.OnItemRemoved);
49 if (m_bIsControlledInit)
75 SCR_InventoryStorageManagerComponent invManager = SCR_InventoryStorageManagerComponent.Cast(character.FindComponent(SCR_InventoryStorageManagerComponent));
79 invManager.m_OnItemRemovedInvoker.Remove(
m_GadgetManager.OnItemRemoved);
112 CleanupLocalInvokers(entity);
113 CleanupInvokers(entity);
129 static override bool DependsOn(
string className)
131 if (className ==
"SCR_CharacterInventoryStorageComponent")
140 int m_iRadiosFlags = EGadgetType.RADIO | EGadgetType.RADIO_BACKPACK;
142 ref ScriptInvoker<SCR_GadgetComponent> m_OnGadgetAdded =
new ScriptInvoker();
143 ref ScriptInvoker<SCR_GadgetComponent> m_OnGadgetRemoved =
new ScriptInvoker();
144 protected static ref ScriptInvoker<IEntity, SCR_GadgetManagerComponent> s_OnGadgetInitDone =
new ScriptInvoker();
146 protected bool m_bIsGadgetADS;
147 protected IEntity m_HeldGadget;
148 protected SCR_GadgetComponent m_HeldGadgetComponent;
149 protected SCR_GadgetComponent m_HiddenGadgetComponent;
150 protected InputManager m_InputManager;
151 protected SCR_InventoryStorageManagerComponent m_InventoryStorageMgr;
153 protected SCR_VONController m_VONController;
156 protected ref array<ref array <SCR_GadgetComponent>> m_aInventoryGadgetTypes = {};
157 protected ref map<EGadgetType, int> m_aGadgetArrayMap =
new map<EGadgetType, int>;
160 protected SCR_GadgetComponent m_LastHeldGadgetComponent;
168 static ScriptInvoker GetOnGadgetInitDoneInvoker()
170 return s_OnGadgetInitDone;
184 return gadgetManager;
190 IEntity GetHeldGadget()
194 else if (m_HiddenGadgetComponent)
195 return m_HiddenGadgetComponent.GetOwner();
203 bool GetIsGadgetADS()
205 return m_bIsGadgetADS;
211 SCR_GadgetComponent GetHeldGadgetComponent()
213 if (m_HeldGadgetComponent)
214 return m_HeldGadgetComponent;
216 return m_HiddenGadgetComponent;
225 void SetGadgetMode(IEntity gadget, EGadgetMode targetMode,
bool doFocus =
false)
230 SCR_GadgetComponent gadgetComp = SCR_GadgetComponent.Cast( gadget.FindComponent(SCR_GadgetComponent) );
234 if ( targetMode == EGadgetMode.IN_HAND && !gadgetComp.CanBeHeld() )
238 if (!CanChangeGadgetMode(gadget, gadgetComp, targetMode))
241 if ( targetMode == EGadgetMode.IN_HAND || gadgetComp.GetMode() == EGadgetMode.IN_HAND || gadgetComp == m_HiddenGadgetComponent )
246 if (targetMode == EGadgetMode.IN_HAND)
251 if (gadgetComp.CanBeRaised())
252 m_Controller.TakeGadgetInLeftHand(gadget, gadgetComp.GetAnimVariable(), doFocus);
254 m_Controller.TakeGadgetInLeftHand(gadget, gadgetComp.GetAnimVariable());
258 if (targetMode == EGadgetMode.ON_GROUND)
261 m_Controller.RemoveGadgetFromHand(gadgetComp == m_HiddenGadgetComponent);
263 if (gadgetComp.GetType() == EGadgetType.MAP)
266 if (gadgetComp.m_bFocused)
267 gadgetComp.ToggleFocused(
false);
271 gadgetComp.OnModeChanged(targetMode,
GetOwner());
278 static void SetGadgetModeStashed(SCR_GadgetComponent gadgetComp, EGadgetMode targetMode)
280 if (targetMode != EGadgetMode.ON_GROUND && targetMode != EGadgetMode.IN_STORAGE)
283 gadgetComp.OnModeChanged(targetMode,
null);
288 void RemoveHeldGadget()
291 SetGadgetMode(m_HeldGadget, EGadgetMode.IN_SLOT);
292 else if (m_HiddenGadgetComponent)
293 SetGadgetMode(m_HiddenGadgetComponent.GetOwner(), EGadgetMode.IN_SLOT);
298 protected void OnActivateHeldGadget()
300 if (m_HeldGadgetComponent)
301 m_HeldGadgetComponent.ActivateAction();
302 else if (m_HiddenGadgetComponent)
303 m_HiddenGadgetComponent.ActivateAction();
309 void ToggleHeldGadget(
bool state)
311 if (m_HeldGadgetComponent)
312 m_HeldGadgetComponent.ToggleActive(state);
313 else if (m_HiddenGadgetComponent)
314 m_HiddenGadgetComponent.ToggleActive(state);
321 array<SCR_GadgetComponent> GetGadgetsByType(EGadgetType
type)
323 int gadgetArrayID = m_aGadgetArrayMap.Get(
type);
324 if (gadgetArrayID != -1)
326 array<SCR_GadgetComponent> arr = {};
327 arr.Copy(m_aInventoryGadgetTypes[gadgetArrayID]);
339 IEntity GetGadgetByType(EGadgetType
type)
341 array<SCR_GadgetComponent> gadgets = GetGadgetsByType(
type);
342 if (!gadgets || gadgets.IsEmpty())
345 foreach (SCR_GadgetComponent comp : gadgets)
348 return comp.GetOwner();
350 Print(
GetOwner().ToString() +
" SCR_GadgetManager::GetGadgetsByType returned null entry in an array of " +
typename.EnumToString(EGadgetType,
type) +
" gadgets", LogLevel.ERROR);
360 IEntity GetQuickslotGadgetByType(EGadgetType
type)
362 SCR_CharacterInventoryStorageComponent charStorage = SCR_CharacterInventoryStorageComponent.Cast(
GetOwner().FindComponent(SCR_CharacterInventoryStorageComponent) );
363 array<IEntity> quickslotEntities = charStorage.GetQuickSlotItems();
365 foreach (IEntity entity : quickslotEntities )
370 SCR_GadgetComponent gadgetComp = SCR_GadgetComponent.Cast( entity.FindComponent(SCR_GadgetComponent) );
373 if (gadgetComp.GetType() ==
type)
385 bool IsGadgetOwned(SCR_GadgetComponent gadgetComp)
387 int gadgetArrayID = m_aGadgetArrayMap.Get(gadgetComp.GetType());
388 if (gadgetArrayID != 1)
390 return m_aInventoryGadgetTypes[gadgetArrayID].Contains(gadgetComp);
405 protected bool CanChangeGadgetMode(IEntity gadget, SCR_GadgetComponent gadgetComp, EGadgetMode targetMode)
407 if ( targetMode == EGadgetMode.IN_HAND || gadgetComp.GetMode() == EGadgetMode.IN_HAND )
409 CompartmentAccessComponent compAccess = CompartmentAccessComponent.Cast(
GetOwner().FindComponent(CompartmentAccessComponent));
410 if (compAccess && compAccess.IsInCompartment())
413 if (!invItemComponent)
420 if (targetMode == EGadgetMode.IN_HAND && !charModifData.CanBeEquippedInVehicle())
431 protected void UpdateHeldGadget(IEntity gadget)
434 if (gadget && gadget == m_HeldGadget)
437 m_bIsGadgetADS =
false;
442 if (m_HeldGadgetComponent && m_HeldGadgetComponent.m_bFocused)
443 OnGadgetFocusStateChanged(m_HeldGadget,
false);
446 m_LastHeldGadgetComponent = m_HeldGadgetComponent;
447 m_HeldGadgetComponent =
null;
450 IEntity hiddenGadget = inputCtx.GetWantedLefHandGadgetEntity();
453 SCR_GadgetComponent gadgetComp = SCR_GadgetComponent.Cast( hiddenGadget.FindComponent(SCR_GadgetComponent) );
457 m_HiddenGadgetComponent = gadgetComp;
459 ConnectToGadgetsManagerSystem();
463 m_HiddenGadgetComponent =
null;
468 SCR_GadgetComponent gadgetComp = SCR_GadgetComponent.Cast( gadget.FindComponent(SCR_GadgetComponent) );
472 m_HeldGadget = gadget;
473 m_HeldGadgetComponent = gadgetComp;
474 m_HiddenGadgetComponent =
null;
475 ConnectToGadgetsManagerSystem();
483 void HandleInput(IEntity gadget,
float inputVal)
485 EGadgetMode targetMode = EGadgetMode.IN_HAND;
486 bool doFocus = inputVal != 1;
488 if (m_HeldGadget == gadget)
489 targetMode = EGadgetMode.IN_STORAGE;
491 SCR_GadgetComponent gadgetComp = SCR_GadgetComponent.Cast( gadget.FindComponent(SCR_GadgetComponent) );
495 if (inputVal != 1 && gadgetComp.CanBeToggled())
496 gadgetComp.ToggleActive(!gadgetComp.IsToggledOn());
498 SetGadgetMode(gadget, targetMode, doFocus);
503 protected void ClearToggleState()
505 m_bIsGadgetADS =
false;
506 if (m_HeldGadgetComponent)
508 m_HeldGadgetComponent.ToggleFocused(
false);
521 void OnItemAdded(IEntity item, BaseInventoryStorageComponent storageOwner)
523 SCR_GadgetComponent gadgetComp = SCR_GadgetComponent.Cast(item.FindComponent(SCR_GadgetComponent));
527 EGadgetType
type = gadgetComp.GetType();
529 int gadgetArrayID = m_aGadgetArrayMap.Get(
type);
530 if (gadgetArrayID == -1)
533 m_aInventoryGadgetTypes[gadgetArrayID].Insert(gadgetComp);
536 EquipmentStorageComponent equipmentComp = EquipmentStorageComponent.Cast(storageOwner);
541 SetGadgetMode(item, EGadgetMode.IN_SLOT);
543 else if (
type == EGadgetType.RADIO_BACKPACK)
544 SetGadgetMode(item, EGadgetMode.IN_SLOT);
546 SetGadgetMode(item, EGadgetMode.IN_STORAGE);
548 m_OnGadgetAdded.Invoke(gadgetComp);
552 BaseRadioComponent radioComp = BaseRadioComponent.Cast(gadgetComp.GetOwner().FindComponent(BaseRadioComponent));
557 int count = radioComp.TransceiversCount();
558 for (
int i = 0; i < count; i++)
561 radioEntry.SetRadioEntry(radioComp.GetTransceiver(i), i + 1, gadgetComp);
571 void OnItemRemoved(IEntity item, BaseInventoryStorageComponent storageOwner)
573 SCR_GadgetComponent gadgetComp = SCR_GadgetComponent.Cast(item.FindComponent(SCR_GadgetComponent));
581 EGadgetType
type = gadgetComp.GetType();
583 int gadgetArrayID = m_aGadgetArrayMap.Get(gadgetComp.GetType());
584 if (gadgetArrayID == -1)
587 int gadgetPos = m_aInventoryGadgetTypes[gadgetArrayID].Find(gadgetComp);
589 m_aInventoryGadgetTypes[gadgetArrayID].Remove(gadgetPos);
591 m_OnGadgetRemoved.Invoke(gadgetComp);
595 array<ref SCR_VONEntry> entries = {};
598 for (
int i = entries.Count() - 1; i >= 0; --i)
601 if (entry && entry.GetGadget() == gadgetComp)
619 UnregisterVONEntries();
621 DisconnectFromGadgetsManagerSystem();
630 if (System.IsConsoleApp())
638 m_pInvokersState.CleanupLocalInvokers(
GetOwner());
640 UnregisterVONEntries();
641 DisconnectFromGadgetsManagerSystem();
645 m_pInvokersState.InitControlledInvokers(owner,
m_Controller);
647 RegisterVONEntries();
648 ConnectToGadgetsManagerSystem();
657 void OnGadgetStateChanged(IEntity gadget,
bool isInHand,
bool isOnGround)
659 SCR_GadgetComponent gadgetComp = SCR_GadgetComponent.Cast(gadget.FindComponent(SCR_GadgetComponent));
665 UpdateHeldGadget(gadget);
666 gadgetComp.OnModeChanged(EGadgetMode.IN_HAND,
GetOwner());
670 UpdateHeldGadget(
null);
674 gadgetComp.OnModeChanged(EGadgetMode.ON_GROUND,
GetOwner());
679 IEntity parent = gadget.GetParent();
686 if (storageSlot && storageSlot.GetOwner() == parent)
688 gadgetComp.OnModeChanged(EGadgetMode.IN_SLOT,
GetOwner());
694 gadgetComp.OnModeChanged(EGadgetMode.IN_STORAGE,
GetOwner());
702 void OnGadgetFocusStateChanged(IEntity gadget,
bool isFocused)
704 SCR_GadgetComponent gadgetComponent;
707 gadgetComponent = m_HeldGadgetComponent;
712 gadgetComponent = m_LastHeldGadgetComponent;
716 m_bIsGadgetADS = isFocused;
720 || ( !isFocused && gadgetComponent.GetType() == EGadgetType.MAP && gadgetComponent.GetMode() == EGadgetMode.IN_HAND ) )
723 gadgetComponent.ToggleFocused(isFocused);
728 void OnEditorOpened()
741 protected void OnGadgetInput(
float value, EActionTrigger reason)
743 EGadgetType input = GetGadgetInputAction();
748 IEntity gadget = GetQuickslotGadgetByType(input);
750 gadget = GetGadgetByType(input);
755 HandleInput(gadget, value);
762 protected void ActionFlashlightToggle(
float value, EActionTrigger reason)
765 IEntity flashlight = GetQuickslotGadgetByType(EGadgetType.FLASHLIGHT);
767 flashlight = GetGadgetByType(EGadgetType.FLASHLIGHT);
772 SCR_GadgetComponent gadgetComp = SCR_GadgetComponent.Cast(flashlight.FindComponent(SCR_GadgetComponent));
774 gadgetComp.ActivateAction();
779 protected void OnPauseMenu()
781 if (SCR_MapGadgetComponent.Cast(m_HeldGadgetComponent))
782 SetGadgetMode( m_HeldGadget, EGadgetMode.IN_STORAGE );
787 protected void OnGadgetADS()
789 SetGadgetADS(!m_bIsGadgetADS);
796 protected void OnGadgetADSHold(
float value, EActionTrigger reason)
798 SetGadgetADS(reason == EActionTrigger.DOWN);
804 void SetGadgetADS(
bool gadgetADS)
809 if (m_HeldGadgetComponent && m_HeldGadgetComponent.CanBeRaised())
813 else if (m_HiddenGadgetComponent && m_HiddenGadgetComponent.CanBeRaised())
825 protected void RegisterInitialGadgets(IEntity character)
827 array<IEntity> foundItems = {};
828 array<typename> componentsQuery = {SCR_GadgetComponent};
830 m_InventoryStorageMgr = SCR_InventoryStorageManagerComponent.Cast( character.FindComponent(SCR_InventoryStorageManagerComponent) );
831 if (m_InventoryStorageMgr)
832 m_InventoryStorageMgr.FindItemsWithComponents(foundItems, componentsQuery,
EStoragePurpose.PURPOSE_ANY);
834 int count = foundItems.Count();
835 for (
int i = 0; i < count; i++)
837 SCR_GadgetComponent gadgetComp = SCR_GadgetComponent.Cast( foundItems[i].FindComponent(SCR_GadgetComponent) );
838 int gadgetArrayID = m_aGadgetArrayMap.Get(gadgetComp.GetType());
839 if (gadgetArrayID == -1)
842 m_aInventoryGadgetTypes[gadgetArrayID].Insert(gadgetComp);
844 if (gadgetComp.GetMode() != EGadgetMode.ON_GROUND)
846 SetGadgetMode(foundItems[i], gadgetComp.GetMode());
855 if (storageSlot || gadgetComp.GetType() == EGadgetType.RADIO_BACKPACK)
857 SetGadgetMode(foundItems[i], EGadgetMode.IN_SLOT);
862 SetGadgetMode(foundItems[i], EGadgetMode.IN_STORAGE);
865 s_OnGadgetInitDone.Invoke(character,
this);
870 protected void RegisterVONEntries()
879 array<SCR_GadgetComponent> radiosArray = {};
880 radiosArray.Copy(GetGadgetsByType(EGadgetType.RADIO));
881 radiosArray.InsertAll(GetGadgetsByType(EGadgetType.RADIO_BACKPACK));
883 foreach (SCR_GadgetComponent radio : radiosArray)
885 BaseRadioComponent radioComp = BaseRadioComponent.Cast(radio.GetOwner().FindComponent(BaseRadioComponent));
886 int count = radioComp.TransceiversCount();
887 for (
int i = 0 ; i < count; ++i)
890 radioEntry.SetRadioEntry(radioComp.GetTransceiver(i), i + 1, radio);
898 protected void UnregisterVONEntries()
903 array<SCR_GadgetComponent> radiosArray = {};
904 radiosArray.Copy(GetGadgetsByType(EGadgetType.RADIO));
905 radiosArray.InsertAll(GetGadgetsByType(EGadgetType.RADIO_BACKPACK));
907 array<ref SCR_VONEntry> entries = {};
910 foreach (SCR_GadgetComponent radio : radiosArray)
912 for (
int i = entries.Count() - 1; i >= 0; --i)
915 if (entry && entry.GetGadget() == radio)
923 protected void RegisterInputs()
930 m_InputManager.AddActionListener(
"GadgetMap", EActionTrigger.DOWN, OnGadgetInput);
931 m_InputManager.AddActionListener(
"MapEscape", EActionTrigger.DOWN, OnPauseMenu);
932 m_InputManager.AddActionListener(
"GadgetCompass", EActionTrigger.DOWN, OnGadgetInput);
933 m_InputManager.AddActionListener(
"GadgetBinoculars", EActionTrigger.DOWN, OnGadgetInput);
934 m_InputManager.AddActionListener(
"GadgetFlashlight", EActionTrigger.DOWN, OnGadgetInput);
935 m_InputManager.AddActionListener(
"GadgetFlashlightToggle", EActionTrigger.DOWN, ActionFlashlightToggle);
936 m_InputManager.AddActionListener(
"GadgetWatch", EActionTrigger.DOWN, OnGadgetInput);
937 m_InputManager.AddActionListener(
"GadgetADS", EActionTrigger.DOWN, OnGadgetADS);
938 m_InputManager.AddActionListener(
"GadgetADSHold", EActionTrigger.DOWN, OnGadgetADSHold);
939 m_InputManager.AddActionListener(
"GadgetADSHold", EActionTrigger.UP, OnGadgetADSHold);
940 m_InputManager.AddActionListener(
"GadgetCancel", EActionTrigger.DOWN, RemoveHeldGadget);
941 m_InputManager.AddActionListener(
"GadgetActivate", EActionTrigger.DOWN, OnActivateHeldGadget);
946 protected void UnregisterInputs()
952 m_InputManager.RemoveActionListener(
"GadgetMap", EActionTrigger.DOWN, OnGadgetInput);
953 m_InputManager.RemoveActionListener(
"MapEscape", EActionTrigger.DOWN, OnPauseMenu);
954 m_InputManager.RemoveActionListener(
"GadgetCompass", EActionTrigger.DOWN, OnGadgetInput);
955 m_InputManager.RemoveActionListener(
"GadgetBinoculars", EActionTrigger.DOWN, OnGadgetInput);
956 m_InputManager.RemoveActionListener(
"GadgetFlashlight", EActionTrigger.DOWN, OnGadgetInput);
957 m_InputManager.RemoveActionListener(
"GadgetFlashlightToggle", EActionTrigger.DOWN, ActionFlashlightToggle);
958 m_InputManager.RemoveActionListener(
"GadgetWatch", EActionTrigger.DOWN, OnGadgetInput);
959 m_InputManager.RemoveActionListener(
"GadgetADS", EActionTrigger.DOWN, OnGadgetADS);
960 m_InputManager.RemoveActionListener(
"GadgetADSHold", EActionTrigger.DOWN, OnGadgetADSHold);
961 m_InputManager.RemoveActionListener(
"GadgetADSHold", EActionTrigger.UP, OnGadgetADSHold);
962 m_InputManager.RemoveActionListener(
"GadgetCancel", EActionTrigger.DOWN, RemoveHeldGadget);
963 m_InputManager.RemoveActionListener(
"GadgetActivate", EActionTrigger.DOWN, OnActivateHeldGadget);
969 protected EGadgetType GetGadgetInputAction()
972 return EGadgetType.MAP;
975 return EGadgetType.COMPASS;
978 return EGadgetType.BINOCULARS;
981 return EGadgetType.FLASHLIGHT;
984 return EGadgetType.WRISTWATCH;
992 protected void InitComponent(IEntity owner)
1003 m_pInvokersState.InitControlledInvokers(owner,
m_Controller);
1006 if (m_pInvokersState.IsInit())
1008 RegisterInitialGadgets(owner);
1010 if (m_pInvokersState.m_bIsControlledEnt)
1013 RegisterVONEntries();
1016 DisconnectFromGadgetsManagerSystem();
1028 void AskToggleGadget(SCR_GadgetComponent gadgetComp,
bool state)
1030 RplId
id = Replication.FindId(gadgetComp);
1033 Rpc(RPC_AskToggleGadget,
id, state);
1040 [
RplRpc(RplChannel.Reliable, RplRcver.Server)]
1041 protected void RPC_AskToggleGadget(RplId
id,
bool state)
1045 SCR_GadgetComponent gadgetComp = SCR_GadgetComponent.Cast(Replication.FindItem(
id));
1049 gadgetComp.OnToggleActive(state);
1050 Rpc(RPC_DoToggleGadget,
id, state);
1057 [
RplRpc(RplChannel.Reliable, RplRcver.Broadcast, RplCondition.NoOwner)]
1058 protected void RPC_DoToggleGadget(RplId
id,
bool state)
1062 SCR_GadgetComponent gadgetComp = SCR_GadgetComponent.Cast(Replication.FindItem(
id));
1066 gadgetComp.OnToggleActive(state);
1070 protected void ConnectToGadgetsManagerSystem()
1072 World world =
GetOwner().GetWorld();
1074 if (!gadgetManagersSystem)
1077 gadgetManagersSystem.Register(
this);
1081 protected void DisconnectFromGadgetsManagerSystem()
1083 World world =
GetOwner().GetWorld();
1085 if (!gadgetManagersSystem)
1088 gadgetManagersSystem.Unregister(
this);
1092 override void OnDelete(IEntity owner)
1094 DisconnectFromGadgetsManagerSystem();
1096 super.OnDelete(owner);
1099 #ifndef DISABLE_GADGETS
1103 void Update(
float timeSlice)
1105 if (!m_pInvokersState.IsInit())
1109 if (!
m_InputManager || !(m_HeldGadgetComponent || m_HiddenGadgetComponent))
1111 DisconnectFromGadgetsManagerSystem();
1115 SCR_GadgetComponent gadgetComp;
1116 if (m_HeldGadgetComponent)
1117 gadgetComp = m_HeldGadgetComponent;
1119 gadgetComp = m_HiddenGadgetComponent;
1123 if (gadgetComp.CanBeToggled())
1126 if (gadgetComp.IsUsingADSControls())
1129 SCR_ChimeraCharacter character = SCR_ChimeraCharacter.Cast(
GetOwner());
1130 if (character && character.IsDriving(1))
1133 SetGadgetADS(
false);
1141 if (gadgetComp.GetType() == EGadgetType.MAP)
1146 override void OnPostInit(IEntity owner)
1148 if ( g_Game.InPlayMode() )
1149 ConnectToGadgetsManagerSystem();
1152 typename gadgetTypes = EGadgetType;
1153 int count = gadgetTypes.GetVariableCount();
1154 array <SCR_GadgetComponent> gadgets;
1155 for (
int i = 0; i < count; i++ )
1158 m_aInventoryGadgetTypes.Insert(gadgets);
1159 m_aGadgetArrayMap.Insert( Math.Pow(2, i), i);