std::vector<Weapon*> m_Weapons;
Weapon **m_Weapons;
class Ship {
public:
void FireWeapons(int t);
private:
std::list<Weapon*> m_weaons;
};
void Ship::FireWeapons(int t) {
std::list<Weapon*>::iterator w;
for (w = m_weapons.begin(); w != m_weapons.end(); w++)
if ((*w)->type == t) (*w)->Fire();
// add anything you can think of
}
// Somewhere
Ship* sh = new Ship("Millenium Falcon");
sh->FireWeapons(WT_STD);
sh->FireWeapons(WT_ULTRA);
sh->FireWeapons(WT_HANS_SECRET_SUPER_BEAM);
//create custom container
class cWeaponContainer
{
public:
int DefineNumberOfWeaponSlots(int NumberOfSlots)
{
cWeaponSlot * TempWeaponSlots;
if(NumberOfSlots < m_NumberOfSlots)
{
//put out of bound slot items in inventory
}
TempWeaponSlots = new cWeaponSlot[NumberOfSlots]
//handle the differences between the two list
delete [] m_WeaponSlots;
m_WeaponSlots = TempWeaponSlots;
m_NumberOfSlots = NumberOfSlots;
}
m_WeaponSlot * GetWeaponSlot(int Slot)
{
if(Slot >= 0 && Slot <= m_NumberOfSlots)
{
if(m_WeaponSlots != 0)
{
return m_WeaponSlot[Slot];
}
}
return 0; //null
}
int SetWeaponSlot(int Slot, cWeaponSlot * WeaponSlot)
{
if(Slot >= 0 && Slot <= m_NumberOfSlots)
{
if(m_WeaponSlots != 0)
{
m_WeaponSlot[Slot] = WeaponSlot;
return 1;
}
}
return 0;
}
int RemWeapon(int Slot)// ect…
private:
int m_NumberOfSlots;
cWeaponSlot * m_WeaponSlots[];
}
Ships will be created by players by taking various components and installing them onto a Chassis, which then becomes a ship once parts are installed. As the parts are assigned, they're stuck into various fields on the Ship class. The bit I'm having trouble with (because the rest of the process is easy) is weapons. The problem is that Chassis's will have varied amounts of places to mount a weapon. To use a common example, an A-Wing chassis would have 2 weapon mounts, while an X-wing would have 4 weapon mounts.
How do I handle this code-wise without making a bunch of fields like:
?
Is it possible to do something like:
or something along those lines?