Validate movechoice target
This commit is contained in:
parent
b4e8ad1cd2
commit
b3529fa22f
|
@ -236,7 +236,9 @@ public class BattleImpl : ScriptSource, IBattle
|
|||
// TODO: Hook to change number of PP needed.
|
||||
if (moveChoice.ChosenMove.CurrentPp < 1)
|
||||
return false;
|
||||
// TODO: Validate target
|
||||
if (!TargetResolver.IsValidTarget(moveChoice.TargetSide, moveChoice.TargetPosition,
|
||||
moveChoice.ChosenMove.MoveData.Target, moveChoice.User))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
|
@ -26,6 +26,60 @@ public static class TargetResolver
|
|||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validates whether a given target is valid for a move choice. Returns true if the target is valid.
|
||||
/// </summary>
|
||||
public static bool IsValidTarget(byte side, byte position, MoveTarget target, IPokemon user)
|
||||
{
|
||||
var userBattleData = user.BattleData;
|
||||
if (userBattleData == null)
|
||||
throw new ArgumentNullException(nameof(user.BattleData));
|
||||
var userSide = userBattleData.SideIndex;
|
||||
var userPosition = userBattleData.Position;
|
||||
|
||||
switch (target)
|
||||
{
|
||||
case MoveTarget.Adjacent:
|
||||
case MoveTarget.AllAdjacent:
|
||||
{
|
||||
var diff = Math.Abs(position - userPosition);
|
||||
if (diff == 0)
|
||||
return userSide == side;
|
||||
return diff <= 1;
|
||||
}
|
||||
case MoveTarget.AdjacentAlly:
|
||||
{
|
||||
if (userSide != side)
|
||||
return false;
|
||||
return Math.Abs(position - userPosition) == 1;
|
||||
}
|
||||
case MoveTarget.AdjacentAllySelf:
|
||||
{
|
||||
if (userSide != side)
|
||||
return false;
|
||||
return Math.Abs(position - userPosition) <= 1;
|
||||
}
|
||||
case MoveTarget.AdjacentOpponent:
|
||||
case MoveTarget.AllAdjacentOpponent:
|
||||
{
|
||||
if (userSide == side)
|
||||
return false;
|
||||
return Math.Abs(position - userPosition) <= 1;
|
||||
}
|
||||
case MoveTarget.All:
|
||||
case MoveTarget.Any:
|
||||
case MoveTarget.RandomOpponent:
|
||||
return true;
|
||||
case MoveTarget.AllAlly:
|
||||
return userSide == side;
|
||||
case MoveTarget.AllOpponent:
|
||||
return userSide != side;
|
||||
case MoveTarget.SelfUse:
|
||||
return userSide == side && userPosition == position;
|
||||
}
|
||||
throw new ArgumentOutOfRangeException(nameof(target), target, null);
|
||||
}
|
||||
|
||||
private static IReadOnlyList<IPokemon?> GetAllTargets(IBattle battle) =>
|
||||
battle.Sides.SelectMany(x => x.Pokemon).ToList();
|
||||
|
||||
|
|
|
@ -9,10 +9,10 @@ public static class StaticHelpers
|
|||
/// A function to get the current date and time. This can be replaced in cases where the date and time
|
||||
/// may not be the same as the system time.
|
||||
/// </summary>
|
||||
public static Func<DateTime> DateTimeProvider { get; set; } = () => DateTime.Now;
|
||||
public static Func<DateTimeOffset> DateTimeProvider { get; set; } = () => DateTimeOffset.Now;
|
||||
|
||||
/// <summary>
|
||||
/// Get the current date and time.
|
||||
/// </summary>
|
||||
public static DateTime GetCurrentDateTime() => DateTimeProvider();
|
||||
public static DateTimeOffset GetCurrentDateTime() => DateTimeProvider();
|
||||
}
|
|
@ -24,7 +24,7 @@ public class Gen7MiscLibrary : IMiscLibrary
|
|||
/// <inheritdoc />
|
||||
public TimeOfDay GetTimeOfDay()
|
||||
{
|
||||
var time = StaticHelpers.GetCurrentDateTime();
|
||||
var time = StaticHelpers.GetCurrentDateTime().LocalDateTime;
|
||||
var hour = time.Hour;
|
||||
return hour switch
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue