Hey,

I've stumbled upon a small problem with my game.
I have 10 wasps that have some AI, just wandering around and whenever a wasp gets close to the queen wasp it starts chasing it. the queen just wanders around aswell. I can also move with my character and if I get close to either the wasps or the queen they will evade me. this all works fine however, I want to perform an action if ALL the wasps are chasing the queen. (I have an enum state for wander/chasing/evading) I made the array wasp in the game class and send them to my wasp class where all the code is in.
so my code for the wasps is:

Game1.cs

wasps = new Wasp[10];
            for (int i = 0; i < 10; i++)
            {
                wasps[i] = new Wasp(this, random, Wasp.kindofWasp.normal);
                Components.Add(wasps[i]);
                
            }

wasps.cs

if (kind == kindofWasp.normal)
{
//code for the small wasps
}
if (kind == kindofWasp.queen)
{
//code for the queen wasp
}

but if I use: if (state == State.chasing) it will function even if its only 1 wasp chasing the queen.

so to make a long story short, how do I make an if statement that checks if all of the wasp meet a certain condition?

Sorry for the long explaination, im not very good at explaining things :S

Thx in advance :)

You could create a method purely for this task.

You pass the method your array and check against it.

So maybe something like

public Boolean CheckChasing(Wasp[] waspArray)
{
    foreach(Wasp waspCheck in waspArray)
    {
        if(waspCheck.State != State.Chasing)
            return false;
    }
    return true;
}

This is pretty simple and is neither efficient nor flexible. You could perhaps use your own comparison delegate as an argument, or have a static wasp extension etc.

This should set you going anyway, but I do encourage you to experiment and find out how far the language goes =)


EDIT: The same thing in LINQ

IEnumerable<Wasp> waspResult = from result in waspArray
                               where result.State != State.Chasing
                               select result;
if(waspResult == null)
{ /* No results. All wasps are chasing */ }
else
{ /* Result returned. At least one not chasing */ }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.