2021-10-15 21:06:51 +01:00
using HarmonyLib ;
using OWML.Common ;
2021-12-25 23:03:23 -08:00
using QSB.Animation.NPC.Messages ;
2021-04-29 18:30:45 +01:00
using QSB.Animation.NPC.WorldObjects ;
using QSB.ConversationSync ;
2021-12-23 13:49:04 -08:00
using QSB.Messaging ;
2021-04-26 14:30:21 +01:00
using QSB.Patches ;
using QSB.Player ;
2022-01-12 20:58:38 -08:00
using QSB.TriggerSync.WorldObjects ;
2021-04-26 14:30:21 +01:00
using QSB.Utility ;
using QSB.WorldSync ;
2021-04-29 18:30:45 +01:00
using System.Linq ;
2021-04-26 14:30:21 +01:00
using UnityEngine ;
2022-03-02 19:46:33 -08:00
namespace QSB.Animation.NPC.Patches ;
[HarmonyPatch]
public class CharacterAnimationPatches : QSBPatch
2021-04-26 14:30:21 +01:00
{
2022-03-02 19:46:33 -08:00
public override QSBPatchTypes Type = > QSBPatchTypes . OnClientConnect ;
2022-02-27 04:40:44 -08:00
2022-03-02 19:46:33 -08:00
[HarmonyPrefix]
[HarmonyPatch(typeof(CharacterAnimController), nameof(CharacterAnimController.OnAnimatorIK))]
public static bool AnimatorIKReplacement (
CharacterAnimController __instance )
{
if ( ! QSBWorldSync . AllObjectsReady | | ConversationManager . Instance = = null )
2021-04-26 14:30:21 +01:00
{
2022-03-02 19:46:33 -08:00
return true ;
}
2021-04-26 14:30:21 +01:00
2022-03-02 19:46:33 -08:00
var playerId = ConversationManager . Instance . GetPlayerTalkingToTree ( __instance . _dialogueTree ) ;
var player = QSBPlayerManager . GetPlayer ( playerId ) ;
2022-01-13 12:13:47 +00:00
2022-03-02 19:46:33 -08:00
if ( __instance . playerTrackingZone = = null )
{
return true ;
}
2022-01-13 12:13:47 +00:00
2022-03-02 19:46:33 -08:00
var qsbObj = __instance . playerTrackingZone . GetWorldObject < QSBCharacterTrigger > ( ) ; // OPTIMIZE : maybe cache this somewhere... or assess how slow this is
2021-04-26 14:30:21 +01:00
2022-03-02 19:46:33 -08:00
PlayerInfo playerToUse = null ;
if ( __instance . _inConversation )
{
if ( playerId = = uint . MaxValue )
2021-04-26 14:30:21 +01:00
{
2022-03-02 19:46:33 -08:00
DebugLog . ToConsole ( $"Error - {__instance.name} is in conversation with a null player! Defaulting to active camera." , MessageType . Error ) ;
playerToUse = QSBPlayerManager . LocalPlayer ;
2022-02-27 04:40:44 -08:00
}
2022-03-02 19:46:33 -08:00
else
2022-02-27 04:40:44 -08:00
{
2022-03-02 19:46:33 -08:00
playerToUse = player . CameraBody = = null
? QSBPlayerManager . LocalPlayer
: player ;
2021-04-26 14:30:21 +01:00
}
2022-03-02 19:46:33 -08:00
}
else if ( ! __instance . lookOnlyWhenTalking & & qsbObj . Occupants . Count ! = 0 ) // IDEA : maybe this would be more fun if characters looked between players at random times? :P
{
playerToUse = QSBPlayerManager . GetClosestPlayerToWorldPoint ( qsbObj . Occupants , __instance . transform . position ) ;
}
else if ( QSBPlayerManager . PlayerList . Count ! = 0 )
{
playerToUse = QSBPlayerManager . GetClosestPlayerToWorldPoint ( __instance . transform . position , true ) ;
}
2021-04-26 14:30:21 +01:00
2022-03-02 19:46:33 -08:00
var localPosition = playerToUse ! = null
? __instance . _animator . transform . InverseTransformPoint ( playerToUse . CameraBody . transform . position )
: Vector3 . zero ;
2021-04-26 14:30:21 +01:00
2022-03-02 19:46:33 -08:00
var targetWeight = __instance . headTrackingWeight ;
if ( __instance . lookOnlyWhenTalking )
{
if ( ! __instance . _inConversation
| | qsbObj . Occupants . Count = = 0
| | ! qsbObj . Occupants . Contains ( playerToUse ) )
2021-04-29 18:30:45 +01:00
{
2022-03-02 19:46:33 -08:00
targetWeight * = 0 ;
2021-04-29 18:30:45 +01:00
}
2022-02-27 04:40:44 -08:00
}
2022-03-02 19:46:33 -08:00
else
2022-02-24 22:04:54 -08:00
{
2022-03-02 19:46:33 -08:00
if ( qsbObj . Occupants . Count = = 0
| | ! qsbObj . Occupants . Contains ( playerToUse ) )
2022-02-27 04:40:44 -08:00
{
2022-03-02 19:46:33 -08:00
targetWeight * = 0 ;
2022-02-27 04:40:44 -08:00
}
2022-03-02 19:46:33 -08:00
}
2021-04-29 18:30:45 +01:00
2022-03-02 19:46:33 -08:00
__instance . _currentLookWeight = Mathf . Lerp ( __instance . _currentLookWeight , targetWeight , Time . deltaTime * 2f ) ;
__instance . _currentLookTarget = __instance . lookSpring . Update ( __instance . _currentLookTarget , localPosition , Time . deltaTime ) ;
__instance . _animator . SetLookAtPosition ( __instance . _animator . transform . TransformPoint ( __instance . _currentLookTarget ) ) ;
__instance . _animator . SetLookAtWeight ( __instance . _currentLookWeight ) ;
return false ;
2022-02-24 22:04:54 -08:00
2022-03-02 19:46:33 -08:00
}
2021-06-18 22:38:32 +01:00
2022-03-02 19:46:33 -08:00
[HarmonyPrefix]
[HarmonyPatch(typeof(FacePlayerWhenTalking), nameof(FacePlayerWhenTalking.OnStartConversation))]
public static bool OnStartConversation ( FacePlayerWhenTalking __instance )
{
var playerId = ConversationManager . Instance . GetPlayerTalkingToTree ( __instance . _dialogueTree ) ;
if ( playerId = = uint . MaxValue )
{
DebugLog . ToConsole ( $"Error - No player talking to {__instance._dialogueTree.name}!" , MessageType . Error ) ;
2022-02-27 04:40:44 -08:00
return false ;
}
2022-02-24 22:04:54 -08:00
2022-03-02 19:46:33 -08:00
var player = QSBPlayerManager . GetPlayer ( playerId ) ;
var distance = player . Body . transform . position - __instance . transform . position ;
var vector2 = distance - Vector3 . Project ( distance , __instance . transform . up ) ;
var angle = Vector3 . Angle ( __instance . transform . forward , vector2 ) * Mathf . Sign ( Vector3 . Dot ( vector2 , __instance . transform . right ) ) ;
var axis = __instance . transform . parent . InverseTransformDirection ( __instance . transform . up ) ;
var lhs = Quaternion . AngleAxis ( angle , axis ) ;
__instance . FaceLocalRotation ( lhs * __instance . transform . localRotation ) ;
2022-02-27 04:40:44 -08:00
2022-03-02 19:46:33 -08:00
return false ;
}
[HarmonyPrefix]
[HarmonyPatch(typeof(CharacterDialogueTree), nameof(CharacterDialogueTree.StartConversation))]
public static bool StartConversation ( CharacterDialogueTree __instance )
{
var allNpcAnimControllers = QSBWorldSync . GetWorldObjects < INpcAnimController > ( ) ;
var ownerOfThis = allNpcAnimControllers . FirstOrDefault ( x = > x . GetDialogueTree ( ) = = __instance ) ;
if ( ownerOfThis = = default )
{
2021-04-29 18:30:45 +01:00
return true ;
}
2022-03-02 19:46:33 -08:00
ownerOfThis . SendMessage ( new NpcAnimationMessage ( true ) ) ;
return true ;
}
2022-02-27 04:40:44 -08:00
2022-03-02 19:46:33 -08:00
[HarmonyPrefix]
[HarmonyPatch(typeof(CharacterDialogueTree), nameof(CharacterDialogueTree.EndConversation))]
public static bool EndConversation ( CharacterDialogueTree __instance )
{
var allNpcAnimControllers = QSBWorldSync . GetWorldObjects < INpcAnimController > ( ) ;
var ownerOfThis = allNpcAnimControllers . FirstOrDefault ( x = > x . GetDialogueTree ( ) = = __instance ) ;
if ( ownerOfThis = = default )
{
2021-04-29 18:30:45 +01:00
return true ;
}
2021-04-29 20:59:25 +01:00
2022-03-02 19:46:33 -08:00
ownerOfThis . SendMessage ( new NpcAnimationMessage ( false ) ) ;
return true ;
}
2021-04-29 20:59:25 +01:00
2022-03-02 19:46:33 -08:00
[HarmonyPrefix]
[HarmonyPatch(typeof(KidRockController), nameof(KidRockController.Update))]
public static bool UpdateReplacement ( KidRockController __instance )
{
if ( ! QSBWorldSync . AllObjectsReady )
{
return true ;
}
2021-06-18 22:38:32 +01:00
2022-03-02 19:46:33 -08:00
var qsbObj = QSBWorldSync . GetWorldObjects < QSBCharacterAnimController > ( ) . First ( x = > x . GetDialogueTree ( ) = = __instance . _dialogueTree ) ;
2022-02-24 22:04:54 -08:00
2022-03-02 19:46:33 -08:00
if ( ! __instance . _throwingRock & & ! qsbObj . InConversation ( ) & & Time . time > __instance . _nextThrowTime )
{
__instance . StartRockThrow ( ) ;
2022-02-27 04:40:44 -08:00
}
2022-03-02 19:46:33 -08:00
return false ;
2021-04-26 14:30:21 +01:00
}
}