Skip to content

Commit

Permalink
Added Attack Multiplier and fixed button hover
Browse files Browse the repository at this point in the history
  • Loading branch information
Pokachi committed May 30, 2021
1 parent ba8033d commit 680ab48
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 16 deletions.
62 changes: 62 additions & 0 deletions DifficultyMod/Core/DifficultySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ public static class DifficultySettings
{
private const float ENEMY_HP_MULTIPLIER_MIN = 0.25f;
private const float ENEMY_HP_MULTIPLIER_MAX = 5f;
private const float ENEMY_ATTACK_MULTIPLIER_MIN = 0.25f;
private const float ENEMY_ATTACK_MULTIPLIER_MAX = 5f;

public static float EnemyHPMultiplier { get; set; } = 2;
public static float EnemyAttackMultiplier { get; set; } = 2;

public static void DecreaseEnemyHPMultiplier(Action<float> callBack)
{
Expand Down Expand Up @@ -45,6 +48,37 @@ public static int ModifyEnemyHPMultiplier(int defaultHP, PieceConfig pieceConfig
return defaultHP;
}


public static void DecreaseEnemyAttackMultiplier(Action<float> callBack)
{
if (EnemyAttackMultiplier > ENEMY_ATTACK_MULTIPLIER_MIN)
{
EnemyAttackMultiplier -= 0.25f;
}

callBack(EnemyAttackMultiplier);
}

public static void IncreaseEnemyAttackMultiplier(Action<float> callBack)
{
if (EnemyAttackMultiplier < ENEMY_ATTACK_MULTIPLIER_MAX)
{
EnemyAttackMultiplier += 0.25f;
}

callBack(EnemyAttackMultiplier);
}

public static int ModifyEnemyAttackMultiplier(int defaultAttack, PieceConfig pieceConfig)
{
if (pieceConfig.HasPieceType(DataKeys.PieceType.Enemy))
{
return (int)(defaultAttack * EnemyAttackMultiplier);
}

return defaultAttack;
}

[HarmonyPatch(typeof(Piece), "CreatePiece")]
public static class EnemyHPMultiplierPatcher
{
Expand Down Expand Up @@ -73,5 +107,33 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> inst
}
}
}

[HarmonyPatch(typeof(Piece), "CreatePiece")]
public static class EnemyAttackMultiplierPatcher
{
private const int numInstructionToWait = 1;
static MethodInfo m_MyExtraMethod = AccessTools.Method(typeof(DifficultySettings), nameof(ModifyEnemyAttackMultiplier), new[] { typeof(int), typeof(PieceConfig) });

static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
foreach (CodeInstruction instruction in instructions)
{
if (instruction.opcode == OpCodes.Callvirt)
{
string strOperand = instruction.operand.ToString();
if (strOperand.Contains("get_AttackDamage"))
{
yield return instruction;

yield return new CodeInstruction(OpCodes.Ldarg_0);
yield return new CodeInstruction(OpCodes.Call, m_MyExtraMethod);

continue;
}
}
yield return instruction;
}
}
}
}
}
34 changes: 18 additions & 16 deletions DifficultyMod/UI/DifficultyMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@ namespace DemeoMods.DifficultyMod.UI
{
class DifficultyMenu : MonoBehaviour
{

private TextMeshPro _EnemyHPMultiplier;

private void Awake()
{
Initialize();
}

public void Initialize()
{
MelonLogger.Msg("(DifficultyMod|Info) Initializing...");
MelonLogger.Msg("Creating UI Elements...");
gameObject.SetActive(false);
gameObject.layer = 5; //UI layer

Expand All @@ -36,9 +33,10 @@ public void Initialize()
transform.rotation = Quaternion.Euler(275, 40, 0);
transform.localScale = new Vector3(3, 1, 2);

gameObject.AddComponent<BoxCollider>();

gameObject.SetActive(true);

MelonLogger.Msg("Creating UI Elements...");

// First Page
GameObject difficultySettingsPageOne = new GameObject("Difficulty Settings 1");
Expand All @@ -60,11 +58,16 @@ public void Initialize()
enemyHPMultiplier.SetActive(true);

CreateText(enemyHPMultiplier.transform, new Vector3(0.036f, 0.15f, 1.4f), 3.5f, new Color(0.0392f, 0.0157f, 0, 1), "Enemy HP Multiplier", TextAlignmentOptions.Center, FontStyles.Normal);
_EnemyHPMultiplier = CreateText(enemyHPMultiplier.transform, new Vector3(0.036f, 0.15f, 0.4f), 5f, new Color(0.0392f, 0.0157f, 0, 1), "Enemy HP Multiplier Text", DifficultySettings.EnemyHPMultiplier.ToString(), TextAlignmentOptions.Center, FontStyles.Normal);
TextMeshPro _EnemyHPMultiplier = CreateText(enemyHPMultiplier.transform, new Vector3(0.036f, 0.15f, 0.4f), 7f, new Color(0.0392f, 0.0157f, 0, 1), "Enemy HP Multiplier Text", DifficultySettings.EnemyHPMultiplier.ToString(), TextAlignmentOptions.Center, FontStyles.Normal);
CreateButton(enemyHPMultiplier.transform, new Vector3(-1f, 0.15f, 0.4f), "Enemy HP Multiplier Down", "DreadArrowDown", () => { DifficultySettings.DecreaseEnemyHPMultiplier(text => { UpdateText(_EnemyHPMultiplier, text); }); });
CreateButton(enemyHPMultiplier.transform, new Vector3(1f, 0.15f, 0.4f), "Enemy HP Multiplier Up", "DreadArrowUp", () => { DifficultySettings.IncreaseEnemyHPMultiplier(text => { UpdateText(_EnemyHPMultiplier, text); }); });


CreateText(enemyHPMultiplier.transform, new Vector3(0.036f, 0.15f, -.6f), 3.5f, new Color(0.0392f, 0.0157f, 0, 1), "Enemy Attack Multiplier", TextAlignmentOptions.Center, FontStyles.Normal);
TextMeshPro _EnemyAttackMultiplier = CreateText(enemyHPMultiplier.transform, new Vector3(0.036f, 0.15f, -1.6f), 7f, new Color(0.0392f, 0.0157f, 0, 1), "Enemy Attack Multiplier Text", DifficultySettings.EnemyAttackMultiplier.ToString(), TextAlignmentOptions.Center, FontStyles.Normal);
CreateButton(enemyHPMultiplier.transform, new Vector3(-1f, 0.15f, -1.6f), "Enemy Attack Multiplier Down", "DreadArrowDown", () => { DifficultySettings.DecreaseEnemyAttackMultiplier(text => { UpdateText(_EnemyAttackMultiplier, text); }); });
CreateButton(enemyHPMultiplier.transform, new Vector3(1f, 0.15f, -1.6f), "Enemy Attack Multiplier Up", "DreadArrowUp", () => { DifficultySettings.IncreaseEnemyAttackMultiplier(text => { UpdateText(_EnemyAttackMultiplier, text); }); });

MelonLogger.Msg("Completed Creating UI Elements.");
}

Expand All @@ -75,7 +78,7 @@ private static void UpdateText(TextMeshPro textMeshPro, float text)

private static ClickableButton CreateButton(Transform parent, Vector3 position, string buttonName, string meshName, Action callback)
{
GameObject gameObject = new GameObject(buttonName, typeof(MeshFilter), typeof(MeshRenderer), typeof(BoxCollider), typeof(ClickableButton), typeof(MenuButtonHoverEffect));
GameObject gameObject = new GameObject(buttonName, typeof(MeshFilter), typeof(MeshRenderer), typeof(MenuButtonHoverEffect), typeof(ClickableButton));
gameObject.SetActive(false);
gameObject.layer = 5; //UI layer

Expand All @@ -95,19 +98,17 @@ private static ClickableButton CreateButton(Transform parent, Vector3 position,
MeshRenderer meshRenderer = gameObject.GetComponent<MeshRenderer>();
meshRenderer.material = Resources.FindObjectsOfTypeAll<Material>().First(x => x.name == "MainMenuMat");

// Box Collider
BoxCollider boxCollider = gameObject.GetComponent<BoxCollider>();
boxCollider.size = new Vector3(1.65f, 0.49f, 1.27f);

// ClickableButton
ClickableButton clickableButton = gameObject.GetComponent<ClickableButton>();
clickableButton.InitButton(0, "", callback, false);

// Menu Button Hover Effect
MenuButtonHoverEffect menuButtonHoverEffect = gameObject.GetComponent<MenuButtonHoverEffect>();
menuButtonHoverEffect.hoverMaterial = Resources.FindObjectsOfTypeAll<Material>().First(x => x.name == "MainMenuHover");
menuButtonHoverEffect.Init();

// ClickableButton
ClickableButton clickableButton = gameObject.GetComponent<ClickableButton>();
clickableButton.InitButton(0, "", callback, false);

// We add Box Collider so we don't need to make any adjustments
BoxCollider boxCollider = gameObject.AddComponent<BoxCollider>();

gameObject.SetActive(true);

Expand All @@ -122,8 +123,9 @@ private static TextMeshPro CreateText(Transform parent, Vector3 position, float

private static TextMeshPro CreateText(Transform parent, Vector3 position, float fontSize, Color color, string name, string text, TextAlignmentOptions alignment, FontStyles fontStyles)
{
GameObject gameObject = new GameObject(text, typeof(TextMeshPro));
GameObject gameObject = new GameObject(name, typeof(TextMeshPro));
gameObject.SetActive(false);
gameObject.layer = 5; //UI layer

// RectTransform
RectTransform rectTransform = (RectTransform) gameObject.transform;
Expand Down

0 comments on commit 680ab48

Please sign in to comment.