Vlosyros

Class1.cs

Mar 17th, 2026 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.58 KB | Source Code | 0 0
  1. using HarmonyLib;
  2. using Il2Cpp;
  3. using Il2CppControllers;
  4. using MelonLoader;
  5. using UnityEngine;
  6. using Vector2 = UnityEngine.Vector2;
  7. using Vector3 = UnityEngine.Vector3;
  8.  
  9. namespace TLFDeathlessSpikes
  10. {
  11.     public class Class1 : MelonMod
  12.     {
  13.         static GameObject? player;
  14.         static BaseMovements2D? movement;
  15.         static PlayerHealth? health;
  16.  
  17.         static Vector2 safe_pos;
  18.         static Vector2 fallback_safe_pos;
  19.  
  20.         static int RespawnCounter = 0;
  21.  
  22.         // -----------------------------
  23.         // Harmony Patch
  24.         // -----------------------------
  25.         [HarmonyPatch(typeof(KillerTrigger), nameof(KillerTrigger.OnTriggerEnter2D))]
  26.         private static class CancelSpikeDeath
  27.         {
  28.             private static bool Prefix(Collider2D col)
  29.             {
  30.                 Melon<Class1>.Logger.Msg($"Trigger from: {col.gameObject.name}");
  31.                 if (player == null || health == null)
  32.                     return true;
  33.  
  34.                 // Check if the collider belongs to the player
  35.                 if (col.gameObject != player)
  36.                     return true;
  37.  
  38.                 Melon<Class1>.Logger.Msg("Player hit spikes");
  39.  
  40.                 float maxHealth = health.health.maximum;
  41.  
  42.                 if (RespawnCounter >= 3)
  43.                 {
  44.                     health.GetDamage(
  45.                         0.01f * maxHealth,
  46.                         0, 0, 2,
  47.                         Il2CppKumiSouls.DamageType.Strong,
  48.                         Il2CppKumiSouls.DamageAttack.None,
  49.                         Il2CppKumiSouls.TypeEffect.None,
  50.                         0, false, 0.5f, true, false,
  51.                         Il2CppKumiSouls.BloodQuantity.VeryHigh
  52.                     );
  53.  
  54.                     player.transform.position = fallback_safe_pos;
  55.                     RespawnCounter = 0;
  56.                 }
  57.                 else
  58.                 {
  59.                     health.GetDamage(
  60.                         0.1f * maxHealth,
  61.                         0, 0, 2,
  62.                         Il2CppKumiSouls.DamageType.Strong,
  63.                         Il2CppKumiSouls.DamageAttack.None,
  64.                         Il2CppKumiSouls.TypeEffect.None,
  65.                         0, false, 0.5f, true, false,
  66.                         Il2CppKumiSouls.BloodQuantity.VeryHigh
  67.                     );
  68.  
  69.                     player.transform.position = safe_pos;
  70.                     RespawnCounter++;
  71.                 }
  72.  
  73.                 return false;
  74.             }
  75.         }
  76.  
  77.         // -----------------------------
  78.         // Scene Load
  79.         // -----------------------------
  80.         public override void OnSceneWasInitialized(int buildIndex, string sceneName)
  81.         {
  82.             Melon<Class1>.Logger.Msg($"Scene loaded: {sceneName}");
  83.             FindPlayer();
  84.         }
  85.  
  86.         // -----------------------------
  87.         // Player Finder
  88.         // -----------------------------
  89.         void FindPlayer()
  90.         {
  91.             if (health != null)
  92.             {
  93.                 fallback_safe_pos = player.transform.position;
  94.                 return;
  95.             }
  96.                
  97.             Melon<Class1>.Logger.Msg("Searching for PlayerHealth...");
  98.  
  99.             health = UnityEngine.Object.FindObjectOfType<PlayerHealth>();
  100.  
  101.             if (health == null)
  102.             {
  103.                 Melon<Class1>.Logger.Warning("PlayerHealth not found yet.");
  104.                 return;
  105.             }
  106.  
  107.             player = health.gameObject;
  108.             movement = player.GetComponent<BaseMovements2D>();
  109.  
  110.             Melon<Class1>.Logger.Msg($"Player found: {player.name}");
  111.  
  112.             if (movement == null)
  113.                 Melon<Class1>.Logger.Warning("BaseMovements2D NOT found on Player");
  114.  
  115.             fallback_safe_pos = player.transform.position;
  116.  
  117.             Melon<Class1>.Logger.Msg($"Fallback position set to {fallback_safe_pos}");
  118.         }
  119.  
  120.         // -----------------------------
  121.         // Update Loop
  122.         // -----------------------------
  123.         public override void OnUpdate()
  124.         {
  125.             if (health == null)
  126.             {
  127.                 FindPlayer();
  128.                 return;
  129.             }
  130.  
  131.             if (player == null || movement == null)
  132.                 return;
  133.  
  134.             if (movement.grounded && !health.invulnerable)
  135.             {
  136.                 if (movement.FacingRight)
  137.                     safe_pos = player.transform.position - Vector3.right;
  138.                 else
  139.                     safe_pos = player.transform.position + Vector3.right;
  140.  
  141.                 RespawnCounter = 0;
  142.             }
  143.         }
  144.     }
  145. }
Tags: MOD
Advertisement
Add Comment
Please, Sign In to add comment