ACE3/addons/common/functions/fnc_switchPersistentLaser.sqf
johnb432 7838dea543
Common - Improve persistent lights/lasers (#10118)
* Improve persistent lasers

* Update addons/common/functions/fnc_switchPersistentLaser.sqf

Co-authored-by: PabstMirror <pabstmirror@gmail.com>

* Fix inventory changes toggling laser, removed alignment code

* Update addons/common/functions/fnc_switchPersistentLaser.sqf

Co-authored-by: PabstMirror <pabstmirror@gmail.com>

* Added API for setting weapon lights/lasers

* Fix undefined variables

---------

Co-authored-by: PabstMirror <pabstmirror@gmail.com>
2024-08-08 01:01:39 -07:00

102 lines
3.1 KiB
Plaintext

#include "..\script_component.hpp"
/*
* Author: Dystopian, johnb43
* Controls persistent laser state.
*
* Arguments:
* 0: Enabled <BOOL>
*
* Return Value:
* None
*
* Example:
* true call ace_common_fnc_switchPersistentLaser
*
* Public: No
*/
params ["_enabled"];
if (!hasInterface) exitwith {};
// Reset state
{
ACE_player setVariable [QGVAR(laserEnabled_) + str _x, nil];
} forEach [0, 1, 2];
if (!_enabled) exitWith {
if (isNil QGVAR(laserKeyDownEH)) exitWith {};
removeUserActionEventHandler ["headlights", "Activate", GVAR(laserKeyDownEH)];
["loadout", GVAR(laserLoadoutEH)] call CBA_fnc_removePlayerEventHandler;
["turret", GVAR(laserTurretEH)] call CBA_fnc_removePlayerEventHandler;
["vehicle", GVAR(laserVehicleEH)] call CBA_fnc_removePlayerEventHandler;
["weapon", GVAR(laserWeaponEH)] call CBA_fnc_removePlayerEventHandler;
GVAR(laserKeyDownEH) = nil;
GVAR(laserLoadoutEH) = nil;
GVAR(laserTurretEH) = nil;
GVAR(laserVehicleEH) = nil;
GVAR(laserWeaponEH) = nil;
};
private _fnc_getLightLaserState = {
private _currentWeapon = currentWeapon ACE_player;
if (_currentWeapon == "") exitWith {};
// Ignore in vehicle except FFV
if !(ACE_player call CBA_fnc_canUseWeapon) exitWith {};
private _weaponIndex = [ACE_player, _currentWeapon] call FUNC(getWeaponIndex);
if (_weaponIndex == -1) exitWith {};
// Light/laser state only changes in the next frame
[{
ACE_player setVariable [
QGVAR(laserEnabled_) + str (_this select 1),
ACE_player isIRLaserOn (_this select 0) || {ACE_player isFlashlightOn (_this select 0)}
];
}, [_currentWeapon, _weaponIndex]] call CBA_fnc_execNextFrame;
};
// Get current weapon light/laser state
call _fnc_getLightLaserState;
// Update state every time it's changed
GVAR(laserKeyDownEH) = addUserActionEventHandler ["headlights", "Activate", _fnc_getLightLaserState];
// Dropping weapons turns off lights/lasers
GVAR(lastWeapons) = [primaryWeapon ACE_player, handgunWeapon ACE_player, secondaryWeapon ACE_player];
// Monitor weapon addition/removal here
GVAR(laserLoadoutEH) = ["loadout", {
params ["_unit"];
private _weapons = [primaryWeapon _unit, handgunWeapon _unit, secondaryWeapon _unit];
if (_weapons isEqualTo GVAR(lastWeapons)) exitWith {};
GVAR(lastWeapons) = _weapons;
[
_unit,
_unit getVariable [QGVAR(laserEnabled_) + str ([_unit, currentWeapon _unit] call FUNC(getWeaponIndex)), false]
] call FUNC(setWeaponLightLaserState);
}] call CBA_fnc_addPlayerEventHandler;
private _fnc_switchPersistentLaserEH = {
params ["_unit"];
[
_unit,
_unit getVariable [QGVAR(laserEnabled_) + str ([_unit, currentWeapon _unit] call FUNC(getWeaponIndex)), false]
] call FUNC(setWeaponLightLaserState);
};
GVAR(laserTurretEH) = ["turret", _fnc_switchPersistentLaserEH] call CBA_fnc_addPlayerEventHandler;
GVAR(laserVehicleEH) = ["vehicle", _fnc_switchPersistentLaserEH] call CBA_fnc_addPlayerEventHandler;
GVAR(laserWeaponEH) = ["weapon", _fnc_switchPersistentLaserEH] call CBA_fnc_addPlayerEventHandler;