1
0
Fork 0
muzika-gromche/MuzikaGromche/SpawnRateManager.cs

70 lines
2.6 KiB
C#

using HarmonyLib;
using System;
using UnityEngine;
namespace MuzikaGromche
{
[HarmonyPatch(typeof(RoundManager))]
static class SpawnRatePatch
{
const string JesterEnemyName = "Jester";
// If set to null, do not override spawn chances. Otherwise, it is an index of Jester in RoundManager.currentLevel.Enemies
static int? JesterEnemyIndex = null;
static float? SpawnTime = null;
[HarmonyPatch(nameof(RoundManager.AssignRandomEnemyToVent))]
[HarmonyPrefix]
static void AssignRandomEnemyToVentPrefix(RoundManager __instance, EnemyVent vent, float spawnTime)
{
if (!Config.OverrideSpawnRates.Value)
{
return;
}
var index = __instance.currentLevel.Enemies.FindIndex(enemy => enemy.enemyType.enemyName == JesterEnemyName);
if (index == -1)
{
return;
}
JesterEnemyIndex = index;
SpawnTime = spawnTime;
}
[HarmonyPatch(nameof(RoundManager.AssignRandomEnemyToVent))]
[HarmonyPostfix]
static void AssignRandomEnemyToVentPostfix(RoundManager __instance, EnemyVent vent, float spawnTime)
{
JesterEnemyIndex = null;
SpawnTime = null;
}
[HarmonyPatch(nameof(RoundManager.GetRandomWeightedIndex))]
[HarmonyPostfix]
static void GetRandomWeightedIndexPostfix(RoundManager __instance, ref int[] weights, ref System.Random randomSeed)
{
if (JesterEnemyIndex is int index && SpawnTime is float spawnTime)
{
if (__instance.EnemyCannotBeSpawned(index))
{
return;
}
var minMultiplierTime = 3 * __instance.timeScript.lengthOfHours;
var maxMultiplierTime = 7 * __instance.timeScript.lengthOfHours;
var normalizedMultiplierTime = Math.Clamp((spawnTime - minMultiplierTime) / (maxMultiplierTime - minMultiplierTime), 0f, 1f);
// TODO: Try Expo function instead of Lerp?
var minMultiplier = Mathf.Max(1, __instance.minEnemiesToSpawn);
var maxMultiplier = 9 + __instance.minEnemiesToSpawn;
var multiplier = Mathf.Lerp(minMultiplier, maxMultiplier, normalizedMultiplierTime);
var newWeight = (int)(weights[index] * multiplier);
Debug.Log($"{nameof(MuzikaGromche)} Overriding Jester spawn weight {weights[index]} * {multiplier} => {newWeight}");
weights[index] = newWeight;
}
}
}
}