A comprehensive and extensible inventory management system designed for RPG games, built with C# and .NET 9.0. This system provides a robust foundation for managing items, equipment, and character interactions in role-playing games.
- Weight-based System: Items have individual weights with configurable maximum carrying capacity
- Slot Management: Configurable maximum inventory slots with efficient space utilization
- Item Stacking: Automatic stacking of identical items with quantity tracking
- Type-safe Operations: Strongly-typed item management with comprehensive validation
- Multiple Item Types: Weapons, Armor, and Potions with unique properties
- Rarity System: Common, Uncommon, Rare, Epic, and Legendary item classifications
- Equipment Categories:
- Weapons: Swords, Bows, Staffs, Daggers, Axes with damage and critical hit chance
- Armor: Helmets, Chestplates, Gloves, Boots, Shields with defense values
- Potions: Healing and buff effects with duration systems
- Observer Pattern: Real-time inventory change notifications for UI updates and logging
- JSON Serialization: Complete save/load functionality with custom type handling
- Character Integration: Seamless equipment system with automatic stat calculations
- Error Handling: Comprehensive exception handling with custom exception types
The system follows SOLID principles and clean architecture patterns:
RPGInventory/
โโโ Api/ # Interfaces and contracts
โโโ Event/ # Event system for inventory changes
โโโ Inventory/ # Core inventory logic
โ โโโ Data/ # Data transfer objects
โ โโโ Exception/ # Custom exceptions
โ โโโ Logger/ # Logging implementation
โ โโโ UI/ # UI observer implementation
โ โโโ Utils/ # Serialization utilities
โโโ Player/ # Character and player logic
โโโ Serializable/ # Item definitions and enums
โโโ Object/ # Item class hierarchy
โโโ Type/ # Enumerations
- Observer Pattern: For inventory change notifications
- Strategy Pattern: For different item behaviors
- Factory Pattern: For item creation and deserialization
- Template Method: In the abstract Item class
// Create a character with inventory
var player = new Character("Hero", 100);
// Set up observers for UI and logging
player.Inventory.Subscribe(new InventoryUI());
player.Inventory.Subscribe(new InventoryLogger());
// Create items
var sword = new Weapon("iron_sword", "Iron Sword", "A basic sword",
50, 2.5f, ItemRarity.Common, 25, WeaponType.Sword, 0.05f);
var potion = new Potion("health_potion", "Health Potion", "Restores 50 HP",
10, 0.2f, ItemRarity.Common, 50, PotionEffect.Healing, 0);
// Add items to inventory
player.Inventory.AddItem(sword);
player.Inventory.AddItem(potion, 3); // Add 3 potions
// Use items
sword.UseItem(player); // Equips the weapon
potion.UseItem(player); // Heals the character
// Save and load
player.Inventory.SaveToFile("save.json");
player.Inventory.LoadFromFile("save.json");
var inventory = new Inventory(maxSlots: 50, maxWeight: 200f);
// Check capacity
Console.WriteLine($"Used: {inventory.UsedSlots}/{inventory.MaxSlots} slots");
Console.WriteLine($"Weight: {inventory.CurrentWeight}/{inventory.MaxWeight}kg");
// Item management
bool added = inventory.AddItem(item, quantity);
bool removed = inventory.RemoveItem(itemId, quantity);
Item retrievedItem = inventory.GetItem(itemId);
int quantity = inventory.GetItemQuantity(itemId);
List<InventorySlot> allItems = inventory.GetAllItems();
var weapon = new Weapon(
id: "legendary_sword",
name: "Excalibur",
description: "The legendary sword",
value: 1000,
weight: 3.0f,
rarity: ItemRarity.Legendary,
damage: 100,
type: WeaponType.Sword,
critChance: 0.25f
);
var armor = new Armor(
id: "dragon_scale",
name: "Dragon Scale Mail",
description: "Armor made from dragon scales",
value: 800,
weight: 15.0f,
rarity: ItemRarity.Epic,
defense: 50,
type: ArmorType.Chestplate
);
var potion = new Potion(
id: "super_heal",
name: "Super Healing Potion",
description: "Fully restores health",
value: 100,
weight: 0.5f,
rarity: ItemRarity.Rare,
healAmount: 100,
effect: PotionEffect.Healing,
duration: 0
);
The inventory system is highly configurable:
- Maximum Slots: Control inventory space limitations
- Maximum Weight: Set carrying capacity constraints
- Item Properties: Customize damage, defense, healing values
- Rarity System: Extend with custom rarity levels
- Observer System: Add custom observers for specialized behavior
The system includes robust JSON serialization with:
- Custom type discriminators for polymorphic items
- Efficient storage of inventory state
- Error handling for corrupted save files
- Backward compatibility considerations
{
"MaxSlots": 30,
"MaxWeight": 100.0,
"Items": [
{
"Item": {
"$type": "Weapon",
"Damage": 25,
"Type": 0,
"CritChance": 0.05,
"Id": "iron_sword",
"Name": "Iron Sword"
},
"Quantity": 1
}
]
}
The project includes a comprehensive demo in Program.cs
that demonstrates:
- Item creation and management
- Observer pattern functionality
- Save/load operations
- Character integration
- Error scenarios
Run the demo:
dotnet run
- .NET 9.0 or later
- System.Text.Json (included in .NET)
The system is designed for easy extension:
- Custom Item Types: Inherit from
Item
class - New Observers: Implement
IInventoryObserver
- Custom Serialization: Extend
InventorySaveDataConverter
- Additional Properties: Add new fields to existing item types
This project is open source. Feel free to use, modify, and distribute according to your needs.