Adds Item interface

This commit is contained in:
Deukhoofd 2022-09-20 18:03:53 +02:00
parent d2069fc938
commit 65d55a95fa
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 178 additions and 0 deletions

View File

@ -0,0 +1,32 @@
using System;
using System.Runtime.InteropServices;
using PkmnLibSharp.StaticData;
namespace PkmnLibSharp.FFI.StaticData
{
internal static class Item
{
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr item_new(IntPtr name, ItemCategory category, BattleItemCategory battleCategory,
int price, IntPtr flags, ulong flagsLength);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern void item_drop(IntPtr ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr item_name(IntPtr ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern ItemCategory item_category(IntPtr ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern BattleItemCategory item_battle_category(IntPtr ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern int item_price(IntPtr ptr);
[DllImport(Data.DllName, CallingConvention = CallingConvention.Cdecl)]
internal static extern byte item_has_flag(IntPtr p, IntPtr flag);
}
}

View File

@ -0,0 +1,115 @@
using System.Collections.Generic;
using System.Linq;
using PkmnLibSharp.Utils;
using Interface = PkmnLibSharp.FFI.StaticData.Item;
namespace PkmnLibSharp.StaticData
{
/// <summary>
/// An item category defines which bag slot items are stored in.
/// </summary>
public enum ItemCategory : byte
{
/// <summary>
/// This is where most items should go.
/// </summary>
MiscItem,
/// <summary>
/// Pokeballs are used for capturing Pokemons.
/// </summary>
Pokeball,
/// <summary>
/// Medicine is used for healing HP, PP, and status effects
/// </summary>
Medicine,
/// <summary>
/// Berry is used for all berries.
/// </summary>
Berry,
/// <summary>
/// TMHM is used for Technical and Hidden Machines.
/// </summary>
TMHM,
/// <summary>
/// Form Changer is used for items that change forms, such as mega stones.
/// </summary>
FormChanger,
/// <summary>
/// Key Items are single stored items, generally used for story progression.
/// </summary>
KeyItem,
/// <summary>
/// Mail is used for mail items.
/// </summary>
Mail,
}
/// <summary>
/// A battle item category defines how the item is categorized when in battle.
/// </summary>
public enum BattleItemCategory : byte
{
/// <summary>
/// This item can't be used in battle.
/// </summary>
None,
/// <summary>
/// This item is used for healing Pokemon.
/// </summary>
Healing,
/// <summary>
/// This item is used for healing Pokemon from a status.
/// </summary>
StatusHealing,
/// <summary>
/// This item is used for capturing Pokemon.
/// </summary>
Pokeball,
/// <summary>
/// This item does not belong in above categories, but is still a battle item.
/// </summary>
MiscBattleItem,
}
public class Item : ExternPointer<Item.CacheData>
{
public class CacheData
{
public string? Name { get; internal set; }
public ItemCategory? Category { get; internal set; }
public BattleItemCategory? BattleCategory { get; internal set; }
public int? Price { get; internal set; }
}
public Item(string name, ItemCategory category, BattleItemCategory battleItemCategory, int price,
IEnumerable<string> flags)
{
var ptrArray = flags.Select(x => x.ToPtr()).ToArray();
var ptrToPtrArray = ptrArray.ArrayPtr();
var ptr = Interface.item_new(name.ToPtr(), category, battleItemCategory, price, ptrToPtrArray,
(ulong)ptrArray.Length);
InitializePointer(ptr, true);
}
public string Name => Cache.Name ??= Interface.item_name(Ptr).PtrString()!;
public ItemCategory Category => Cache.Category ??= Interface.item_category(Ptr);
public BattleItemCategory BattleCategory => Cache.BattleCategory ??= Interface.item_battle_category(Ptr);
public int Price => Cache.Price ??= Interface.item_price(Ptr);
public bool HasFlag(string flag) => Interface.item_has_flag(Ptr, flag.ToPtr()) == 1;
protected override CacheData CreateCache() => new CacheData();
protected override void Destructor() => Interface.item_drop(Ptr);
}
}

View File

@ -0,0 +1,31 @@
using System;
using NUnit.Framework;
using PkmnLibSharp.StaticData;
namespace PkmnLibRSharpTests.StaticData
{
public class ItemTests
{
[Test]
public void BasicTests()
{
using var item = new Item("foobar", ItemCategory.Mail, BattleItemCategory.StatusHealing, 500,
Array.Empty<string>());
Assert.AreEqual("foobar", item.Name);
Assert.AreEqual(ItemCategory.Mail, item.Category);
Assert.AreEqual(BattleItemCategory.StatusHealing, item.BattleCategory);
Assert.AreEqual(500, item.Price);
}
[Test]
public void FlagTests()
{
using var item = new Item("foobar", ItemCategory.Mail, BattleItemCategory.StatusHealing, 500,
new []{"foo", "zet"});
Assert.That(item.HasFlag("foo"));
Assert.That(item.HasFlag("zet"));
Assert.That(!item.HasFlag("bar"));
}
}
}