Files
Pal3.Unity/Assets/Scripts/Pal3.Game/Scene/SceneObjects/ImpulsiveObject.cs
Jiaqi (0x7c13) Liu e870cf487a Add core protocol regression tests and copyright checks
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.
2026-11-12 13:37:27 +08:00

237 lines
8.9 KiB
C#

// ------------------------------------------------------------------------
// Copyright (c) 2021-2026, Jiaqi (0x7c13) Liu. All rights reserved.
// See LICENSE file in the project root for license information.
// ------------------------------------------------------------------------
#if PAL3
namespace Pal3.Game.Scene.SceneObjects
{
using System;
using System.Collections;
using System.Threading;
using Actor.Controllers;
using Command;
using Common;
using Core.Command;
using Core.Command.SceCommands;
using Core.Contract.Constants;
using Core.Contract.Enums;
using Core.DataReader.Cpk;
using Core.DataReader.Pol;
using Core.DataReader.Scn;
using Core.Utilities;
using Data;
using Engine.Animation;
using Engine.Core.Abstraction;
using Engine.Core.Implementation;
using Engine.Coroutine;
using Engine.Extensions;
using Rendering.Renderer;
using Bounds = UnityEngine.Bounds;
using Color = Core.Primitives.Color;
using Vector3 = UnityEngine.Vector3;
[ScnSceneObject(SceneObjectType.Impulsive)]
public sealed class ImpulsiveObject : SceneObject
{
private const float HIT_ANIMATION_DURATION = 0.5f;
private IGameEntity _subObjectGameEntity;
private ImpulsiveMechanismSubObjectController _subObjectController;
private bool _isDuringInteraction;
public ImpulsiveObject(ScnObjectInfo objectInfo, ScnSceneInfo sceneInfo)
: base(objectInfo, sceneInfo)
{
}
public override IGameEntity Activate(GameResourceProvider resourceProvider, Color tintColor)
{
if (IsActivated) return GetGameEntity();
Color subObjectTintColor = tintColor;
#if PAL3
// Fix the color(texture) issue of impulsive mechanism in M11-2 which uses _r.pol as the main model
if (string.Equals(ModelFileVirtualPath, @"M11.cpk\2\_r.pol", StringComparison.OrdinalIgnoreCase))
{
tintColor = new Color(0.9f, 0.45f, 0f, 0.1f);
}
#endif
IGameEntity sceneObjectGameEntity = base.Activate(resourceProvider, tintColor);
_subObjectGameEntity = GameEntityFactory.Create($"Object_{ObjectInfo.Id}_{ObjectInfo.Type}_SubObject",
sceneObjectGameEntity, worldPositionStays: false);
string subObjectModelPath = ModelFileVirtualPath.Insert(ModelFileVirtualPath.LastIndexOf('.'), "a");
PolFile polFile = resourceProvider.GetGameResourceFile<PolFile>(subObjectModelPath);
ITextureResourceProvider textureProvider = resourceProvider.CreateTextureResourceProvider(
CoreUtility.GetDirectoryName(subObjectModelPath, CpkConstants.DirectorySeparatorChar));
PolyModelRenderer subObjectModelRenderer = _subObjectGameEntity.AddComponent<PolyModelRenderer>();
subObjectModelRenderer.Render(polFile,
textureProvider,
resourceProvider.GetMaterialManager(),
isStaticObject: false,
subObjectTintColor);
_subObjectController = _subObjectGameEntity.AddComponent<ImpulsiveMechanismSubObjectController>();
_subObjectController.Init();
_subObjectController.OnPlayerActorHit += OnPlayerActorHit;
return sceneObjectGameEntity;
}
private void OnPlayerActorHit(object sender, IGameEntity playerActorGameEntity)
{
if (_isDuringInteraction) return; // Prevent multiple interactions during animation
_isDuringInteraction = true;
Pal3.Instance.Execute(new ActorStopActionAndStandCommand(ActorConstants.PlayerActorVirtualID));
Pal3.Instance.Execute(new CameraFollowPlayerCommand(0));
RequestForInteraction();
}
public override bool IsDirectlyInteractable(float distance) => false;
public override bool ShouldGoToCutsceneWhenInteractionStarted() => true;
public override IEnumerator InteractAsync(InteractionContext ctx)
{
PlaySfx("wb002");
ctx.PlayerActorGameEntity.GetComponent<ActorActionController>()
.PerformAction(ActorActionType.BeAttack, true, 1);
Vector3 targetPosition = ctx.PlayerActorGameEntity.Transform.Position +
(_subObjectGameEntity.Transform.Forward * 6f) + Vector3.up * 2f;
yield return ctx.PlayerActorGameEntity.Transform.MoveAsync(targetPosition,
HIT_ANIMATION_DURATION);
ctx.PlayerActorGameEntity.GetComponent<ActorMovementController>().SetNavLayer(ObjectInfo.Parameters[2]);
Pal3.Instance.Execute(new ActorSetTilePositionCommand(ActorConstants.PlayerActorVirtualID,
ObjectInfo.Parameters[0],
ObjectInfo.Parameters[1]));
Pal3.Instance.Execute(new CameraFollowPlayerCommand(1));
_isDuringInteraction = false;
}
public override void Deactivate()
{
if (_subObjectController != null)
{
_subObjectController.OnPlayerActorHit -= OnPlayerActorHit;
_subObjectController.Destroy();
_subObjectController = null;
}
if (_subObjectGameEntity != null)
{
_subObjectGameEntity.Destroy();
_subObjectGameEntity = null;
}
base.Deactivate();
}
}
internal sealed class ImpulsiveMechanismSubObjectController : GameEntityScript
{
public event EventHandler<IGameEntity> OnPlayerActorHit;
private const float MIN_Z_POSITION = -1.7f;
private const float MAX_Z_POSITION = 4f;
private const float POSITION_HOLD_TIME = 3f;
private const float MOVEMENT_ANIMATION_DURATION = 2.5f;
private BoundsTriggerController _triggerController;
private CancellationTokenSource _movementAnimationCts = new ();
protected override void OnDisableGameEntity()
{
if (_movementAnimationCts is {IsCancellationRequested: false})
{
_movementAnimationCts.Cancel();
}
if (_triggerController != null)
{
_triggerController.OnPlayerActorEntered -= OnPlayerActorEntered;
_triggerController.Destroy();
_triggerController = null;
}
}
public void Init()
{
// Add collider
Bounds bounds = new()
{
center = new Vector3(0f, 1f, -1f),
size = new Vector3(3f, 2f, 7f),
};
_triggerController = GameEntity.AddComponent<BoundsTriggerController>();
_triggerController.SetBounds(bounds, true);
_triggerController.OnPlayerActorEntered += OnPlayerActorEntered;
// Set initial position
Vector3 subObjectInitPosition = Transform.Position;
subObjectInitPosition.z = MIN_Z_POSITION;
Transform.Position = subObjectInitPosition;
// Start movement
_movementAnimationCts = new CancellationTokenSource();
StartCoroutine(StartMovementAsync(_movementAnimationCts.Token));
}
private void OnPlayerActorEntered(object sender, IGameEntity playerActorGameEntity)
{
OnPlayerActorHit?.Invoke(sender, playerActorGameEntity);
}
private IEnumerator StartMovementAsync(CancellationToken cancellationToken)
{
float startDelay = RandomGenerator.Range(0f, 3.5f);
yield return CoroutineYieldInstruction.WaitForSeconds(startDelay);
Vector3 initPosition = Transform.LocalPosition;
object holdTimeWaiter = CoroutineYieldInstruction.WaitForSeconds(POSITION_HOLD_TIME);
while (!cancellationToken.IsCancellationRequested)
{
yield return CoreAnimation.EnumerateValueAsync(MIN_Z_POSITION,
MAX_Z_POSITION,
MOVEMENT_ANIMATION_DURATION,
AnimationCurveType.Linear,
(value) =>
{
Vector3 newPosition = initPosition;
newPosition.z = value;
Transform.LocalPosition = newPosition;
}, cancellationToken);
yield return holdTimeWaiter;
yield return CoreAnimation.EnumerateValueAsync(MAX_Z_POSITION,
MIN_Z_POSITION,
MOVEMENT_ANIMATION_DURATION,
AnimationCurveType.Linear,
(value) =>
{
Vector3 newPosition = initPosition;
newPosition.z = value;
Transform.LocalPosition = newPosition;
}, cancellationToken);
yield return holdTimeWaiter;
}
}
}
}
#endif