Files
Pal3.Unity/Assets/Scripts/Pal3.Game/GameSystems/Team/TeamManager.cs
Jiaqi (0x7c13) Liu 7e5df726cc Implement PAL3A command handlers and isolate script execution
Correct PAL3A parameter signatures and add handlers for fortune selection,
skybox sequences, actor facing and attacks, team and progression state,
camera constraints, and battle preparation flags.

Give script instances separate locals, advance grouped waits concurrently,
and pause PAL3A parents only for their directly decoded child calls. Cancel
stale interactions and save replay on reset, preserve finite action semantics,
and restore camera orientation after releasing horizontal constraints.

Persist supported state in v2 saves and add protocol, runtime, and progression
regressions. Full combat gameplay remains outside this change.

Validation: PAL3 274/274 and PAL3A 291/291 EditMode tests; 18 PAL3 and 22 PAL3A
runtime checks. All 77,889 PAL3A instruction boundaries match EXE signatures.
2026-11-13 00:49:36 +08:00

234 lines
8.3 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.GameSystems.Team
{
using System;
using System.Collections.Generic;
using System.Linq;
using Actor.Controllers;
using Command;
using Command.Extensions;
using Core.Command;
using Core.Command.SceCommands;
using Core.Contract.Enums;
using Core.Contract.Constants;
using Data;
using Core.Utilities;
using Engine.Core.Abstraction;
using Engine.Logging;
using GamePlay;
using Scene;
using Quaternion = UnityEngine.Quaternion;
using Vector2Int = UnityEngine.Vector2Int;
using Vector3 = UnityEngine.Vector3;
public sealed class TeamManager : IDisposable,
ICommandExecutor<TeamOpenCommand>,
ICommandExecutor<TeamCloseCommand>,
ICommandExecutor<TeamAddOrRemoveActorCommand>,
#if PAL3A
ICommandExecutor<TeamAddOrRemoveActorSilentlyCommand>,
#endif
ICommandExecutor<ResetGameStateCommand>
{
private const float TEAM_OPEN_SPAWN_POINT_PLAYER_OFFSET = 2.5f;
private readonly PlayerActorManager _playerActorManager;
private readonly SceneManager _sceneManager;
private readonly GameResourceProvider _resources;
private readonly HashSet<PlayerActorId> _actorsInTeam = new ();
#if PAL3
private static readonly float[] ActorSpawnPositionDirectionInDegrees =
{
40f,
-40f,
0f,
-80f,
80f,
};
#elif PAL3A
private static readonly float[] ActorSpawnPositionDirectionInDegrees =
{
-40f,
40f,
0f,
-80f,
80f,
};
#endif
public TeamManager(PlayerActorManager playerActorManager, SceneManager sceneManager, GameResourceProvider resources)
{
_playerActorManager = Requires.IsNotNull(playerActorManager, nameof(playerActorManager));
_sceneManager = Requires.IsNotNull(sceneManager, nameof(sceneManager));
_resources = Requires.IsNotNull(resources, nameof(resources));
CommandExecutorRegistry<ICommand>.Instance.Register(this);
}
public void Dispose()
{
CommandExecutorRegistry<ICommand>.Instance.UnRegister(this);
}
public bool IsActorInTeam(PlayerActorId actorId)
{
return _actorsInTeam.Contains(actorId);
}
public HashSet<PlayerActorId> GetActorsInTeam()
{
return _actorsInTeam;
}
public void Execute(TeamOpenCommand command)
{
int playerActorId = _playerActorManager.GetPlayerActorId();
IGameEntity playerActor = _sceneManager.GetCurrentScene().GetActorGameEntity(playerActorId);
int currentNavLayer = playerActor.GetComponent<ActorMovementController>().GetCurrentLayerIndex();
Tilemap tilemap = _sceneManager.GetCurrentScene().GetTilemap();
int index = 0;
foreach (PlayerActorId actor in _actorsInTeam.Where(actor => (int)actor != playerActorId))
{
IGameEntity actorEntity = _sceneManager.GetCurrentScene().GetActorGameEntity((int)actor);
actorEntity.GetComponent<ActorController>().IsActive = true;
Vector3 spawnPosition = CalculateSpawnPosition(playerActor.Transform, index);
Vector2Int tilePosition = tilemap.GetTilePosition(spawnPosition, currentNavLayer);
actorEntity.Transform.Position = tilemap.GetWorldPosition(tilePosition, currentNavLayer);
ActorMovementController actorMovementController = actorEntity.GetComponent<ActorMovementController>();
actorMovementController.SetNavLayer(currentNavLayer);
index++;
}
#if PAL3
if (IsActorInTeam(PlayerActorId.XueJian))
{
IGameEntity huaYingEntity = _sceneManager.GetCurrentScene().GetActorGameEntity((int)PlayerActorId.HuaYing);
huaYingEntity.GetComponent<ActorMovementController>().SetNavLayer(currentNavLayer);
}
#endif
}
private Vector3 CalculateSpawnPosition(ITransform playerActorTransform, int indexInTeam)
{
float directionInDegrees = ActorSpawnPositionDirectionInDegrees[indexInTeam];
return playerActorTransform.Position +
Quaternion.AngleAxis(directionInDegrees, Vector3.up) *
-playerActorTransform.Forward *
TEAM_OPEN_SPAWN_POINT_PLAYER_OFFSET;
}
public void Execute(TeamCloseCommand command)
{
int playerActorId = _playerActorManager.GetPlayerActorId();
#if PAL3A
// Need to add all active player actors into the team within certain radius
{
IGameEntity playerActor = _sceneManager.GetCurrentScene().GetActorGameEntity((int)playerActorId);
Vector3 playerActorPosition = playerActor.Transform.Position;
IEnumerable<int> playerActorIds = Enum.GetValues(typeof(PlayerActorId)).Cast<int>();
IEnumerable<KeyValuePair<int, IGameEntity>> activePlayerActors = _sceneManager.GetCurrentScene()
.GetAllActorGameEntities()
.Where(actor => playerActorIds.Contains(actor.Key) &&
actor.Value.GetComponent<ActorController>().IsActive);
foreach (KeyValuePair<int, IGameEntity> activePlayerActor in activePlayerActors)
{
if (Vector3.Distance(playerActorPosition,
activePlayerActor.Value.Transform.Position) < 13f) // 13f is about right
{
AddActor((PlayerActorId)activePlayerActor.Key);
}
}
}
#endif
foreach (PlayerActorId actor in _actorsInTeam.Where(actor => (int)actor != playerActorId))
{
Pal3.Instance.Execute(new ActorActivateCommand((int)actor, 0));
}
}
public void AddActor(PlayerActorId actorId)
{
if (!_actorsInTeam.Contains(actorId))
{
_actorsInTeam.Add(actorId);
}
}
public void RemoveActor(PlayerActorId actorId)
{
if (_actorsInTeam.Contains(actorId))
{
_actorsInTeam.Remove(actorId);
}
}
public void Execute(TeamAddOrRemoveActorCommand command)
{
SetMembership(command.ActorId, command.IsIn, notify: true);
}
private void SetMembership(int actorId, int isIn, bool notify)
{
if (!Enum.IsDefined(typeof(PlayerActorId), actorId))
{
EngineLogger.LogError("Cannot add non-player actor to the team");
return;
}
PlayerActorId actor = (PlayerActorId)actorId;
#if PAL3
if (actor == PlayerActorId.HuaYing)
#elif PAL3A
if (actor == PlayerActorId.TaoZi)
#endif
{
EngineLogger.LogError($"{actor} cannot be added to the team");
return;
}
bool inTeam = isIn != 0;
if (inTeam)
{
AddActor(actor);
}
else
{
RemoveActor(actor);
}
#if PAL3A
if (notify)
{
string name = _resources.GetCombatActorInfos()[ActorConstants.MainActorCombatActorIdMap[actor]].Name;
Pal3.Instance.Execute(new UIDisplayNoteCommand($"{name}{(inTeam ? "加入" : "离开")}队伍"));
}
#endif
}
#if PAL3A
public void Execute(TeamAddOrRemoveActorSilentlyCommand command)
{
SetMembership(command.ActorId, command.IsIn, notify: false);
}
#endif
public void Execute(ResetGameStateCommand command)
{
_actorsInTeam.Clear();
}
}
}