M1 回合制战斗架构收敛与 1v1 攻击闭环:BattleRuntime 内核、组件唯一入口、UI 数据驱动与 EditMode 测试
- 删除中间契约(BattleStartRequest/BattlePlayerInput/BattleReturnPlan/桥接与策略接口/FlowState),收敛为 BattleViewState/BattleStep/一层 BattleEvent - 新增内部 BattleRuntime/BattleUnit/BattleStatus 共享上下文;TurnBattleComponent 成为唯一外部入口,吸收启动/暂停/回写职责 - 内核支持 1v1 攻击闭环:效果公式、HP/MP 钳制、阵亡、Victory/TotalDefeat、敌人随机决策;敌人行动间歇 0.6s 由组件协程编排并经 StepListener 推送 - UI:玩家卡片/敌人槽/回合槽按战局重建,图标配置驱动(含新增 EnemyConfig.Icon),每轮刷新一次 TurnSlots 且轮内只移动高亮 - 新增 Tests 程序集 + InternalsVisibleTo,25 个 BattleRuntime 纯逻辑用例全部通过
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2d55ddba8b14246bcb3c84719d8ef2b1
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ff9bdccc0a8fa4a70a750d29c757252a
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,212 @@
|
||||
using System.Collections.Generic;
|
||||
using NUnit.Framework;
|
||||
using SepCore.Battle;
|
||||
using SepCore.CustomComponent;
|
||||
using SepCore.Definition;
|
||||
|
||||
namespace SepCore.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class BattleRuntimeAttackLoopTests
|
||||
{
|
||||
[Test]
|
||||
public void PlayerAttack_DealsAtkDamageAndHandsTurnToEnemy()
|
||||
{
|
||||
BattleRuntime runtime = TestBattle.Create1v1(TestBattle.Player());
|
||||
|
||||
BattleStep step = runtime.SubmitCommand(TestBattle.Attack(1, 2));
|
||||
|
||||
Assert.Null(step.Result);
|
||||
Assert.AreEqual(1, step.Events.Count);
|
||||
BattleEvent battleEvent = step.Events[0];
|
||||
Assert.AreEqual(1, battleEvent.ActorUnitId);
|
||||
Assert.AreEqual(BattleActionType.Attack, battleEvent.CommandType);
|
||||
Assert.AreEqual(1, battleEvent.ActionConfigId);
|
||||
Assert.AreEqual(2, battleEvent.TargetUnitId);
|
||||
Assert.AreEqual(50, battleEvent.BeforeHp);
|
||||
Assert.AreEqual(36, battleEvent.AfterHp);
|
||||
Assert.AreEqual(36, runtime.GetUnit(2).CurrentHp);
|
||||
Assert.AreEqual(120, runtime.GetUnit(1).CurrentHp);
|
||||
Assert.AreEqual(2, runtime.CurrentActorUnitId);
|
||||
Assert.AreEqual(1, runtime.RoundNumber);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EnemyCounterAttack_DamagesPlayerAndStartsNextRound()
|
||||
{
|
||||
BattleRuntime runtime = TestBattle.Create1v1(TestBattle.Player());
|
||||
runtime.SubmitCommand(TestBattle.Attack(1, 2));
|
||||
|
||||
BattleStep step = runtime.AdvanceEnemyTurn();
|
||||
|
||||
Assert.Null(step.Result);
|
||||
Assert.AreEqual(1, step.Events.Count);
|
||||
BattleEvent battleEvent = step.Events[0];
|
||||
Assert.AreEqual(2, battleEvent.ActorUnitId);
|
||||
Assert.AreEqual(1, battleEvent.TargetUnitId);
|
||||
Assert.AreEqual(120, battleEvent.BeforeHp);
|
||||
Assert.AreEqual(112, battleEvent.AfterHp);
|
||||
Assert.AreEqual(112, runtime.GetUnit(1).CurrentHp);
|
||||
Assert.AreEqual(1, runtime.CurrentActorUnitId);
|
||||
Assert.AreEqual(2, runtime.RoundNumber);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AttackLoop_EndsWithVictory()
|
||||
{
|
||||
BattleRuntime runtime = TestBattle.Create1v1(TestBattle.Player());
|
||||
|
||||
BattleStep final = PlayToCompletion(runtime);
|
||||
|
||||
Assert.NotNull(final.Result);
|
||||
Assert.AreEqual(BattleOutcome.Victory, final.Result.Outcome);
|
||||
Assert.AreEqual(1, final.Result.EncounterId);
|
||||
Assert.AreEqual(1, final.Result.Players.Count);
|
||||
Assert.AreEqual(1001, final.Result.Players[0].CharacterId);
|
||||
Assert.False(final.Result.Players[0].WasDefeated);
|
||||
Assert.False(final.Result.Players[0].Escaped);
|
||||
Assert.Greater(final.Result.Players[0].CurrentHp, 0);
|
||||
Assert.True(runtime.IsCompleted);
|
||||
Assert.AreEqual(0, runtime.CurrentActorUnitId);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AttackLoop_EndsWithTotalDefeat_WhenPlayerDies()
|
||||
{
|
||||
BattleRuntime runtime = TestBattle.Create1v1(TestBattle.Player(currentHp: 5, maxHp: 5));
|
||||
|
||||
BattleStep final = PlayToCompletion(runtime);
|
||||
|
||||
Assert.NotNull(final.Result);
|
||||
Assert.AreEqual(BattleOutcome.TotalDefeat, final.Result.Outcome);
|
||||
Assert.AreEqual(0, final.Result.Players[0].CurrentHp);
|
||||
Assert.True(final.Result.Players[0].WasDefeated);
|
||||
Assert.True(runtime.IsCompleted);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PlayerAttack_WithHealEffect_ClampsHpToMax()
|
||||
{
|
||||
TestConfigProvider config = TestConfigProvider.Standard1v1();
|
||||
config.AddAction(TestConfigs.AttackAction(2, 200, BattleStatType.None, 0));
|
||||
BattleRuntime runtime = TestBattle.Create1v1(TestBattle.Player(), config);
|
||||
|
||||
BattleStep step = runtime.SubmitCommand(TestBattle.Attack(1, 2, 2));
|
||||
|
||||
Assert.AreEqual(1, step.Events.Count);
|
||||
Assert.AreEqual(50, step.Events[0].AfterHp);
|
||||
Assert.AreEqual(50, runtime.GetUnit(2).CurrentHp);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InvalidCommand_WrongActor_IsRejectedWithoutConsumption()
|
||||
{
|
||||
BattleRuntime runtime = TestBattle.Create1v1(TestBattle.Player());
|
||||
|
||||
BattleStep step = runtime.SubmitCommand(TestBattle.Attack(999, 2));
|
||||
|
||||
Assert.Null(step.Result);
|
||||
Assert.IsEmpty(step.Events);
|
||||
Assert.AreEqual(1, runtime.CurrentActorUnitId);
|
||||
Assert.AreEqual(120, runtime.GetUnit(1).CurrentHp);
|
||||
Assert.AreEqual(50, runtime.GetUnit(2).CurrentHp);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InvalidCommand_SkillCommand_IsRejected()
|
||||
{
|
||||
BattleRuntime runtime = TestBattle.Create1v1(TestBattle.Player());
|
||||
|
||||
BattleStep step = runtime.SubmitCommand(
|
||||
new BattleCommand(1, BattleActionType.Skill, 101, new List<int> { 2 }));
|
||||
|
||||
Assert.Null(step.Result);
|
||||
Assert.IsEmpty(step.Events);
|
||||
Assert.AreEqual(1, runtime.CurrentActorUnitId);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InvalidCommand_ActionConfigTypeMismatch_IsRejected()
|
||||
{
|
||||
TestConfigProvider config = TestConfigProvider.Standard1v1();
|
||||
BattleRuntime runtime = TestBattle.Create1v1(TestBattle.Player(), config);
|
||||
|
||||
// 行动 1 是攻击,却用 Skill 类型提交
|
||||
BattleStep step = runtime.SubmitCommand(
|
||||
new BattleCommand(1, BattleActionType.Skill, 1, new List<int> { 2 }));
|
||||
|
||||
Assert.Null(step.Result);
|
||||
Assert.IsEmpty(step.Events);
|
||||
Assert.AreEqual(1, runtime.CurrentActorUnitId);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InvalidCommand_AllyTarget_IsRejected()
|
||||
{
|
||||
BattleRuntime runtime = TestBattle.Create1v1(TestBattle.Player());
|
||||
|
||||
BattleStep step = runtime.SubmitCommand(TestBattle.Attack(1, 1));
|
||||
|
||||
Assert.Null(step.Result);
|
||||
Assert.IsEmpty(step.Events);
|
||||
Assert.AreEqual(1, runtime.CurrentActorUnitId);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void InvalidCommand_NoTarget_IsRejected()
|
||||
{
|
||||
BattleRuntime runtime = TestBattle.Create1v1(TestBattle.Player());
|
||||
|
||||
BattleStep step = runtime.SubmitCommand(
|
||||
new BattleCommand(1, BattleActionType.Attack, 1, new List<int>()));
|
||||
|
||||
Assert.Null(step.Result);
|
||||
Assert.IsEmpty(step.Events);
|
||||
Assert.AreEqual(1, runtime.CurrentActorUnitId);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CompletedBattle_RejectsFurtherCommands()
|
||||
{
|
||||
BattleRuntime runtime = TestBattle.Create1v1(TestBattle.Player());
|
||||
PlayToCompletion(runtime);
|
||||
Assert.True(runtime.IsCompleted);
|
||||
|
||||
BattleStep step = runtime.SubmitCommand(TestBattle.Attack(1, 2));
|
||||
|
||||
Assert.Null(step.Result);
|
||||
Assert.IsEmpty(step.Events);
|
||||
Assert.AreEqual(0, runtime.CurrentActorUnitId);
|
||||
}
|
||||
|
||||
private static BattleStep PlayToCompletion(BattleRuntime runtime)
|
||||
{
|
||||
BattleStep step = null;
|
||||
int guard = 0;
|
||||
while (runtime.Result == null && guard++ < 200)
|
||||
{
|
||||
BattleUnit actor = runtime.CurrentActor;
|
||||
if (actor != null && actor.Faction == BattleFaction.Player)
|
||||
{
|
||||
int targetUnitId = 0;
|
||||
foreach (BattleUnit unit in runtime.Units)
|
||||
{
|
||||
if (unit.Faction == BattleFaction.Enemy && BattleRuntime.IsActive(unit))
|
||||
{
|
||||
targetUnitId = unit.UnitId;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
step = runtime.SubmitCommand(TestBattle.Attack(actor.UnitId, targetUnitId));
|
||||
}
|
||||
else
|
||||
{
|
||||
step = runtime.AdvanceEnemyTurn();
|
||||
}
|
||||
}
|
||||
|
||||
return step;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5e6abbbfbf1d9431ea5cfe74917ee817
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,79 @@
|
||||
using System.Collections.Generic;
|
||||
using NUnit.Framework;
|
||||
using SepCore.Battle;
|
||||
using SepCore.CustomComponent;
|
||||
|
||||
namespace SepCore.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class BattleRuntimeCreateTests
|
||||
{
|
||||
[Test]
|
||||
public void Create_WithStandard1v1Inputs_BuildsExpectedUnits()
|
||||
{
|
||||
BattleRuntime runtime = TestBattle.Create1v1(TestBattle.Player());
|
||||
|
||||
Assert.NotNull(runtime);
|
||||
Assert.False(runtime.IsCompleted);
|
||||
Assert.AreEqual(1, runtime.RoundNumber);
|
||||
Assert.AreEqual(2, runtime.Units.Length);
|
||||
|
||||
BattleUnit player = runtime.Units[0];
|
||||
Assert.AreEqual(1, player.UnitId);
|
||||
Assert.AreEqual(BattleFaction.Player, player.Faction);
|
||||
Assert.AreEqual(1001, player.ConfigId);
|
||||
Assert.AreEqual(1, player.PartyOrder);
|
||||
Assert.AreEqual(120, player.CurrentHp);
|
||||
Assert.AreEqual(120, player.MaxHp);
|
||||
Assert.AreEqual(40, player.CurrentMp);
|
||||
Assert.AreEqual(14, player.Atk);
|
||||
Assert.AreEqual(6, player.Mat);
|
||||
Assert.AreEqual(12, player.Speed);
|
||||
CollectionAssert.AreEqual(new List<int> { 1, 101 }, player.ActionIds);
|
||||
|
||||
BattleUnit enemy = runtime.Units[1];
|
||||
Assert.AreEqual(2, enemy.UnitId);
|
||||
Assert.AreEqual(BattleFaction.Enemy, enemy.Faction);
|
||||
Assert.AreEqual(3001, enemy.ConfigId);
|
||||
Assert.AreEqual(50, enemy.CurrentHp);
|
||||
Assert.AreEqual(8, enemy.Atk);
|
||||
CollectionAssert.AreEqual(new List<int> { 201 }, enemy.ActionIds);
|
||||
|
||||
Assert.AreEqual(1, runtime.CurrentActorUnitId);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Create_WithNullInputs_ReturnsNull()
|
||||
{
|
||||
RunPlayerState player = TestBattle.Player();
|
||||
BattleEncounter encounter = TestBattle.Encounter();
|
||||
TestConfigProvider config = TestConfigProvider.Standard1v1();
|
||||
IRunRandomSource random = new TestRandomSource();
|
||||
|
||||
Assert.Null(BattleRuntime.Create(null, new List<RunPlayerState> { player }, config, random));
|
||||
Assert.Null(BattleRuntime.Create(encounter, null, config, random));
|
||||
Assert.Null(BattleRuntime.Create(encounter, new List<RunPlayerState>(), config, random));
|
||||
Assert.Null(BattleRuntime.Create(encounter, new List<RunPlayerState> { player }, null, random));
|
||||
Assert.Null(BattleRuntime.Create(encounter, new List<RunPlayerState> { player }, config, null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Create_WithMissingEnemyParty_ReturnsNull()
|
||||
{
|
||||
TestConfigProvider config = TestConfigProvider.Standard1v1();
|
||||
|
||||
Assert.Null(BattleRuntime.Create(new BattleEncounter(1, 999, false),
|
||||
new List<RunPlayerState> { TestBattle.Player() }, config, new TestRandomSource()));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Create_WithMissingEnemyConfig_ReturnsNull()
|
||||
{
|
||||
TestConfigProvider config = TestConfigProvider.Standard1v1();
|
||||
config.AddParty(TestConfigs.Party(5001, 9999));
|
||||
|
||||
Assert.Null(BattleRuntime.Create(new BattleEncounter(1, 5001, false),
|
||||
new List<RunPlayerState> { TestBattle.Player() }, config, new TestRandomSource()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6f72e4b9e477543aea96022278d94914
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,138 @@
|
||||
using System.Collections.Generic;
|
||||
using NUnit.Framework;
|
||||
using SepCore.Battle;
|
||||
using SepCore.CustomComponent;
|
||||
using SepCore.Definition;
|
||||
|
||||
namespace SepCore.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class BattleRuntimeEnemyTurnTests
|
||||
{
|
||||
[Test]
|
||||
public void AdvanceEnemyTurn_WhenPlayerIsCurrent_ReturnsNoOp()
|
||||
{
|
||||
BattleRuntime runtime = TestBattle.Create1v1(TestBattle.Player());
|
||||
|
||||
BattleStep step = runtime.AdvanceEnemyTurn();
|
||||
|
||||
Assert.Null(step.Result);
|
||||
Assert.IsEmpty(step.Events);
|
||||
Assert.AreEqual(1, runtime.CurrentActorUnitId);
|
||||
Assert.AreEqual(120, runtime.GetUnit(1).CurrentHp);
|
||||
Assert.AreEqual(50, runtime.GetUnit(2).CurrentHp);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EnemyWithoutUsableActions_SkipsItsTurn()
|
||||
{
|
||||
TestConfigProvider config = TestConfigProvider.Standard1v1();
|
||||
// 敌人唯一行动改为 MP 不足(100 > 敌人 MP 0),无可用行动
|
||||
config.AddAction(TestConfigs.AttackAction(201, 0, BattleStatType.ATK, -1000, 100));
|
||||
BattleRuntime runtime = TestBattle.Create1v1(TestBattle.Player(), config);
|
||||
|
||||
runtime.SubmitCommand(TestBattle.Attack(1, 2));
|
||||
BattleStep step = runtime.AdvanceEnemyTurn();
|
||||
|
||||
Assert.Null(step.Result);
|
||||
Assert.IsEmpty(step.Events);
|
||||
Assert.AreEqual(1, runtime.CurrentActorUnitId);
|
||||
Assert.AreEqual(2, runtime.RoundNumber);
|
||||
Assert.AreEqual(120, runtime.GetUnit(1).CurrentHp);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EnemyWithNoActionIds_SkipsItsTurn()
|
||||
{
|
||||
TestConfigProvider config = TestConfigProvider.Standard1v1();
|
||||
config.AddEnemy(TestConfigs.Enemy(3001, 50, 8));
|
||||
BattleRuntime runtime = TestBattle.Create1v1(TestBattle.Player(), config);
|
||||
|
||||
runtime.SubmitCommand(TestBattle.Attack(1, 2));
|
||||
BattleStep step = runtime.AdvanceEnemyTurn();
|
||||
|
||||
Assert.Null(step.Result);
|
||||
Assert.IsEmpty(step.Events);
|
||||
Assert.AreEqual(1, runtime.CurrentActorUnitId);
|
||||
Assert.AreEqual(2, runtime.RoundNumber);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EnemyWithMultipleActions_ChoosesByRandomSequence()
|
||||
{
|
||||
TestConfigProvider config = TestConfigProvider.Standard1v1();
|
||||
config.AddAction(TestConfigs.AttackAction(201, 0, BattleStatType.ATK, -1000));
|
||||
config.AddAction(TestConfigs.AttackAction(202, -30, BattleStatType.None, 0));
|
||||
config.AddEnemy(TestConfigs.Enemy(3001, 50, 8, 201, 202));
|
||||
config.AddParty(TestConfigs.Party(4001, 3001));
|
||||
|
||||
// 随机序列 [0] -> 选 201,伤害 = ATK = 8
|
||||
BattleRuntime runtimeA = TestBattle.Create1v1(TestBattle.Player(), config, new TestRandomSource(0));
|
||||
runtimeA.SubmitCommand(TestBattle.Attack(1, 2));
|
||||
BattleStep stepA = runtimeA.AdvanceEnemyTurn();
|
||||
Assert.AreEqual(112, stepA.Events[0].AfterHp);
|
||||
|
||||
// 随机序列 [1] -> 选 202,固定伤害 30
|
||||
BattleRuntime runtimeB = TestBattle.Create1v1(TestBattle.Player(), config, new TestRandomSource(1));
|
||||
runtimeB.SubmitCommand(TestBattle.Attack(1, 2));
|
||||
BattleStep stepB = runtimeB.AdvanceEnemyTurn();
|
||||
Assert.AreEqual(90, stepB.Events[0].AfterHp);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SameInputsAndRandomSequence_ProduceSameRecords()
|
||||
{
|
||||
TestConfigProvider config = TestConfigProvider.Standard1v1();
|
||||
config.AddAction(TestConfigs.AttackAction(201, 0, BattleStatType.ATK, -1000));
|
||||
config.AddAction(TestConfigs.AttackAction(202, -30, BattleStatType.None, 0));
|
||||
config.AddEnemy(TestConfigs.Enemy(3001, 50, 8, 201, 202));
|
||||
config.AddParty(TestConfigs.Party(4001, 3001));
|
||||
|
||||
BattleRuntime runtimeA = TestBattle.Create1v1(TestBattle.Player(), config, new TestRandomSource(0, 0, 0, 0));
|
||||
BattleRuntime runtimeB = TestBattle.Create1v1(TestBattle.Player(), config, new TestRandomSource(0, 0, 0, 0));
|
||||
|
||||
List<string> recordsA = CollectRecords(runtimeA);
|
||||
List<string> recordsB = CollectRecords(runtimeB);
|
||||
|
||||
CollectionAssert.AreEqual(recordsA, recordsB);
|
||||
}
|
||||
|
||||
private static List<string> CollectRecords(BattleRuntime runtime)
|
||||
{
|
||||
List<string> records = new List<string>();
|
||||
int guard = 0;
|
||||
while (runtime.Result == null && guard++ < 200)
|
||||
{
|
||||
BattleUnit actor = runtime.CurrentActor;
|
||||
BattleStep step;
|
||||
if (actor != null && actor.Faction == BattleFaction.Player)
|
||||
{
|
||||
int targetUnitId = 0;
|
||||
foreach (BattleUnit unit in runtime.Units)
|
||||
{
|
||||
if (unit.Faction == BattleFaction.Enemy && BattleRuntime.IsActive(unit))
|
||||
{
|
||||
targetUnitId = unit.UnitId;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
step = runtime.SubmitCommand(TestBattle.Attack(actor.UnitId, targetUnitId));
|
||||
}
|
||||
else
|
||||
{
|
||||
step = runtime.AdvanceEnemyTurn();
|
||||
}
|
||||
|
||||
foreach (BattleEvent battleEvent in step.Events)
|
||||
{
|
||||
records.Add(string.Format("{0}:{1}:{2}:{3}:{4}",
|
||||
battleEvent.ActorUnitId, battleEvent.ActionConfigId, battleEvent.TargetUnitId,
|
||||
battleEvent.AfterHp, battleEvent.AfterMp));
|
||||
}
|
||||
}
|
||||
|
||||
return records;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8d0eb453c4b42453abb2e86fad636fe9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,122 @@
|
||||
using System.Collections.Generic;
|
||||
using NUnit.Framework;
|
||||
using SepCore.Battle;
|
||||
using SepCore.CustomComponent;
|
||||
|
||||
namespace SepCore.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class BattleRuntimeViewAndLifecycleTests
|
||||
{
|
||||
[Test]
|
||||
public void BuildViewState_ReflectsUnitsAndTurnOrder()
|
||||
{
|
||||
BattleRuntime runtime = TestBattle.Create1v1(TestBattle.Player());
|
||||
|
||||
BattleViewState view = runtime.BuildViewState();
|
||||
|
||||
Assert.AreEqual(1, view.RoundNumber);
|
||||
Assert.AreEqual(1, view.CurrentActorUnitId);
|
||||
Assert.AreEqual(2, view.Units.Count);
|
||||
CollectionAssert.AreEqual(new List<int> { 1, 2 }, view.RemainingTurnOrder);
|
||||
CollectionAssert.AreEqual(new List<int> { 1, 101 }, view.AvailableActionIds);
|
||||
|
||||
BattleUnitView playerView = view.Units[0];
|
||||
Assert.AreEqual(BattleFaction.Player, playerView.Faction);
|
||||
Assert.AreEqual(1001, playerView.ConfigId);
|
||||
Assert.AreEqual(1, playerView.PartyOrder);
|
||||
Assert.AreEqual(120, playerView.CurrentHp);
|
||||
Assert.AreEqual(40, playerView.CurrentMp);
|
||||
Assert.AreEqual(12, playerView.Speed);
|
||||
Assert.False(playerView.IsDefeated);
|
||||
Assert.False(playerView.IsEscaped);
|
||||
Assert.IsEmpty(playerView.Statuses);
|
||||
|
||||
BattleUnitView enemyView = view.Units[1];
|
||||
Assert.AreEqual(BattleFaction.Enemy, enemyView.Faction);
|
||||
Assert.AreEqual(3001, enemyView.ConfigId);
|
||||
Assert.AreEqual(50, enemyView.CurrentHp);
|
||||
Assert.False(enemyView.IsDefeated);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BuildViewState_AfterPlayerAction_HighlightsEnemyAndKeepsAllUnits()
|
||||
{
|
||||
BattleRuntime runtime = TestBattle.Create1v1(TestBattle.Player());
|
||||
runtime.SubmitCommand(TestBattle.Attack(1, 2));
|
||||
|
||||
BattleViewState view = runtime.BuildViewState();
|
||||
|
||||
Assert.AreEqual(2, view.CurrentActorUnitId);
|
||||
// 同轮内列表仍包含全部单位(已行动玩家不被隐藏)
|
||||
CollectionAssert.AreEqual(new List<int> { 2 }, view.RemainingTurnOrder);
|
||||
Assert.AreEqual(2, view.Units.Count);
|
||||
Assert.AreEqual(36, view.Units[1].CurrentHp);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BuildViewState_AfterCompletion_HasNoActor()
|
||||
{
|
||||
BattleRuntime runtime = TestBattle.Create1v1(TestBattle.Player());
|
||||
int guard = 0;
|
||||
while (runtime.Result == null && guard++ < 200)
|
||||
{
|
||||
BattleUnit actor = runtime.CurrentActor;
|
||||
if (actor != null && actor.Faction == BattleFaction.Player)
|
||||
{
|
||||
int targetUnitId = 0;
|
||||
foreach (BattleUnit unit in runtime.Units)
|
||||
{
|
||||
if (unit.Faction == BattleFaction.Enemy && BattleRuntime.IsActive(unit))
|
||||
{
|
||||
targetUnitId = unit.UnitId;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
runtime.SubmitCommand(TestBattle.Attack(actor.UnitId, targetUnitId));
|
||||
}
|
||||
else
|
||||
{
|
||||
runtime.AdvanceEnemyTurn();
|
||||
}
|
||||
}
|
||||
|
||||
BattleViewState view = runtime.BuildViewState();
|
||||
|
||||
Assert.AreEqual(0, view.CurrentActorUnitId);
|
||||
Assert.IsEmpty(view.RemainingTurnOrder);
|
||||
Assert.IsEmpty(view.AvailableActionIds);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SequentialBattles_DoNotInheritState()
|
||||
{
|
||||
// 第一场打到玩家残血
|
||||
BattleRuntime first = TestBattle.Create1v1(TestBattle.Player());
|
||||
first.SubmitCommand(TestBattle.Attack(1, 2));
|
||||
first.AdvanceEnemyTurn();
|
||||
Assert.AreEqual(112, first.GetUnit(1).CurrentHp);
|
||||
|
||||
// 第二场用同一份输入重新创建:应从满值开局
|
||||
BattleRuntime second = TestBattle.Create1v1(TestBattle.Player());
|
||||
Assert.AreEqual(120, second.GetUnit(1).CurrentHp);
|
||||
Assert.AreEqual(50, second.GetUnit(2).CurrentHp);
|
||||
Assert.AreEqual(1, second.RoundNumber);
|
||||
Assert.AreEqual(1, second.CurrentActorUnitId);
|
||||
Assert.False(second.IsCompleted);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void SequentialBattles_UseIndependentActedSets()
|
||||
{
|
||||
BattleRuntime first = TestBattle.Create1v1(TestBattle.Player());
|
||||
first.SubmitCommand(TestBattle.Attack(1, 2));
|
||||
Assert.IsTrue(first.ActedUnitIds.Contains(1));
|
||||
|
||||
BattleRuntime second = TestBattle.Create1v1(TestBattle.Player());
|
||||
Assert.IsEmpty(second.ActedUnitIds);
|
||||
Assert.AreEqual(1, second.CurrentActorUnitId);
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c78ea02415ec84677ae1925546f846c5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,207 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Runtime.Serialization;
|
||||
using SepCore.Battle;
|
||||
using SepCore.CustomComponent;
|
||||
using SepCore.Definition;
|
||||
|
||||
namespace SepCore.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// 用反射构造 Luban 生成的只读配置行,供测试的假配置提供者使用。
|
||||
/// </summary>
|
||||
internal static class TestConfigFactory
|
||||
{
|
||||
public static T Create<T>(params object[] fieldValues)
|
||||
{
|
||||
T instance = (T)FormatterServices.GetUninitializedObject(typeof(T));
|
||||
for (int i = 0; i < fieldValues.Length; i += 2)
|
||||
{
|
||||
FieldInfo field = typeof(T).GetField((string)fieldValues[i]);
|
||||
field.SetValue(instance, fieldValues[i + 1]);
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 与真实配表数值对齐的测试配置构建器。
|
||||
/// </summary>
|
||||
internal static class TestConfigs
|
||||
{
|
||||
/// <summary>
|
||||
/// 单体敌方攻击行动。
|
||||
/// </summary>
|
||||
public static BattleActionConfig AttackAction(int id, int flat, BattleStatType sourceStat,
|
||||
int sourceScalePermille, int mpCost = 0)
|
||||
{
|
||||
return TestConfigFactory.Create<BattleActionConfig>(
|
||||
"Id", id, "Name", "攻击" + id, "ActionType", BattleActionType.Attack,
|
||||
"TargetType", BattleTargetType.SingleEnemy, "MpCost", mpCost,
|
||||
"Effects", new List<BattleEffect>
|
||||
{
|
||||
TestConfigFactory.Create<BattleEffect>(
|
||||
"TargetStat", BattleStatType.HP, "FlatValue", flat,
|
||||
"SourceStat", sourceStat, "SourceScalePermille", sourceScalePermille,
|
||||
"Status", BattleStatusType.None, "DurationRounds", 0)
|
||||
});
|
||||
}
|
||||
|
||||
public static EnemyConfig Enemy(int id, int maxHp, int atk, params int[] actionIds)
|
||||
{
|
||||
return TestConfigFactory.Create<EnemyConfig>(
|
||||
"Id", id, "Name", "敌人" + id, "MaxHp", maxHp, "MaxMp", 0, "Atk", atk, "Mat", 0,
|
||||
"Speed", 8, "ThreatLevelId", 1, "AiType", EnemyAiType.Random,
|
||||
"ActionIds", new List<int>(actionIds), "DropTableId", 0);
|
||||
}
|
||||
|
||||
public static EnemyPartyConfig Party(int id, params int[] enemyIds)
|
||||
{
|
||||
return TestConfigFactory.Create<EnemyPartyConfig>(
|
||||
"Id", id, "Name", "队伍" + id, "EnemyIds", new List<int>(enemyIds));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 内存配置提供者:EditMode 使用,不依赖 Luban 与 GameEntry。
|
||||
/// </summary>
|
||||
internal sealed class TestConfigProvider : IBattleConfigProvider
|
||||
{
|
||||
private readonly Dictionary<int, EnemyPartyConfig> _parties = new Dictionary<int, EnemyPartyConfig>();
|
||||
private readonly Dictionary<int, EnemyConfig> _enemies = new Dictionary<int, EnemyConfig>();
|
||||
private readonly Dictionary<int, BattleActionConfig> _actions = new Dictionary<int, BattleActionConfig>();
|
||||
private GlobalConfig _global;
|
||||
|
||||
public void AddParty(EnemyPartyConfig party)
|
||||
{
|
||||
_parties[party.Id] = party;
|
||||
}
|
||||
|
||||
public void AddEnemy(EnemyConfig enemy)
|
||||
{
|
||||
_enemies[enemy.Id] = enemy;
|
||||
}
|
||||
|
||||
public void AddAction(BattleActionConfig action)
|
||||
{
|
||||
_actions[action.Id] = action;
|
||||
}
|
||||
|
||||
public void SetGlobal(GlobalConfig global)
|
||||
{
|
||||
_global = global;
|
||||
}
|
||||
|
||||
public EnemyPartyConfig GetEnemyParty(int id)
|
||||
{
|
||||
EnemyPartyConfig value;
|
||||
return _parties.TryGetValue(id, out value) ? value : null;
|
||||
}
|
||||
|
||||
public EnemyConfig GetEnemy(int id)
|
||||
{
|
||||
EnemyConfig value;
|
||||
return _enemies.TryGetValue(id, out value) ? value : null;
|
||||
}
|
||||
|
||||
public BattleActionConfig GetAction(int id)
|
||||
{
|
||||
BattleActionConfig value;
|
||||
return _actions.TryGetValue(id, out value) ? value : null;
|
||||
}
|
||||
|
||||
public GlobalConfig GetGlobal()
|
||||
{
|
||||
return _global;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 标准 1v1:行动 1(玩家攻击,伤害 = ATK)、行动 201(敌人攻击,伤害 = ATK)、
|
||||
/// 敌人 3001(50 HP,ATK 8,行动 201)、队伍 4001(单个 3001)。
|
||||
/// </summary>
|
||||
public static TestConfigProvider Standard1v1()
|
||||
{
|
||||
TestConfigProvider provider = new TestConfigProvider();
|
||||
provider.AddAction(TestConfigs.AttackAction(1, 0, BattleStatType.ATK, -1000));
|
||||
provider.AddAction(TestConfigs.AttackAction(201, 0, BattleStatType.ATK, -1000));
|
||||
provider.AddEnemy(TestConfigs.Enemy(3001, 50, 8, 201));
|
||||
provider.AddParty(TestConfigs.Party(4001, 3001));
|
||||
return provider;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 可注入序列的测试随机源;用尽后返回 minInclusive。
|
||||
/// </summary>
|
||||
internal sealed class TestRandomSource : IRunRandomSource
|
||||
{
|
||||
private readonly Queue<int> _values = new Queue<int>();
|
||||
|
||||
public TestRandomSource(params int[] values)
|
||||
{
|
||||
foreach (int value in values)
|
||||
{
|
||||
_values.Enqueue(value);
|
||||
}
|
||||
}
|
||||
|
||||
public int NextInt(int minInclusive, int maxExclusive)
|
||||
{
|
||||
return _values.Count > 0 ? _values.Dequeue() : minInclusive;
|
||||
}
|
||||
|
||||
public bool RollPermille(int successPermille)
|
||||
{
|
||||
return NextInt(0, 1000) < successPermille;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 标准测试输入与指令构造。
|
||||
/// </summary>
|
||||
internal static class TestBattle
|
||||
{
|
||||
/// <summary>
|
||||
/// 标准玩家 1001:120 HP / 40 MP,ATK 14,攻击行动 1,技能行动 101。
|
||||
/// </summary>
|
||||
public static RunPlayerState Player(int currentHp = 120, int maxHp = 120, int atk = 14)
|
||||
{
|
||||
return new RunPlayerState
|
||||
{
|
||||
CharacterId = 1001,
|
||||
PartyOrder = 1,
|
||||
CurrentHp = currentHp,
|
||||
CurrentMp = 40,
|
||||
MaxHp = maxHp,
|
||||
MaxMp = 40,
|
||||
Atk = atk,
|
||||
Mat = 6,
|
||||
Speed = 12,
|
||||
AttackActionId = 1,
|
||||
SkillActionId = 101
|
||||
};
|
||||
}
|
||||
|
||||
public static BattleEncounter Encounter(int encounterId = 1, int enemyPartyConfigId = 4001)
|
||||
{
|
||||
return new BattleEncounter(encounterId, enemyPartyConfigId, false);
|
||||
}
|
||||
|
||||
public static BattleCommand Attack(int actorUnitId, int targetUnitId, int actionId = 1)
|
||||
{
|
||||
return new BattleCommand(actorUnitId, BattleActionType.Attack, actionId,
|
||||
new List<int> { targetUnitId });
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建标准 1v1 运行时。
|
||||
/// </summary>
|
||||
public static BattleRuntime Create1v1(RunPlayerState player, IBattleConfigProvider config = null,
|
||||
IRunRandomSource random = null)
|
||||
{
|
||||
return BattleRuntime.Create(Encounter(), new List<RunPlayerState> { player },
|
||||
config ?? TestConfigProvider.Standard1v1(), random ?? new TestRandomSource());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 12a6461a326b54f55bdfa3630c77e175
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"name": "Tests",
|
||||
"optionalUnityReferences": [
|
||||
"TestAssemblies"
|
||||
],
|
||||
"references": [
|
||||
"GUID:11ec41dbc3557e6499b2845ba531a9b0",
|
||||
"GUID:a04b272484018094ab427253f20c416c",
|
||||
"GUID:363c5eb08ff8e6a439b85e37b8c20d96"
|
||||
],
|
||||
"includePlatforms": [
|
||||
"Editor"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6ca799cc4b05645b1842927808f15538
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user