This repository has been archived by the owner on Nov 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Fire CharacterMoveEvent only if the character's position change …
…+ added cache to avoid killing the GBC
- Loading branch information
Showing
2 changed files
with
18 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,31 @@ | ||
using HarmonyLib; | ||
using Longship.Events; | ||
using UnityEngine; | ||
|
||
namespace Longship.Patches | ||
{ | ||
[HarmonyPatch(typeof(Character), "UpdateMotion")] | ||
public class PatchListenToCharacterMovements | ||
{ | ||
static void Prefix(Character __instance, out CharacterMoveEvent __state) | ||
// Using a position cache to avoid over-using the Garbage collector by instancing too much vectors | ||
private static Vector3 _positionCache = new Vector3(); | ||
|
||
static void Prefix(Character __instance) | ||
{ | ||
__state = new CharacterMoveEvent(__instance, __instance.transform.position); | ||
var tmp = __instance.transform.position; | ||
_positionCache.Set(tmp.x, tmp.y, tmp.z); | ||
} | ||
|
||
static void Postfix(Character __instance, CharacterMoveEvent __state) | ||
static void Postfix(Character __instance) | ||
{ | ||
__state.NewPos = __instance.transform.position; | ||
Longship.Instance.EventManager.DispatchEvent(__state); | ||
if (_positionCache.Equals(__instance.transform.position)) | ||
{ | ||
return; | ||
} | ||
|
||
// Only fire the event if the position is different | ||
Longship.Instance.EventManager.DispatchEvent( | ||
new CharacterMoveEvent(__instance, _positionCache, __instance.transform.position)); | ||
} | ||
} | ||
} |