Patch_ObjectScript_Die.cs 1.6 KB

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