Skip to content

A comprehensive, weight-based inventory management system for RPG games built with C# and .NET 9.0. Features polymorphic item types (Weapons, Armor, Potions), Observer pattern for real-time updates, JSON serialization for save/load functionality, and seamless character integration.

Notifications You must be signed in to change notification settings

xFedeT/RPGInventory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

2 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

RPG Inventory System

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.

๐ŸŽฎ Features

Core Inventory Management

  • 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

Item System

  • 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

Advanced Features

  • 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

๐Ÿ—๏ธ Architecture

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

Design Patterns Used

  • 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

๐Ÿš€ Quick Start

Basic Usage

// 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");

Inventory Operations

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();

๐Ÿ“ Item Types

Weapons

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
);

Armor

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
);

Potions

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
);

๐Ÿ”ง Configuration

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

๐Ÿ’พ Serialization

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
    }
  ]
}

๐Ÿงช Testing

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

๐Ÿ› ๏ธ Requirements

  • .NET 9.0 or later
  • System.Text.Json (included in .NET)

๐Ÿ“ˆ Extensibility

The system is designed for easy extension:

  1. Custom Item Types: Inherit from Item class
  2. New Observers: Implement IInventoryObserver
  3. Custom Serialization: Extend InventorySaveDataConverter
  4. Additional Properties: Add new fields to existing item types

๐Ÿ“„ License

This project is open source. Feel free to use, modify, and distribute according to your needs.

About

A comprehensive, weight-based inventory management system for RPG games built with C# and .NET 9.0. Features polymorphic item types (Weapons, Armor, Potions), Observer pattern for real-time updates, JSON serialization for save/load functionality, and seamless character integration.

Topics

Resources

Stars

Watchers

Forks

Languages