Skip to content

Commit

Permalink
Use reflected delegate instead of using outdated copy of PawnRenderer…
Browse files Browse the repository at this point in the history
….CarryWeaponOpenly code, some code cleanup in similar code
  • Loading branch information
lbmaian committed Jul 24, 2021
1 parent 52019c7 commit 1d46f03
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 49 deletions.
Binary file modified 1.3/Assemblies/CompOversizedWeapon.dll
Binary file not shown.
Binary file modified 1.3/Assemblies/PawnShields.dll
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,20 @@ public static void DrawEquipmentAimingPostFix(Pawn ___pawn, Thing eq, Vector3 dr

// start copied vanilla code (with mesh = flip ? MeshPool.plane10Flip : MeshPool.plane10)
var flip = false;
var num = aimAngle - 90f;
var angle = aimAngle - 90f;
if (aimAngle > 20f && aimAngle < 160f)
{
num += eq.def.equippedAngleOffset;
angle += eq.def.equippedAngleOffset;
}
else if (aimAngle > 200f && aimAngle < 340f)
{
flip = true;
num -= 180f;
num -= eq.def.equippedAngleOffset;
angle -= 180f;
angle -= eq.def.equippedAngleOffset;
}
else
{
num += eq.def.equippedAngleOffset;
angle += eq.def.equippedAngleOffset;
}
// end copied vanilla code

Expand All @@ -132,18 +132,18 @@ public static void DrawEquipmentAimingPostFix(Pawn ___pawn, Thing eq, Vector3 dr
if (animationTicks > 0)
{
if (flip)
num -= (animationTicks + 1) / 2;
angle -= (animationTicks + 1) / 2;
else
num += (animationTicks + 1) / 2;
angle += (animationTicks + 1) / 2;
}
}

num %= 360f; // copied vanilla code
angle %= 360f; // copied vanilla code

var matSingle = compActivatableEffect.Graphic.MatSingle;

var s = new Vector3(eq.def.graphicData.drawSize.x, 1f, eq.def.graphicData.drawSize.y);
var matrix = Matrix4x4.TRS(drawLoc + offset, Quaternion.AngleAxis(num, Vector3.up), s);
var matrix = Matrix4x4.TRS(drawLoc + offset, Quaternion.AngleAxis(angle, Vector3.up), s);
Graphics.DrawMesh(flip ? MeshPool.plane10Flip : MeshPool.plane10, matrix, matSingle, 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ public static void DrawEquipmentAimingPostFix(Pawn ___pawn, Thing eq, Vector3 dr

// start copied vanilla code (with mesh = flip ? MeshPool.plane10Flip : MeshPool.plane10)
var flip = false;
var num = aimAngle - 90f;
var angle = aimAngle - 90f;
if (aimAngle > 20f && aimAngle < 160f)
{
num += eq.def.equippedAngleOffset;
angle += eq.def.equippedAngleOffset;
}
else if (aimAngle > 200f && aimAngle < 340f)
{
flip = true;
num -= 180f;
num -= eq.def.equippedAngleOffset;
angle -= 180f;
angle -= eq.def.equippedAngleOffset;
}
else
{
num += eq.def.equippedAngleOffset;
angle += eq.def.equippedAngleOffset;
}
// end copied vanilla code

Expand All @@ -60,20 +60,20 @@ public static void DrawEquipmentAimingPostFix(Pawn ___pawn, Thing eq, Vector3 dr
if (animationTicks > 0)
{
if (flip)
num -= (animationTicks + 1) / 2;
angle -= (animationTicks + 1) / 2;
else
num += (animationTicks + 1) / 2;
angle += (animationTicks + 1) / 2;
}
}

num %= 360f; // copied vanilla code
angle %= 360f; // copied vanilla code

var matSingle = eq.Graphic is Graphic_StackCount graphic_StackCount
? graphic_StackCount.SubGraphicForStackCount(1, eq.def).MatSingle
: eq.Graphic.MatSingle;
var s = new Vector3(eq.def.graphicData.drawSize.x, 1f,
eq.def.graphicData.drawSize.y);
var matrix = Matrix4x4.TRS(drawLoc, Quaternion.AngleAxis(num, Vector3.up), s);
var matrix = Matrix4x4.TRS(drawLoc, Quaternion.AngleAxis(angle, Vector3.up), s);
Graphics.DrawMesh(flip ? MeshPool.plane10Flip : MeshPool.plane10, matrix, matSingle, 0);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ public static bool DrawEquipmentAiming_PreFix(Pawn ___pawn, Thing eq, Vector3 dr
{
// start copied vanilla code (with mesh = flip ? MeshPool.plane10Flip : MeshPool.plane10)
var flip = false;
var num = aimAngle - 90f;
var angle = aimAngle - 90f;
if (aimAngle > 20f && aimAngle < 160f)
{
num += eq.def.equippedAngleOffset;
angle += eq.def.equippedAngleOffset;
}
else if (aimAngle > 200f && aimAngle < 340f)
{
flip = true;
num -= 180f;
num -= eq.def.equippedAngleOffset;
angle -= 180f;
angle -= eq.def.equippedAngleOffset;
}
else
{
num += eq.def.equippedAngleOffset;
angle += eq.def.equippedAngleOffset;
}
num %= 360f;
angle %= 360f;
// end copied vanilla code

var installedWeaponGraphic = installedComp.Props?.installedWeaponGraphic;
Expand All @@ -56,7 +56,7 @@ public static bool DrawEquipmentAiming_PreFix(Pawn ___pawn, Thing eq, Vector3 dr
var s = new Vector3(
installedWeaponGraphic?.drawSize.x ?? eq.def.graphicData.drawSize.x, 1f,
installedWeaponGraphic?.drawSize.y ?? eq.def.graphicData.drawSize.y);
var matrix = Matrix4x4.TRS(drawLoc, Quaternion.AngleAxis(num, Vector3.up), s);
var matrix = Matrix4x4.TRS(drawLoc, Quaternion.AngleAxis(angle, Vector3.up), s);
Graphics.DrawMesh(flip ? MeshPool.plane10Flip : MeshPool.plane10, matrix, matSingle, 0);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,64 +32,64 @@ public static bool DrawEquipmentAimingPreFix(Pawn ___pawn, Thing eq, Vector3 dra
if (compOversizedWeapon.CompDeflectorIsAnimatingNow)
return false;

var props = compOversizedWeapon.Props;
var atPeace = !___pawn.IsFighting() && props != null;
var aimingSouth = false;
var rotation = ___pawn.Rotation;

// start copied vanilla code (with mesh = flip ? MeshPool.plane10Flip : MeshPool.plane10)
var flip = false;
var num = aimAngle - 90f;
var angle = aimAngle - 90f;
if (aimAngle > 20f && aimAngle < 160f)
{
num += eq.def.equippedAngleOffset;
angle += eq.def.equippedAngleOffset;
}
else if (aimAngle > 200f && aimAngle < 340f)
{
flip = true;
num -= 180f;
num -= eq.def.equippedAngleOffset;
angle -= 180f;
angle -= eq.def.equippedAngleOffset;
}
else
{
num += eq.def.equippedAngleOffset;
angle += eq.def.equippedAngleOffset;
aimingSouth = true; // custom
}
// end copied vanilla code

if (atPeace)
var props = compOversizedWeapon.Props;
var rotation = ___pawn.Rotation;

if (props != null && !___pawn.IsFighting()) // at peace
{
if (aimingSouth && props.verticalFlipOutsideCombat)
num += 180f;
angle += 180f;
if (props.verticalFlipNorth && rotation == Rot4.North)
num += 180f;
num += props.NonCombatAngleAdjustment(rotation);
angle += 180f;
angle += props.NonCombatAngleAdjustment(rotation);
}

num %= 360f; // copied vanilla code
angle %= 360f; // copied vanilla code

var matSingle = eq.Graphic is Graphic_StackCount graphic_StackCount
? graphic_StackCount.SubGraphicForStackCount(1, eq.def).MatSingle
: eq.Graphic.MatSingle;
var s = new Vector3(eq.def.graphicData.drawSize.x, 1f, eq.def.graphicData.drawSize.y);
var curOffset = props != null ? props.OffsetFromRotation(rotation) : Vector3.zero;
var matrix = Matrix4x4.TRS(drawLoc + curOffset, Quaternion.AngleAxis(num, Vector3.up), s);
var matrix = Matrix4x4.TRS(drawLoc + curOffset, Quaternion.AngleAxis(angle, Vector3.up), s);
Graphics.DrawMesh(flip ? MeshPool.plane10Flip : MeshPool.plane10, matrix, matSingle, 0);

if (props != null && props.isDualWeapon)
{
curOffset = new Vector3(-1f * curOffset.x, curOffset.y, curOffset.z);
if (rotation == Rot4.North || rotation == Rot4.South)
{
num += 135f;
num %= 360f;
angle += 135f;
angle %= 360f;
}
else
{
curOffset = new Vector3(curOffset.x, curOffset.y - 0.1f, curOffset.z + 0.15f);
flip = !flip;
}
matrix.SetTRS(drawLoc + curOffset, Quaternion.AngleAxis(num, Vector3.up), s);
matrix.SetTRS(drawLoc + curOffset, Quaternion.AngleAxis(angle, Vector3.up), s);
Graphics.DrawMesh(flip ? MeshPool.plane10 : MeshPool.plane10Flip, matrix, matSingle, 0);
}
return false;
Expand Down
16 changes: 9 additions & 7 deletions Source/AllModdingComponents/PawnShields/ThingComps/CompShield.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using RimWorld;
using System;
using HarmonyLib;
using RimWorld;
using UnityEngine;
using Verse;

Expand Down Expand Up @@ -127,12 +129,7 @@ public virtual void MakeBlockEffect(Pawn defender, DamageInfo dinfo, bool ranged
/// <param name="pawn">Shield bearer.</param>
public virtual void RenderShield(Vector3 loc, Rot4 rot, Pawn pawn, Thing thing)
{
var carryShieldOpenly =
(pawn.carryTracker == null || pawn.carryTracker.CarriedThing == null) &&
(pawn.Drafted || (pawn.CurJob != null && pawn.CurJob.def.alwaysShowWeapon) ||
(pawn.mindState.duty != null && pawn.mindState.duty.def.alwaysShowWeapon));

if (!ShieldProps.renderProperties.renderWhenPeaceful && !carryShieldOpenly)
if (!ShieldProps.renderProperties.renderWhenPeaceful && !pawnRendererCarryWeaponOpenly(pawn.Drawer.renderer))
return;

if (ShieldProps.wieldedGraphic != null && ShieldProps.wieldedGraphic.Graphic.MatSingle != null)
Expand Down Expand Up @@ -173,5 +170,10 @@ public virtual void RenderShield(Vector3 loc, Rot4 rot, Pawn pawn, Thing thing)
thing.Graphic.Draw(loc, rot, thing);
}
}

// Note: This is an open instance delegate where the first argument is the instance.
private static readonly Func<PawnRenderer, bool> pawnRendererCarryWeaponOpenly =
(Func<PawnRenderer, bool>)AccessTools.Method(typeof(PawnRenderer), "CarryWeaponOpenly")
.CreateDelegate(typeof(Func<PawnRenderer, bool>));
}
}

0 comments on commit 1d46f03

Please sign in to comment.