Advertisement
Mudbill

HPL2 Menu Script

Dec 8th, 2022
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.23 KB | Gaming | 0 0
  1. void BeginMenu(string asMenuArea, int alInputFreq, bool abCanRepeat, float afRepeatDelay, string asInputCallback) {
  2.     SetLocalVarInt("_MenuInputFreq", alInputFreq);
  3.     SetLocalVarBool("_MenuCanRepeat", abCanRepeat);
  4.     SetLocalVarFloat("_MenuRepeatDelay", afRepeatDelay);
  5.     SetLocalVarString("_MenuInputCallback", asInputCallback);
  6.     SetLocalVarBool("_FirstLoop", true);
  7.     SetLocalVarBool("_MenuCanClick", true);
  8.  
  9.     TeleportPlayer(asMenuArea);
  10.     SetPlayerCrouching(true);
  11.     ShowPlayerCrossHairIcons(false);
  12.     SetPlayerJumpDisabled(true);
  13.     SetPlayerCrouchDisabled(true);
  14.     SetInDarknessEffectsActive(false);
  15.     SetPlayerLookSpeedMul(0.0f);
  16.     SetPlayerMoveSpeedMul(0.05f);
  17.     SetInventoryDisabled(true);
  18.     GiveItemFromFile("lantern", "lantern.ent");
  19.     SetLanternLitCallback("_MenuConfirm");
  20.     SetLocalVarInt("PPosX", GetPlayerPosX());
  21.     SetLocalVarInt("PPosY", GetPlayerPosY());
  22.     SetLocalVarInt("PPosZ", GetPlayerPosZ());
  23.  
  24.     AddEntityCollideCallback("Player", "AreaS", "_MenuHandler", true, 1);
  25.     AddEntityCollideCallback("Player", "AreaA", "_MenuHandler", true, 1);
  26.     AddEntityCollideCallback("Player", "AreaW", "_MenuHandler", true, 1);
  27.     AddEntityCollideCallback("Player", "AreaD", "_MenuHandler", true, 1);
  28.     SetLocalVarBool("_MenuHasCallback_AreaW", true);
  29.     SetLocalVarBool("_MenuHasCallback_AreaA", true);
  30.     SetLocalVarBool("_MenuHasCallback_AreaS", true);
  31.     SetLocalVarBool("_MenuHasCallback_AreaD", true);
  32.     SetEntityPlayerInteractCallback("AreaW", "_MenuConfirm", false);
  33. }
  34. void EndMenu() {
  35.     SetPlayerCrouching(false);
  36.     ShowPlayerCrossHairIcons(true);
  37.     StopPlayerLookAt();
  38.     MovePlayerHeadPos(0.0f, 0.0f, 0.0f, 10.0f, 1.0f);
  39.     SetPlayerMoveSpeedMul(1.0f);
  40.     SetPlayerJumpDisabled(false);
  41.     SetPlayerCrouchDisabled(false);
  42.     SetInDarknessEffectsActive(true);
  43.     SetPlayerLookSpeedMul(1.0f);
  44.     SetInventoryDisabled(false);
  45.     SetLanternLitCallback("");
  46.     RemoveItem("lantern");
  47.     SetPlayerActive(true);
  48. }
  49. void _MenuHandler(string &in asParent, string &in asChild, int alState) {
  50.     //setup data
  51.     SetLocalVarBool("_MenuHasCallback_"+asChild, false);
  52.     int inputFreq = GetLocalVarInt("_MenuInputFreq");
  53.     bool canRepeat = GetLocalVarBool("_MenuCanRepeat");
  54.     float repeatDelay = GetLocalVarFloat("_MenuRepeatDelay");
  55.     string inputCallback = GetLocalVarString("_MenuInputCallback");
  56.     float resetTimeout = (1.0f/inputFreq)*2;
  57.     float repeatDelayTimeLeft = GetTimerTimeLeft("repeat_delay");
  58.     string inputKey = StringSub(asChild, 4, 1); //Gets the input key (WASD)
  59.        
  60.     if(canRepeat)
  61.         if(repeatDelayTimeLeft <= 0)
  62.             AddTimer(inputKey, 0.0f, inputCallback);
  63.    
  64.     //Calculate how many inputs are allowed per second.
  65.     if(GetTimerTimeLeft("input_freq") <= 0)
  66.         AddTimer("input_freq", 1.0f/inputFreq, "_MenuInputFrequency");
  67.  
  68.     //Calculate the timer for detecting when an input is no longer given.
  69.     if(GetTimerTimeLeft("reset_timeout") > 0) {
  70.         RemoveTimer("reset_timeout");
  71.         SetLocalVarBool("_FirstLoop", false);
  72.     } else SetLocalVarBool("_FirstLoop", true);
  73.    
  74.     AddTimer("reset_timeout", resetTimeout, "_MenuResetTimeout");
  75.  
  76.     //Calculate delay before a repeated input is accepted.
  77.     if(GetLocalVarBool("_FirstLoop") && repeatDelayTimeLeft <= 0) {
  78.         if(canRepeat) {
  79.             if(menuInputDebug) Log("-----Repeat timer has started!");
  80.             AddTimer("repeat_delay", repeatDelay/1000, "_MenuRepeatDelay");
  81.         } else {
  82.             AddTimer(inputKey, 0.0f, inputCallback);
  83.             if(menuInputDebug) Log("Can not repeat. Waiting for release.");
  84.         }
  85.         SetLocalVarBool("_FirstLoop", false);
  86.     }
  87. }
  88. void _MenuRepeatDelay(string &in asTimer) {
  89.     if(menuInputDebug) Log("-----Repeat timer has ended!");
  90.     SetLocalVarBool("_FirstLoop", false);
  91. }
  92. void _MenuResetTimeout(string &in asTimer) {
  93.     if(menuInputDebug) Log("-----Repeat timer was cancelled!");
  94.     if(menuInputDebug) Log("----------Button released!");
  95.     RemoveTimer("repeat_delay");
  96. }
  97. void _MenuInputFrequency(string &in asTimer) {
  98.     //TeleportPlayer("MenuArea"); //reset position.
  99.     float fX = 0.0f, fZ = -10.0f;
  100.     fX = GetLocalVarFloat("PPosX");
  101.     fZ = GetLocalVarFloat("PPosZ");
  102.     SetPlayerPos(fX, GetPlayerPosY(), fZ);
  103.     if(!GetLocalVarBool("_MenuHasCallback_AreaS")) {
  104.         AddEntityCollideCallback("Player", "AreaS", "_MenuHandler", true, 1);
  105.         SetLocalVarBool("_MenuHasCallback_AreaS", true);
  106.     }
  107.     if(!GetLocalVarBool("_MenuHasCallback_AreaA")) {
  108.         AddEntityCollideCallback("Player", "AreaA", "_MenuHandler", true, 1);
  109.         SetLocalVarBool("_MenuHasCallback_AreaA", true);
  110.     }
  111.     if(!GetLocalVarBool("_MenuHasCallback_AreaW")) {
  112.         AddEntityCollideCallback("Player", "AreaW", "_MenuHandler", true, 1);
  113.         SetLocalVarBool("_MenuHasCallback_AreaW", true);
  114.     }
  115.     if(!GetLocalVarBool("_MenuHasCallback_AreaD")) {
  116.         AddEntityCollideCallback("Player", "AreaD", "_MenuHandler", true, 1);
  117.         SetLocalVarBool("_MenuHasCallback_AreaD", true);
  118.     }
  119. }
  120. void _MenuConfirm(bool abLit) {
  121.     if(abLit) {
  122.         if(menuInputDebug) Log("Hit Enter");
  123.         SetLanternActive(false, false);
  124.        
  125.         //if(GetLocalVarBool("_MenuCanClick")) {
  126.             //SetLocalVarBool("_MenuCanClick", false);     
  127.             HandleInput("Enter");
  128.         //}
  129.     }
  130. }
  131.  
  132. void SetLocalVarBool(string & in asVarName, bool abVal) {
  133.     SetLocalVarInt("bool_"+asVarName, abVal == true ? 1 : 0);
  134. }
  135. bool GetLocalVarBool(string & in asVarName) {
  136.     return GetLocalVarInt("bool_"+asVarName) == 1 ? true : false;
  137. }
  138. void Log(string text) {
  139.     AddDebugMessage(text, false);
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement