Unofficial CardLife revival project, pronounced like "celery"
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
7.0KB

  1. using System.Collections;
  2. using System.Reflection;
  3. using CLre.API.Engines;
  4. using Game.Building;
  5. using Game.Character;
  6. using Game.Utilities;
  7. using HarmonyLib;
  8. using Svelto.ECS;
  9. using Svelto.Tasks;
  10. using UnityEngine;
  11. using voxelfarm.VFCol;
  12. namespace CLre.Fixes
  13. {
  14. [Bugfix(name = "UnderStructureCollider",
  15. description = "Prevent passing through structures from below a bit better",
  16. more = "https://trello.com/c/nfuaZWQZ/10-passing-through-structures",
  17. component = BugfixType.Initialiser, id = 7)]
  18. public static class UnderStructureCollider
  19. {
  20. public static void Init()
  21. {
  22. #if DEBUG
  23. MainLevel_BuildDeprecatedEngines_Patch.afterBuildEngines.Add(new CharacterGroundedCheckEngine());
  24. #endif
  25. MainLevel_BuildDeprecatedEngines_Patch.afterBuildEngines.Add(new CharacterUnderStructureRaycastEngine());
  26. }
  27. }
  28. [Bugfix(name = "UnderStructureCollider",
  29. description = "Prevent passing through structures from below a bit better",
  30. more = "https://trello.com/c/nfuaZWQZ/10-passing-through-structures",
  31. component = BugfixType.HarmonyPatch, id = 7)]
  32. [HarmonyPatch(typeof(VFColUtility), "Move")]
  33. class VFCOlUtility_Move_Patch
  34. {
  35. internal static bool Override = false;
  36. [HarmonyPostfix]
  37. public static void AfterMethodCall(ref Vector3 __result)
  38. {
  39. #if DEBUG
  40. API.Utility.Logging.MetaLog("Intercepting CharacterMovementEngine.ApplyForces(...)");
  41. #endif
  42. if (Override)
  43. {
  44. if (__result.y > 0)
  45. {
  46. __result = Vector3.zero;
  47. }
  48. }
  49. }
  50. }
  51. #if DEBUG
  52. [Bugfix(name = "UnderStructureCollider",
  53. description = "Prevent passing through structures from below a bit better",
  54. more = "https://trello.com/c/nfuaZWQZ/10-passing-through-structures",
  55. component = BugfixType.Debug, id = 7)]
  56. public class CharacterGroundedCheckEngine : SingleEntityViewEngine<CharacterOnStructureNode>, ICLreEngine
  57. {
  58. private CharacterOnStructureNode _characterOnStructureNode;
  59. public void Ready() {}
  60. public IEntitiesDB entitiesDB { get; set; }
  61. public IEntityFactory entityFactory { get; set; }
  62. protected override void Add(CharacterOnStructureNode entityView)
  63. {
  64. _characterOnStructureNode = entityView;
  65. CheckNode().Run();
  66. }
  67. protected override void Remove(CharacterOnStructureNode entityView)
  68. {
  69. _characterOnStructureNode = null;
  70. }
  71. public IEnumerator CheckNode()
  72. {
  73. while (_characterOnStructureNode != null)
  74. {
  75. if (_characterOnStructureNode.characterGroundedRaycastComponent.didHit)
  76. {
  77. if (_characterOnStructureNode.characterGroundedRaycastComponent.hitResult.transform != null)
  78. {
  79. API.Utility.Logging.MetaLog($"HIT: {_characterOnStructureNode.characterGroundedRaycastComponent.hitResult.transform.tag} (audio: '{_characterOnStructureNode.characterOnStructureComponent.characterPositionStructureAudioSwitch.value}')");
  80. RaycastHit raycastHitInfo = _characterOnStructureNode.characterGroundedRaycastComponent.hitResult;
  81. if (raycastHitInfo.transform.tag == GameTags.STRUCTURE_TAG)
  82. {
  83. int instanceID = raycastHitInfo.transform.GetComponentInParent<DEPRECATED_REMOVE_EntityProxy>().gameObject.GetInstanceID();
  84. if (entitiesDB.TryQueryEntityView(instanceID, DEPRECATED_SveltoExtensions.DEPRECATED_GROUP,
  85. out PlacedStructureNode entityView))
  86. {
  87. API.Utility.Logging.MetaLog($"Structure: {entityView.structureNameComponent.resourceName}");
  88. }
  89. }
  90. }
  91. else
  92. {
  93. API.Utility.Logging.MetaLog("No transform (not on a structure?)");
  94. }
  95. }
  96. else
  97. {
  98. API.Utility.Logging.MetaLog("No hit");
  99. }
  100. yield return null;
  101. }
  102. }
  103. }
  104. #endif
  105. [Bugfix(name = "UnderStructureCollider",
  106. description = "Prevent passing through structures from below a bit better",
  107. more = "https://trello.com/c/nfuaZWQZ/10-passing-through-structures",
  108. component = BugfixType.Miscellaneous, id = 7)]
  109. public class CharacterUnderStructureRaycastEngine : SingleEntityViewEngine<CharacterMovementNode>, ICLreEngine
  110. {
  111. private bool _playerExists = false;
  112. private int _layer = GameLayers.CARD_INTERACTABLE;
  113. public void Ready()
  114. {
  115. }
  116. public IEntitiesDB entitiesDB { get; set; }
  117. public IEntityFactory entityFactory { get; set; }
  118. protected override void Add(CharacterMovementNode entityView)
  119. {
  120. _playerExists = true;
  121. RaycastStructureCheck(entityView.ID.entityID).RunOnScheduler(StandardSchedulers.physicScheduler);
  122. }
  123. protected override void Remove(CharacterMovementNode entityView)
  124. {
  125. _playerExists = false;
  126. }
  127. public IEnumerator RaycastStructureCheck(int playerId)
  128. {
  129. while (_playerExists)
  130. {
  131. CharacterMovementNode cmn;
  132. while (entitiesDB.TryQueryEntityView(playerId, DEPRECATED_SveltoExtensions.DEPRECATED_GROUP, out cmn))
  133. {
  134. Vector3 position = cmn.characterMovementComponent.characterController.transform.position;
  135. CharacterController cc = cmn.characterMovementComponent.characterController;
  136. RaycastHit rayInfo;
  137. bool didHit = Physics.SphereCast(
  138. position, //+ Vector3.up * (cc.height * 0.5f), // center of player
  139. cc.radius,
  140. Vector3.up,
  141. out rayInfo,
  142. cc.height, //* 0.5f - cc.radius * 0.05f,
  143. _layer);
  144. if (didHit && rayInfo.transform != null)
  145. {
  146. if (rayInfo.transform.CompareTag(GameTags.STRUCTURE_TAG))
  147. {
  148. #if DEBUG
  149. API.Utility.Logging.MetaLog("Structure is above me!");
  150. #endif
  151. VFCOlUtility_Move_Patch.Override = true;
  152. }
  153. }
  154. else
  155. {
  156. #if DEBUG
  157. API.Utility.Logging.MetaLog("No structure above :(");
  158. #endif
  159. VFCOlUtility_Move_Patch.Override = false;
  160. }
  161. yield return null;
  162. }
  163. yield return null;
  164. }
  165. }
  166. }
  167. }