102 lines
3.2 KiB
C
102 lines
3.2 KiB
C
[ComponentEditorProps(category: "GameScripted/Character", description: "Erlaubt automatisches Laufen und Gehen")]
|
|
class SCR_AutorunComponentClass: ScriptComponentClass
|
|
{
|
|
}
|
|
|
|
class SCR_AutorunComponent: ScriptComponent
|
|
{
|
|
protected bool m_bIsAutoSprintEnabled = false;
|
|
protected bool m_bIsAutoWalkEnabled = false;
|
|
protected bool m_bWaitForKeyRelease = false;
|
|
|
|
protected SCR_CharacterControllerComponent m_CharacterController;
|
|
protected InputManager m_InputManager;
|
|
|
|
// --- NEU: Hilfsfunktion für die Sicherung (damit wir den Code nicht doppelt schreiben müssen) ---
|
|
protected void CheckForExistingInput(bool isEnabled)
|
|
{
|
|
if (isEnabled && m_InputManager.GetActionValue("CharacterForward") > 0.5)
|
|
m_bWaitForKeyRelease = true;
|
|
else
|
|
m_bWaitForKeyRelease = false;
|
|
}
|
|
|
|
// --- Aktion für SPRINTEN (Rennen) ---
|
|
protected void OnAutorunPressed()
|
|
{
|
|
m_bIsAutoSprintEnabled = !m_bIsAutoSprintEnabled;
|
|
if (m_bIsAutoSprintEnabled) m_bIsAutoWalkEnabled = false; // Gehen ausschalten, falls an
|
|
|
|
CheckForExistingInput(m_bIsAutoSprintEnabled);
|
|
}
|
|
|
|
// --- Aktion für GEHEN/JOGGEN (Normales Laufen) ---
|
|
protected void OnAutowalkPressed()
|
|
{
|
|
m_bIsAutoWalkEnabled = !m_bIsAutoWalkEnabled;
|
|
if (m_bIsAutoWalkEnabled) m_bIsAutoSprintEnabled = false; // Sprint ausschalten, falls an
|
|
|
|
CheckForExistingInput(m_bIsAutoWalkEnabled);
|
|
}
|
|
|
|
protected void OnLifeStateChangedCallback()
|
|
{
|
|
m_bIsAutoSprintEnabled = false;
|
|
m_bIsAutoWalkEnabled = false;
|
|
}
|
|
|
|
protected void OnControlledByPlayerCallback(IEntity owner, bool controlled)
|
|
{
|
|
m_bIsAutoSprintEnabled = false;
|
|
m_bIsAutoWalkEnabled = false;
|
|
}
|
|
|
|
protected void OnPrepareControlsCallback(IEntity owner, ActionManager am, float dt, bool player)
|
|
{
|
|
// Abbrechen, wenn weder Sprint noch Gehen aktiv sind
|
|
if (!m_bIsAutoSprintEnabled && !m_bIsAutoWalkEnabled)
|
|
return;
|
|
|
|
float forwardInput = m_InputManager.GetActionValue("CharacterForward");
|
|
float rightInput = m_InputManager.GetActionValue("CharacterRight");
|
|
|
|
if (m_bWaitForKeyRelease && forwardInput < 0.5)
|
|
{
|
|
m_bWaitForKeyRelease = false;
|
|
}
|
|
|
|
// Autorun abbrechen, wenn manuell eingegriffen wird
|
|
if (!m_InputManager.IsContextActive("CharacterMovementContext") ||
|
|
am.GetActionTriggered("GadgetMap") ||
|
|
Math.AbsFloat(rightInput) >= 0.75 ||
|
|
(!m_bWaitForKeyRelease && Math.AbsFloat(forwardInput) >= 0.75))
|
|
{
|
|
m_bIsAutoSprintEnabled = false;
|
|
m_bIsAutoWalkEnabled = false;
|
|
return;
|
|
}
|
|
|
|
// Bewegungswerte setzen: "Forward" (W-Taste) ist bei beiden aktiv
|
|
m_InputManager.SetActionValue("CharacterForward", 1.0);
|
|
|
|
// "Sprint" (Shift-Taste) kommt nur beim Autorun dazu
|
|
if (m_bIsAutoSprintEnabled)
|
|
{
|
|
m_InputManager.SetActionValue("CharacterSprint", 1.0);
|
|
}
|
|
}
|
|
|
|
override void OnPostInit(IEntity owner)
|
|
{
|
|
m_CharacterController = SCR_CharacterControllerComponent.Cast(owner.FindComponent(SCR_CharacterControllerComponent));
|
|
if (!m_CharacterController)
|
|
return;
|
|
|
|
m_InputManager = GetGame().GetInputManager();
|
|
m_CharacterController.m_OnPrepareControls.Insert(OnPrepareControlsCallback);
|
|
|
|
// Beide Actions registrieren
|
|
m_InputManager.AddActionListener("SCR_Autorun", EActionTrigger.DOWN, OnAutorunPressed);
|
|
m_InputManager.AddActionListener("SCR_Autowalk", EActionTrigger.DOWN, OnAutowalkPressed); // <--- NEU
|
|
}
|
|
} |