Initial set up for item use
This commit is contained in:
@@ -58,7 +58,7 @@ public class DynamicLibraryImpl : IDynamicLibrary
|
||||
throw new InvalidOperationException("Stat calculator not found in plugins.");
|
||||
if (registry.MiscLibrary is null)
|
||||
throw new InvalidOperationException("Misc library not found in plugins.");
|
||||
var scriptResolver = new ScriptResolver(registry.ScriptTypes);
|
||||
var scriptResolver = new ScriptResolver(registry.ScriptTypes, registry.ItemScriptTypes);
|
||||
return new DynamicLibraryImpl(staticLibrary, registry.BattleStatCalculator,
|
||||
registry.DamageCalculator, registry.MiscLibrary, scriptResolver);
|
||||
}
|
||||
|
||||
@@ -314,10 +314,9 @@ public class BattleImpl : ScriptSource, IBattle
|
||||
{
|
||||
if (weatherName.HasValue)
|
||||
{
|
||||
if (!Library.ScriptResolver.TryResolve(ScriptCategory.Weather, weatherName.Value, out var script))
|
||||
if (!Library.ScriptResolver.TryResolve(ScriptCategory.Weather, weatherName.Value, null, out var script))
|
||||
throw new InvalidOperationException($"Weather script {weatherName} not found.");
|
||||
_weatherScript.Set(script);
|
||||
script.OnInitialize(Library, null);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -25,10 +25,10 @@ internal static class MoveTurnExecutor
|
||||
var secondaryEffect = moveData.SecondaryEffect;
|
||||
if (secondaryEffect != null)
|
||||
{
|
||||
if (moveChoice.User.Library.ScriptResolver.TryResolve(ScriptCategory.Move, secondaryEffect.Name, out var script))
|
||||
if (moveChoice.User.Library.ScriptResolver.TryResolve(ScriptCategory.Move, secondaryEffect.Name,
|
||||
secondaryEffect.Parameters, out var script))
|
||||
{
|
||||
moveChoice.Script.Set(script);
|
||||
script.OnInitialize(moveChoice.User.Library, secondaryEffect.Parameters);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -90,7 +90,9 @@ public static class TurnRunner
|
||||
case IFleeChoice fleeChoice:
|
||||
ExecuteFleeChoice(battle, fleeChoice);
|
||||
break;
|
||||
// TODO: Implement item choice types
|
||||
case IItemChoice itemChoice:
|
||||
ExecuteItemChoice(battle, itemChoice);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,4 +157,36 @@ public static class TurnRunner
|
||||
battle.ValidateBattleState();
|
||||
}
|
||||
|
||||
private static void ExecuteItemChoice(IBattle battle, IItemChoice itemChoice)
|
||||
{
|
||||
var user = itemChoice.User;
|
||||
var battleData = user.BattleData;
|
||||
if (battleData == null)
|
||||
return;
|
||||
if (!battle.Library.ScriptResolver.TryResolveBattleItemScript(itemChoice.Item, out var itemScript))
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (!itemScript.IsItemUsable)
|
||||
return;
|
||||
itemScript.OnInitialize(itemChoice.Item.BattleEffect!.Parameters);
|
||||
IPokemon? target = null;
|
||||
if (itemChoice is { TargetSide: not null, TargetPosition: not null })
|
||||
{
|
||||
var side = battle.Sides[itemChoice.TargetSide.Value];
|
||||
target = side.Pokemon[itemChoice.TargetPosition.Value];
|
||||
}
|
||||
|
||||
var requiresTarget = itemScript.RequiresTarget;
|
||||
if (requiresTarget)
|
||||
{
|
||||
target ??= user;
|
||||
itemScript.OnUseWithTarget(target);
|
||||
}
|
||||
else
|
||||
{
|
||||
itemScript.OnUse();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,19 +8,40 @@ namespace PkmnLib.Dynamic.Models.Choices;
|
||||
/// </summary>
|
||||
public interface IItemChoice : ITurnChoice
|
||||
{
|
||||
/// <summary>
|
||||
/// The item that is used.
|
||||
/// </summary>
|
||||
public IItem Item { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The side the move is targeted at.
|
||||
/// </summary>
|
||||
byte? TargetSide { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The position the move is targeted at.
|
||||
/// </summary>
|
||||
byte? TargetPosition { get; }
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IItemChoice"/>
|
||||
public class ItemChoice : TurnChoice, IItemChoice
|
||||
{
|
||||
public ItemChoice(IPokemon user, IItem item) : base(user)
|
||||
public ItemChoice(IPokemon user, IItem item, byte? targetSide, byte? targetPosition) : base(user)
|
||||
{
|
||||
Item = item;
|
||||
TargetSide = targetSide;
|
||||
TargetPosition = targetPosition;
|
||||
}
|
||||
|
||||
public IItem Item { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte? TargetSide { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte? TargetPosition { get; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override int ScriptCount => User.ScriptCount;
|
||||
|
||||
|
||||
@@ -46,10 +46,10 @@ public class MoveChoice : TurnChoice, IMoveChoice
|
||||
var secondaryEffect = usedMove.MoveData.SecondaryEffect;
|
||||
if (secondaryEffect != null)
|
||||
{
|
||||
if (user.Library.ScriptResolver.TryResolve(ScriptCategory.Move, secondaryEffect.Name, out var script))
|
||||
if (user.Library.ScriptResolver.TryResolve(ScriptCategory.Move, secondaryEffect.Name,
|
||||
secondaryEffect.Parameters, out var script))
|
||||
{
|
||||
Script.Set(script);
|
||||
script.OnInitialize(user.Library, secondaryEffect.Parameters);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -465,7 +465,7 @@ public class PokemonImpl : ScriptSource, IPokemon
|
||||
|
||||
if (serializedPokemon.Status != null)
|
||||
{
|
||||
if (!library.ScriptResolver.TryResolve(ScriptCategory.Status, serializedPokemon.Status,
|
||||
if (!library.ScriptResolver.TryResolve(ScriptCategory.Status, serializedPokemon.Status, null,
|
||||
out var statusScript))
|
||||
throw new KeyNotFoundException($"Status script {serializedPokemon.Status} not found");
|
||||
StatusScript.Set(statusScript);
|
||||
@@ -668,7 +668,7 @@ public class PokemonImpl : ScriptSource, IPokemon
|
||||
{
|
||||
if (HeldItem is null)
|
||||
return false;
|
||||
if (!Library.ScriptResolver.TryResolveItemScript(HeldItem, out _))
|
||||
if (!Library.ScriptResolver.TryResolveBattleItemScript(HeldItem, out _))
|
||||
return false;
|
||||
// TODO: actually consume the item
|
||||
throw new NotImplementedException();
|
||||
@@ -777,10 +777,10 @@ public class PokemonImpl : ScriptSource, IPokemon
|
||||
AbilityScript.Clear();
|
||||
if (!Library.StaticLibrary.Abilities.TryGet(newAbility, out var ability))
|
||||
throw new KeyNotFoundException($"Ability {newAbility} not found.");
|
||||
if (Library.ScriptResolver.TryResolve(ScriptCategory.Ability, newAbility, out var abilityScript))
|
||||
if (Library.ScriptResolver.TryResolve(ScriptCategory.Ability, newAbility, ability.Parameters,
|
||||
out var abilityScript))
|
||||
{
|
||||
AbilityScript.Set(abilityScript);
|
||||
abilityScript.OnInitialize(Library, ability.Parameters);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
54
PkmnLib.Dynamic/ScriptHandling/ItemScript.cs
Normal file
54
PkmnLib.Dynamic/ScriptHandling/ItemScript.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using PkmnLib.Dynamic.Models;
|
||||
using PkmnLib.Static.Utils;
|
||||
|
||||
namespace PkmnLib.Dynamic.ScriptHandling;
|
||||
|
||||
public abstract class ItemScript : IDeepCloneable
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes the script with the given parameters for a specific item
|
||||
/// </summary>
|
||||
public virtual void OnInitialize(IReadOnlyDictionary<StringKey, object?>? parameters)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns whether the item is usable in the current context.
|
||||
/// </summary>
|
||||
public virtual bool IsItemUsable => false;
|
||||
|
||||
/// <summary>
|
||||
/// Returns whether the item requires a target to be used.
|
||||
/// </summary>
|
||||
public virtual bool RequiresTarget => false;
|
||||
|
||||
/// <summary>
|
||||
/// Returns whether the item is usable on the given target.
|
||||
/// </summary>
|
||||
public virtual bool IsTargetValid(IPokemon target) => false;
|
||||
|
||||
/// <summary>
|
||||
/// Returns whether the item can be held by a Pokémon.
|
||||
/// </summary>
|
||||
public virtual bool IsHoldable => false;
|
||||
|
||||
/// <summary>
|
||||
/// Returns whether the item can be held by the given target.
|
||||
/// </summary>
|
||||
public virtual bool CanTargetHold(IPokemon pokemon) => false;
|
||||
|
||||
/// <summary>
|
||||
/// Handles the use of the item.
|
||||
/// </summary>
|
||||
public virtual void OnUse()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Handles the use of the item on the given target.
|
||||
/// </summary>
|
||||
/// <param name="target"></param>
|
||||
public virtual void OnUseWithTarget(IPokemon target)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using JetBrains.Annotations;
|
||||
using PkmnLib.Static.Utils;
|
||||
|
||||
namespace PkmnLib.Dynamic.ScriptHandling.Registry;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
[MeansImplicitUse]
|
||||
public class ItemScriptAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// The name of the script.
|
||||
/// </summary>
|
||||
public StringKey Name { get; }
|
||||
|
||||
/// <inheritdoc cref="ItemScriptAttribute"/>
|
||||
public ItemScriptAttribute(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ namespace PkmnLib.Dynamic.ScriptHandling.Registry;
|
||||
public class ScriptRegistry
|
||||
{
|
||||
private readonly Dictionary<(ScriptCategory category, StringKey name), Func<Script>> _scriptTypes = new();
|
||||
private readonly Dictionary<StringKey, Func<ItemScript>> _itemScriptTypes = new();
|
||||
private IBattleStatCalculator? _battleStatCalculator;
|
||||
private IDamageCalculator? _damageCalculator;
|
||||
private IMiscLibrary? _miscLibrary;
|
||||
@@ -32,6 +33,15 @@ public class ScriptRegistry
|
||||
|
||||
RegisterScriptType(attribute.Category, attribute.Name, type);
|
||||
}
|
||||
var itemBaseType = typeof(ItemScript);
|
||||
foreach (var type in assembly.GetTypes().Where(t => itemBaseType.IsAssignableFrom(t)))
|
||||
{
|
||||
var attribute = type.GetCustomAttribute<ItemScriptAttribute>();
|
||||
if (attribute == null)
|
||||
continue;
|
||||
|
||||
RegisterItemScriptType(attribute.Name, type);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -52,6 +62,25 @@ public class ScriptRegistry
|
||||
// This is more performant than using Activator.CreateInstance.
|
||||
_scriptTypes[(category, name)] = Expression.Lambda<Func<Script>>(Expression.New(constructor)).Compile();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register an item script type with the given name.
|
||||
/// </summary>
|
||||
[PublicAPI]
|
||||
public void RegisterItemScriptType(StringKey name, Type type)
|
||||
{
|
||||
if (type == null)
|
||||
throw new ArgumentNullException(nameof(type));
|
||||
|
||||
var constructor = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
|
||||
null, Type.EmptyTypes, null);
|
||||
if (constructor == null)
|
||||
throw new ArgumentException($"Type {type} does not have a parameterless constructor.");
|
||||
|
||||
// We create a lambda that creates a new instance of the script type.
|
||||
// This is more performant than using Activator.CreateInstance.
|
||||
_itemScriptTypes[name] = Expression.Lambda<Func<ItemScript>>(Expression.New(constructor)).Compile();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register a battle stat calculator.
|
||||
@@ -71,7 +100,8 @@ public class ScriptRegistry
|
||||
public void RegisterMiscLibrary<T>(T miscLibrary) where T : IMiscLibrary
|
||||
=> _miscLibrary = miscLibrary;
|
||||
|
||||
internal Dictionary<(ScriptCategory category, StringKey name), Func<Script>> ScriptTypes => _scriptTypes;
|
||||
internal IReadOnlyDictionary<(ScriptCategory category, StringKey name), Func<Script>> ScriptTypes => _scriptTypes;
|
||||
internal IReadOnlyDictionary<StringKey, Func<ItemScript>> ItemScriptTypes => _itemScriptTypes;
|
||||
internal IBattleStatCalculator? BattleStatCalculator => _battleStatCalculator;
|
||||
internal IDamageCalculator? DamageCalculator => _damageCalculator;
|
||||
internal IMiscLibrary? MiscLibrary => _miscLibrary;
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using PkmnLib.Dynamic.Libraries;
|
||||
using PkmnLib.Dynamic.Models;
|
||||
using PkmnLib.Dynamic.Models.Choices;
|
||||
using PkmnLib.Dynamic.ScriptHandling.Registry;
|
||||
@@ -74,7 +73,7 @@ public abstract class Script : IDeepCloneable
|
||||
/// <summary>
|
||||
/// This function is ran when this script starts being in effect.
|
||||
/// </summary>
|
||||
public virtual void OnInitialize(IDynamicLibrary library, IReadOnlyDictionary<StringKey, object?>? parameters)
|
||||
public virtual void OnInitialize(IReadOnlyDictionary<StringKey, object?>? parameters)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -9,33 +9,57 @@ namespace PkmnLib.Dynamic.ScriptHandling;
|
||||
/// </summary>
|
||||
public class ScriptResolver
|
||||
{
|
||||
private Dictionary<(ScriptCategory, StringKey), Func<Script>> _scriptCtors;
|
||||
private IReadOnlyDictionary<(ScriptCategory, StringKey), Func<Script>> _scriptCtors;
|
||||
private IReadOnlyDictionary<StringKey, Func<ItemScript>> _itemScriptCtors;
|
||||
|
||||
/// <inheritdoc cref="ScriptResolver"/>
|
||||
public ScriptResolver(Dictionary<(ScriptCategory, StringKey), Func<Script>> scriptCtors)
|
||||
public ScriptResolver(IReadOnlyDictionary<(ScriptCategory, StringKey), Func<Script>> scriptCtors,
|
||||
IReadOnlyDictionary<StringKey, Func<ItemScript>> itemScriptCtors)
|
||||
{
|
||||
_scriptCtors = scriptCtors;
|
||||
_itemScriptCtors = itemScriptCtors;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Try and create a new script for the given category and key. If the script does not exist, return false.
|
||||
/// </summary>
|
||||
public bool TryResolve(ScriptCategory category, StringKey key, [MaybeNullWhen(false)] out Script script)
|
||||
public bool TryResolve(ScriptCategory category, StringKey key, IReadOnlyDictionary<StringKey, object?>? parameters,
|
||||
[MaybeNullWhen(false)] out Script script)
|
||||
{
|
||||
if (!_scriptCtors.TryGetValue((category, key), out var scriptCtor))
|
||||
{
|
||||
script = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
script = scriptCtor();
|
||||
if (parameters != null)
|
||||
{
|
||||
script.OnInitialize(parameters);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Try and resolve an item script for the given item. If the item does not have a script, return false.
|
||||
/// </summary>
|
||||
public bool TryResolveItemScript(IItem item, [MaybeNullWhen(false)] out Script script)
|
||||
public bool TryResolveBattleItemScript(IItem item, [MaybeNullWhen(false)] out ItemScript script)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
var effect = item.BattleEffect;
|
||||
if (effect == null)
|
||||
{
|
||||
script = null;
|
||||
return false;
|
||||
}
|
||||
if (!_itemScriptCtors.TryGetValue(effect.Name, out var scriptCtor))
|
||||
{
|
||||
script = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
script = scriptCtor();
|
||||
script.OnInitialize(effect.Parameters);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user