My implementation would be an inventory in a game. We store items in there go figure. So base class item, but items can be weapons, armor, crafting materials, quest items, ammunitions, crafting designs, or useables. Now approaching this issue I thought a base class of Item would work with derived classes for each of the more specific items would work. Now when you have something in your inventory it is of the base class, but lets say we want to equip it ok so I would need to use a static_cast<Armor> Item_ptr now for the internal working of the classes each will load a database with information specific to that kind of item so weapons will have damage specs and armor will have defensive specs. Is this a good way to approach this problem? I would think it would be more memory efficent than giving every item stats it may not ever use.
Code:
#ifndef ITEM_H
#define ITEM_H
class Item
{
public:
Item(int ID, int Mod, int Mods[8])
{
id = ID;
Modifier = Mod;
for(int i=0;i<8;i++)Modifacations[i] = Mods[i];
}
void Set_Item(int ID,int Mod, int Mods[8])
{
id = ID;
Modifier = Mod;
for(int i=0;i<8;i++)Modifacations[i] = Mods[i];
}
Item & operator|(Item &rhs) //swap function
{
int temp;
temp = rhs.id;
rhs.id = id;
id = temp;
temp = rhs.Modifier;
rhs.Modifier = Modifier;
Modifier = temp;
for(int i=0;i<8;i++)
{
temp = Modifacations[i];
Modifacations[i] = rhs.Modifacations[i];
rhs.Modifacations[i] = temp;
}
return *this;
}
void Set_Modifiers(int new_mods[8]){for(int i=0;i<8;i++)Modifacations[i] = new_mods[i];}
int Get_ID(){return id;}
int Get_Modifier(){return Modifier;}
private:
int id; //
int Modifier; //Crafted modifier
int Modifacations[8];
};
#endif
Code:
#ifndef WEAPON_H
#define WEAPON_H
#include "Datatypes.h" //contains Damage struct for storing damage
#include "Item.h" //Base class the very most basic definition of an item
#include <iostream>
#include <fstream>
using namespace std;
class Weapon:public Item
{
public:
Weapon(int ID, int Mod,int new_mods[8]) : Item(ID,Mod,new_mods)
{
}
Weapon & operator=(const Weapon &other)
{
Item::operator=(other);
min_dmg = other.min_dmg;
max_dmg = other.max_dmg;
hands = other.hands;
speed = other.speed;
range = other.range;
return *this;
}
Weapon & operator|(Weapon &other)
{
Item::operator|(other);
int temp;
temp = min_dmg;
min_dmg = other.min_dmg;
other.min_dmg = temp;
temp = max_dmg;
max_dmg = other.max_dmg;
other.max_dmg = temp;
temp = hands;
hands = other.hands;
other.hands = temp;
temp = speed;
speed = other.speed;
other.speed = temp;
temp = range;
range = other.range;
other.range = temp;
return *this;
}
Damage Get_Damage()
{
Damage *dmgptr;
dmgptr = new Damage;
dmgptr->norm = (int)((Get_Modifier()/100)*(min_dmg+rand()%(max_dmg-min_dmg)));
dmgptr->fire = 0;
dmgptr->ice = 0;
dmgptr->poison = 0;
return *dmgptr;
}
int Get_Hands() {return hands;}
int Get_Speed() {return speed;}
int Get_Range() {return range;}
int Get_Slot() {return 0;}
static void LOAD_DATA(char *data_file_path)
{
LOADED = true;
ifstream stream;
stream.open(data_file_path);
stream>>nmbr;
infolist = new weapon_info*[nmbr];
for(int i=0;i<nmbr;i++)
{
infolist[i] = new weapon_info;
stream>>infolist[i]->id
>>infolist[i]->mdmg
>>infolist[i]->xdmg
>>infolist[i]->range
>>infolist[i]->speed
>>infolist[i]->hands;
}
}
private:
static int nmbr;
static bool LOADED;
static weapon_info **infolist;
int min_dmg, max_dmg;
int hands;
int speed;
int range;
};
#endif