-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # Source/Mod.cs
- Loading branch information
Showing
4 changed files
with
109 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using HarmonyLib; | ||
using Verse; | ||
|
||
namespace ZzZomboRW | ||
{ | ||
[StaticConstructorOnStartup] | ||
internal static class HarmonyHelper | ||
{ | ||
static HarmonyHelper() | ||
{ | ||
var harmony = new Harmony($"ZzZomboRW.{MOD.NAME}"); | ||
harmony.PatchAll(); | ||
} | ||
|
||
[HarmonyPatch(typeof(Verb_LaunchProjectile), nameof(Verb_LaunchProjectile.Available), Priority.Last)] | ||
public static class Verb_LaunchProjectile_AvailablePatch | ||
{ | ||
private static void Postfix(ref bool __result, Verb_LaunchProjectile __instance) | ||
{ | ||
if(__instance is Verb_Shoot verb) | ||
{ | ||
var comp = verb.EquipmentSource?.GetComp<CompGunWithMagazines>(); | ||
if(__result && comp?.Enabled is true) | ||
{ | ||
__result = comp.CurrentAmmo > 0; | ||
} | ||
} | ||
} | ||
} | ||
|
||
[HarmonyPatch(typeof(Verb_Shoot), nameof(Verb_Shoot.TryCastShot))] | ||
public static class Verb_Shoot_TryCastShotPatch | ||
{ | ||
private static void Postfix(ref bool __result, Verb_Shoot __instance) | ||
{ | ||
var comp = __instance.EquipmentSource?.GetComp<CompGunWithMagazines>(); | ||
if(__result && comp?.Enabled is true && comp.CurrentAmmo > 0) | ||
{ | ||
--comp.CurrentAmmo; | ||
} | ||
} | ||
} | ||
|
||
[HarmonyPatch(typeof(Verb_Shoot), nameof(Verb_Shoot.WarmupComplete))] | ||
public static class Verb_Shoot_WarmupCompletePatch | ||
{ | ||
private static void Postfix(Verb_Shoot __instance) | ||
{ | ||
var comp = __instance.EquipmentSource?.GetComp<CompGunWithMagazines>(); | ||
if(comp?.Enabled is true && comp.CurrentAmmo < 1) | ||
{ | ||
comp.CurrentAmmo = comp.MaxAmmo; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using RimWorld; | ||
using UnityEngine; | ||
using Verse; | ||
|
||
namespace ZzZomboRW | ||
{ | ||
public class CompProperties_GunWithMagazines: CompProperties | ||
{ | ||
public bool enabled = true; | ||
public int currentAmmo = -1; | ||
public CompProperties_GunWithMagazines() | ||
{ | ||
this.compClass = typeof(CompGunWithMagazines); | ||
Log.Message($"[{this.GetType().FullName}] Initialized:\n" + | ||
$"\tCurrent ammo: {this.currentAmmo};\n" + | ||
$"\tEnabled: {this.enabled}."); | ||
} | ||
|
||
} | ||
public class CompGunWithMagazines: ThingComp | ||
{ | ||
public CompProperties_GunWithMagazines Props => (CompProperties_GunWithMagazines)this.props; | ||
public bool Enabled => this.Props.enabled && this.MaxAmmo > 1; | ||
public int MaxAmmo | ||
{ | ||
get => (int)this.parent.GetStatValue(DefDatabase<StatDef>.GetNamed("ZzZomboRW_GunWithMagazines_MaxAmmo"), true); | ||
} | ||
public int CurrentAmmo | ||
{ | ||
get => this.Props.currentAmmo; | ||
set => this.Props.currentAmmo = Mathf.Clamp(value, 0, this.MaxAmmo); | ||
} | ||
public override void Initialize(CompProperties props) | ||
{ | ||
base.Initialize(props); | ||
if(this.CurrentAmmo < 0) | ||
{ | ||
this.CurrentAmmo = this.MaxAmmo; | ||
} | ||
Log.Message($"[{this.GetType().FullName}] Initialized for {this.parent}:\n" + | ||
$"\tCurrent ammo: {this.CurrentAmmo};\n" + | ||
$"\tMax ammo: {this.MaxAmmo};\n" + | ||
$"\tEnabled: {this.Props.enabled}."); | ||
} | ||
public override void PostExposeData() | ||
{ | ||
Scribe_Values.Look(ref this.Props.currentAmmo, "currentAmmo", 1, false); | ||
Scribe_Values.Look(ref this.Props.enabled, "enabled", true, false); | ||
} | ||
} | ||
} |
Binary file not shown.