mirror of
https://github.com/0x7c13/Pal3.Unity.git
synced 2026-11-26 17:38:10 +08:00
Cover SCE command contracts, binary readers, resource layouts and full local CPK decompression for PAL3 and PAL3A. Fix CVD time rebasing, binary read bounds and short reads, and CPK/NAV validation. Standardize first-party copyright headers, preserve correct creation years, and update console source generation. Validate both variants in Unity and verify all live CPK entries.
111 lines
4.1 KiB
C#
111 lines
4.1 KiB
C#
// ------------------------------------------------------------------------
|
|
// Copyright (c) 2021-2026, Jiaqi (0x7c13) Liu. All rights reserved.
|
|
// See LICENSE file in the project root for license information.
|
|
// ------------------------------------------------------------------------
|
|
|
|
namespace Pal3.Game.Actor.Controllers
|
|
{
|
|
using System;
|
|
using System.Collections;
|
|
using Command;
|
|
using Command.Extensions;
|
|
using Core.Command;
|
|
using Core.Command.SceCommands;
|
|
using Core.Contract.Enums;
|
|
using Core.Primitives;
|
|
using Engine.Core.Implementation;
|
|
using Engine.Extensions;
|
|
using Engine.Services;
|
|
using Script.Waiter;
|
|
|
|
using Vector3 = UnityEngine.Vector3;
|
|
|
|
public class FlyingActorController : TickableGameEntityScript,
|
|
ICommandExecutor<FlyingActorFlyToCommand>
|
|
{
|
|
public const float DefaultFlySpeed = 7.5f;
|
|
public const float MaxFlySpeed = 11f;
|
|
|
|
private const float FLYING_MOVEMENT_MODE_SWITCH_DISTANCE = 5f;
|
|
private const float MAX_TARGET_DISTANCE = 20f;
|
|
|
|
private ActorBase _actor;
|
|
private ActorController _actorController;
|
|
private ActorActionController _actionController;
|
|
|
|
protected override void OnEnableGameEntity()
|
|
{
|
|
CommandExecutorRegistry<ICommand>.Instance.Register(this);
|
|
}
|
|
|
|
protected override void OnDisableGameEntity()
|
|
{
|
|
CommandExecutorRegistry<ICommand>.Instance.UnRegister(this);
|
|
}
|
|
|
|
public void Init(ActorBase actor, ActorController actorController, ActorActionController actionController)
|
|
{
|
|
_actor = actor;
|
|
_actorController = actorController;
|
|
_actionController = actionController;
|
|
}
|
|
|
|
public void Execute(FlyingActorFlyToCommand command)
|
|
{
|
|
Vector3 targetPosition = new GameBoxVector3(
|
|
command.GameBoxXPosition,
|
|
command.GameBoxYPosition,
|
|
command.GameBoxZPosition).ToUnityPosition();
|
|
|
|
// If actor is inactive or at it's init position, just teleport to target position
|
|
if (!_actorController.IsActive ||
|
|
Vector3.Distance(Transform.Position, NpcInfoFactory.ActorInitPosition) < float.Epsilon)
|
|
{
|
|
Transform.Position = targetPosition;
|
|
return;
|
|
}
|
|
|
|
// In case the target position is too far away
|
|
if (Vector3.Distance(Transform.Position, targetPosition) > MAX_TARGET_DISTANCE)
|
|
{
|
|
Transform.Position = (Transform.Position - targetPosition).normalized * MAX_TARGET_DISTANCE + targetPosition;
|
|
}
|
|
|
|
WaitUntilCanceled waiter = new();
|
|
Pal3.Instance.Execute(new ScriptRunnerAddWaiterRequest(waiter));
|
|
|
|
float distance = (targetPosition - Transform.Position).magnitude;
|
|
float duration = distance / DefaultFlySpeed;
|
|
|
|
_actionController.PerformAction(distance < FLYING_MOVEMENT_MODE_SWITCH_DISTANCE
|
|
? _actor.GetMovementAction(MovementMode.Walk)
|
|
: _actor.GetMovementAction(MovementMode.Run));
|
|
|
|
StartCoroutine(FlyToAsync(targetPosition, duration, () => waiter.CancelWait()));
|
|
}
|
|
|
|
private IEnumerator FlyToAsync(Vector3 targetPosition, float duration, Action onFinished = null)
|
|
{
|
|
Vector3 oldPosition = Transform.Position;
|
|
|
|
// Facing towards target position, ignoring y
|
|
Transform.LookAt(new Vector3(targetPosition.x, oldPosition.y, targetPosition.z));
|
|
|
|
float timePast = 0f;
|
|
while (timePast < duration)
|
|
{
|
|
Vector3 newPosition = oldPosition;
|
|
newPosition += (timePast / duration) * (targetPosition - oldPosition);
|
|
|
|
Transform.Position = newPosition;
|
|
|
|
timePast += GameTimeProvider.Instance.DeltaTime;
|
|
yield return null;
|
|
}
|
|
|
|
Transform.Position = targetPosition;
|
|
_actionController.PerformAction(_actor.GetIdleAction());
|
|
onFinished?.Invoke();
|
|
}
|
|
}
|
|
} |