Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using HarmonyLib;
- using Il2Cpp;
- using Il2CppControllers;
- using MelonLoader;
- using UnityEngine;
- using Vector2 = UnityEngine.Vector2;
- using Vector3 = UnityEngine.Vector3;
- namespace TLFDeathlessSpikes
- {
- public class Class1 : MelonMod
- {
- static GameObject? player;
- static BaseMovements2D? movement;
- static PlayerHealth? health;
- static Vector2 safe_pos;
- static Vector2 fallback_safe_pos;
- static int RespawnCounter = 0;
- // -----------------------------
- // Harmony Patch
- // -----------------------------
- [HarmonyPatch(typeof(KillerTrigger), nameof(KillerTrigger.OnTriggerEnter2D))]
- private static class CancelSpikeDeath
- {
- private static bool Prefix(Collider2D col)
- {
- Melon<Class1>.Logger.Msg($"Trigger from: {col.gameObject.name}");
- if (player == null || health == null)
- return true;
- // Check if the collider belongs to the player
- if (col.gameObject != player)
- return true;
- Melon<Class1>.Logger.Msg("Player hit spikes");
- float maxHealth = health.health.maximum;
- if (RespawnCounter >= 3)
- {
- health.GetDamage(
- 0.01f * maxHealth,
- 0, 0, 2,
- Il2CppKumiSouls.DamageType.Strong,
- Il2CppKumiSouls.DamageAttack.None,
- Il2CppKumiSouls.TypeEffect.None,
- 0, false, 0.5f, true, false,
- Il2CppKumiSouls.BloodQuantity.VeryHigh
- );
- player.transform.position = fallback_safe_pos;
- RespawnCounter = 0;
- }
- else
- {
- health.GetDamage(
- 0.1f * maxHealth,
- 0, 0, 2,
- Il2CppKumiSouls.DamageType.Strong,
- Il2CppKumiSouls.DamageAttack.None,
- Il2CppKumiSouls.TypeEffect.None,
- 0, false, 0.5f, true, false,
- Il2CppKumiSouls.BloodQuantity.VeryHigh
- );
- player.transform.position = safe_pos;
- RespawnCounter++;
- }
- return false;
- }
- }
- // -----------------------------
- // Scene Load
- // -----------------------------
- public override void OnSceneWasInitialized(int buildIndex, string sceneName)
- {
- Melon<Class1>.Logger.Msg($"Scene loaded: {sceneName}");
- FindPlayer();
- }
- // -----------------------------
- // Player Finder
- // -----------------------------
- void FindPlayer()
- {
- if (health != null)
- {
- fallback_safe_pos = player.transform.position;
- return;
- }
- Melon<Class1>.Logger.Msg("Searching for PlayerHealth...");
- health = UnityEngine.Object.FindObjectOfType<PlayerHealth>();
- if (health == null)
- {
- Melon<Class1>.Logger.Warning("PlayerHealth not found yet.");
- return;
- }
- player = health.gameObject;
- movement = player.GetComponent<BaseMovements2D>();
- Melon<Class1>.Logger.Msg($"Player found: {player.name}");
- if (movement == null)
- Melon<Class1>.Logger.Warning("BaseMovements2D NOT found on Player");
- fallback_safe_pos = player.transform.position;
- Melon<Class1>.Logger.Msg($"Fallback position set to {fallback_safe_pos}");
- }
- // -----------------------------
- // Update Loop
- // -----------------------------
- public override void OnUpdate()
- {
- if (health == null)
- {
- FindPlayer();
- return;
- }
- if (player == null || movement == null)
- return;
- if (movement.grounded && !health.invulnerable)
- {
- if (movement.FacingRight)
- safe_pos = player.transform.position - Vector3.right;
- else
- safe_pos = player.transform.position + Vector3.right;
- RespawnCounter = 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment