Implementation of Pokeballs

This commit is contained in:
2025-01-10 11:58:23 +01:00
parent 0518499a4c
commit 42e3273483
15 changed files with 254 additions and 12 deletions

View File

@@ -1,10 +1,19 @@
using PkmnLib.Dynamic.Models;
using PkmnLib.Static;
using PkmnLib.Static.Utils;
namespace PkmnLib.Dynamic.ScriptHandling;
public abstract class ItemScript : IDeepCloneable
{
protected ItemScript(IItem item)
{
Item = item;
}
protected IItem Item { get; private set; }
/// <summary>
/// Initializes the script with the given parameters for a specific item
/// </summary>

View File

@@ -0,0 +1,24 @@
using PkmnLib.Dynamic.Models;
using PkmnLib.Static;
namespace PkmnLib.Dynamic.ScriptHandling;
public abstract class PokeballScript : ItemScript
{
/// <inheritdoc />
protected PokeballScript(IItem item) : base(item)
{
}
public abstract byte GetCatchRate(IPokemon target);
/// <inheritdoc />
public override void OnUseWithTarget(IPokemon target)
{
var battleData = target.BattleData;
if (battleData == null)
return;
battleData.Battle.AttempCapture(battleData.SideIndex, battleData.Position, Item);
}
}

View File

@@ -2,6 +2,7 @@ using System.Linq.Expressions;
using System.Reflection;
using JetBrains.Annotations;
using PkmnLib.Dynamic.Libraries;
using PkmnLib.Static;
using PkmnLib.Static.Utils;
namespace PkmnLib.Dynamic.ScriptHandling.Registry;
@@ -13,10 +14,11 @@ 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 readonly Dictionary<StringKey, Func<IItem, ItemScript>> _itemScriptTypes = new();
private IBattleStatCalculator? _battleStatCalculator;
private IDamageCalculator? _damageCalculator;
private IMiscLibrary? _miscLibrary;
private ICaptureLibrary? _captureLibrary;
/// <summary>
/// Automatically register all scripts in the given assembly that have the <see cref="ScriptAttribute"/>, and
@@ -73,13 +75,15 @@ public class ScriptRegistry
throw new ArgumentNullException(nameof(type));
var constructor = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
null, Type.EmptyTypes, null);
null, [typeof(IItem)], null);
if (constructor == null)
throw new ArgumentException($"Type {type} does not have a parameterless constructor.");
throw new ArgumentException($"Type {type} does not have a constructor that takes an IItem.");
// 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();
var parameterExpression = Expression.Parameter(typeof(IItem), "item");
var newExpression = Expression.New(constructor, parameterExpression);
_itemScriptTypes[name] = Expression.Lambda<Func<IItem, ItemScript>>(newExpression, parameterExpression).Compile();
}
/// <summary>
@@ -99,10 +103,17 @@ public class ScriptRegistry
/// </summary>
public void RegisterMiscLibrary<T>(T miscLibrary) where T : IMiscLibrary
=> _miscLibrary = miscLibrary;
/// <summary>
/// Register a capture library.
/// </summary>
public void RegisterCaptureLibrary<T>(T captureLibrary) where T : ICaptureLibrary
=> _captureLibrary = captureLibrary;
internal IReadOnlyDictionary<(ScriptCategory category, StringKey name), Func<Script>> ScriptTypes => _scriptTypes;
internal IReadOnlyDictionary<StringKey, Func<ItemScript>> ItemScriptTypes => _itemScriptTypes;
internal IReadOnlyDictionary<StringKey, Func<IItem, ItemScript>> ItemScriptTypes => _itemScriptTypes;
internal IBattleStatCalculator? BattleStatCalculator => _battleStatCalculator;
internal IDamageCalculator? DamageCalculator => _damageCalculator;
internal IMiscLibrary? MiscLibrary => _miscLibrary;
internal ICaptureLibrary? CaptureLibrary => _captureLibrary;
}

View File

@@ -493,7 +493,7 @@ public abstract class Script : IDeepCloneable
/// rate of this attempt. Pokeball modifier effects should be implemented here, as well as for
/// example status effects that change capture rates.
/// </summary>
public virtual void ChangeCaptureRateBonus(IPokemon pokemon, IItem pokeball, ref byte modifier)
public virtual void ChangeCatchRateBonus(IPokemon pokemon, IItem pokeball, ref byte modifier)
{
}
}

View File

@@ -10,11 +10,12 @@ namespace PkmnLib.Dynamic.ScriptHandling;
public class ScriptResolver
{
private IReadOnlyDictionary<(ScriptCategory, StringKey), Func<Script>> _scriptCtors;
private IReadOnlyDictionary<StringKey, Func<ItemScript>> _itemScriptCtors;
private IReadOnlyDictionary<StringKey, Func<IItem, ItemScript>> _itemScriptCtors;
private readonly Dictionary<IItem, ItemScript> _itemBattleScripts = new();
/// <inheritdoc cref="ScriptResolver"/>
public ScriptResolver(IReadOnlyDictionary<(ScriptCategory, StringKey), Func<Script>> scriptCtors,
IReadOnlyDictionary<StringKey, Func<ItemScript>> itemScriptCtors)
IReadOnlyDictionary<StringKey, Func<IItem, ItemScript>> itemScriptCtors)
{
_scriptCtors = scriptCtors;
_itemScriptCtors = itemScriptCtors;
@@ -46,6 +47,11 @@ public class ScriptResolver
/// </summary>
public bool TryResolveBattleItemScript(IItem item, [MaybeNullWhen(false)] out ItemScript script)
{
if (_itemBattleScripts.TryGetValue(item, out script))
{
return true;
}
var effect = item.BattleEffect;
if (effect == null)
{
@@ -58,8 +64,9 @@ public class ScriptResolver
return false;
}
script = scriptCtor();
script = scriptCtor(item);
script.OnInitialize(effect.Parameters);
_itemBattleScripts[item] = script;
return true;
}
}