Combat system WIP

This commit is contained in:
Jiaqi Liu committed 2023-08-26 20:06:22 -07:00
1 parent 0185eba411
commit 2aa2229bcf
7 files changed
+141 -53

No files matched your search

@@ -171,27 +171,9 @@ namespace Pal3.Actor.Controllers
(int) ServiceLocator.Instance.Get<PlayerActorManager>()
.GetPlayerActor() == actorController.GetActor().Id)
{
// Player actor collides with combat NPC in maze
CommandDispatcher<ICommand>.Instance.Dispatch(
new ActorActivateCommand(_actor.Id, 0));
if (ServiceLocator.Instance.Get<GameSettings>().IsTurnBasedCombatEnabled)
{
CommandDispatcher<ICommand>.Instance.Dispatch(
new ActorStopActionAndStandCommand(ActorConstants.PlayerActorVirtualID));
CommandDispatcher<ICommand>.Instance.Dispatch(
new CombatEnterNormalFightCommand(
_actor.Info.NumberOfMonsters,
_actor.Info.MonsterIds[0],
_actor.Info.MonsterIds[1],
_actor.Info.MonsterIds[2]));
}
else // TODO: Remove once combat system is fully implemented
{
CommandDispatcher<ICommand>.Instance.Dispatch(
new PlaySfxCommand("wd130", 1));
}
new CombatActorCollideWithPlayerActorNotification(
_actor.Id, actorController.GetActor().Id));
}
}
@@ -0,0 +1,21 @@
// ---------------------------------------------------------------------------------------------
// Copyright (c) 2021-2022, Jiaqi Liu. All rights reserved.
// See LICENSE file in the project root for license information.
// ---------------------------------------------------------------------------------------------
namespace Pal3.Command.InternalCommands
{
public class CombatActorCollideWithPlayerActorNotification : ICommand
{
public CombatActorCollideWithPlayerActorNotification(
int combatActorId,
int playerActorId)
{
CombatActorId = combatActorId;
PlayerActorId = playerActorId;
}
public int CombatActorId { get; }
public int PlayerActorId { get; }
}
}
@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: c2123cde96d84ce09278172ad047b6ae
timeCreated: 1693103104
@@ -8,6 +8,13 @@ namespace Pal3.GameSystems.Combat
using Core.Contracts;
using Script.Waiter;
public enum MeetType
{
RunningIntoEachOther,
PlayerChasingEnemy,
EnemyChasingPlayer,
}
/// <summary>
/// Data contract for combat context.
/// </summary>
@@ -17,11 +24,12 @@ namespace Pal3.GameSystems.Combat
public string CombatSceneName { get; private set; }
public ElementType CombatSceneElementType { get; private set; }
public bool IsScriptTriggeredCombat { get; private set; }
public MeetType MeetType { get; private set; }
public string CombatMusicName { get; private set; }
public int MaxRound { get; private set; }
public bool IsUnbeatable { get; private set; }
public bool IsNoGameOverWhenLose { get; private set; }
public uint[] MonsterIds { get; private set; }
public uint[] EnemyIds { get; private set; }
public CombatContext()
{
@@ -48,6 +56,11 @@ namespace Pal3.GameSystems.Combat
IsScriptTriggeredCombat = isScriptTriggeredCombat;
}
public void SetMeetType(MeetType meetType)
{
MeetType = meetType;
}
public void SetCombatMusicName(string combatMusicName)
{
CombatMusicName = combatMusicName;
@@ -68,22 +81,22 @@ namespace Pal3.GameSystems.Combat
IsNoGameOverWhenLose = isNoGameOverWhenLose;
}
public void SetMonsterIds(
uint monster1Id,
uint monster2Id,
uint monster3Id,
uint monster4Id,
uint monster5Id,
uint monster6Id)
public void SetEnemyIds(
uint enemy1Id,
uint enemy2Id,
uint enemy3Id,
uint enemy4Id,
uint enemy5Id,
uint enemy6Id)
{
MonsterIds = new []
EnemyIds = new []
{
monster1Id,
monster2Id,
monster3Id,
monster4Id,
monster5Id,
monster6Id
enemy1Id,
enemy2Id,
enemy3Id,
enemy4Id,
enemy5Id,
enemy6Id
};
}
@@ -94,10 +107,11 @@ namespace Pal3.GameSystems.Combat
CombatSceneElementType = ElementType.None;
CombatMusicName = string.Empty;
IsScriptTriggeredCombat = false;
MeetType = MeetType.RunningIntoEachOther;
MaxRound = -1; // -1 means no limit
IsUnbeatable = false;
IsNoGameOverWhenLose = false;
MonsterIds = new uint[6];
EnemyIds = new uint[6];
}
public override string ToString()
@@ -106,10 +120,12 @@ namespace Pal3.GameSystems.Combat
$"CombatScene: {CombatSceneName} " +
$"ElementType: {CombatSceneElementType} " +
$"CombatMusic: {CombatMusicName} " +
$"IsScriptTriggeredCombat: {IsScriptTriggeredCombat} " +
$"MeetType: {MeetType} " +
$"MaxRound: {MaxRound} " +
$"IsUnbeatable: {IsUnbeatable} " +
$"IsNoGameOverWhenLose: {IsNoGameOverWhenLose} " +
$"Monsters: {string.Join(" ", MonsterIds)}";
$"Enemies: {string.Join(" ", EnemyIds)}";
}
}
}
@@ -32,6 +32,7 @@ namespace Pal3.GameSystems.Combat
/// Receive events and gather context to start a combat.
/// </summary>
public sealed class CombatCoordinator : IDisposable,
ICommandExecutor<CombatActorCollideWithPlayerActorNotification>,
ICommandExecutor<CombatEnterNormalFightCommand>,
ICommandExecutor<CombatEnterBossFightCommand>,
#if PAL3A
@@ -222,7 +223,7 @@ namespace Pal3.GameSystems.Combat
(monsterIds[i], monsterIds[randomIndex]) = (monsterIds[randomIndex], monsterIds[i]);
}
_currentCombatContext.SetMonsterIds(
_currentCombatContext.SetEnemyIds(
monsterIds[0],
monsterIds[1],
monsterIds[2],
@@ -238,7 +239,7 @@ namespace Pal3.GameSystems.Combat
public void Execute(CombatEnterBossFightCommand command)
{
_currentCombatContext.SetMonsterIds(
_currentCombatContext.SetEnemyIds(
command.Monster1Id,
command.Monster2Id,
command.Monster3Id,
@@ -252,7 +253,7 @@ namespace Pal3.GameSystems.Combat
#if PAL3A
public void Execute(CombatEnterBossFightUsingMusicCommand command)
{
_currentCombatContext.SetMonsterIds(
_currentCombatContext.SetEnemyIds(
command.Monster1Id,
command.Monster2Id,
command.Monster3Id,
@@ -266,7 +267,7 @@ namespace Pal3.GameSystems.Combat
public void Execute(CombatEnterBossFightUsingMusicWithSpecialActorCommand command)
{
_currentCombatContext.SetMonsterIds(
_currentCombatContext.SetEnemyIds(
command.Monster1Id,
command.Monster2Id,
command.Monster3Id,
@@ -299,5 +300,59 @@ namespace Pal3.GameSystems.Combat
{
_currentCombatContext.ResetContext();
}
// Player actor collides with combat NPC in maze
public void Execute(CombatActorCollideWithPlayerActorNotification command)
{
CommandDispatcher<ICommand>.Instance.Dispatch(
new ActorActivateCommand(command.CombatActorId, 0));
if (_gameSettings.IsTurnBasedCombatEnabled)
{
CommandDispatcher<ICommand>.Instance.Dispatch(
new ActorStopActionAndStandCommand(ActorConstants.PlayerActorVirtualID));
var currentScene = _sceneManager.GetCurrentScene();
var combatActor = currentScene.GetActor(command.CombatActorId);
var playerTransform = currentScene.GetActorGameObject(command.PlayerActorId).transform;
var enemyTransform = currentScene.GetActorGameObject(command.CombatActorId).transform;
_currentCombatContext.SetMeetType(CalculateMeetType(playerTransform, enemyTransform));
Execute(new CombatEnterNormalFightCommand(
combatActor.Info.NumberOfMonsters,
combatActor.Info.MonsterIds[0],
combatActor.Info.MonsterIds[1],
combatActor.Info.MonsterIds[2]));
}
else // TODO: Remove once combat system is fully implemented
{
CommandDispatcher<ICommand>.Instance.Dispatch(
new PlaySfxCommand("wd130", 1));
}
}
private MeetType CalculateMeetType(Transform playerTransform, Transform enemyTransform)
{
Vector3 relativePosition = playerTransform.position - enemyTransform.position;
// Normalize the vector for more accurate dot product
relativePosition.Normalize();
if (Vector3.Dot(playerTransform.forward, -enemyTransform.forward) > 0.7f)
{
return MeetType.RunningIntoEachOther;
}
else if (Vector3.Dot(playerTransform.forward, -relativePosition) > 0.7f)
{
return MeetType.PlayerChasingEnemy;
}
else if (Vector3.Dot(enemyTransform.forward, relativePosition) > 0.7f)
{
return MeetType.EnemyChasingPlayer;
}
return MeetType.RunningIntoEachOther;
}
}
}
@@ -70,13 +70,13 @@ namespace Pal3.GameSystems.Combat
CombatScene scene = _sceneManager.LoadCombatScene(combatContext.CombatSceneName);
Dictionary<int, CombatActorInfo> monsterActors = new ();
Dictionary<int, CombatActorInfo> enemyActors = new ();
for (int i = 0; i < combatContext.MonsterIds.Length; i++)
for (int i = 0; i < combatContext.EnemyIds.Length; i++)
{
var monsterActorId = combatContext.MonsterIds[i];
if (monsterActorId == 0) continue;
monsterActors[i] = _combatActorInfos[(int)monsterActorId];
var enemyActorId = combatContext.EnemyIds[i];
if (enemyActorId == 0) continue;
enemyActors[i] = _combatActorInfos[(int)enemyActorId];
}
Dictionary<int, CombatActorInfo> playerActors = new ();
@@ -87,11 +87,16 @@ namespace Pal3.GameSystems.Combat
playerActors[positionIndex++] = _combatActorInfos[combatActorId];
}
scene.LoadActors(monsterActors, playerActors);
scene.LoadActors(enemyActors, playerActors, combatContext.MeetType);
SetCameraPosition(_combatCameraConfigFile.DefaultCamConfigs[0]);
CommandDispatcher<ICommand>.Instance.Dispatch(new CameraFadeInCommand());
CommandDispatcher<ICommand>.Instance.Dispatch(
new UIDisplayNoteCommand(combatContext.MeetType == MeetType.PlayerChasingEnemy
? "偷袭敌方成功!"
: "被敌人偷袭!"));
}
public void ExitCombat()
+12 -6
View File
@@ -14,6 +14,7 @@ namespace Pal3.Scene
using Core.DataReader.Pol;
using Core.GameBox;
using Data;
using GameSystems.Combat;
using MetaData;
using Rendering.Material;
using Rendering.Renderer;
@@ -36,7 +37,7 @@ namespace Pal3.Scene
private (PolFile PolFile, ITextureResourceProvider TextureProvider) _scenePolyMesh;
private CombatConfigFile _combatConfigFile;
private static readonly Quaternion MonsterFormationRotation = Quaternion.Euler(0, 145, 0);
private static readonly Quaternion EnemyFormationRotation = Quaternion.Euler(0, 145, 0);
private static readonly Quaternion PlayerFormationRotation = Quaternion.Euler(0, -45, 0);
public void Init(GameResourceProvider resourceProvider)
@@ -86,8 +87,9 @@ namespace Pal3.Scene
isStaticObject: true); // Combat Scene mesh is static
}
public void LoadActors(Dictionary<int, CombatActorInfo> monsterActors,
Dictionary<int, CombatActorInfo> playerActors)
public void LoadActors(Dictionary<int, CombatActorInfo> enemyActors,
Dictionary<int, CombatActorInfo> playerActors,
MeetType meetType)
{
foreach ((int positionIndex, CombatActorInfo actorInfo) in playerActors)
{
@@ -106,10 +108,12 @@ namespace Pal3.Scene
_combatConfigFile.ActorGameBoxPositions[positionIndex]);
combatActorGameObject.transform.SetPositionAndRotation(position,
PlayerFormationRotation);
meetType is MeetType.PlayerChasingEnemy or MeetType.RunningIntoEachOther
? PlayerFormationRotation
: EnemyFormationRotation);
}
foreach ((int positionIndex, CombatActorInfo actorInfo) in monsterActors)
foreach ((int positionIndex, CombatActorInfo actorInfo) in enemyActors)
{
CombatActor combatActor = new CombatActor(_resourceProvider, actorInfo);
GameObject combatActorGameObject = ActorFactory.CreateCombatActorGameObject(
@@ -123,7 +127,9 @@ namespace Pal3.Scene
_combatConfigFile.ActorGameBoxPositions[5 + positionIndex]);
combatActorGameObject.transform.SetPositionAndRotation(position,
MonsterFormationRotation);
meetType is MeetType.EnemyChasingPlayer or MeetType.RunningIntoEachOther
? EnemyFormationRotation
: PlayerFormationRotation);
}
}
}