Skip to content

Commit

Permalink
Add animation, sound effects, and tooltip for ammo checking
Browse files Browse the repository at this point in the history
  • Loading branch information
FeeeeK committed Jan 16, 2024
1 parent 75f1862 commit a307eb9
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 49 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.1.0

- Added actual animation and sfx instead hacky workaround
- Added tooltip for checking ammo
- Fixed bug with interaction with buttons and doors

# 1.0.0

Initial release
32 changes: 24 additions & 8 deletions LCAmmoCheck/LCAmmoCheck.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
using System.Reflection;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.Assertions;

namespace LCAmmoCheck
{
[BepInPlugin(GeneratedPluginInfo.Identifier, GeneratedPluginInfo.Name, GeneratedPluginInfo.Version)]
public class LCAmmoCheckPlugin : BaseUnityPlugin
{
public static LCAmmoCheckPlugin? Instance { get; private set; }
private static Harmony? harmony;
public static LCAmmoCheckPlugin? Instance { get; private set; }
public static AnimationClip? ShotgunInspectClip { get; private set; }
public static AudioClip? ShotgunInspectSFX { get; private set; }

private static void LoadAssetBundle()
{
Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("LCAmmoCheck.lcammocheck");
AssetBundle ACAssetBundle = AssetBundle.LoadFromStream(stream);
ShotgunInspectClip = ACAssetBundle.LoadAsset<AnimationClip>("Assets/AnimationClip/ShotgunInspect.anim");
Assert.IsNotNull(ShotgunInspectClip);
ShotgunInspectSFX = ACAssetBundle.LoadAsset<AudioClip>("Assets/AudioClip/ShotgunInspect.ogg");
Assert.IsNotNull(ShotgunInspectSFX);
ShotgunInspectSFX?.LoadAudioData();
ACAssetBundle.Unload(false);
}


#pragma warning disable IDE0051
private void Awake()
public void Awake()
{
Instance = this;
LoadAssetBundle();
harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), GeneratedPluginInfo.Identifier);
Logger.Log(LogLevel.Message, "LCAmmoCheck loaded!");
}

static private void OnDestroy()
public static void OnDestroy()
{
Instance = null;
harmony?.UnpatchSelf();
Instance = null;
harmony = null;
Debug.Log("LCAmmoCheck unloaded!");
}
#pragma warning restore IDE0051
}
}
6 changes: 5 additions & 1 deletion LCAmmoCheck/LCAmmoCheck.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
<PackageReadMeFile>..\README.md</PackageReadMeFile>
<PluginChangelogFile>..\CHANGELOG.md</PluginChangelogFile>
<PluginId>me.axd1x8a.lcammocheck</PluginId>
<Version>1.0.0</Version>
<Version>1.1.0</Version>
<ThunderWebsiteUrl>https://github.com/feeeek/LCAmmoCheck</ThunderWebsiteUrl>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="lcammocheck" />
</ItemGroup>

</Project>
103 changes: 64 additions & 39 deletions LCAmmoCheck/Patches/ShotgunItem.cs
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using HarmonyLib;
using UnityEngine;
using GameNetcodeStuff;

namespace LCAmmoCheck.Patches;

[HarmonyPatch(typeof(ShotgunItem))]
sealed class ShotgunItemPatch
{
private static readonly Dictionary<int, AnimationClip> originalClips = [];
static AnimatorOverrideController OverrideController(Animator animator)
{
if (animator.runtimeAnimatorController is AnimatorOverrideController controller)
{
return controller;
}
AnimatorOverrideController overrideController = new(animator.runtimeAnimatorController);
animator.runtimeAnimatorController = overrideController;
return overrideController;
}
static IEnumerator CheckAmmoAnimation(ShotgunItem s)
{
AnimatorOverrideController overrideController = OverrideController(s.playerHeldBy.playerBodyAnimator);
int playerAnimatorId = s.playerHeldBy.playerBodyAnimator.GetInstanceID();
originalClips[playerAnimatorId] = overrideController["ShotgunReloadOneShell"];
overrideController["ShotgunReloadOneShell"] = LCAmmoCheckPlugin.ShotgunInspectClip!;
s.isReloading = true;
s.shotgunShellLeft.enabled = s.shellsLoaded > 0;
s.shotgunShellRight.enabled = s.shellsLoaded > 1;
// Start hand animation
s.playerHeldBy.playerBodyAnimator.SetBool("ReloadShotgun", value: true);

yield return new WaitForSeconds(0.3f);
// Start gun animation and sound
s.gunAudio.clip = s.gunReloadSFX;
s.gunAudio.Play();

s.gunAudio.PlayOneShot(LCAmmoCheckPlugin.ShotgunInspectSFX!);
s.gunAnimator.SetBool("Reloading", value: true);
yield return new WaitForSeconds(0.45f);
// Stop gun reload sound at time of shell insertion
s.gunAudio.Stop();
// Stop hand animation at time of shell insertion
s.playerHeldBy.playerBodyAnimator.speed = 0.2f;
// Start gun reload sound at time after shell insertion
s.gunAudio.time = 0.70f;
s.gunAudio.Play();

yield return new WaitForSeconds(0.95f);
s.playerHeldBy.playerBodyAnimator.speed = 0.6f;
s.playerHeldBy.playerBodyAnimator.SetBool("ReloadShotgun", value: false);
yield return new WaitForSeconds(0.95f);
yield return new WaitForSeconds(0.15f);
s.gunAnimator.SetBool("Reloading", value: false);

yield return new WaitForSeconds(0.3f);
s.gunAudio.time = 0.0f;
s.gunAudio.Stop();
s.playerHeldBy.playerBodyAnimator.speed = 1.0f;
yield return new WaitForSeconds(0.25f);
s.playerHeldBy.playerBodyAnimator.SetBool("ReloadShotgun", value: false);
yield return new WaitForSeconds(0.25f);
originalClips.Remove(playerAnimatorId, out AnimationClip clip);
overrideController["ShotgunReloadOneShell"] = clip;
s.isReloading = false;
s.ReloadGunEffectsServerRpc(start: false);
yield break;
}

private static void CleanUp(Animator animator)
{
if (animator.runtimeAnimatorController is AnimatorOverrideController overrideController)
{
if (originalClips.Remove(animator.GetInstanceID(), out AnimationClip? clip))
{
overrideController["ShotgunReloadOneShell"] = clip;
}
}
}


[HarmonyPrefix]
[HarmonyPatch("StopUsingGun")]
public static bool StopUsingGunPrefix(ShotgunItem __instance)
{
// playerHeldBy is null when the gun is dropped so we use previousPlayerHeldBy
PlayerControllerB playerHeldBy = __instance.playerHeldBy ?? __instance.previousPlayerHeldBy;
if (playerHeldBy == null)
{
return true;
}
if (playerHeldBy.playerBodyAnimator.speed != 1.0f)
{
playerHeldBy.playerBodyAnimator.speed = 1.0f;
}
if (__instance.gunAudio.time != 0.0f)
{
__instance.gunAudio.time = 0.0f;
}
CleanUp(__instance.playerHeldBy.playerBodyAnimator);
return true;
}


[HarmonyPrefix]
[HarmonyPatch("StartReloadGun")]
public static bool StartReloadGunPrefix(ShotgunItem __instance)
Expand All @@ -84,6 +84,31 @@ public static bool StartReloadGunPrefix(ShotgunItem __instance)
return true;
}

[HarmonyPrefix]
[HarmonyPatch("ItemInteractLeftRight")]
public static bool ItemInteractLeftRightPrefix(ShotgunItem __instance, bool right)
{
// right = true -> Interact (E)
if (!right)
{
return true;
}
if (__instance.playerHeldBy.hit.collider != null && __instance.playerHeldBy.hit.collider.tag == "InteractTrigger")
{
return false;
}
return true;
}

[HarmonyPrefix]
[HarmonyPatch("Start")]
public static bool StartPrefix(ShotgunItem __instance)
{
string[] toolTips = __instance.itemProperties.toolTips;
toolTips[1] = "Reload / Check ammo : [E]";
return true;
}

[HarmonyTranspiler]
[HarmonyPatch(typeof(ShotgunItem), "StartReloadGun")]
public static IEnumerable<CodeInstruction> StartReloadGunTranspiler(IEnumerable<CodeInstruction> instructions)
Expand Down
Binary file added LCAmmoCheck/lcammocheck
Binary file not shown.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Mod for Lethal Company that allows you to check how much ammo you have in shotgu

You can install the mod in one of two ways:

- Download it from [Thunderstore](https://thunderstore.io/) with a mod manager like [r2modman](https://github.com/ebkr/r2modmanPlus).
- Download it from [Thunderstore](https://thunderstore.io/c/lethal-company/p/axd1x8a/LCAmmoCheck/) with a mod manager like [r2modman](https://github.com/ebkr/r2modmanPlus).
- Download DLL from [Releases](https://github.com/feeeek/LCAmmoCheck/releases) and put it in `BepInEx/plugins` folder.

## Contributing
Expand Down

0 comments on commit a307eb9

Please sign in to comment.