Skip to content

Commit

Permalink
Merge branch 'template-update'
Browse files Browse the repository at this point in the history
# Conflicts:
#	Source/Mod.cs
  • Loading branch information
ZzZombo committed Apr 14, 2021
2 parents d513f1e + 7bfda72 commit bc6dbc8
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 101 deletions.
57 changes: 57 additions & 0 deletions Source/Harmony.cs
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;
}
}
}
}
}
102 changes: 1 addition & 101 deletions Source/Mod.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using UnityEngine;
using HarmonyLib;
using Verse;
using RimWorld;
using Verse;

internal static class MOD
{
Expand Down Expand Up @@ -64,102 +62,4 @@ public override float GetValueUnfinalized(StatRequest req, bool applyPostProcess
return 0;
}
}

public class CompProperties_GunWithMagazines: CompProperties
{
public bool enabled = true;
public int currentAmmo = -1;
public CompProperties_GunWithMagazines()
{
this.compClass = typeof(CompGunWithMagazines);
Log.Message($"[ZzZomboRW.CompProperties_GunWithMagazines] 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($"[ZzZomboRW.CompGunWithMagazines] 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);
}
}

[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;
}
}
}
}
51 changes: 51 additions & 0 deletions Source/ThingComps.cs
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 added v1.x/Assemblies/ZzZomboRW-Mod.dll
Binary file not shown.

0 comments on commit bc6dbc8

Please sign in to comment.