Skip to content

Commit

Permalink
feat: fix walkie text & sync frequencies between clients
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Dec 18, 2023
1 parent 8742741 commit b6a53d7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
19 changes: 17 additions & 2 deletions FrequencyWalkie.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ public static IEnumerator OnFrequencyChanged(WalkieTalkie walkie, bool increased
var text = canvas.GetComponentInChildren<Text>();
text.text = $"<b><size=40>{frequencies[walkieTalkieFrequencies[walkie.GetInstanceID()]]}</size><i><size=30>MHz</size></i></b>";

MethodInfo SendWalkieTalkieStartTransmissionSFX = AccessTools.Method(typeof(WalkieTalkie), "SendWalkieTalkieStartTransmissionSFX");
SendWalkieTalkieStartTransmissionSFX.Invoke(walkie, new object[] {(int)walkie.playerHeldBy.playerClientId});

// we show the broadcast icon if frequency is 0 (broad)
if (walkieTalkieFrequencies[walkie.GetInstanceID()] == 0)
{
Expand Down Expand Up @@ -137,10 +140,22 @@ public static void SetPlayerSpeakingOnWalkieTalkieClientRpc(WalkieTalkie instanc
if (rpc_exec_stage != (int)RpcExecStage.Client || !networkManager.IsClient && !networkManager.IsHost)
return;

if (walkieTalkieFrequencies[instance.GetInstanceID()] != frequency && frequency != 0)
// update the frequency on the incoming walkie talkie
for (int i = 0; i < WalkieTalkie.allWalkieTalkies.Count; i++)
{
return;
if ((int)WalkieTalkie.allWalkieTalkies[i].playerHeldBy.playerClientId == playerId)
{
walkieTalkieFrequencies[WalkieTalkie.allWalkieTalkies[i].GetInstanceID()] = frequency;
// update text
instance.StartCoroutine(OnFrequencyChanged(WalkieTalkie.allWalkieTalkies[i], false));
break;
}
}

if (walkieTalkieFrequencies[instance.GetInstanceID()] != frequency && frequency != 0)
return;


StartOfRound.Instance.allPlayerScripts[playerId].speakingToWalkieTalkie = true;
instance.clientIsHoldingAndSpeakingIntoThis = true;
SendWalkieTalkieStartTransmissionSFX.Invoke(instance, new object[] {playerId});
Expand Down
42 changes: 18 additions & 24 deletions Patches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,22 @@ public static void ApplyPatches(Harmony harmony)
MethodInfo originEquipItem = AccessTools.Method(typeof(WalkieTalkie), "EquipItem");
MethodInfo patchEquipItem = AccessTools.Method(typeof(Patches), "WalkieTalkie_EquipItem");
harmony.Patch(originEquipItem, null, new HarmonyMethod(patchEquipItem));

MethodInfo originPocketItem = AccessTools.Method(typeof(WalkieTalkie), "PocketItem");
MethodInfo patchPocketItem = AccessTools.Method(typeof(Patches), "WalkieTalkie_PocketItem");
harmony.Patch(originPocketItem, null, new HarmonyMethod(patchPocketItem));
}

private static void WalkieTalkie_EquipItem(WalkieTalkie __instance)
{
// Only shown once
HUDManager.Instance.DisplayTip("FrequencyWalkie", "Press [R] or [F] to cycle between frequencies. BR.d will broadcast to all walkie talkies.", false, true, "FW_EquipTip");

// check if another walkie talkie in our inventory && being used
for (int i = 0; i < __instance.playerHeldBy.ItemSlots.Length; i++)
{
if (__instance.playerHeldBy.ItemSlots[i] == __instance && __instance.isBeingUsed) {
__instance.gameObject.GetComponent<Canvas>().enabled = true;
continue;
}

if (__instance.playerHeldBy.ItemSlots[i] == __instance)
continue;

// if we have another walkie talkie in our inventory and it's being used
// we disable the canvas (fixes double canvas bug)
if (__instance.playerHeldBy.ItemSlots[i] is WalkieTalkie walkie)
{
walkie.gameObject.GetComponent<Canvas>().enabled = false;
}
}
// enable canvas if walkie talkie is on
if (__instance.isBeingUsed)
__instance.gameObject.GetComponent<Canvas>().enabled = true;
}

private static void WalkieTalkie_PocketItem(WalkieTalkie __instance)
{
__instance.gameObject.GetComponent<Canvas>().enabled = false;
}

private static void WalkieTalkie_Start(WalkieTalkie __instance)
Expand Down Expand Up @@ -173,12 +164,18 @@ private static void WalkieTalkie_Start(WalkieTalkie __instance)

text.transform.localScale = new Vector3(0.015f, 0.01f, 0.01f);

// disable canvas by default
// will be enabled when walkie talkie is on
canvas.enabled = false;
__instance.walkieTalkieLight.intensity /= 3;
}

private static void WalkieTalkie_SwitchWalkieTalkieOn(WalkieTalkie __instance)
{
__instance.gameObject.GetComponent<Canvas>().enabled = __instance.isBeingUsed;

if (__instance.playerHeldBy == GameNetworkManager.Instance.localPlayerController && __instance.isBeingUsed)
HUDManager.Instance.DisplayTip("FrequencyWalkie", "Press [R] or [F] to cycle between frequencies. BR.d will broadcast to all walkie talkies.", false, true, "FW_UseTip");
}

private static void WalkieTalkie_Update(WalkieTalkie __instance)
Expand All @@ -189,7 +186,6 @@ private static void WalkieTalkie_Update(WalkieTalkie __instance)
// and walkie talkie has to be turned on
if (!__instance.isBeingUsed || !isCurrentItemWalkie) return;

MethodInfo SendWalkieTalkieStartTransmissionSFX = AccessTools.Method(typeof(WalkieTalkie), "SendWalkieTalkieStartTransmissionSFX");
if (UnityInput.Current.GetKeyUp(KeyCode.F))
{
FrequencyWalkie.walkieTalkieFrequencies[__instance.GetInstanceID()]--;
Expand All @@ -198,13 +194,11 @@ private static void WalkieTalkie_Update(WalkieTalkie __instance)
FrequencyWalkie.walkieTalkieFrequencies[__instance.GetInstanceID()] = FrequencyWalkie.frequencies.Count - 1;
}

SendWalkieTalkieStartTransmissionSFX.Invoke(__instance, new object[] {(int)__instance.playerHeldBy.playerClientId});
__instance.StartCoroutine(FrequencyWalkie.OnFrequencyChanged(__instance, false));
} else if (UnityInput.Current.GetKeyUp(KeyCode.R))
{
FrequencyWalkie.walkieTalkieFrequencies[__instance.GetInstanceID()] = (FrequencyWalkie.walkieTalkieFrequencies[__instance.GetInstanceID()] + 1) % FrequencyWalkie.frequencies.Count;

SendWalkieTalkieStartTransmissionSFX.Invoke(__instance, new object[] {(int)__instance.playerHeldBy.playerClientId});
__instance.StartCoroutine(FrequencyWalkie.OnFrequencyChanged(__instance, true));
}
}
Expand Down

0 comments on commit b6a53d7

Please sign in to comment.