PkmnLibSharp/PkmnLibSharpTests/Battling/BattleTests/BasicBattleTests.cs

164 lines
6.3 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using PkmnLibSharp.Battling;
using PkmnLibSharp.Battling.ChoiceTurn;
using PkmnLibSharp.Battling.Events;
namespace PkmnLibSharpTests.Battling.BattleTests
{
public class BasicBattleTests
{
[Test]
public void InitializeBattle()
{
var lib = BattleLibraryHelper.GetLibrary();
var battle = new BattleBuilder(lib, true, 2, 1).Build();
Assert.AreEqual(lib, battle.Value.Library);
Assert.AreEqual(true, battle.Value.CanFlee);
Assert.AreEqual(2, battle.Value.SidesCount);
Assert.AreEqual(false, battle.Value.HasEnded);
Assert.AreEqual(0, battle.Value.PartiesCount);
battle.Value.Dispose();
}
private static PokemonParty BuildTestParty(BattleLibrary lib)
{
var party = new PokemonParty();
party.SwapInto(0,
new PokemonBuilder(lib, "testSpecies", 50)
.LearnMove("testMove", MoveLearnMethod.Unknown)
.Build().TakeOwnership());
return party;
}
[Test]
public void InitializeBattleWithParties()
{
var lib = BattleLibraryHelper.GetLibrary();
var p1 = BuildTestParty(lib);
var p2 = BuildTestParty(lib);
var battle =
new BattleBuilder(lib, true, 2, 1)
.WithPartyOnPositions(p1, new BattlePosition(0, 0))
.WithPartyOnPositions(p2, new BattlePosition(1, 0))
.Build();
Assert.AreEqual(2, battle.Value.PartiesCount);
Assert.AreEqual(p1, battle.Value.Parties[0].Party);
Assert.AreEqual(p2, battle.Value.Parties[1].Party);
battle.Value.Dispose();
}
[Test]
public void RunTurn()
{
var lib = BattleLibraryHelper.GetLibrary();
var p1 = BuildTestParty(lib);
var p2 = BuildTestParty(lib);
var battle =
new BattleBuilder(lib, true, 2, 1)
.WithPartyOnPositions(p1, new BattlePosition(0, 0))
.WithPartyOnPositions(p2, new BattlePosition(1, 0))
.Build();
battle.Value.SwitchPokemon(0, 0, p1.GetAtIndex(0));
battle.Value.SwitchPokemon(1, 0, p2.GetAtIndex(0));
Assert.AreEqual(0, battle.Value.CurrentTurn);
var moveTurn1 = new MoveTurnChoice(p1.GetAtIndex(0), p1.GetAtIndex(0).Moves[0], 1, 0 );
var moveTurn2 = new MoveTurnChoice(p2.GetAtIndex(0), p2.GetAtIndex(0).Moves[0], 0, 0 );
Assert.That(battle.Value.TrySetChoice(moveTurn1));
Assert.That(battle.Value.TrySetChoice(moveTurn2));
Assert.AreEqual(battle.Value.CurrentTurn, 1);
battle.Value.Dispose();
}
[Test]
public void EventListener()
{
var lib = BattleLibraryHelper.GetLibrary();
var p1 = BuildTestParty(lib);
var p2 = BuildTestParty(lib);
var battle =
new BattleBuilder(lib, true, 2, 1)
.WithPartyOnPositions(p1, new BattlePosition(0, 0))
.WithPartyOnPositions(p2, new BattlePosition(1, 0))
.WithRandomSeed(20)
.Build();
var evts = new List<EventData>();
var eventListener = new BattleEventListener(evt =>
{
evts.Add(evt);
return Task.CompletedTask;
});
battle.Value.RegisterEventListener(eventListener);
battle.Value.SwitchPokemon(0, 0, p1.GetAtIndex(0));
battle.Value.SwitchPokemon(1, 0, p2.GetAtIndex(0));
Assert.AreEqual(0, battle.Value.CurrentTurn);
var moveTurn1 = new MoveTurnChoice(p1.GetAtIndex(0), p1.GetAtIndex(0).Moves[0], 1, 0 );
var moveTurn2 = new MoveTurnChoice(p2.GetAtIndex(0), p2.GetAtIndex(0).Moves[0], 0, 0 );
Assert.That(battle.Value.TrySetChoice(moveTurn1));
Assert.That(battle.Value.TrySetChoice(moveTurn2));
Assert.AreEqual(1, battle.Value.CurrentTurn);
eventListener.EnsureFinishedListening();
var damageEvents = evts.Where(x => x.Kind == EventDataKind.Damage).Cast<DamageEvent>().ToArray();
Assert.AreEqual(2, damageEvents.Length);
Assert.AreEqual(damageEvents[0].Pokemon, p2.GetAtIndex(0));
Assert.AreEqual(damageEvents[1].Pokemon, p1.GetAtIndex(0));
battle.Value.Dispose();
}
[Test]
public void HealAfterBattle()
{
var lib = BattleLibraryHelper.GetLibrary();
var p1 = BuildTestParty(lib);
var mon1 = p1.GetAtIndex(0);
var p2 = BuildTestParty(lib);
var mon2 = p2.GetAtIndex(0);
var battle =
new BattleBuilder(lib, true, 2, 1)
.WithPartyOnPositions(p1, new BattlePosition(0, 0))
.WithPartyOnPositions(p2, new BattlePosition(1, 0))
.Build();
var evts = new List<EventData>();
var eventListener = new BattleEventListener(evt =>
{
evts.Add(evt);
return Task.CompletedTask;
});
battle.Value.RegisterEventListener(eventListener);
battle.Value.SwitchPokemon(0, 0, p1.GetAtIndex(0));
battle.Value.SwitchPokemon(1, 0, p2.GetAtIndex(0));
Assert.AreEqual(0, battle.Value.CurrentTurn);
var moveTurn1 = new MoveTurnChoice(p1.GetAtIndex(0), p1.GetAtIndex(0).Moves[0], 1, 0 );
var moveTurn2 = new MoveTurnChoice(p2.GetAtIndex(0), p2.GetAtIndex(0).Moves[0], 0, 0 );
Assert.That(battle.Value.TrySetChoice(moveTurn1));
Assert.That(battle.Value.TrySetChoice(moveTurn2));
Assert.AreEqual(battle.Value.CurrentTurn, 1);
battle.Value.Dispose();
mon1.Heal(1000, true);
mon2.Heal(1000, true);
}
}
}