From febee04ea5a0586df2e453ff6fd0a45355483394 Mon Sep 17 00:00:00 2001 From: Jackie Liu Date: Fri, 25 Nov 2022 00:09:16 -0800 Subject: [PATCH] Better actor interaction logic and behavior --- .../Pal3/Actor/ActorActionController.cs | 6 ++ .../Pal3/Actor/ActorMovementController.cs | 39 ++++++++++-- .../PlayerActorLookAtSceneObjectCommand.cs | 18 ++++++ ...layerActorLookAtSceneObjectCommand.cs.meta | 3 + .../Pal3/Command/Pal3ADebugConsoleCommands.cs | 8 +++ .../Pal3/Command/Pal3DebugConsoleCommands.cs | 8 +++ .../Pal3/Player/PlayerGamePlayController.cs | 63 +++++++++++++++---- .../Pal3/Scene/SceneObjects/SwitchObject.cs | 2 + .../WaterSurfaceMechanismObject.cs | 4 ++ 9 files changed, 135 insertions(+), 16 deletions(-) create mode 100644 Assets/Scripts/Pal3/Command/InternalCommands/PlayerActorLookAtSceneObjectCommand.cs create mode 100644 Assets/Scripts/Pal3/Command/InternalCommands/PlayerActorLookAtSceneObjectCommand.cs.meta diff --git a/Assets/Scripts/Pal3/Actor/ActorActionController.cs b/Assets/Scripts/Pal3/Actor/ActorActionController.cs index 080c1178..87f30fcf 100644 --- a/Assets/Scripts/Pal3/Actor/ActorActionController.cs +++ b/Assets/Scripts/Pal3/Actor/ActorActionController.cs @@ -106,6 +106,12 @@ namespace Pal3.Actor return _currentAction; } + public bool IsCurrentActionIdleAction() + { + return !string.IsNullOrEmpty(_currentAction) && + string.Equals(_currentAction, _actor.GetIdleAction(), StringComparison.OrdinalIgnoreCase); + } + public Rigidbody GetRigidBody() { return _rigidbody; diff --git a/Assets/Scripts/Pal3/Actor/ActorMovementController.cs b/Assets/Scripts/Pal3/Actor/ActorMovementController.cs index fc3a6657..fdcd153b 100644 --- a/Assets/Scripts/Pal3/Actor/ActorMovementController.cs +++ b/Assets/Scripts/Pal3/Actor/ActorMovementController.cs @@ -19,6 +19,7 @@ namespace Pal3.Actor using MetaData; using Scene; using Scene.SceneObjects.Common; + using Script; using Script.Waiter; using UnityEngine; using Random = UnityEngine.Random; @@ -42,7 +43,8 @@ namespace Pal3.Actor ICommandExecutor, ICommandExecutor, ICommandExecutor, - ICommandExecutor + ICommandExecutor, + ICommandExecutor { private const float MAX_CROSS_LAYER_Y_DIFFERENTIAL = 2f; private const float DEFAULT_ROTATION_SPEED = 20f; @@ -53,6 +55,7 @@ namespace Pal3.Actor private int _currentLayerIndex = 0; private readonly Path _currentPath = new (); + private bool _isMovementOnHold; private WaitUntilCanceled _movementWaiter; private CancellationTokenSource _movementCts = new (); @@ -123,19 +126,29 @@ namespace Pal3.Actor _currentLayerIndex = layerIndex; } - public bool MovementInProgress() + public bool IsMovementInProgress() { return !_currentPath.IsEndOfPath(); } - public void CancelCurrentMovement() + public void CancelMovement() { _currentPath.Clear(); } + public void PauseMovement() + { + _isMovementOnHold = true; + + if (IsMovementInProgress()) + { + _actionController.PerformAction(_actor.GetIdleAction()); + } + } + private void Update() { - if (_currentPath.IsEndOfPath()) return; + if (_isMovementOnHold || _currentPath.IsEndOfPath()) return; MovementResult result = MoveTowards(_currentPath.GetCurrentWayPoint(), _currentPath.MovementMode, _currentPath.IgnoreObstacle); @@ -502,6 +515,7 @@ namespace Pal3.Actor private IEnumerator WaitForSomeTimeAndFollowPath(Vector3[] waypoints, int mode, CancellationToken cancellationToken) { yield return new WaitForSeconds(Random.Range(3, 8)); + yield return new WaitUntil(() => !_isMovementOnHold); if (!cancellationToken.IsCancellationRequested) { SetupPath(waypoints, mode, EndOfPathActionType.Reverse, ignoreObstacle: true); @@ -615,7 +629,8 @@ namespace Pal3.Actor public void Execute(ActorSetTilePositionCommand command) { if (_actor.Info.Id != command.ActorId) return; - CancelCurrentMovement(); + + CancelMovement(); var tilePosition = new Vector2Int(command.TileXPosition, command.TileYPosition); @@ -719,5 +734,19 @@ namespace Pal3.Actor _isDuringCollision = false; } } + + public void Execute(ScriptFinishedRunningNotification command) + { + if (command.ScriptType == PalScriptType.Scene && + command.ScriptId == _actor.Info.ScriptId && + _isMovementOnHold) + { + _isMovementOnHold = false; + if (IsMovementInProgress()) + { + _actionController.PerformAction(_actor.GetMovementAction(_currentPath.MovementMode)); + } + } + } } } \ No newline at end of file diff --git a/Assets/Scripts/Pal3/Command/InternalCommands/PlayerActorLookAtSceneObjectCommand.cs b/Assets/Scripts/Pal3/Command/InternalCommands/PlayerActorLookAtSceneObjectCommand.cs new file mode 100644 index 00000000..31860a00 --- /dev/null +++ b/Assets/Scripts/Pal3/Command/InternalCommands/PlayerActorLookAtSceneObjectCommand.cs @@ -0,0 +1,18 @@ +// --------------------------------------------------------------------------------------------- +// Copyright (c) 2021-2022, Jiaqi Liu. All rights reserved. +// See LICENSE file in the project root for license information. +// --------------------------------------------------------------------------------------------- + +namespace Pal3.Command.InternalCommands +{ + [AvailableInConsole] + public class PlayerActorLookAtSceneObjectCommand : ICommand + { + public PlayerActorLookAtSceneObjectCommand(int sceneObjectId) + { + SceneObjectId = sceneObjectId; + } + + public int SceneObjectId { get; } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Pal3/Command/InternalCommands/PlayerActorLookAtSceneObjectCommand.cs.meta b/Assets/Scripts/Pal3/Command/InternalCommands/PlayerActorLookAtSceneObjectCommand.cs.meta new file mode 100644 index 00000000..9a532b4a --- /dev/null +++ b/Assets/Scripts/Pal3/Command/InternalCommands/PlayerActorLookAtSceneObjectCommand.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 712dbb380620444fbff1fe5720fd7bda +timeCreated: 1669359502 \ No newline at end of file diff --git a/Assets/Scripts/Pal3/Command/Pal3ADebugConsoleCommands.cs b/Assets/Scripts/Pal3/Command/Pal3ADebugConsoleCommands.cs index dbe824c2..1137c66a 100644 --- a/Assets/Scripts/Pal3/Command/Pal3ADebugConsoleCommands.cs +++ b/Assets/Scripts/Pal3/Command/Pal3ADebugConsoleCommands.cs @@ -1303,6 +1303,14 @@ namespace Pal3.Command new PlayerActorClimbObjectCommand(objectId, fromPosition, toPosition, crossLayer)); } + // + [ConsoleMethodAttribute("PlayerActorLookAtSceneObject", "Execute PlayerActorLookAtSceneObjectCommand.")] + public static void PlayerActorLookAtSceneObject(System.Int32 sceneObjectId) + { + CommandDispatcher.Instance.Dispatch( + new PlayerActorLookAtSceneObjectCommand(sceneObjectId)); + } + // [ConsoleMethodAttribute("PlayerInteractionRequest", "Execute PlayerInteractionRequest.")] public static void PlayerInteractionRequest() diff --git a/Assets/Scripts/Pal3/Command/Pal3DebugConsoleCommands.cs b/Assets/Scripts/Pal3/Command/Pal3DebugConsoleCommands.cs index c44a4b63..b0face1a 100644 --- a/Assets/Scripts/Pal3/Command/Pal3DebugConsoleCommands.cs +++ b/Assets/Scripts/Pal3/Command/Pal3DebugConsoleCommands.cs @@ -1207,6 +1207,14 @@ namespace Pal3.Command new PlayerActorClimbObjectCommand(objectId, fromPosition, toPosition, crossLayer)); } + // + [ConsoleMethodAttribute("PlayerActorLookAtSceneObject", "Execute PlayerActorLookAtSceneObjectCommand.")] + public static void PlayerActorLookAtSceneObject(System.Int32 sceneObjectId) + { + CommandDispatcher.Instance.Dispatch( + new PlayerActorLookAtSceneObjectCommand(sceneObjectId)); + } + // [ConsoleMethodAttribute("PlayerInteractionRequest", "Execute PlayerInteractionRequest.")] public static void PlayerInteractionRequest() diff --git a/Assets/Scripts/Pal3/Player/PlayerGamePlayController.cs b/Assets/Scripts/Pal3/Player/PlayerGamePlayController.cs index 43a7ae8f..42a36ff2 100644 --- a/Assets/Scripts/Pal3/Player/PlayerGamePlayController.cs +++ b/Assets/Scripts/Pal3/Player/PlayerGamePlayController.cs @@ -38,6 +38,7 @@ namespace Pal3.Player #endif ICommandExecutor, ICommandExecutor, + ICommandExecutor, ICommandExecutor { private GameStateManager _gameStateManager; @@ -252,7 +253,7 @@ namespace Pal3.Player var movementMode = movement.magnitude < 0.7f ? 0 : 1; ActorActionType movementAction = movementMode == 0 ? ActorActionType.Walk : ActorActionType.Run; - _playerActorMovementController.CancelCurrentMovement(); + _playerActorMovementController.CancelMovement(); Transform cameraTransform = _camera.transform; Vector3 inputDirection = cameraTransform.forward * movement.y + cameraTransform.right * movement.x; @@ -473,21 +474,45 @@ namespace Pal3.Player facingAngle < nearestInteractableFacingAngle) { nearestInteractableFacingAngle = facingAngle; - interactionAction = () => - { - CommandDispatcher.Instance.Dispatch( - new GameStateChangeRequest(GameState.Cutscene)); - CommandDispatcher.Instance.Dispatch( - new ActorStopActionAndStandCommand(ActorConstants.PlayerActorVirtualID)); - Actor actor = _sceneManager.GetCurrentScene().GetActor(actorInfo.Key); - CommandDispatcher.Instance.Dispatch(new ScriptRunCommand((int)actor.Info.ScriptId)); - }; + interactionAction = () => InteractWithOtherActor(actorInfo.Key, actorInfo.Value); } } interactionAction?.Invoke(); } + + private void InteractWithOtherActor(int actorId, GameObject actorGameObject) + { + CommandDispatcher.Instance.Dispatch( + new GameStateChangeRequest(GameState.Cutscene)); + CommandDispatcher.Instance.Dispatch( + new ActorStopActionAndStandCommand(ActorConstants.PlayerActorVirtualID)); + Actor targetActor = _sceneManager.GetCurrentScene().GetActor(actorId); + + // Pause current movement of the target actor + if (actorGameObject.GetComponent() is { } movementController) + { + movementController.PauseMovement(); + } + + // Look at the target actor + CommandDispatcher.Instance.Dispatch( + new ActorLookAtActorCommand(_playerActor.Info.Id, targetActor.Info.Id)); + + // Only look at player actor when the target actor is in idle state + if (actorGameObject.GetComponent() is { } actionController && + actionController.IsCurrentActionIdleAction()) + { + CommandDispatcher.Instance.Dispatch( + new ActorLookAtActorCommand(targetActor.Info.Id, _playerActor.Info.Id)); + } + + // Run dialogue script + CommandDispatcher.Instance.Dispatch( + new ScriptRunCommand((int)targetActor.Info.ScriptId)); + } + private void PortalToTapPosition() { if (!_lastInputTapPosition.HasValue) return; @@ -752,6 +777,22 @@ namespace Pal3.Player callback?.Invoke(); } + public void Execute(PlayerActorLookAtSceneObjectCommand command) + { + Transform actorTransform = _playerActorGameObject.transform; + SceneObject sceneObject = _sceneManager.GetCurrentScene().GetSceneObject(command.SceneObjectId); + + if (sceneObject?.GetGameObject() is { } sceneObjectGameObject) + { + Vector3 objectPosition = sceneObjectGameObject.transform.position; + + actorTransform.LookAt(new Vector3( + objectPosition.x, + actorTransform.position.y, + objectPosition.z)); + } + } + public void Execute(ActorEnablePlayerControlCommand command) { if (command.ActorId == ActorConstants.PlayerActorVirtualID) return; @@ -825,7 +866,7 @@ namespace Pal3.Player { if (_playerActorMovementController != null) { - _playerActorMovementController.CancelCurrentMovement(); + _playerActorMovementController.CancelMovement(); } if (_playerActorActionController != null && diff --git a/Assets/Scripts/Pal3/Scene/SceneObjects/SwitchObject.cs b/Assets/Scripts/Pal3/Scene/SceneObjects/SwitchObject.cs index 11ab31a4..1b7580e7 100644 --- a/Assets/Scripts/Pal3/Scene/SceneObjects/SwitchObject.cs +++ b/Assets/Scripts/Pal3/Scene/SceneObjects/SwitchObject.cs @@ -42,6 +42,8 @@ namespace Pal3.Scene.SceneObjects new GameStateChangeRequest(GameState.Cutscene)); CommandDispatcher.Instance.Dispatch( new ActorStopActionAndStandCommand(ActorConstants.PlayerActorVirtualID)); + CommandDispatcher.Instance.Dispatch( + new PlayerActorLookAtSceneObjectCommand(Info.Id)); CommandDispatcher.Instance.Dispatch( new ActorPerformActionCommand(ActorConstants.PlayerActorVirtualID, ActorConstants.ActionNames[ActorActionType.Check], diff --git a/Assets/Scripts/Pal3/Scene/SceneObjects/WaterSurfaceMechanismObject.cs b/Assets/Scripts/Pal3/Scene/SceneObjects/WaterSurfaceMechanismObject.cs index 483b9c66..35c37686 100644 --- a/Assets/Scripts/Pal3/Scene/SceneObjects/WaterSurfaceMechanismObject.cs +++ b/Assets/Scripts/Pal3/Scene/SceneObjects/WaterSurfaceMechanismObject.cs @@ -5,6 +5,8 @@ namespace Pal3.Scene.SceneObjects { + using Command; + using Command.SceCommands; using Core.Animation; using Core.DataReader.Scn; using Core.GameBox; @@ -67,6 +69,8 @@ namespace Pal3.Scene.SceneObjects Vector3 finalPosition = gameObject.transform.position; finalPosition.y = GameBoxInterpreter.ToUnityYPosition(_surfaceMechanismObject.Info.Parameters[0]); + CommandDispatcher.Instance.Dispatch(new PlaySfxCommand("wc007", 1)); + StartCoroutine(AnimationHelper.MoveTransform(gameObject.transform, finalPosition, WATER_ANIMATION_DURATION,