Items.cs, Creates dictionaries, enumerators, and classes for every type of item.
using System;
using System.Collections.Generic;
public class Item {
// Medical
public static Dictionary < Medical, ItemMedical > Medicals = new Dictionary<Medical, ItemMedical>() {
{ Medical.bandage, new ItemMedical() { name = "bandage", healing = 15, weight = 0.2f, value = 75, description =
"Simple bandage to dress minor injuries. For patching up boo boos when you fall off your bicycle, won't save you from a gunshot." }},
{ Medical.tourniquet, new ItemMedical() { name = "tourniquet", healing = 25, weight = 0.7f, value = 225, description =
"A device that tightly wraps around a limb near the wound to stop the flow of blood, more importantly, it stops blood from flowing OUT." }}
};
// Weapon
public static Dictionary < Weapon, ItemWeapon > Weapons = new Dictionary < Weapon, ItemWeapon > () {
{ Weapon.shortsword, new ItemWeapon() { name = "shortsword", Damage = 25, weight = 4, value = 750, description =
"A relatively short sword, hence the very creative name \"Shortsword\"." }},
{ Weapon.longsword, new ItemWeapon() { name = "longsword", Damage = 40, weight = 6, value = 950, description =
"A relatively long sword, hence the very creative name \"Longsword\"." }}
};
// Armor
public static Dictionary < Armor, ItemArmor > Armors = new Dictionary < Armor, ItemArmor > () {
{ Armor.police_vest, new ItemArmor() { name = "police vest", Resistance = 25, Durability = 15, weight = 5, value = 1200, description =
"A pistol grade body armor used by police forces." }},
{ Armor.military_vest, new ItemArmor() { name = "military vest", Resistance = 60, Durability = 40, weight = 8, value = 2400, description =
"A heavy military vest capable of withstanding some rifle rounds." }}
};
public static ItemMedical Get(Medical key) => Medicals[key];
public static ItemWeapon Get(Weapon key) => Weapons[key];
public static ItemArmor Get(Armor key) => Armors[key];
}
public enum Medical {
bandage,
tourniquet
}
public enum Weapon {
shortsword,
longsword
}
public enum Armor {
police_vest,
military_vest
}
public class ItemMedical: ItemBase {
public float healing = 0;
}
public class ItemWeapon: ItemBase {
private float damage = 0;
public float Damage {
get => damage;
set => damage = value;
}
}
public class ItemArmor: ItemBase {
// Damage required to penetrate armor
public float Resistance = 0;
// "health" of the armor, damaged much more if penetrated
private float durability = 0;
public float Durability {
get => durability;
set => durability = Math.Clamp(value, 0, value);
}
}
public class ItemBase {
public string name = "No name";
public string description = "No description";
public float weight = 0;
public float value = 0;
}
Inventory.cs, equipment is an array because the slots never change, but backpack is a list because i dont know how many items will be in there.
public static ItemBase[] equipment = new ItemBase[4] {
// Primary weapon
Item.Get(Weapon.longsword),
// Secondary weapon
Item.Get(Weapon.shortsword),
// Body armor
Item.Get(Armor.police_vest),
// Rig
null
};
public static List<ItemBase> backpack = new List<ItemBase>() {
Item.Get(Medical.bandage),
Item.Get(Medical.bandage),
};
}
Usage
class Program {
static void Main(string[] args) {
Console.WriteLine("Items in inventory:");
foreach(ItemBase item in Inventory.equipment) {
if (item != null) Console.WriteLine(item.name);
else Console.WriteLine("Nothing");
}
Console.WriteLine("\nItems in backpack:");
foreach(ItemBase item in Inventory.backpack) {
Console.WriteLine(item.name);
}
}
}
Output:
Items in inventory:
longsword
shortsword
police vest
Nothing
Items in backpack:
bandage
bandage
My first actually competant looking piece of code as a beginner. Really proud of it.