Creating a complete backpack system similar to Ultima Online in C# involves several steps, including handling objects, managing inventory capacity, and ensuring item interactions. Below is a simplified version to get you started. This script will manage basic backpack functionality, such as adding, removing, and organizing items.
Step 1: Define Inventory Item
First, you need a class to represent an inventory item.
public class InventoryItem
{
public string Name { get; set; }
public int Weight { get; set; }
public int Quantity { get; set; } // For stackable items
public InventoryItem(string name, int weight, int quantity = 1)
{
Name = name;
Weight = weight;
Quantity = quantity;
}
}
Step 2: Define Backpack Class
Next, create a class for the backpack. This class will handle adding, removing, and organizing items.
using System;
using System.Collections.Generic;
using System.Linq;
public class Backpack
{
public int MaxWeight { get; set; }
public int CurrentWeight => Items.Sum(item => item.Weight * item.Quantity);
public List<InventoryItem> Items { get; private set; }
public Backpack(int maxWeight)
{
MaxWeight = maxWeight;
Items = new List<InventoryItem>();
}
public bool AddItem(InventoryItem newItem)
{
if (CurrentWeight + (newItem.Weight * newItem.Quantity) > MaxWeight)
{
Console.WriteLine("Cannot add item. Backpack is over the weight limit.");
return false;
}
var existingItem = Items.FirstOrDefault(item => item.Name == newItem.Name);
if (existingItem != null && existingItem.Quantity + newItem.Quantity >= 1)
{
existingItem.Quantity += newItem.Quantity;
}
else
{
Items.Add(newItem);
}
Console.WriteLine($"{newItem.Quantity} x {newItem.Name} added to backpack.");
return true;
}
public bool RemoveItem(string itemName, int quantity = 1)
{
var item = Items.FirstOrDefault(i => i.Name == itemName);
if (item == null || item.Quantity < quantity)
{
Console.WriteLine("Cannot remove item. Item not found or insufficient quantity.");
return false;
}
item.Quantity -= quantity;
if (item.Quantity <= 0)
{
Items.Remove(item);
}
Console.WriteLine($"{quantity} x {itemName} removed from backpack.");
return true;
}
public void DisplayContents()
{
Console.WriteLine("Backpack Contents:");
foreach (var item in Items)
{
Console.WriteLine($"{item.Quantity} x {item.Name} (Weight: {item.Weight * item.Quantity})");
}
Console.WriteLine($"Total Weight: {CurrentWeight}/{MaxWeight}");
}
public void OrganizeBackpack()
{
Items = Items.OrderBy(i => i.Name).ToList();
Console.WriteLine("Backpack organized.");
}
}
Step 3: Testing the Backpack System
Here's a simple test program to illustrate how you might use these classes:
public class Program
{
public static void Main()
{
Backpack backpack = new Backpack(100); // Max Weight 100
// Adding items
backpack.AddItem(new InventoryItem("Sword", 10));
backpack.AddItem(new InventoryItem("Shield", 15));
backpack.AddItem(new InventoryItem("Potion", 5, 10)); // Stackable item
backpack.AddItem(new InventoryItem("Potion", 5, 5));
// Display contents
backpack.DisplayContents();
// Removing items
backpack.RemoveItem("Potion", 8);
// Display contents again
backpack.DisplayContents();
// Organize backpack
backpack.OrganizeBackpack();
// Display organized contents
backpack.DisplayContents();
}
}
Explanation
- InventoryItem Class: Holds basic details about each item, such as its name, weight, and quantity (to handle stackable items).
- Backpack Class: Manages items, enforces weight limits, and provides functionality to add, remove, display, and organize items.
- Main Program: Tests the functionality by creating a backpack, adding/removing items, and displaying contents.
This example provides a basic framework. In a real application, you might extend it with additional features such as durability, item types (consumables, equipment, etc.), serialization for saving/loading inventory state, and handling nested containers.