Patch_ObjectScript_Die.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using GadgetCore.API;
  2. using HarmonyLib;
  3. using System.Collections;
  4. using UnityEngine;
  5. namespace Subworlds.Patches
  6. {
  7. [HarmonyPatch(typeof(ObjectScript))]
  8. [HarmonyPatch("Die")]
  9. [HarmonyGadget("SubworldCore")]
  10. public static class Patch_ObjectScript_Die
  11. {
  12. [HarmonyPostfix]
  13. public static void Prefix(ObjectScript __instance, ref int ___id)
  14. {
  15. if (Network.isServer && Core.spawnObjects.Contains(___id))
  16. {
  17. if (Random.Range(0, 2) == 0)
  18. __instance.StartCoroutine(SpawnMultiple(__instance.transform.position.x + 2, __instance.transform.position.y, 2, 1 + Random.Range(0, 3), "e/Subworlds/BrokenSliver", 0.1f));
  19. else if (Random.Range(0, 12) == 0)
  20. {
  21. Item item = GadgetCoreAPI.EmptyItem();
  22. item.id = Core.itemOldTex.GetID();
  23. GadgetCoreAPI.SpawnItem(new Vector3(__instance.transform.position.x + 2, __instance.transform.position.y, 0.2f), item);
  24. }
  25. }
  26. }
  27. private static IEnumerator SpawnMultiple(float xBase, float yBase, float range, int amount, string name, float speed)
  28. {
  29. for (int i = 0; i < amount; i++)
  30. {
  31. int x = 0;
  32. int y = 0;
  33. if (range > 0)
  34. {
  35. x = Random.Range(-1 * (int)(range * 10), (int)(range * 10));
  36. y = Random.Range(-1 * (int)(range * 10), (int)(range * 10));
  37. }
  38. Network.Instantiate(Resources.Load(name), new Vector3(xBase + x / 10f, yBase + 1.1f + y / 10f, 0f), Quaternion.identity, 0);
  39. yield return new WaitForSeconds(speed);
  40. }
  41. yield break;
  42. }
  43. }
  44. }