33 static PlayerController s_pLocalPlayerController;
34 protected static const float WALK_SPEED = 0.5;
35 protected static const float FOCUS_ACTIVATION = 0.1;
36 protected static const float FOCUS_DEACTIVATION = 0.05;
37 protected static const float FOCUS_TIMEOUT = 0.3;
38 protected static const float FOCUS_TOLERANCE = 0.005;
39 protected static float s_fADSFocus = 0.7;
40 protected static float s_fFocusTimeout;
41 protected static float s_fFocusAnalogue;
42 protected static bool s_bWasADS;
44 protected CharacterControllerComponent m_CharacterController;
45 protected bool m_bIsLocalPlayerController;
46 protected bool m_bIsPaused;
48 protected bool m_bGadgetFocus;
49 protected bool m_bFocusToggle;
50 protected float m_fCharacterSpeed;
53 [
RplProp(onRplName:
"OnRplMainEntityFromID")]
54 protected RplId m_MainEntityID;
55 protected IEntity m_MainEntity;
58 protected bool m_bIsPossessing;
71 return m_OnOwnershipChangedInvoker;
78 protected override void OnOwnershipChanged(
bool changing,
bool becameOwner)
80 super.OnOwnershipChanged(changing, becameOwner);
81 m_OnOwnershipChangedInvoker.Invoke(changing, becameOwner);
84 override void OnControlledEntityChanged(IEntity from, IEntity to)
87 m_OnControlledEntityChanged.Invoke(from, to);
93 void SetGameUserSettings()
96 if (!controlledEntity)
100 m_CharacterController = CharacterControllerComponent.Cast(controlledEntity.FindComponent(CharacterControllerComponent));
106 BaseContainer aimSensitivitySettings =
GetGame().GetGameUserSettings().GetModule(
"SCR_AimSensitivitySettings");
108 if (aimSensitivitySettings)
110 float aimSensitivityMouse;
111 float aimSensitivityGamepad;
114 if (aimSensitivitySettings.Get(
"m_fMouseSensitivity", aimSensitivityMouse) &&
115 aimSensitivitySettings.Get(
"m_fStickSensitivity", aimSensitivityGamepad) &&
116 aimSensitivitySettings.Get(
"m_fAimADS", aimMultipADS))
118 m_CharacterController.SetAimingSensitivity(aimSensitivityMouse, aimSensitivityGamepad, aimMultipADS);
124 BaseContainer gameplaySettings =
GetGame().GetGameUserSettings().GetModule(
"SCR_GameplaySettings");
126 if (gameplaySettings)
128 bool stickyADS =
true;
129 if (gameplaySettings.Get(
"m_bStickyADS", stickyADS))
132 bool stickyGadgets =
true;
133 if (gameplaySettings.Get(
"m_bStickyGadgets", stickyGadgets))
136 bool mouseControlAircraft =
true;
137 if (gameplaySettings.Get(
"m_bMouseControlAircraft", mouseControlAircraft))
143 if (gameplaySettings.Get(
"m_eDrivingAssistance", drivingAssistance))
144 VehicleControllerComponent.SetDrivingAssistanceMode(drivingAssistance);
148 if (gameplaySettings.Get(
"m_eDrivingAssistance", drivingAssistance))
149 VehicleControllerComponent_SA.SetDrivingAssistanceMode(drivingAssistance);
154 BaseContainer fovSettings =
GetGame().GetGameUserSettings().GetModule(
"SCR_FieldOfViewSettings");
157 float focusInADS = 0.5;
158 if (fovSettings.Get(
"m_fFocusInADS", focusInADS))
159 s_fADSFocus = focusInADS;
169 void SetPossessedEntity(IEntity entity)
171 if (!m_bIsPossessing)
175 m_OnBeforePossess.Invoke(entity);
177 m_bIsPossessing =
true;
181 m_MainEntityID = RplId.Invalid();
182 if (controlledEntity)
184 RplComponent rpl = RplComponent.Cast(controlledEntity.FindComponent(RplComponent));
187 rpl.GiveExt(RplIdentity.Local(),
false);
188 m_MainEntityID = rpl.Id();
192 OnRplMainEntityFromID();
193 Replication.BumpMe();
196 SCR_PossessingManagerComponent possessingManager = SCR_PossessingManagerComponent.GetInstance();
197 if (possessingManager)
198 possessingManager.SetMainEntity(
GetPlayerId(), entity, controlledEntity, m_bIsPossessing);
201 RplComponent rpl = RplComponent.Cast(entity.FindComponent(RplComponent));
204 SetAIActivation(entity,
false);
206 m_OnPossessed.Invoke(entity);
214 m_bIsPossessing =
false;
217 m_MainEntityID = RplId.Invalid();
218 OnRplMainEntityFromID();
219 Replication.BumpMe();
221 SCR_PossessingManagerComponent possessingManager = SCR_PossessingManagerComponent.GetInstance();
222 if (possessingManager)
227 if (controlledEntity)
229 RplComponent rpl = RplComponent.Cast(controlledEntity.FindComponent(RplComponent));
231 rpl.GiveExt(RplIdentity.Local(),
false);
233 SetAIActivation(controlledEntity,
true);
239 RplComponent rpl = RplComponent.Cast(m_MainEntity.FindComponent(RplComponent));
244 m_OnPossessed.Invoke(m_MainEntity);
248 Print(
string.Format(
"Error when switching control back to m_MainEntity = %1!", m_MainEntity), LogLevel.WARNING);
253 SetPossessedEntity(
null);
254 SetPossessedEntity(entity);
255 m_OnPossessed.Invoke(entity);
265 void SetInitialMainEntity(notnull IEntity entity)
267 RplComponent rpl = RplComponent.Cast(entity.FindComponent(RplComponent));
271 m_MainEntityID = rpl.Id();
272 OnRplMainEntityFromID();
273 Replication.BumpMe();
275 SCR_PossessingManagerComponent possessingManager = SCR_PossessingManagerComponent.GetInstance();
276 if (possessingManager)
280 SetAIActivation(entity,
false);
283 m_OnPossessed.Invoke(entity);
293 return m_bIsPossessing;
302 IEntity GetMainEntity()
310 protected void OnRplMainEntityFromID()
313 RplComponent rpl = RplComponent.Cast(Replication.FindItem(m_MainEntityID));
315 m_MainEntity = rpl.GetEntity();
318 protected void SetAIActivation(IEntity entity,
bool activate)
323 AIControlComponent aiControl = AIControlComponent.Cast(entity.FindComponent(AIControlComponent));
328 aiControl.ActivateAI();
330 aiControl.DeactivateAI();
335 static int GetLocalPlayerId()
337 PlayerController pPlayerController =
GetGame().GetPlayerController();
338 if (!pPlayerController)
341 return pPlayerController.GetPlayerId();
349 static IEntity GetLocalControlledEntity()
351 PlayerController pPlayerController =
GetGame().GetPlayerController();
352 if (pPlayerController)
353 return pPlayerController.GetControlledEntity();
363 static IEntity GetLocalMainEntity()
366 if (playerController)
367 return playerController.GetMainEntity();
377 static Faction GetLocalControlledEntityFaction()
379 PlayerController playerController =
GetGame().GetPlayerController();
380 if (!playerController)
383 IEntity controlledEntity = playerController.GetControlledEntity();
384 if (!controlledEntity)
387 FactionAffiliationComponent factionAffiliation = FactionAffiliationComponent.Cast(controlledEntity.FindComponent(FactionAffiliationComponent));
388 if (factionAffiliation)
389 return factionAffiliation.GetAffiliatedFaction();
399 static Faction GetLocalMainEntityFaction()
402 if (!playerController)
405 IEntity controlledEntity = playerController.GetMainEntity();
406 if (!controlledEntity)
409 FactionAffiliationComponent factionAffiliation = FactionAffiliationComponent.Cast(controlledEntity.FindComponent(FactionAffiliationComponent));
410 if (factionAffiliation)
411 return factionAffiliation.GetAffiliatedFaction();
417 override void OnDestroyed(notnull
Instigator killer)
419 super.OnDestroyed(killer);
420 IEntity killerEntity = killer.GetInstigatorEntity();
425 override void OnUpdate(
float timeSlice)
427 if (!s_pLocalPlayerController)
428 UpdateLocalPlayerController();
430 if (m_bIsLocalPlayerController)
438 protected void UpdateLocalPlayerController()
440 m_bIsLocalPlayerController =
this ==
GetGame().GetPlayerController();
441 if (!m_bIsLocalPlayerController)
444 s_pLocalPlayerController =
this;
445 InputManager inputManager =
GetGame().GetInputManager();
449 inputManager.AddActionListener(
"WeaponChangeMagnification", EActionTrigger.VALUE, ChangeMagnification);
450 inputManager.AddActionListener(
"CharacterWalk", EActionTrigger.DOWN, OnWalk);
451 inputManager.AddActionListener(
"CharacterWalk", EActionTrigger.UP, OnEndWalk);
452 inputManager.AddActionListener(
"FocusToggle", EActionTrigger.DOWN, ActionFocusToggle);
453 inputManager.AddActionListener(
"FocusToggleUnarmed", EActionTrigger.DOWN, ActionFocusToggleUnarmed);
454 inputManager.AddActionListener(
"Inventory", EActionTrigger.DOWN, ActionOpenInventory );
455 inputManager.AddActionListener(
"TacticalPing", EActionTrigger.DOWN, ActionGesturePing );
456 inputManager.AddActionListener(
"TacticalPingHold", EActionTrigger.DOWN, ActionGesturePingHold );
457 inputManager.AddActionListener(
"TacticalPingHold", EActionTrigger.UP, ActionGesturePingHold );
458 inputManager.AddActionListener(
"WeaponSwitchOptics", EActionTrigger.UP, ChangeWeaponOptics);
462 protected void UpdateControls()
464 bool disableControls =
GetGame().GetMenuManager().IsAnyMenuOpen();
465 if (m_bIsPaused != disableControls)
467 m_bIsPaused = disableControls;
468 SetDisableControls(disableControls);
472 protected void UpdateUI()
477 CharacterAnimationComponent animComp =
char.GetAnimationComponent();
484 int lrExitState = ladderCMD.CanExitLR();
485 if (lrExitState & 0x1)
487 Print(
"Can exit right");
489 if (lrExitState & 0x2)
491 Print(
"Can exit left");
495 protected void ChangeMagnification(
float value)
498 if (characterController)
499 characterController.SetNextSightsFOVInfo(value);
502 protected void ChangeWeaponOptics()
505 if (characterController)
506 characterController.SetNextSights();
521 private void SetDisableControls(
bool value)
524 if (!characterController)
527 characterController.SetDisableViewControls(value);
528 characterController.SetDisableWeaponControls(value);
529 characterController.SetDisableMovementControls(value)
538 float GetFocusValue(
float adsProgress = 0,
float dt = -1)
547 focus = s_fADSFocus * Math.Min(adsProgress, 1);
549 InputManager inputManager =
GetGame().GetInputManager();
552 bool inputDigital = inputManager.GetActionTriggered(
"Focus");
553 if (inputDigital && m_bFocusToggle)
554 m_bFocusToggle =
false;
558 if (m_bFocusToggle && character)
560 if (character.IsInVehicle())
564 m_bFocusToggle =
false;
568 m_bFocusToggle =
false;
574 m_bFocusToggle =
false;
578 m_bFocusToggle =
false;
584 if (!inputManager.IsContextActive(
"CarContext") && !inputManager.IsContextActive(
"HelicopterContext"))
587 float focusAnalogue = Math.Sqrt(inputManager.GetActionValue(
"FocusAnalog"));
590 if (focusAnalogue < FOCUS_DEACTIVATION)
591 s_fFocusAnalogue = 0;
592 else if (!
float.AlmostEqual(s_fFocusAnalogue, focusAnalogue, FOCUS_TOLERANCE))
593 s_fFocusAnalogue = focusAnalogue;
595 inputAnalogue = s_fFocusAnalogue;
605 if (!isADS && character)
607 CompartmentAccessComponent compartmentAccess = character.GetCompartmentAccessComponent();
609 BaseCompartmentSlot compartment;
610 if (compartmentAccess)
611 compartment = compartmentAccess.GetCompartment();
617 if (turretController)
618 isADS = turretController.IsWeaponADS();
623 if (inputAnalogue < FOCUS_DEACTIVATION)
624 s_fFocusTimeout = FOCUS_TIMEOUT;
625 else if (s_bWasADS != isADS && s_fFocusTimeout > 0)
626 s_fFocusTimeout = FOCUS_TIMEOUT;
627 else if (inputAnalogue < FOCUS_ACTIVATION && s_fFocusTimeout > 0)
628 s_fFocusTimeout = FOCUS_TIMEOUT;
629 else if (s_fFocusTimeout > dt)
630 s_fFocusTimeout -= dt;
635 if (m_bFocusToggle && s_fFocusTimeout == 0)
636 m_bFocusToggle =
false;
642 if (m_bFocusToggle || inputDigital)
644 else if (s_fFocusTimeout == 0)
645 input = inputAnalogue;
648 focus = Math.Max(focus, input);
651 focus = Math.Clamp(focus, 0, 1);
657 void SetGadgetFocus(
bool gadgetFocus)
659 m_bGadgetFocus = gadgetFocus;
664 bool GetGadgetFocus()
666 return m_bGadgetFocus;
670 void ActionFocusToggle(
float value = 0.0, EActionTrigger reason = 0)
677 if (character && !character.IsInVehicle())
688 m_bFocusToggle = !m_bFocusToggle;
692 void ActionFocusToggleUnarmed(
float value = 0.0, EActionTrigger reason = 0)
699 if (character && character.IsInVehicle())
716 m_bFocusToggle = !m_bFocusToggle;
741 void ActionOpenInventory()
743 IEntity entity = s_pLocalPlayerController.GetControlledEntity();
747 SCR_InventoryStorageManagerComponent inventory = SCR_InventoryStorageManagerComponent.Cast(entity.FindComponent(SCR_InventoryStorageManagerComponent));
749 inventory.Action_OpenInventory();
753 void ActionGesturePing(
float value = 0.0, EActionTrigger reason = 0)
763 void ActionGesturePingHold(
float value = 0.0, EActionTrigger reason = 0)
769 if (reason == EActionTrigger.DOWN)
772 }
else if ( reason == EActionTrigger.UP)
779 override void EOnInit(IEntity owner)
781 super.EOnInit(owner);
784 SCR_InteractionHandlerComponent handler = SCR_InteractionHandlerComponent.Cast(owner.FindComponent(SCR_InteractionHandlerComponent));
788 m_OnControlledEntityChanged.Insert(handler.OnControlledEntityChanged);