Better actor interaction logic and behavior

This commit is contained in:
Jackie Liu committed 2022-11-25 00:09:16 -08:00
1 parent 959631c340
commit febee04ea5
9 files changed
+135 -16

No files matched your search

@@ -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;
@@ -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<ActorStopActionAndStandCommand>,
ICommandExecutor<ActorMoveOutOfScreenCommand>,
ICommandExecutor<ActorActivateCommand>,
ICommandExecutor<ActorSetNavLayerCommand>
ICommandExecutor<ActorSetNavLayerCommand>,
ICommandExecutor<ScriptFinishedRunningNotification>
{
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));
}
}
}
}
}
@@ -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; }
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 712dbb380620444fbff1fe5720fd7bda
timeCreated: 1669359502
@@ -1303,6 +1303,14 @@ namespace Pal3.Command
new PlayerActorClimbObjectCommand(objectId, fromPosition, toPosition, crossLayer));
}
// <auto-generated/>
[ConsoleMethodAttribute("PlayerActorLookAtSceneObject", "Execute PlayerActorLookAtSceneObjectCommand.")]
public static void PlayerActorLookAtSceneObject(System.Int32 sceneObjectId)
{
CommandDispatcher<ICommand>.Instance.Dispatch(
new PlayerActorLookAtSceneObjectCommand(sceneObjectId));
}
// <auto-generated/>
[ConsoleMethodAttribute("PlayerInteractionRequest", "Execute PlayerInteractionRequest.")]
public static void PlayerInteractionRequest()
@@ -1207,6 +1207,14 @@ namespace Pal3.Command
new PlayerActorClimbObjectCommand(objectId, fromPosition, toPosition, crossLayer));
}
// <auto-generated/>
[ConsoleMethodAttribute("PlayerActorLookAtSceneObject", "Execute PlayerActorLookAtSceneObjectCommand.")]
public static void PlayerActorLookAtSceneObject(System.Int32 sceneObjectId)
{
CommandDispatcher<ICommand>.Instance.Dispatch(
new PlayerActorLookAtSceneObjectCommand(sceneObjectId));
}
// <auto-generated/>
[ConsoleMethodAttribute("PlayerInteractionRequest", "Execute PlayerInteractionRequest.")]
public static void PlayerInteractionRequest()
@@ -38,6 +38,7 @@ namespace Pal3.Player
#endif
ICommandExecutor<SceneLeavingCurrentSceneNotification>,
ICommandExecutor<ScenePostLoadingNotification>,
ICommandExecutor<PlayerActorLookAtSceneObjectCommand>,
ICommandExecutor<ResetGameStateCommand>
{
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<ICommand>.Instance.Dispatch(
new GameStateChangeRequest(GameState.Cutscene));
CommandDispatcher<ICommand>.Instance.Dispatch(
new ActorStopActionAndStandCommand(ActorConstants.PlayerActorVirtualID));
Actor actor = _sceneManager.GetCurrentScene().GetActor(actorInfo.Key);
CommandDispatcher<ICommand>.Instance.Dispatch(new ScriptRunCommand((int)actor.Info.ScriptId));
};
interactionAction = () => InteractWithOtherActor(actorInfo.Key, actorInfo.Value);
}
}
interactionAction?.Invoke();
}
private void InteractWithOtherActor(int actorId, GameObject actorGameObject)
{
CommandDispatcher<ICommand>.Instance.Dispatch(
new GameStateChangeRequest(GameState.Cutscene));
CommandDispatcher<ICommand>.Instance.Dispatch(
new ActorStopActionAndStandCommand(ActorConstants.PlayerActorVirtualID));
Actor targetActor = _sceneManager.GetCurrentScene().GetActor(actorId);
// Pause current movement of the target actor
if (actorGameObject.GetComponent<ActorMovementController>() is { } movementController)
{
movementController.PauseMovement();
}
// Look at the target actor
CommandDispatcher<ICommand>.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<ActorActionController>() is { } actionController &&
actionController.IsCurrentActionIdleAction())
{
CommandDispatcher<ICommand>.Instance.Dispatch(
new ActorLookAtActorCommand(targetActor.Info.Id, _playerActor.Info.Id));
}
// Run dialogue script
CommandDispatcher<ICommand>.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 &&
@@ -42,6 +42,8 @@ namespace Pal3.Scene.SceneObjects
new GameStateChangeRequest(GameState.Cutscene));
CommandDispatcher<ICommand>.Instance.Dispatch(
new ActorStopActionAndStandCommand(ActorConstants.PlayerActorVirtualID));
CommandDispatcher<ICommand>.Instance.Dispatch(
new PlayerActorLookAtSceneObjectCommand(Info.Id));
CommandDispatcher<ICommand>.Instance.Dispatch(
new ActorPerformActionCommand(ActorConstants.PlayerActorVirtualID,
ActorConstants.ActionNames[ActorActionType.Check],
@@ -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<ICommand>.Instance.Dispatch(new PlaySfxCommand("wc007", 1));
StartCoroutine(AnimationHelper.MoveTransform(gameObject.transform,
finalPosition,
WATER_ANIMATION_DURATION,