// First of all, CMissile (I assume) has previous and next pointers
// They need to be nullified in the CMissile constructor
// Now, you need to decide which classes are going to do what work
// Right now, you are mixing management of the missile linked list in the enemy and the missile class
// I highly recommend putting the linked list management in the hands of the missile class, so that
// it is easier to use elsewhere (like in the player code)
// So first, a function to add a missile to the list
// This will work no matter which member of the list you call from, since it automatically finds the end of the list
void CMissile::AddMissile(CMissile *missile)
{
// Get the last entry in this linked list
CMissile *node = this;
while( node->next ) {
node = node->next;
}
// Node is now the last item in the list, so we need to set its next entry to our new missile
node->next = missile;
missile->prev = node;
// Now our new missile is in the list
}
// Next, a function to remove a missile from the list
// To use this, have the missile you want to remove call the function
void CMissile::RemoveSelf()
{
// Have the previous entry point to our next entry
if( prev )
prev->next = next; // This works even if our next is null - it just sets it to null, like we want
// Do the same for our next entry
if( next )
next->prev = prev;
// Now we are out of the list, we just have to delete ourself
}
// Destroying the entire list is simple. By having the destructor delete the next entry in the list, then the
// list will recursively self-destruct. The only requirement is to delete from the first entry
// If you want to delete a single entry from the list only, then you call RemoveSelf() first, so deleting the
// entry will have no effect on the rest of the entries
CMissile::~CMissile()
{
if( next )
delete next;
next = NULL;
prev = NULL; // We dont delete the previous entry, that is already being deleted
}
// Your Explode function can now look like this:
void CMissile::Explode(CMissile *ent)
{
testing = Distance2(origin,neworg);
if(Distance2(origin,neworg) > distance) {
RemoveSelf();
dead=true;
}
}
// The entire explode function is a little weird. Why don't you just have a function called ShouldExplode
// that checks the distance, and returns a bool on whether it should explode. Then if it should, you could remove the
// missile from the list and delete it
// FireWeapon needs to be rewritten too. It should look like this now:
void CEnemy::FireWeapon(CVector3 org, int weapon)
{
if(timeFromStart - lasttime < 320)
return;
// Create our new missile
weaptemp2 = new CMissile;
// If we already have a list, add it to our list
if(missile)
{
missile->AddMissile( weaptemp2 );
}
// Otherwise, set our list to our new missile
else
{
missile = weaptemp2;
}
// missile should always point to the first item in the list, while weaptemp2 is trivial
// weaptemp2 could point to the last entry (if you dont change it, this add code will do that)
switch (weapon) {
// ...
}
// One more change - the destructor
// You have to destroy the list, so you must do this:
if( missile ) delete missile;
missile = NULL;
// Now, as you know, the entire list should recursively self-destruct! w00t!
// That should do it, hope that helps.