|
There is a huge hybrid battle ship that sail, load cargo, carry ammunition, commandos, & sometimes go down the water like a submarine, and some time camouflager itself based on the surroundings. It does a lot of things, & equally has a lot of properties like, color, size, Housing capacity, firing capabilities. etc.
Now, on the ship, there's only one place where I can mount ammunition.
I have a set of ammunition. Anti-aircraft guns, fire shells, Launch missiles, etc etc. but all these are "mutually exclusive".
If I would like to mount the ship with Anti-aircraft guns, all other ammo go unusable. I can dynamically swap between available ammo.
class BattleShip
{
propert A,B ,.....Z;
void MountAndFire(Ammo ammo_in)
{
int ammo_in.Fire(numberofrounds:30);
float ammo_in.Fire(direction:120, distance:200);
int ammo_in.Fire(Numberofcluster:600, direction:120, Distance:200);
}
};
How do you have a generic design to address this? I thought of having a Ammo Interface, and have a Fire() call,
But the parameter for the fire call differ for all the ammo. So I cant' say:
interface IAmmo
{
virtual int Fire();
}
Missile: IAmmo
{
int Fire()
{
}
}
The simple ordinary way is to check:
void MountAndFire(object ammo_in)
{
if(ammo_in.GetType==typeof(Antiaircraftgun)
{
AntiAirCraftGun aag = (AntiAirCraftGun) ammo_in;
aag.Fire(30);
}
if(ammo_in.GetType==typeof(Missile)
{
Missle missile = (Missile) ammo_in;
missile.Fire(120,200);
}
}
You have a better idea for this?
Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.
|
|
|
|
|
You have to know what sort of weapon it is, to know what parameters to provide. The only other option I can see is a class that contains all the possible params, on the basis that you're saying 'I don't know what you're going to shoot, but if it's a missile do this, if its the anti aircraft gun, do this, etc. Given that they all do different things, and are good in different circumstances, I don't see how it's useful to NOT know what you're about to pull the trigger on.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
Christian Graus wrote: I don't see how it's useful to NOT know what you're about to pull the trigger on.
The ship is on the screen. On the right bottom you display 4 ammo options for the player. Anything he chooses, would be the ammo for the ship for the next 30 seconds. So you get the picture? We know exactly on what we are pulling the trigger. The Ship just has to be dynamic enough to mount anything on and start the fire.
PS: I misfired twice on the wrong button, instead of quote, I pressed Post Message!
Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.
|
|
|
|
|
OK, well, I guess that means the player knows what settings they are setting, right ? So the only ( ugly ) way is to have all those methods, and just assume your code will only call the one that sets settings for the currently selected weapon.
Christian Graus
Driven to the arms of OSX by Vista.
Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
|
|
|
|
|
VuNic wrote: I have a set of ammunition. Anti-aircraft guns, fire shells, Launch missiles,
etc etc. but all these are "mutually exclusive". If I would like to
mount the ship with Anti-aircraft guns, all other ammo go unusable. I can
dynamically swap between available ammo.
Those are contradictory.
I suspect that last one is what you actually want.
So you have
1. Collection of ammunitions types
2. A pointer that specifies the active type.
1 can be implemented either as multiple classes or a single class with an attribute that specifies type.
The ship, at any time, can have a collection that has zero or more types available. However it might also be convenient to fix the collection size and just let the ammunication counts go to zero to indicate 'none'. Or have a flag that indicates something like 'not yet available'. The later allows it to collected but not usable until some game action occurs.
|
|
|
|
|
Who fires? Is it the battleship, or the ammo that triggers? Anyway, I'd still resort to inheritance, as been said before;
class BattleShip
{
void MountAndFire(AmmoBase ammo_in)
{
Fire(new FireArgsMissile { direction = 120, distance = 200 });
}
int Fire(FireArgs a)
{
return a.Fire();
}
}
public class FireArgs
{
public int Fire()
{
return 0;
}
}
public class FireArgsMissile : FireArgs
{
public int direction { get; set; }
public int distance { get; set; }
}
public abstract class AmmoBase
{
public abstract int Fire(FireArgs a);
}
public class Missile : AmmoBase
{
public override int Fire(FireArgs a)
{
return 0;
}
}
|
|
|
|
|
It seems to me that you want to:
- mount ammo into a slot/inventory
- fire a weapon that can only fire if there is the right ammo in the slot/inventory
... so what about something like this:
interface IShip : IName
{
IEnumerator<IGun> GunEnumerator
{
get;
}
IGun ActiveGun
{
get;
}
void ActivateGun(IGun gun);
IAmmoInventory AmmoInventory
{
get;
}
}
interface IGun : IName
{
bool Aim(Vector3 directionLocal);
bool Fire();
}
interface IAmmoInventory
{
IEnumerator<IAmmo> AmmoEnumerator
{
get;
}
void SelectAmmo(IAmmo ammo);
IAmmo SelectedAmmo
{
get;
}
bool Add(IAmmo ammo);
bool Remove(IAmmo ammo);
}
interface IAmmo : IName
{
int Damage
{
get;
}
int Count
{
get;
}
}
interface IName
{
string Name
{
get;
}
}
The Aim() part is pretty naive... but I hope you get the idea.
If IGun could be mounted on something that is not a ship, you could make interfaces like IGunOwner and IAmmoOwner that could be inherited by both IShip and IDeathStar
Kind Regards,
Keld Ølykke
|
|
|
|
|